Logo AND Algorithmique Numérique Distribuée

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