Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] bunch of const
[simgrid.git] / include / simgrid / s4u / Engine.hpp
1 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_S4U_ENGINE_HPP
7 #define SIMGRID_S4U_ENGINE_HPP
8
9 #include <xbt/base.h>
10
11 #include <simgrid/forward.h>
12
13 #include <simgrid/kernel/resource/Model.hpp>
14 #include <simgrid/s4u/NetZone.hpp>
15
16 #include <set>
17 #include <string>
18 #include <utility>
19 #include <vector>
20
21 namespace simgrid {
22 namespace s4u {
23 /** @brief Simulation engine
24  *
25  * This is a singleton containing all the main functions of the simulation.
26  */
27 class XBT_PUBLIC Engine {
28 #ifndef DOXYGEN
29   friend simgrid::kernel::EngineImpl;
30 #endif
31
32 public:
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 */
38 #ifndef DOXYGEN
39   Engine(const Engine&) = delete;
40   Engine(Engine&&)      = delete;
41   ~Engine();
42 #endif
43
44   /** Finalize the default engine and all its dependencies */
45   void shutdown();
46
47   /** Run the simulation until its end */
48   void run() const;
49
50   /** Run the simulation until the specified date */
51   void run_until(double max_date) const;
52
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; }
59
60   void load_platform(const std::string& platf) const;
61
62   void register_function(const std::string& name, const std::function<void(int, char**)>& code);
63   void register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code);
64   void register_function(const std::string& name, const kernel::actor::ActorCodeFactory& factory);
65
66   void register_default(const std::function<void(int, char**)>& code);
67   void register_default(const kernel::actor::ActorCodeFactory& factory);
68
69   template <class F> void register_actor(const std::string& name)
70   {
71     kernel::actor::ActorCodeFactory code_factory = [](std::vector<std::string> args) {
72       return kernel::actor::ActorCode([args = std::move(args)]() mutable {
73         F code(std::move(args));
74         code();
75       });
76     };
77     register_function(name, code_factory);
78   }
79   template <class F> void register_actor(const std::string& name, F code)
80   {
81     kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
82       return kernel::actor::ActorCode([code, args = std::move(args)]() mutable { code(std::move(args)); });
83     };
84     register_function(name, code_factory);
85   }
86
87   /** If non-null, the provided set will be filled with all activities that fail to start because of a veto */
88   void track_vetoed_activities(std::set<Activity*>* vetoed_activities) const;
89
90   void load_deployment(const std::string& deploy) const;
91
92 protected:
93 #ifndef DOXYGEN
94   friend Host;
95   friend Link;
96   friend Disk;
97   friend kernel::routing::NetPoint;
98   friend kernel::routing::NetZoneImpl;
99   friend kernel::resource::HostImpl;
100   friend kernel::resource::LinkImpl;
101   void host_register(const std::string& name, Host* host);
102   void host_unregister(const std::string& name);
103   void link_register(const std::string& name, const Link* link);
104   void link_unregister(const std::string& name);
105   void netpoint_register(simgrid::kernel::routing::NetPoint* card);
106   void netpoint_unregister(simgrid::kernel::routing::NetPoint* card);
107 #endif /*DOXYGEN*/
108
109 public:
110   /** Returns the amount of hosts existing in the platform. */
111   size_t get_host_count() const;
112   /** Returns a vector of all hosts found in the platform.
113    *
114    * The order is generally different from the creation/declaration order in the XML platform because we use a hash
115    * table internally.
116    */
117   std::vector<Host*> get_all_hosts() const;
118   std::vector<Host*> get_filtered_hosts(const std::function<bool(Host*)>& filter) const;
119   Host* host_by_name(const std::string& name) const;
120   Host* host_by_name_or_null(const std::string& name) const;
121
122   size_t get_link_count() const;
123   std::vector<Link*> get_all_links() const;
124   std::vector<Link*> get_filtered_links(const std::function<bool(Link*)>& filter) const;
125   Link* link_by_name(const std::string& name) const;
126   /**
127    * @brief Find a split-duplex link from its name.
128    * @throw std::invalid_argument if the searched link does not exist.
129    */
130   SplitDuplexLink* split_duplex_link_by_name(const std::string& name) const;
131   Link* link_by_name_or_null(const std::string& name) const;
132
133   Mailbox* mailbox_by_name_or_create(const std::string& name) const;
134
135   size_t get_actor_count() const;
136   std::vector<ActorPtr> get_all_actors() const;
137   std::vector<ActorPtr> get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const;
138
139   std::vector<kernel::routing::NetPoint*> get_all_netpoints() const;
140   kernel::routing::NetPoint* netpoint_by_name_or_null(const std::string& name) const;
141   /**
142    * @brief Get netpoint by its name
143    *
144    * @param name Netpoint name
145    * @throw std::invalid_argument if netpoint doesn't exist
146    */
147   kernel::routing::NetPoint* netpoint_by_name(const std::string& name) const;
148
149   NetZone* get_netzone_root() const;
150   void set_netzone_root(const NetZone* netzone);
151
152   NetZone* netzone_by_name_or_null(const std::string& name) const;
153
154   /**
155    * @brief Add a model to engine list
156    *
157    * @param model        Pointer to model
158    * @param dependencies List of dependencies for this model (optional)
159    */
160   void add_model(std::shared_ptr<simgrid::kernel::resource::Model> model,
161                  const std::vector<kernel::resource::Model*>& dependencies = {});
162
163   /** @brief Get list of all models managed by this engine */
164   const std::vector<simgrid::kernel::resource::Model*>& get_all_models() const;
165
166   /** @brief Retrieves all netzones of the type indicated by the template argument */
167   template <class T> std::vector<T*> get_filtered_netzones() const
168   {
169     static_assert(std::is_base_of<kernel::routing::NetZoneImpl, T>::value,
170                   "Filtering netzones is only possible for subclasses of kernel::routing::NetZoneImpl");
171     std::vector<T*> res;
172     get_filtered_netzones_recursive(get_netzone_root(), &res);
173     return res;
174   }
175
176   kernel::EngineImpl* get_impl() const { return pimpl; }
177
178   /** Returns whether SimGrid was initialized yet -- mostly for internal use */
179   static bool is_initialized();
180   /** @brief set a configuration variable
181    *
182    * @beginrst
183    * Do --help on any simgrid binary to see the list of currently existing configuration variables
184    * (see also :ref:`options`).
185    * @endrst
186    *
187    * Example:
188    * simgrid::s4u::Engine::set_config("host/model:ptask_L07");
189    */
190   static void set_config(const std::string& str);
191   static void set_config(const std::string& name, int value);
192   static void set_config(const std::string& name, bool value);
193   static void set_config(const std::string& name, double value);
194   static void set_config(const std::string& name, const std::string& value);
195
196   Engine* set_default_comm_data_copy_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t));
197   /** Callback fired when the platform is created (ie, the xml file parsed),
198    * right before the actual simulation starts. */
199   static xbt::signal<void()> on_platform_created;
200
201   /** Callback fired when the platform is about to be created
202    * (ie, after any configuration change and just before the resource creation) */
203   static xbt::signal<void()> on_platform_creation;
204
205   /** Callback fired when the main simulation loop ends, just before the end of Engine::run() */
206   static xbt::signal<void()> on_simulation_end;
207
208   /** Callback fired when the time jumps into the future */
209   static xbt::signal<void(double)> on_time_advance;
210
211   /** Callback fired when the time cannot advance because of inter-actors deadlock. Note that the on_exit of each actor
212    * is also executed on deadlock. */
213   static xbt::signal<void(void)> on_deadlock;
214
215 private:
216   kernel::EngineImpl* const pimpl;
217   static Engine* instance_;
218   void initialize(int* argc, char** argv);
219 };
220
221 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename);
222
223 #ifndef DOXYGEN /* Internal use only, no need to expose it */
224 template <class T>
225 XBT_PRIVATE void get_filtered_netzones_recursive(const s4u::NetZone* current, std::vector<T*>* whereto)
226 {
227   static_assert(std::is_base_of<kernel::routing::NetZoneImpl, T>::value,
228                 "Filtering netzones is only possible for subclasses of kernel::routing::NetZoneImpl");
229   for (auto const& elem : current->get_children()) {
230     get_filtered_netzones_recursive(elem, whereto);
231     auto* elem_impl = dynamic_cast<T*>(elem->get_impl());
232     if (elem_impl != nullptr)
233       whereto->push_back(elem_impl);
234   }
235 }
236 #endif
237 } // namespace s4u
238 } // namespace simgrid
239
240 #endif /* SIMGRID_S4U_ENGINE_HPP */