1 /* Copyright (c) 2006-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_S4U_ENGINE_HPP
7 #define SIMGRID_S4U_ENGINE_HPP
11 #include <simgrid/forward.h>
13 #include <simgrid/kernel/resource/Model.hpp>
14 #include <simgrid/s4u/NetZone.hpp>
23 /** @brief Simulation engine
25 * This is a singleton containing all the main functions of the simulation.
27 class XBT_PUBLIC Engine {
29 friend simgrid::kernel::EngineImpl;
33 /** Constructor, taking only the name of your main function */
34 explicit Engine(std::string name);
35 /** Constructor, taking the command line parameters of your main function */
36 explicit Engine(int* argc, char** argv);
37 /* Currently, only one instance is allowed to exist. This is why you can't copy or move it */
39 Engine(const Engine&) = delete;
40 Engine(Engine&&) = delete;
44 /** Finalize the default engine and all its dependencies */
45 XBT_ATTRIB_DEPRECATED_v335("Users are not supposed to shutdown the Engine") void shutdown();
47 /** Run the simulation until its end */
50 /** Run the simulation until the specified date */
51 void run_until(double max_date) const;
53 /** @brief Retrieve the simulation time (in seconds) */
54 static double get_clock();
55 /** @brief Retrieve the engine singleton */
56 static s4u::Engine* get_instance();
57 static s4u::Engine* get_instance(int* argc, char** argv);
58 static bool has_instance() { return instance_ != nullptr; }
60 void load_platform(const std::string& platf) const;
61 void seal_platform() const;
63 /** @verbatim embed:rst:inline Bind an actor name that could be found in :ref:`pf_tag_actor` tag to a function taking classical argc/argv parameters. See the :ref:`example <s4u_ex_actors_create>`. @endverbatim */
64 void register_function(const std::string& name, const std::function<void(int, char**)>& code);
65 /** @verbatim embed:rst:inline Bind an actor name that could be found in :ref:`pf_tag_actor` tag to a function taking a vector of strings as a parameter. See the :ref:`example <s4u_ex_actors_create>`. @endverbatim */
66 void register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code);
67 void register_function(const std::string& name, const kernel::actor::ActorCodeFactory& factory);
69 /** @verbatim embed:rst:inline Provide a default function to be used when the name used in a :ref:`pf_tag_actor` tag was not binded with ``register_function`` nor ``register_actor``. @endverbatim */
70 void register_default(const std::function<void(int, char**)>& code);
71 void register_default(const kernel::actor::ActorCodeFactory& factory);
73 /** @verbatim embed:rst:inline Bind an actor name that could be found in :ref:`pf_tag_actor` tag to a class name passed as a template parameter. See the :ref:`example <s4u_ex_actors_create>`. @endverbatim */
74 template <class F> void register_actor(const std::string& name)
76 kernel::actor::ActorCodeFactory code_factory = [](std::vector<std::string> args) {
77 return kernel::actor::ActorCode([args = std::move(args)]() mutable {
78 F code(std::move(args));
82 register_function(name, code_factory);
84 /** @verbatim embed:rst:inline Bind an actor name that could be found in :ref:`pf_tag_actor` tag to a function name passed as a parameter. See the :ref:`example <s4u_ex_actors_create>`. @endverbatim */
85 template <class F> void register_actor(const std::string& name, F code)
87 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
88 return kernel::actor::ActorCode([code, args = std::move(args)]() mutable { code(std::move(args)); });
90 register_function(name, code_factory);
93 /** If non-null, the provided set will be filled with all activities that fail to start because of a veto */
94 void track_vetoed_activities(std::set<Activity*>* vetoed_activities) const;
96 /** @verbatim embed:rst:inline Load a deployment file. See:ref:`deploy` and the :ref:`example <s4u_ex_actors_create>`. @endverbatim */
97 void load_deployment(const std::string& deploy) const;
104 friend kernel::routing::NetPoint;
105 friend kernel::routing::NetZoneImpl;
106 friend kernel::resource::HostImpl;
107 friend kernel::resource::StandardLinkImpl;
108 void host_register(const std::string& name, Host* host);
109 void host_unregister(const std::string& name);
110 void netpoint_register(simgrid::kernel::routing::NetPoint* card);
111 void netpoint_unregister(simgrid::kernel::routing::NetPoint* card);
112 void set_netzone_root(const NetZone* netzone);
116 /** Returns the amount of hosts existing in the platform. */
117 size_t get_host_count() const;
118 /** Returns a vector of all hosts found in the platform.
120 * The order is generally different from the creation/declaration order in the XML platform because we use a hash
123 std::vector<Host*> get_all_hosts() const;
124 std::vector<Host*> get_filtered_hosts(const std::function<bool(Host*)>& filter) const;
125 Host* host_by_name(const std::string& name) const;
126 Host* host_by_name_or_null(const std::string& name) const;
128 size_t get_link_count() const;
129 std::vector<Link*> get_all_links() const;
130 std::vector<Link*> get_filtered_links(const std::function<bool(Link*)>& filter) const;
131 Link* link_by_name(const std::string& name) const;
133 * @brief Find a split-duplex link from its name.
134 * @throw std::invalid_argument if the searched link does not exist.
136 SplitDuplexLink* split_duplex_link_by_name(const std::string& name) const;
137 Link* link_by_name_or_null(const std::string& name) const;
139 Mailbox* mailbox_by_name_or_create(const std::string& name) const;
141 size_t get_actor_count() const;
142 std::vector<ActorPtr> get_all_actors() const;
143 std::vector<ActorPtr> get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const;
145 std::vector<kernel::routing::NetPoint*> get_all_netpoints() const;
146 kernel::routing::NetPoint* netpoint_by_name_or_null(const std::string& name) const;
148 * @brief Get netpoint by its name
150 * @param name Netpoint name
151 * @throw std::invalid_argument if netpoint doesn't exist
153 kernel::routing::NetPoint* netpoint_by_name(const std::string& name) const;
155 NetZone* get_netzone_root() const;
157 NetZone* netzone_by_name_or_null(const std::string& name) const;
160 * @brief Add a model to engine list
162 * @param model Pointer to model
163 * @param dependencies List of dependencies for this model (optional)
165 void add_model(std::shared_ptr<simgrid::kernel::resource::Model> model,
166 const std::vector<kernel::resource::Model*>& dependencies = {});
168 /** @brief Get list of all models managed by this engine */
169 const std::vector<simgrid::kernel::resource::Model*>& get_all_models() const;
171 /** @brief Retrieves all netzones of the type indicated by the template argument */
172 template <class T> std::vector<T*> get_filtered_netzones() const
174 static_assert(std::is_base_of<kernel::routing::NetZoneImpl, T>::value,
175 "Filtering netzones is only possible for subclasses of kernel::routing::NetZoneImpl");
177 get_filtered_netzones_recursive(get_netzone_root(), &res);
181 kernel::EngineImpl* get_impl() const { return pimpl; }
183 /** Returns whether SimGrid was initialized yet -- mostly for internal use */
184 static bool is_initialized();
185 /** @brief set a configuration variable
188 * Do --help on any simgrid binary to see the list of currently existing configuration variables
189 * (see also :ref:`options`).
193 * simgrid::s4u::Engine::set_config("host/model:ptask_L07");
195 static void set_config(const std::string& str);
196 static void set_config(const std::string& name, int value);
197 static void set_config(const std::string& name, bool value);
198 static void set_config(const std::string& name, double value);
199 static void set_config(const std::string& name, const std::string& value);
202 set_default_comm_data_copy_callback(const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback);
204 /** Add a callback fired when the platform is created (ie, the xml file parsed),
205 * right before the actual simulation starts. */
206 static void on_platform_created_cb(const std::function<void()>& cb) { on_platform_created.connect(cb); }
207 /** Add a callback fired when the platform is about to be created
208 * (ie, after any configuration change and just before the resource creation) */
209 static void on_platform_creation_cb(const std::function<void()>& cb) { on_platform_creation.connect(cb); }
210 /** Add a callback fired when the main simulation loop starts, at the beginning of the first call to Engine::run() */
211 static void on_simulation_start_cb(const std::function<void()>& cb) { on_simulation_start.connect(cb); }
212 /** Add a callback fired when the main simulation loop ends, just before the end of Engine::run() */
213 static void on_simulation_end_cb(const std::function<void()>& cb) { on_simulation_end.connect(cb); }
215 /** Add a callback fired when the time jumps into the future.
217 * It is fired right after the time change (use get_clock() to get the new timestamp).
218 * The callback parameter is the time delta since previous timestamp. */
219 static void on_time_advance_cb(const std::function<void(double)>& cb) { on_time_advance.connect(cb); }
221 /** Add a callback fired when the time cannot advance because of inter-actors deadlock. Note that the on_exit of each
222 * actor is also executed on deadlock. */
223 static void on_deadlock_cb(const std::function<void(void)>& cb) { on_deadlock.connect(cb); }
226 /* FIXME signals should be private */
227 static xbt::signal<void()> on_platform_created;
228 static xbt::signal<void()> on_platform_creation;
232 static xbt::signal<void()> on_simulation_start;
233 static xbt::signal<void(double)> on_time_advance;
234 static xbt::signal<void(void)> on_deadlock;
235 static xbt::signal<void()> on_simulation_end;
237 kernel::EngineImpl* const pimpl;
238 static Engine* instance_;
239 void initialize(int* argc, char** argv);
242 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename);
243 std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename);
245 #ifndef DOXYGEN /* Internal use only, no need to expose it */
247 XBT_PRIVATE void get_filtered_netzones_recursive(const s4u::NetZone* current, std::vector<T*>* whereto)
249 static_assert(std::is_base_of<kernel::routing::NetZoneImpl, T>::value,
250 "Filtering netzones is only possible for subclasses of kernel::routing::NetZoneImpl");
251 for (auto const& elem : current->get_children()) {
252 get_filtered_netzones_recursive(elem, whereto);
253 auto* elem_impl = dynamic_cast<T*>(elem->get_impl());
254 if (elem_impl != nullptr)
255 whereto->push_back(elem_impl);
260 } // namespace simgrid
262 #endif /* SIMGRID_S4U_ENGINE_HPP */