Logo AND Algorithmique Numérique Distribuée

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