Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #374 from adegomme/master
[simgrid.git] / src / s4u / s4u_Engine.cpp
1 /* s4u::Engine Simulation Engine and global functions. */
2
3 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "mc/mc.h"
9 #include "simgrid/kernel/routing/NetPoint.hpp"
10 #include "simgrid/kernel/routing/NetZoneImpl.hpp"
11 #include "simgrid/s4u/Disk.hpp"
12 #include "simgrid/s4u/Engine.hpp"
13 #include "simgrid/s4u/Host.hpp"
14 #include "simgrid/s4u/Mailbox.hpp"
15 #include "simgrid/s4u/NetZone.hpp"
16 #include "simgrid/simix.h"
17 #include "src/instr/instr_private.hpp"
18 #include "src/kernel/EngineImpl.hpp"
19 #include "src/simix/smx_private.hpp" // For access to simix_global->process_list
20 #include "src/surf/network_interface.hpp"
21 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
22 #include <simgrid/Exception.hpp>
23
24 #include <algorithm>
25 #include <string>
26
27 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
29
30 namespace simgrid {
31 namespace s4u {
32 xbt::signal<void()> Engine::on_platform_creation;
33 xbt::signal<void()> Engine::on_platform_created;
34 xbt::signal<void()> Engine::on_simulation_end;
35 xbt::signal<void(double)> Engine::on_time_advance;
36 xbt::signal<void(void)> Engine::on_deadlock;
37
38 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
39
40 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
41 {
42   xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
43   instr::init();
44   SIMIX_global_init(argc, argv);
45
46   Engine::instance_ = this;
47 }
48
49 Engine::~Engine()
50 {
51   delete pimpl;
52   Engine::instance_ = nullptr;
53 }
54
55 /** @brief Retrieve the engine singleton */
56 Engine* Engine::get_instance()
57 {
58   if (Engine::instance_ == nullptr) {
59     auto e = new Engine(nullptr, nullptr);
60     xbt_assert(Engine::instance_ == e);
61   }
62   return Engine::instance_;
63 }
64
65 void Engine::shutdown()
66 {
67   delete Engine::instance_;
68   Engine::instance_ = nullptr;
69 }
70
71 double Engine::get_clock()
72 {
73   return SIMIX_get_clock();
74 }
75
76 void Engine::add_model(simgrid::kernel::resource::Model::Type type,
77                        std::shared_ptr<simgrid::kernel::resource::Model> model)
78 {
79   simgrid::kernel::actor::simcall([this, type, &model] { pimpl->add_model(type, std::move(model)); });
80 }
81
82 /** @brief Get list of models created for a resource type */
83 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_model_list(simgrid::kernel::resource::Model::Type type)
84 {
85   return pimpl->get_model_list(type);
86 }
87
88 const std::vector<std::shared_ptr<simgrid::kernel::resource::Model>>& Engine::get_all_models() const
89 {
90   return pimpl->get_all_models();
91 }
92
93 /**
94  * Creates a new platform, including hosts, links, and the routing table.
95  *
96  * @beginrst
97  * See also: :ref:`platform`.
98  * @endrst
99  */
100 void Engine::load_platform(const std::string& platf) const
101 {
102   double start = xbt_os_time();
103   parse_platform_file(platf);
104
105   double end = xbt_os_time();
106   XBT_DEBUG("PARSE TIME: %g", (end - start));
107 }
108
109 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
110 {
111   kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
112     return xbt::wrap_main(code, std::move(args));
113   };
114   register_function(name, code_factory);
115 }
116 void Engine::register_default(int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
117 {
118   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
119 }
120
121 /** Registers the main function of an actor that will be launched from the deployment file */
122 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
123 {
124   kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
125     return xbt::wrap_main(code, std::move(args));
126   };
127   register_function(name, code_factory);
128 }
129
130 /** Registers the main function of an actor that will be launched from the deployment file */
131 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
132 {
133   kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
134     return std::bind(std::move(code), std::move(args));
135   };
136   register_function(name, code_factory);
137 }
138 /** Registers a function as the default main function of actors
139  *
140  * It will be used as fallback when the function requested from the deployment file was not registered.
141  * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
142  */
143 void Engine::register_default(const std::function<void(int, char**)>& code)
144 {
145   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
146 }
147 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
148 {
149   simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
150 }
151
152 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
153 {
154   simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
155 }
156
157 /** Load a deployment file and launch the actors that it contains
158  *
159  * @beginrst
160  * See also: :ref:`deploy`.
161  * @endrst
162  */
163 void Engine::load_deployment(const std::string& deploy) const
164 {
165   pimpl->load_deployment(deploy);
166 }
167
168 /** Returns the amount of hosts in the platform */
169 size_t Engine::get_host_count() const
170 {
171   return pimpl->hosts_.size();
172 }
173
174 std::vector<Host*> Engine::get_all_hosts() const
175 {
176   std::vector<Host*> res;
177   for (auto const& kv : pimpl->hosts_)
178     res.push_back(kv.second);
179   return res;
180 }
181
182 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
183 {
184   std::vector<Host*> hosts;
185   for (auto const& kv : pimpl->hosts_) {
186     if (filter(kv.second))
187       hosts.push_back(kv.second);
188   }
189
190   return hosts;
191 }
192
193 void Engine::host_register(const std::string& name, Host* host)
194 {
195   pimpl->hosts_[name] = host;
196 }
197
198 void Engine::host_unregister(const std::string& name)
199 {
200   pimpl->hosts_.erase(name);
201 }
202
203 /** @brief Find a host from its name.
204  *
205  *  @throw std::invalid_argument if the searched host does not exist.
206  */
207 Host* Engine::host_by_name(const std::string& name) const
208 {
209   auto host = pimpl->hosts_.find(name);
210   if (host == pimpl->hosts_.end())
211     throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
212   return host->second;
213 }
214
215 /** @brief Find a host from its name (or nullptr if that host does not exist) */
216 Host* Engine::host_by_name_or_null(const std::string& name) const
217 {
218   auto host = pimpl->hosts_.find(name);
219   return host == pimpl->hosts_.end() ? nullptr : host->second;
220 }
221
222 /** @brief Find a link from its name.
223  *
224  *  @throw std::invalid_argument if the searched link does not exist.
225  */
226 Link* Engine::link_by_name(const std::string& name) const
227 {
228   auto link = pimpl->links_.find(name);
229   if (link == pimpl->links_.end())
230     throw std::invalid_argument(std::string("Link not found: ") + name);
231   return link->second->get_iface();
232 }
233
234 /** @brief Find a link from its name (or nullptr if that link does not exist) */
235 Link* Engine::link_by_name_or_null(const std::string& name) const
236 {
237   auto link = pimpl->links_.find(name);
238   return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
239 }
240
241 void Engine::link_register(const std::string& name, const Link* link)
242 {
243   pimpl->links_[name] = link->get_impl();
244 }
245
246 void Engine::link_unregister(const std::string& name)
247 {
248   pimpl->links_.erase(name);
249 }
250
251 /** @brief Returns the amount of links in the platform */
252 size_t Engine::get_link_count() const
253 {
254   return pimpl->links_.size();
255 }
256
257 /** @brief Returns the list of all links found in the platform */
258 std::vector<Link*> Engine::get_all_links() const
259 {
260   std::vector<Link*> res;
261   for (auto const& kv : pimpl->links_)
262     res.push_back(kv.second->get_iface());
263   return res;
264 }
265
266 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
267 {
268   std::vector<Link*> filtered_list;
269   for (auto const& kv : pimpl->links_) {
270     Link* l = kv.second->get_iface();
271     if (filter(l))
272       filtered_list.push_back(l);
273   }
274   return filtered_list;
275 }
276
277 size_t Engine::get_actor_count() const
278 {
279   return simix_global->process_list.size();
280 }
281
282 std::vector<ActorPtr> Engine::get_all_actors() const
283 {
284   std::vector<ActorPtr> actor_list;
285   for (auto const& kv : simix_global->process_list) {
286     actor_list.push_back(kv.second->get_iface());
287   }
288   return actor_list;
289 }
290
291 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
292 {
293   std::vector<ActorPtr> actor_list;
294   for (auto const& kv : simix_global->process_list) {
295     if (filter(kv.second->get_iface()))
296       actor_list.push_back(kv.second->get_iface());
297   }
298   return actor_list;
299 }
300
301 void Engine::run() const
302 {
303   /* Clean IO before the run */
304   fflush(stdout);
305   fflush(stderr);
306
307   if (MC_is_active()) {
308     MC_run();
309   } else {
310     SIMIX_run();
311   }
312 }
313
314 /** @brief Retrieve the root netzone, containing all others */
315 s4u::NetZone* Engine::get_netzone_root() const
316 {
317   return pimpl->netzone_root_->get_iface();
318 }
319 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
320 void Engine::set_netzone_root(const s4u::NetZone* netzone)
321 {
322   xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
323   pimpl->netzone_root_ = netzone->get_impl();
324 }
325
326 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
327 {
328   if (current->get_name() == name)
329     return current;
330
331   for (auto const& elem : current->get_children()) {
332     NetZone* tmp = netzone_by_name_recursive(elem, name);
333     if (tmp != nullptr) {
334       return tmp;
335     }
336   }
337   return nullptr;
338 }
339
340 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
341 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
342 {
343   return netzone_by_name_recursive(get_netzone_root(), name);
344 }
345
346 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
347 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
348 {
349   auto netp = pimpl->netpoints_.find(name);
350   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
351 }
352
353 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
354 {
355   std::vector<kernel::routing::NetPoint*> res;
356   for (auto const& kv : pimpl->netpoints_)
357     res.push_back(kv.second);
358   return res;
359 }
360
361 /** @brief Register a new netpoint to the system */
362 void Engine::netpoint_register(kernel::routing::NetPoint* point)
363 {
364   simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
365 }
366
367 /** @brief Unregister a given netpoint */
368 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
369 {
370   kernel::actor::simcall([this, point] {
371     pimpl->netpoints_.erase(point->get_name());
372     delete point;
373   });
374 }
375
376 bool Engine::is_initialized()
377 {
378   return Engine::instance_ != nullptr;
379 }
380 void Engine::set_config(const std::string& str)
381 {
382   config::set_parse(str);
383 }
384 void Engine::set_config(const std::string& name, int value)
385 {
386   config::set_value(name.c_str(), value);
387 }
388 void Engine::set_config(const std::string& name, double value)
389 {
390   config::set_value(name.c_str(), value);
391 }
392 void Engine::set_config(const std::string& name, bool value)
393 {
394   config::set_value(name.c_str(), value);
395 }
396 void Engine::set_config(const std::string& name, const std::string& value)
397 {
398   config::set_value(name.c_str(), value);
399 }
400
401 } // namespace s4u
402 } // namespace simgrid
403
404 /* **************************** Public C interface *************************** */
405 void simgrid_init(int* argc, char** argv)
406 {
407   simgrid::s4u::Engine e(argc, argv);
408 }
409 void simgrid_load_platform(const char* file)
410 {
411   simgrid::s4u::Engine::get_instance()->load_platform(file);
412 }
413
414 void simgrid_load_deployment(const char* file)
415 {
416   simgrid::s4u::Engine::get_instance()->load_deployment(file);
417 }
418 void simgrid_run()
419 {
420   simgrid::s4u::Engine::get_instance()->run();
421 }
422 void simgrid_register_function(const char* name, void (*code)(int, char**))
423 {
424   simgrid::s4u::Engine::get_instance()->register_function(name, code);
425 }
426 void simgrid_register_default(void (*code)(int, char**))
427 {
428   simgrid::s4u::Engine::get_instance()->register_default(code);
429 }
430 double simgrid_get_clock()
431 {
432   return simgrid::s4u::Engine::get_clock();
433 }
434
435 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
436 {
437   return simgrid::s4u::Engine::get_instance()->get_actor_count();
438 }