1 /* Copyright (c) 2016-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_KERNEL_ENGINEIMPL_HPP
7 #define SIMGRID_KERNEL_ENGINEIMPL_HPP
9 #include <simgrid/kernel/resource/Model.hpp>
10 #include <simgrid/s4u/Engine.hpp>
11 #include <simgrid/s4u/NetZone.hpp>
12 #include <simgrid/simix.hpp>
13 #include <xbt/dynar.h>
14 #include <xbt/functional.hpp>
16 #include "src/kernel/activity/ExecImpl.hpp"
17 #include "src/kernel/activity/IoImpl.hpp"
18 #include "src/kernel/activity/MailboxImpl.hpp"
19 #include "src/kernel/activity/SleepImpl.hpp"
20 #include "src/kernel/activity/Synchro.hpp"
21 #include "src/kernel/actor/ActorImpl.hpp"
22 #include "src/kernel/resource/SplitDuplexLinkImpl.hpp"
24 #include <boost/intrusive/list.hpp>
29 #include <unordered_map>
34 // In MC mode, the application sends these pointers to the MC
35 xbt_dynar_t get_actors_addr();
38 std::map<std::string, s4u::Host*, std::less<>> hosts_;
39 std::map<std::string, resource::StandardLinkImpl*, std::less<>> links_;
40 /* save split-duplex links separately, keep links_ with only LinkImpl* seen by the user
41 * members of a split-duplex are saved in the links_ */
42 std::map<std::string, std::unique_ptr<resource::SplitDuplexLinkImpl>, std::less<>> split_duplex_links_;
43 std::unordered_map<std::string, routing::NetPoint*> netpoints_;
44 std::unordered_map<std::string, activity::MailboxImpl*> mailboxes_;
46 std::unordered_map<std::string, actor::ActorCodeFactory> registered_functions; // Maps function names to actor code
47 actor::ActorCodeFactory default_function; // Function to use as a fallback when the provided name matches nothing
48 std::vector<resource::Model*> models_;
49 std::unordered_map<std::string, std::shared_ptr<resource::Model>> models_prio_;
50 routing::NetZoneImpl* netzone_root_ = nullptr;
51 std::set<actor::ActorImpl*> daemons_;
52 std::vector<actor::ActorImpl*> actors_to_run_;
53 std::vector<actor::ActorImpl*> actors_that_ran_;
54 std::map<aid_t, actor::ActorImpl*> actor_list_;
55 boost::intrusive::list<actor::ActorImpl,
56 boost::intrusive::member_hook<actor::ActorImpl, boost::intrusive::list_member_hook<>,
57 &actor::ActorImpl::kernel_destroy_list_hook>>
60 /* MCer cannot read members actor_list_ above in the remote process, so we copy the info it needs in a dynar.
61 * FIXME: This is supposed to be a temporary hack.
62 * A better solution would be to change the split between MCer and MCed, where the responsibility
63 * to compute the list of the enabled transitions goes to the MCed.
64 * That way, the MCer would not need to have the list of actors on its side.
65 * These info could be published by the MCed to the MCer in a way inspired of vd.so
67 xbt_dynar_t actors_vector_ = xbt_dynar_new(sizeof(actor::ActorImpl*), nullptr);
70 std::vector<xbt::Task<void()>> tasks;
73 static EngineImpl* instance_;
74 actor::ActorImpl* maestro_ = nullptr;
75 context::ContextFactory* context_factory_ = nullptr;
77 std::unique_ptr<void, std::function<int(void*)>> platf_handle_; //!< handle for platform library
81 EngineImpl() = default;
83 /* Currently, only one instance is allowed to exist. This is why you can't copy or move it */
85 EngineImpl(const EngineImpl&) = delete;
86 EngineImpl& operator=(const EngineImpl&) = delete;
87 virtual ~EngineImpl();
88 static void shutdown();
91 void initialize(int* argc, char** argv);
92 void load_platform(const std::string& platf);
93 void load_deployment(const std::string& file) const;
94 void seal_platform() const;
95 void register_function(const std::string& name, const actor::ActorCodeFactory& code);
96 void register_default(const actor::ActorCodeFactory& code);
98 bool is_maestro(const actor::ActorImpl* actor) const { return actor == maestro_; }
99 void set_maestro(actor::ActorImpl* actor) { maestro_ = actor; }
100 actor::ActorImpl* get_maestro() const { return maestro_; }
102 context::ContextFactory* get_context_factory() const { return context_factory_; }
103 void set_context_factory(context::ContextFactory* factory) { context_factory_ = factory; }
104 bool has_context_factory() const { return context_factory_ != nullptr; }
105 void destroy_context_factory()
107 delete context_factory_;
108 context_factory_ = nullptr;
111 void context_mod_init() const;
113 * @brief Add a model to engine list
115 * @param model Pointer to model
116 * @param list List of dependencies for this model
118 void add_model(std::shared_ptr<simgrid::kernel::resource::Model> model,
119 const std::vector<resource::Model*>& dep_models = {});
121 /** @brief Get list of all models managed by this engine */
122 const std::vector<resource::Model*>& get_all_models() const { return models_; }
124 static bool has_instance() { return s4u::Engine::has_instance(); }
125 static EngineImpl* get_instance() { return s4u::Engine::get_instance()->pimpl; }
126 static EngineImpl* get_instance(int* argc, char** argv) { return s4u::Engine::get_instance(argc, argv)->pimpl; }
128 actor::ActorCodeFactory get_function(const std::string& name)
130 auto res = registered_functions.find(name);
131 if (res == registered_functions.end())
132 return default_function;
137 routing::NetZoneImpl* get_netzone_root() const { return netzone_root_; }
139 void add_daemon(actor::ActorImpl* d) { daemons_.insert(d); }
140 void remove_daemon(actor::ActorImpl* d);
141 void add_actor_to_run_list(actor::ActorImpl* actor);
142 void add_actor_to_run_list_no_check(actor::ActorImpl* actor);
143 void add_actor_to_destroy_list(actor::ActorImpl& actor) { actors_to_destroy_.push_back(actor); }
145 bool has_actors_to_run() const { return not actors_to_run_.empty(); }
146 const actor::ActorImpl* get_first_actor_to_run() const { return actors_to_run_.front(); }
147 const actor::ActorImpl* get_actor_to_run_at(unsigned long int i) const { return actors_to_run_[i]; }
148 unsigned long int get_actor_to_run_count() const { return actors_to_run_.size(); }
149 size_t get_actor_count() const { return actor_list_.size(); }
150 actor::ActorImpl* get_actor_by_pid(aid_t pid);
151 void add_actor(aid_t pid, actor::ActorImpl* actor) { actor_list_[pid] = actor; }
152 void remove_actor(aid_t pid) { actor_list_.erase(pid); }
153 void add_split_duplex_link(const std::string& name, std::unique_ptr<resource::SplitDuplexLinkImpl> link);
156 xbt_dynar_t get_actors_vector() const { return actors_vector_; }
157 void reset_actor_dynar() { xbt_dynar_reset(actors_vector_); }
158 void add_actor_to_dynar(actor::ActorImpl* actor) { xbt_dynar_push_as(actors_vector_, actor::ActorImpl*, actor); }
161 const std::map<aid_t, actor::ActorImpl*>& get_actor_list() const { return actor_list_; }
162 const std::vector<actor::ActorImpl*>& get_actors_to_run() const { return actors_to_run_; }
163 const std::vector<actor::ActorImpl*>& get_actors_that_ran() const { return actors_that_ran_; }
165 std::mutex& get_mutex() { return mutex_; }
166 bool execute_tasks();
167 void add_task(xbt::Task<void()>&& t) { tasks.push_back(std::move(t)); }
168 void wake_all_waiting_actors() const;
172 * Should be called some time to time to free the memory allocated for actors that have finished (or killed).
175 void display_all_actor_status() const;
176 void run_all_actors();
178 /* @brief Finish simulation initialization
179 * This function must be called before the first call to solve()
181 void presolve() const;
182 /** @brief Performs a part of the simulation
183 * @param max_date Maximum date to update the simulation to, or -1
184 * @return the elapsed time, or -1.0 if no event could be executed
186 * This function execute all possible events, update the action states and returns the time elapsed.
187 * When you call execute or communicate on a model, the corresponding actions are not executed immediately but only
188 * when you call solve().
189 * Note that the returned elapsed time can be zero.
191 double solve(double max_date) const;
193 /** @brief Run the main simulation loop until the specified date (or infinitly if max_date is negative). */
194 void run(double max_date);
196 /** @brief Return the current time in milliseconds. */
197 static double get_clock();
200 } // namespace kernel
201 } // namespace simgrid