Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce dependencies on <simgrid/version.h>.
[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 #include "xbt/config.hpp"
20
21 #include <algorithm>
22 #include <string>
23
24 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
26
27 static simgrid::kernel::actor::ActorCode maestro_code;
28
29 namespace simgrid {
30 namespace s4u {
31 xbt::signal<void()> Engine::on_platform_creation;
32 xbt::signal<void()> Engine::on_platform_created;
33 xbt::signal<void()> Engine::on_simulation_start;
34 xbt::signal<void()> Engine::on_simulation_end;
35 xbt::signal<void(double)> Engine::on_time_advance;
36 xbt::signal<void(void)> Engine::on_deadlock;
37
38 Engine* Engine::instance_ = nullptr; /* This singleton is awful, but I don't see no other solution right now. */
39
40 void Engine::initialize(int* argc, char** argv)
41 {
42   xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
43   Engine::instance_ = this;
44   instr::init();
45   pimpl->initialize(argc, argv);
46   // Either create a new context with maestro or create
47   // a context object with the current context maestro):
48   kernel::actor::create_maestro(maestro_code);
49 }
50
51 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl())
52 {
53   int argc   = 1;
54   char* argv = &name[0];
55   initialize(&argc, &argv);
56 }
57
58 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
59 {
60   initialize(argc, argv);
61 }
62
63 Engine::~Engine()
64 {
65   kernel::EngineImpl::shutdown();
66   Engine::instance_ = nullptr;
67 }
68
69 /** @brief Retrieve the engine singleton */
70 Engine* Engine::get_instance()
71 {
72   int argc   = 0;
73   char* argv = nullptr;
74   return get_instance(&argc, &argv);
75 }
76 Engine* Engine::get_instance(int* argc, char** argv)
77 {
78   if (Engine::instance_ == nullptr) {
79     auto e = new Engine(argc, argv);
80     xbt_assert(Engine::instance_ == e);
81   }
82   return Engine::instance_;
83 }
84
85 void Engine::shutdown() // XBT_ATTRIB_DEPRECATED_v335
86 {
87   delete Engine::instance_;
88 }
89
90 double Engine::get_clock()
91 {
92   if (MC_is_active() || MC_record_replay_is_active()) {
93     return MC_process_clock_get(kernel::actor::ActorImpl::self());
94   } else {
95     return kernel::EngineImpl::get_clock();
96   }
97 }
98
99 void Engine::add_model(std::shared_ptr<kernel::resource::Model> model,
100                        const std::vector<kernel::resource::Model*>& dependencies)
101 {
102   kernel::actor::simcall_answered([this, &model, &dependencies] { pimpl->add_model(std::move(model), dependencies); });
103 }
104
105 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
106 {
107   return pimpl->get_all_models();
108 }
109
110 /**
111  * Creates a new platform, including hosts, links, and the routing table.
112  *
113  * @beginrst
114  * See also: :ref:`platform`.
115  * @endrst
116  */
117 void Engine::load_platform(const std::string& platf) const
118 {
119   pimpl->load_platform(platf);
120 }
121
122 /**
123  * @brief Seals the platform, finishing the creation of its resources.
124  *
125  * This method is optional. The seal() is done automatically when you call Engine::run.
126  */
127 void Engine::seal_platform() const
128 {
129   pimpl->seal_platform();
130 }
131
132 /** Registers the main function of an actor that will be launched from the deployment file */
133 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
134 {
135   kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
136     return xbt::wrap_main(code, std::move(args));
137   };
138   register_function(name, code_factory);
139 }
140
141 /** Registers the main function of an actor that will be launched from the deployment file */
142 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
143 {
144   kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
145     return std::bind(std::move(code), std::move(args));
146   };
147   register_function(name, code_factory);
148 }
149 /** Registers a function as the default main function of actors
150  *
151  * It will be used as fallback when the function requested from the deployment file was not registered.
152  * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
153  */
154 void Engine::register_default(const std::function<void(int, char**)>& code)
155 {
156   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
157 }
158 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
159 {
160   simgrid::kernel::actor::simcall_answered([this, &code]() { pimpl->register_default(code); });
161 }
162
163 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
164 {
165   simgrid::kernel::actor::simcall_answered([this, name, &code]() { pimpl->register_function(name, code); });
166 }
167
168 /** Load a deployment file and launch the actors that it contains
169  *
170  * @beginrst
171  * See also: :ref:`deploy`.
172  * @endrst
173  */
174 void Engine::load_deployment(const std::string& deploy) const
175 {
176   pimpl->load_deployment(deploy);
177 }
178
179 /** Returns the amount of hosts in the platform */
180 size_t Engine::get_host_count() const
181 {
182   return pimpl->hosts_.size();
183 }
184
185 std::vector<Host*> Engine::get_all_hosts() const
186 {
187   std::vector<Host*> res;
188   for (auto const& kv : pimpl->hosts_)
189     res.push_back(kv.second);
190   return res;
191 }
192
193 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
194 {
195   std::vector<Host*> hosts;
196   for (auto const& kv : pimpl->hosts_) {
197     if (filter(kv.second))
198       hosts.push_back(kv.second);
199   }
200
201   return hosts;
202 }
203
204 void Engine::host_register(const std::string& name, Host* host)
205 {
206   pimpl->hosts_[name] = host;
207 }
208
209 void Engine::host_unregister(const std::string& name)
210 {
211   pimpl->hosts_.erase(name);
212 }
213
214 /** @brief Find a host from its name.
215  *
216  *  @throw std::invalid_argument if the searched host does not exist.
217  */
218 Host* Engine::host_by_name(const std::string& name) const
219 {
220   auto host = pimpl->hosts_.find(name);
221   if (host == pimpl->hosts_.end())
222     throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
223   return host->second;
224 }
225
226 /** @brief Find a host from its name (or nullptr if that host does not exist) */
227 Host* Engine::host_by_name_or_null(const std::string& name) const
228 {
229   auto host = pimpl->hosts_.find(name);
230   return host == pimpl->hosts_.end() ? nullptr : host->second;
231 }
232
233 /** @brief Find a link from its name.
234  *
235  *  @throw std::invalid_argument if the searched link does not exist.
236  */
237 Link* Engine::link_by_name(const std::string& name) const
238 {
239   auto link = pimpl->links_.find(name);
240   if (link == pimpl->links_.end())
241     throw std::invalid_argument(std::string("Link not found: ") + name);
242   return link->second->get_iface();
243 }
244
245 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
246 {
247   auto link = pimpl->split_duplex_links_.find(name);
248   if (link == pimpl->split_duplex_links_.end())
249     throw std::invalid_argument(std::string("Link not found: ") + name);
250   return link->second->get_iface();
251 }
252
253 /** @brief Find a link from its name (or nullptr if that link does not exist) */
254 Link* Engine::link_by_name_or_null(const std::string& name) const
255 {
256   auto link = pimpl->links_.find(name);
257   return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
258 }
259
260 /** @brief Find a mailox from its name or create one if it does not exist) */
261 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
262 {
263   /* two actors may have pushed the same mbox_create simcall at the same time */
264   kernel::activity::MailboxImpl* mbox = kernel::actor::simcall_answered([&name, this] {
265     auto m = pimpl->mailboxes_.emplace(name, nullptr);
266     if (m.second) {
267       m.first->second = new kernel::activity::MailboxImpl(name);
268       XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
269     }
270     return m.first->second;
271   });
272   return mbox->get_iface();
273 }
274
275 void Engine::link_register(const std::string& name, const Link* link)
276 {
277   pimpl->links_[name] = link->get_impl();
278 }
279
280 void Engine::link_unregister(const std::string& name)
281 {
282   pimpl->links_.erase(name);
283 }
284
285 /** @brief Returns the amount of links in the platform */
286 size_t Engine::get_link_count() const
287 {
288   return pimpl->links_.size();
289 }
290
291 /** @brief Returns the list of all links found in the platform */
292 std::vector<Link*> Engine::get_all_links() const
293 {
294   std::vector<Link*> res;
295   for (auto const& kv : pimpl->links_)
296     res.push_back(kv.second->get_iface());
297   return res;
298 }
299
300 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
301 {
302   std::vector<Link*> filtered_list;
303   for (auto const& kv : pimpl->links_) {
304     Link* l = kv.second->get_iface();
305     if (filter(l))
306       filtered_list.push_back(l);
307   }
308   return filtered_list;
309 }
310
311 size_t Engine::get_actor_count() const
312 {
313   return pimpl->get_actor_count();
314 }
315
316 std::vector<ActorPtr> Engine::get_all_actors() const
317 {
318   std::vector<ActorPtr> actor_list;
319   for (auto const& kv : pimpl->get_actor_list()) {
320     actor_list.push_back(kv.second->get_iface());
321   }
322   return actor_list;
323 }
324
325 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
326 {
327   std::vector<ActorPtr> actor_list;
328   for (auto const& kv : pimpl->get_actor_list()) {
329     if (filter(kv.second->get_iface()))
330       actor_list.push_back(kv.second->get_iface());
331   }
332   return actor_list;
333 }
334
335 void Engine::run() const
336 {
337   run_until(-1);
338 }
339 void Engine::run_until(double max_date) const
340 {
341   static bool callback_called = false;
342   if (not callback_called) {
343     on_simulation_start();
344     callback_called = true;
345   }
346   /* Clean IO before the run */
347   fflush(stdout);
348   fflush(stderr);
349
350   pimpl->run(max_date);
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(
457     const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback)
458 {
459   kernel::activity::CommImpl::set_copy_data_callback(callback);
460   return this;
461 }
462
463 } // namespace s4u
464 } // namespace simgrid
465
466 /* **************************** Public C interface *************************** */
467 void simgrid_init(int* argc, char** argv)
468 {
469   static simgrid::s4u::Engine e(argc, argv);
470 }
471 void simgrid_load_platform(const char* file)
472 {
473   simgrid::s4u::Engine::get_instance()->load_platform(file);
474 }
475
476 void simgrid_load_deployment(const char* file)
477 {
478   simgrid::s4u::Engine::get_instance()->load_deployment(file);
479 }
480 void simgrid_run()
481 {
482   simgrid::s4u::Engine::get_instance()->run();
483 }
484 void simgrid_run_until(double max_date)
485 {
486   simgrid::s4u::Engine::get_instance()->run_until(max_date);
487 }
488 void simgrid_register_function(const char* name, void (*code)(int, char**))
489 {
490   simgrid::s4u::Engine::get_instance()->register_function(name, code);
491 }
492 void simgrid_register_default(void (*code)(int, char**))
493 {
494   simgrid::s4u::Engine::get_instance()->register_default(code);
495 }
496 double simgrid_get_clock()
497 {
498   return simgrid::s4u::Engine::get_clock();
499 }
500
501 void simgrid_set_maestro(void (*code)(void*), void* data)
502 {
503 #ifdef _WIN32
504   XBT_WARN("simgrid_set_maestro is believed to not work on windows. Please help us investigating this issue if "
505            "you need that feature");
506 #endif
507   maestro_code = std::bind(code, data);
508 }
509 void SIMIX_set_maestro(void (*code)(void*), void* data) // XBT_ATTRIB_DEPRECATED_v333
510 {
511   simgrid_set_maestro(code, data);
512 }