Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ea624333216051b67e6cd2cd064a4a098e1f0c87
[simgrid.git] / src / s4u / s4u_Engine.cpp
1 /* s4u::Engine Simulation Engine and global functions. */
2
3 /* Copyright (c) 2006-2022. 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 <simgrid/kernel/routing/NetPoint.hpp>
9 #include <simgrid/modelchecker.h>
10 #include <simgrid/s4u/Engine.hpp>
11
12 #define SIMIX_H_NO_DEPRECATED_WARNING // avoid deprecation warning on include (remove with XBT_ATTRIB_DEPRECATED_v333)
13 #include <simgrid/simix.h>
14
15 #include "mc/mc.h"
16 #include "src/instr/instr_private.hpp"
17 #include "src/kernel/EngineImpl.hpp"
18 #include "src/mc/mc_replay.hpp"
19
20 #include <algorithm>
21 #include <string>
22
23 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
25
26 static simgrid::kernel::actor::ActorCode maestro_code;
27
28 namespace simgrid {
29 namespace s4u {
30 xbt::signal<void()> Engine::on_platform_creation;
31 xbt::signal<void()> Engine::on_platform_created;
32 xbt::signal<void()> Engine::on_simulation_start;
33 xbt::signal<void()> Engine::on_simulation_end;
34 xbt::signal<void(double)> Engine::on_time_advance;
35 xbt::signal<void(void)> Engine::on_deadlock;
36
37 Engine* Engine::instance_ = nullptr; /* This singleton is awful, but I don't see no other solution right now. */
38
39 void Engine::initialize(int* argc, char** argv)
40 {
41   xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
42   Engine::instance_ = this;
43   instr::init();
44   pimpl->initialize(argc, argv);
45   // Either create a new context with maestro or create
46   // a context object with the current context maestro):
47   kernel::actor::create_maestro(maestro_code);
48 }
49
50 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl())
51 {
52   int argc   = 1;
53   char* argv = &name[0];
54   initialize(&argc, &argv);
55 }
56
57 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
58 {
59   initialize(argc, argv);
60 }
61
62 Engine::~Engine()
63 {
64   kernel::EngineImpl::shutdown();
65   Engine::instance_ = nullptr;
66 }
67
68 /** @brief Retrieve the engine singleton */
69 Engine* Engine::get_instance()
70 {
71   int argc   = 0;
72   char* argv = nullptr;
73   return get_instance(&argc, &argv);
74 }
75 Engine* Engine::get_instance(int* argc, char** argv)
76 {
77   if (Engine::instance_ == nullptr) {
78     auto e = new Engine(argc, argv);
79     xbt_assert(Engine::instance_ == e);
80   }
81   return Engine::instance_;
82 }
83
84 void Engine::shutdown() // XBT_ATTRIB_DEPRECATED_v335
85 {
86   delete Engine::instance_;
87 }
88
89 double Engine::get_clock()
90 {
91   if (MC_is_active() || MC_record_replay_is_active()) {
92     return MC_process_clock_get(kernel::actor::ActorImpl::self());
93   } else {
94     return kernel::EngineImpl::get_clock();
95   }
96 }
97
98 void Engine::add_model(std::shared_ptr<kernel::resource::Model> model,
99                        const std::vector<kernel::resource::Model*>& dependencies)
100 {
101   kernel::actor::simcall_answered([this, &model, &dependencies] { pimpl->add_model(std::move(model), dependencies); });
102 }
103
104 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
105 {
106   return pimpl->get_all_models();
107 }
108
109 /**
110  * Creates a new platform, including hosts, links, and the routing table.
111  *
112  * @beginrst
113  * See also: :ref:`platform`.
114  * @endrst
115  */
116 void Engine::load_platform(const std::string& platf) const
117 {
118   pimpl->load_platform(platf);
119 }
120
121 /**
122  * @brief Seals the platform, finishing the creation of its resources.
123  *
124  * This method is optional. The seal() is done automatically when you call Engine::run.
125  */
126 void Engine::seal_platform() const
127 {
128   pimpl->seal_platform();
129 }
130
131 /** Registers the main function of an actor that will be launched from the deployment file */
132 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
133 {
134   kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
135     return xbt::wrap_main(code, std::move(args));
136   };
137   register_function(name, code_factory);
138 }
139
140 /** Registers the main function of an actor that will be launched from the deployment file */
141 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
142 {
143   kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
144     return std::bind(std::move(code), std::move(args));
145   };
146   register_function(name, code_factory);
147 }
148 /** Registers a function as the default main function of actors
149  *
150  * It will be used as fallback when the function requested from the deployment file was not registered.
151  * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
152  */
153 void Engine::register_default(const std::function<void(int, char**)>& code)
154 {
155   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
156 }
157 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
158 {
159   simgrid::kernel::actor::simcall_answered([this, &code]() { pimpl->register_default(code); });
160 }
161
162 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
163 {
164   simgrid::kernel::actor::simcall_answered([this, name, &code]() { pimpl->register_function(name, code); });
165 }
166
167 /** Load a deployment file and launch the actors that it contains
168  *
169  * @beginrst
170  * See also: :ref:`deploy`.
171  * @endrst
172  */
173 void Engine::load_deployment(const std::string& deploy) const
174 {
175   pimpl->load_deployment(deploy);
176 }
177
178 /** Returns the amount of hosts in the platform */
179 size_t Engine::get_host_count() const
180 {
181   return pimpl->hosts_.size();
182 }
183
184 std::vector<Host*> Engine::get_all_hosts() const
185 {
186   std::vector<Host*> res;
187   for (auto const& kv : pimpl->hosts_)
188     res.push_back(kv.second);
189   return res;
190 }
191
192 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
193 {
194   std::vector<Host*> hosts;
195   for (auto const& kv : pimpl->hosts_) {
196     if (filter(kv.second))
197       hosts.push_back(kv.second);
198   }
199
200   return hosts;
201 }
202
203 void Engine::host_register(const std::string& name, Host* host)
204 {
205   pimpl->hosts_[name] = host;
206 }
207
208 void Engine::host_unregister(const std::string& name)
209 {
210   pimpl->hosts_.erase(name);
211 }
212
213 /** @brief Find a host from its name.
214  *
215  *  @throw std::invalid_argument if the searched host does not exist.
216  */
217 Host* Engine::host_by_name(const std::string& name) const
218 {
219   auto host = pimpl->hosts_.find(name);
220   if (host == pimpl->hosts_.end())
221     throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
222   return host->second;
223 }
224
225 /** @brief Find a host from its name (or nullptr if that host does not exist) */
226 Host* Engine::host_by_name_or_null(const std::string& name) const
227 {
228   auto host = pimpl->hosts_.find(name);
229   return host == pimpl->hosts_.end() ? nullptr : host->second;
230 }
231
232 /** @brief Find a link from its name.
233  *
234  *  @throw std::invalid_argument if the searched link does not exist.
235  */
236 Link* Engine::link_by_name(const std::string& name) const
237 {
238   auto link = pimpl->links_.find(name);
239   if (link == pimpl->links_.end())
240     throw std::invalid_argument(std::string("Link not found: ") + name);
241   return link->second->get_iface();
242 }
243
244 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
245 {
246   auto link = pimpl->split_duplex_links_.find(name);
247   if (link == pimpl->split_duplex_links_.end())
248     throw std::invalid_argument(std::string("Link not found: ") + name);
249   return link->second->get_iface();
250 }
251
252 /** @brief Find a link from its name (or nullptr if that link does not exist) */
253 Link* Engine::link_by_name_or_null(const std::string& name) const
254 {
255   auto link = pimpl->links_.find(name);
256   return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
257 }
258
259 /** @brief Find a mailox from its name or create one if it does not exist) */
260 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
261 {
262   /* two actors may have pushed the same mbox_create simcall at the same time */
263   kernel::activity::MailboxImpl* mbox = kernel::actor::simcall_answered([&name, this] {
264     auto m = pimpl->mailboxes_.emplace(name, nullptr);
265     if (m.second) {
266       m.first->second = new kernel::activity::MailboxImpl(name);
267       XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
268     }
269     return m.first->second;
270   });
271   return mbox->get_iface();
272 }
273
274 void Engine::link_register(const std::string& name, const Link* link)
275 {
276   pimpl->links_[name] = link->get_impl();
277 }
278
279 void Engine::link_unregister(const std::string& name)
280 {
281   pimpl->links_.erase(name);
282 }
283
284 /** @brief Returns the amount of links in the platform */
285 size_t Engine::get_link_count() const
286 {
287   return pimpl->links_.size();
288 }
289
290 /** @brief Returns the list of all links found in the platform */
291 std::vector<Link*> Engine::get_all_links() const
292 {
293   std::vector<Link*> res;
294   for (auto const& kv : pimpl->links_)
295     res.push_back(kv.second->get_iface());
296   return res;
297 }
298
299 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
300 {
301   std::vector<Link*> filtered_list;
302   for (auto const& kv : pimpl->links_) {
303     Link* l = kv.second->get_iface();
304     if (filter(l))
305       filtered_list.push_back(l);
306   }
307   return filtered_list;
308 }
309
310 size_t Engine::get_actor_count() const
311 {
312   return pimpl->get_actor_count();
313 }
314
315 std::vector<ActorPtr> Engine::get_all_actors() const
316 {
317   std::vector<ActorPtr> actor_list;
318   for (auto const& kv : pimpl->get_actor_list()) {
319     actor_list.push_back(kv.second->get_iface());
320   }
321   return actor_list;
322 }
323
324 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
325 {
326   std::vector<ActorPtr> actor_list;
327   for (auto const& kv : pimpl->get_actor_list()) {
328     if (filter(kv.second->get_iface()))
329       actor_list.push_back(kv.second->get_iface());
330   }
331   return actor_list;
332 }
333
334 void Engine::run() const
335 {
336   run_until(-1);
337 }
338 void Engine::run_until(double max_date) const
339 {
340   static bool callback_called = false;
341   if (not callback_called) {
342     on_simulation_start();
343     callback_called = true;
344   }
345   /* Clean IO before the run */
346   fflush(stdout);
347   fflush(stderr);
348
349   pimpl->run(max_date);
350 }
351
352 void Engine::track_vetoed_activities(std::set<Activity*>* vetoed_activities) const
353 {
354   Activity::set_vetoed_activities(vetoed_activities);
355 }
356
357 /** @brief Retrieve the root netzone, containing all others */
358 s4u::NetZone* Engine::get_netzone_root() const
359 {
360   if (pimpl->netzone_root_)
361     return pimpl->netzone_root_->get_iface();
362   return nullptr;
363 }
364 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
365 void Engine::set_netzone_root(const s4u::NetZone* netzone)
366 {
367   xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
368   pimpl->netzone_root_ = netzone->get_impl();
369 }
370
371 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
372 {
373   if (current->get_name() == name)
374     return current;
375
376   for (auto const& elem : current->get_children()) {
377     NetZone* tmp = netzone_by_name_recursive(elem, name);
378     if (tmp != nullptr) {
379       return tmp;
380     }
381   }
382   return nullptr;
383 }
384
385 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
386 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
387 {
388   return netzone_by_name_recursive(get_netzone_root(), name);
389 }
390
391 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
392 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
393 {
394   auto netp = pimpl->netpoints_.find(name);
395   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
396 }
397
398 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
399 {
400   auto netp = netpoint_by_name_or_null(name);
401   if (netp == nullptr) {
402     throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
403   }
404   return netp;
405 }
406
407 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
408 {
409   std::vector<kernel::routing::NetPoint*> res;
410   for (auto const& kv : pimpl->netpoints_)
411     res.push_back(kv.second);
412   return res;
413 }
414
415 /** @brief Register a new netpoint to the system */
416 void Engine::netpoint_register(kernel::routing::NetPoint* point)
417 {
418   simgrid::kernel::actor::simcall_answered([this, point] { pimpl->netpoints_[point->get_name()] = point; });
419 }
420
421 /** @brief Unregister a given netpoint */
422 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
423 {
424   kernel::actor::simcall_answered([this, point] {
425     pimpl->netpoints_.erase(point->get_name());
426     delete point;
427   });
428 }
429
430 bool Engine::is_initialized()
431 {
432   return Engine::instance_ != nullptr;
433 }
434 void Engine::set_config(const std::string& str)
435 {
436   config::set_parse(str);
437 }
438 void Engine::set_config(const std::string& name, int value)
439 {
440   config::set_value(name.c_str(), value);
441 }
442 void Engine::set_config(const std::string& name, double value)
443 {
444   config::set_value(name.c_str(), value);
445 }
446 void Engine::set_config(const std::string& name, bool value)
447 {
448   config::set_value(name.c_str(), value);
449 }
450 void Engine::set_config(const std::string& name, const std::string& value)
451 {
452   config::set_value(name.c_str(), value);
453 }
454
455 Engine* Engine::set_default_comm_data_copy_callback(
456     const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback)
457 {
458   kernel::activity::CommImpl::set_copy_data_callback(callback);
459   return this;
460 }
461
462 } // namespace s4u
463 } // namespace simgrid
464
465 double SIMIX_get_clock() // XBT_ATTRIB_DEPRECATED_v332
466 {
467   return simgrid::s4u::Engine::get_clock();
468 }
469
470 /* **************************** Public C interface *************************** */
471 void simgrid_init(int* argc, char** argv)
472 {
473   static simgrid::s4u::Engine e(argc, argv);
474 }
475 void simgrid_load_platform(const char* file)
476 {
477   simgrid::s4u::Engine::get_instance()->load_platform(file);
478 }
479
480 void simgrid_load_deployment(const char* file)
481 {
482   simgrid::s4u::Engine::get_instance()->load_deployment(file);
483 }
484 void simgrid_run()
485 {
486   simgrid::s4u::Engine::get_instance()->run();
487 }
488 void simgrid_run_until(double max_date)
489 {
490   simgrid::s4u::Engine::get_instance()->run_until(max_date);
491 }
492 void simgrid_register_function(const char* name, void (*code)(int, char**))
493 {
494   simgrid::s4u::Engine::get_instance()->register_function(name, code);
495 }
496 void simgrid_register_default(void (*code)(int, char**))
497 {
498   simgrid::s4u::Engine::get_instance()->register_default(code);
499 }
500 double simgrid_get_clock()
501 {
502   return simgrid::s4u::Engine::get_clock();
503 }
504
505 void simgrid_set_maestro(void (*code)(void*), void* data)
506 {
507 #ifdef _WIN32
508   XBT_WARN("simgrid_set_maestro is believed to not work on windows. Please help us investigating this issue if "
509            "you need that feature");
510 #endif
511   maestro_code = std::bind(code, data);
512 }
513 void SIMIX_set_maestro(void (*code)(void*), void* data) // XBT_ATTRIB_DEPRECATED_v333
514 {
515   simgrid_set_maestro(code, data);
516 }