Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New function: Engine::track_vetoed_activities()
[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 /** Registers the main function of an actor that will be launched from the deployment file */
116 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
117 {
118   kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
119     return xbt::wrap_main(code, std::move(args));
120   };
121   register_function(name, code_factory);
122 }
123
124 /** Registers the main function of an actor that will be launched from the deployment file */
125 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
126 {
127   kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
128     return std::bind(std::move(code), std::move(args));
129   };
130   register_function(name, code_factory);
131 }
132 /** Registers a function as the default main function of actors
133  *
134  * It will be used as fallback when the function requested from the deployment file was not registered.
135  * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
136  */
137 void Engine::register_default(const std::function<void(int, char**)>& code)
138 {
139   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
140 }
141 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
142 {
143   simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
144 }
145
146 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
147 {
148   simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
149 }
150
151 /** Load a deployment file and launch the actors that it contains
152  *
153  * @beginrst
154  * See also: :ref:`deploy`.
155  * @endrst
156  */
157 void Engine::load_deployment(const std::string& deploy) const
158 {
159   pimpl->load_deployment(deploy);
160 }
161
162 /** Returns the amount of hosts in the platform */
163 size_t Engine::get_host_count() const
164 {
165   return pimpl->hosts_.size();
166 }
167
168 std::vector<Host*> Engine::get_all_hosts() const
169 {
170   std::vector<Host*> res;
171   for (auto const& kv : pimpl->hosts_)
172     res.push_back(kv.second);
173   return res;
174 }
175
176 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
177 {
178   std::vector<Host*> hosts;
179   for (auto const& kv : pimpl->hosts_) {
180     if (filter(kv.second))
181       hosts.push_back(kv.second);
182   }
183
184   return hosts;
185 }
186
187 void Engine::host_register(const std::string& name, Host* host)
188 {
189   pimpl->hosts_[name] = host;
190 }
191
192 void Engine::host_unregister(const std::string& name)
193 {
194   pimpl->hosts_.erase(name);
195 }
196
197 /** @brief Find a host from its name.
198  *
199  *  @throw std::invalid_argument if the searched host does not exist.
200  */
201 Host* Engine::host_by_name(const std::string& name) const
202 {
203   auto host = pimpl->hosts_.find(name);
204   if (host == pimpl->hosts_.end())
205     throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
206   return host->second;
207 }
208
209 /** @brief Find a host from its name (or nullptr if that host does not exist) */
210 Host* Engine::host_by_name_or_null(const std::string& name) const
211 {
212   auto host = pimpl->hosts_.find(name);
213   return host == pimpl->hosts_.end() ? nullptr : host->second;
214 }
215
216 /** @brief Find a link from its name.
217  *
218  *  @throw std::invalid_argument if the searched link does not exist.
219  */
220 Link* Engine::link_by_name(const std::string& name) const
221 {
222   auto link = pimpl->links_.find(name);
223   if (link == pimpl->links_.end())
224     throw std::invalid_argument(std::string("Link not found: ") + name);
225   return link->second->get_iface();
226 }
227
228 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
229 {
230   auto link = pimpl->split_duplex_links_.find(name);
231   if (link == pimpl->split_duplex_links_.end())
232     throw std::invalid_argument(std::string("Link not found: ") + name);
233   return link->second->get_iface();
234 }
235
236 /** @brief Find a link from its name (or nullptr if that link does not exist) */
237 Link* Engine::link_by_name_or_null(const std::string& name) const
238 {
239   auto link = pimpl->links_.find(name);
240   return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
241 }
242
243 /** @brief Find a mailox from its name or create one if it does not exist) */
244 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
245 {
246   /* two actors may have pushed the same mbox_create simcall at the same time */
247   kernel::activity::MailboxImpl* mbox = kernel::actor::simcall([&name, this] {
248     auto m = pimpl->mailboxes_.emplace(name, nullptr);
249     if (m.second) {
250       m.first->second = new kernel::activity::MailboxImpl(name);
251       XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
252     }
253     return m.first->second;
254   });
255   return mbox->get_iface();
256 }
257
258 void Engine::link_register(const std::string& name, const Link* link)
259 {
260   pimpl->links_[name] = link->get_impl();
261 }
262
263 void Engine::link_unregister(const std::string& name)
264 {
265   pimpl->links_.erase(name);
266 }
267
268 /** @brief Returns the amount of links in the platform */
269 size_t Engine::get_link_count() const
270 {
271   return pimpl->links_.size();
272 }
273
274 /** @brief Returns the list of all links found in the platform */
275 std::vector<Link*> Engine::get_all_links() const
276 {
277   std::vector<Link*> res;
278   for (auto const& kv : pimpl->links_)
279     res.push_back(kv.second->get_iface());
280   return res;
281 }
282
283 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
284 {
285   std::vector<Link*> filtered_list;
286   for (auto const& kv : pimpl->links_) {
287     Link* l = kv.second->get_iface();
288     if (filter(l))
289       filtered_list.push_back(l);
290   }
291   return filtered_list;
292 }
293
294 size_t Engine::get_actor_count() const
295 {
296   return pimpl->get_actor_count();
297 }
298
299 std::vector<ActorPtr> Engine::get_all_actors() const
300 {
301   std::vector<ActorPtr> actor_list;
302   for (auto const& kv : pimpl->get_actor_list()) {
303     actor_list.push_back(kv.second->get_iface());
304   }
305   return actor_list;
306 }
307
308 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
309 {
310   std::vector<ActorPtr> actor_list;
311   for (auto const& kv : pimpl->get_actor_list()) {
312     if (filter(kv.second->get_iface()))
313       actor_list.push_back(kv.second->get_iface());
314   }
315   return actor_list;
316 }
317
318 void Engine::run() const
319 {
320   run_until(-1);
321 }
322 void Engine::run_until(double max_date) const
323 {
324   /* sealing resources before run: links */
325   for (auto* link : get_all_links())
326     link->seal();
327   /* seal netzone root, recursively seal children netzones, hosts and disks */
328   get_netzone_root()->seal();
329
330   /* Clean IO before the run */
331   fflush(stdout);
332   fflush(stderr);
333
334   if (MC_is_active()) {
335     MC_run();
336   } else {
337     pimpl->run(max_date);
338   }
339 }
340
341 void Engine::track_vetoed_activities(std::set<Activity*>* vetoed_activities)
342 {
343   Activity::set_vetoed_activities(vetoed_activities);
344 }
345
346 /** @brief Retrieve the root netzone, containing all others */
347 s4u::NetZone* Engine::get_netzone_root() const
348 {
349   if (pimpl->netzone_root_)
350     return pimpl->netzone_root_->get_iface();
351   return nullptr;
352 }
353 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
354 void Engine::set_netzone_root(const s4u::NetZone* netzone)
355 {
356   xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
357   pimpl->netzone_root_ = netzone->get_impl();
358 }
359
360 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
361 {
362   if (current->get_name() == name)
363     return current;
364
365   for (auto const& elem : current->get_children()) {
366     NetZone* tmp = netzone_by_name_recursive(elem, name);
367     if (tmp != nullptr) {
368       return tmp;
369     }
370   }
371   return nullptr;
372 }
373
374 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
375 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
376 {
377   return netzone_by_name_recursive(get_netzone_root(), name);
378 }
379
380 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
381 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
382 {
383   auto netp = pimpl->netpoints_.find(name);
384   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
385 }
386
387 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
388 {
389   auto netp = netpoint_by_name_or_null(name);
390   if (netp == nullptr) {
391     throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
392   }
393   return netp;
394 }
395
396 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
397 {
398   std::vector<kernel::routing::NetPoint*> res;
399   for (auto const& kv : pimpl->netpoints_)
400     res.push_back(kv.second);
401   return res;
402 }
403
404 /** @brief Register a new netpoint to the system */
405 void Engine::netpoint_register(kernel::routing::NetPoint* point)
406 {
407   simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
408 }
409
410 /** @brief Unregister a given netpoint */
411 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
412 {
413   kernel::actor::simcall([this, point] {
414     pimpl->netpoints_.erase(point->get_name());
415     delete point;
416   });
417 }
418
419 bool Engine::is_initialized()
420 {
421   return Engine::instance_ != nullptr;
422 }
423 void Engine::set_config(const std::string& str)
424 {
425   config::set_parse(str);
426 }
427 void Engine::set_config(const std::string& name, int value)
428 {
429   config::set_value(name.c_str(), value);
430 }
431 void Engine::set_config(const std::string& name, double value)
432 {
433   config::set_value(name.c_str(), value);
434 }
435 void Engine::set_config(const std::string& name, bool value)
436 {
437   config::set_value(name.c_str(), value);
438 }
439 void Engine::set_config(const std::string& name, const std::string& value)
440 {
441   config::set_value(name.c_str(), value);
442 }
443
444 Engine* Engine::set_default_comm_data_copy_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t))
445 {
446   kernel::activity::CommImpl::set_copy_data_callback(callback);
447   return this;
448 }
449
450 } // namespace s4u
451 } // namespace simgrid
452
453 double SIMIX_get_clock() // XBT_ATTRIB_DEPRECATED_v332
454 {
455   return simgrid::s4u::Engine::get_clock();
456 }
457
458 /* **************************** Public C interface *************************** */
459 void simgrid_init(int* argc, char** argv)
460 {
461   static simgrid::s4u::Engine e(argc, argv);
462 }
463 void simgrid_load_platform(const char* file)
464 {
465   simgrid::s4u::Engine::get_instance()->load_platform(file);
466 }
467
468 void simgrid_load_deployment(const char* file)
469 {
470   simgrid::s4u::Engine::get_instance()->load_deployment(file);
471 }
472 void simgrid_run()
473 {
474   simgrid::s4u::Engine::get_instance()->run();
475 }
476 void simgrid_run_until(double max_date)
477 {
478   simgrid::s4u::Engine::get_instance()->run_until(max_date);
479 }
480 void simgrid_register_function(const char* name, void (*code)(int, char**))
481 {
482   simgrid::s4u::Engine::get_instance()->register_function(name, code);
483 }
484 void simgrid_register_default(void (*code)(int, char**))
485 {
486   simgrid::s4u::Engine::get_instance()->register_default(code);
487 }
488 double simgrid_get_clock()
489 {
490   return simgrid::s4u::Engine::get_clock();
491 }
492
493 void simgrid_set_maestro(void (*code)(void*), void* data)
494 {
495 #ifdef _WIN32
496   XBT_WARN("simgrid_set_maestro is believed to not work on windows. Please help us investigating this issue if "
497            "you need that feature");
498 #endif
499   maestro_code = std::bind(code, data);
500 }
501 void SIMIX_set_maestro(void (*code)(void*), void* data) // XBT_ATTRIB_DEPRECATED_v333
502 {
503   simgrid_set_maestro(code, data);
504 }