Logo AND Algorithmique Numérique Distribuée

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