Logo AND Algorithmique Numérique Distribuée

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