Logo AND Algorithmique Numérique Distribuée

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