Logo AND Algorithmique Numérique Distribuée

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