Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix build mac/windows
[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 "mc/mc.h"
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 #include "src/instr/instr_private.hpp"
18 #include "src/kernel/EngineImpl.hpp"
19 #include "src/mc/mc_replay.hpp"
20 #include "src/surf/network_interface.hpp"
21 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
22 #include <boost/algorithm/string/predicate.hpp>
23 #include <simgrid/Exception.hpp>
24
25 #include <algorithm>
26 #ifndef _WIN32
27 #include <dlfcn.h>
28 #endif /* _WIN32 */
29 #include <string>
30
31 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
33
34 namespace simgrid {
35 namespace s4u {
36 xbt::signal<void()> Engine::on_platform_creation;
37 xbt::signal<void()> Engine::on_platform_created;
38 xbt::signal<void()> Engine::on_simulation_end;
39 xbt::signal<void(double)> Engine::on_time_advance;
40 xbt::signal<void(void)> Engine::on_deadlock;
41
42 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
43
44 void Engine::initialize(int* argc, char** argv)
45 {
46   xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
47   Engine::instance_ = this;
48   instr::init();
49   SIMIX_global_init(argc, argv);
50 }
51
52 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl())
53 {
54   int argc   = 1;
55   char* argv = &name[0];
56   initialize(&argc, &argv);
57 }
58
59 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
60 {
61   initialize(argc, argv);
62 }
63
64 Engine::~Engine()
65 {
66   delete pimpl;
67   Engine::instance_ = nullptr;
68 }
69
70 /** @brief Retrieve the engine singleton */
71 Engine* Engine::get_instance()
72 {
73   if (Engine::instance_ == nullptr) {
74     auto e = new Engine(nullptr, nullptr);
75     xbt_assert(Engine::instance_ == e);
76   }
77   return Engine::instance_;
78 }
79
80 void Engine::shutdown()
81 {
82   delete Engine::instance_;
83   Engine::instance_ = nullptr;
84 }
85
86 double Engine::get_clock()
87 {
88   if (MC_is_active() || MC_record_replay_is_active()) {
89     return MC_process_clock_get(SIMIX_process_self());
90   } else {
91     return surf_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   simgrid::kernel::actor::simcall([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   double start = xbt_os_time();
116   if (boost::algorithm::ends_with(platf, ".so") or boost::algorithm::ends_with(platf, ".dylib")) {
117 #ifdef _WIN32
118     xbt_die("loading platform through shared library isn't supported on windows");
119 #else
120     void* handle = dlopen(platf.c_str(), RTLD_LAZY);
121     xbt_assert(handle, "Impossible to open platform file: %s", platf.c_str());
122     typedef void (*load_fct_t)(const Engine&);
123     dlerror();
124     load_fct_t callable     = (load_fct_t)dlsym(handle, "load_platform");
125     const char* dlsym_error = dlerror();
126     xbt_assert(not dlsym_error, "Error: %s", dlsym_error);
127     callable(*this);
128     dlclose(handle);
129 #endif /* _WIN32 */
130   } else {
131     parse_platform_file(platf);
132   }
133
134   double end = xbt_os_time();
135   XBT_DEBUG("PARSE TIME: %g", (end - start));
136 }
137
138 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
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 void Engine::register_default(int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
146 {
147   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
148 }
149
150 /** Registers the main function of an actor that will be launched from the deployment file */
151 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
152 {
153   kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
154     return xbt::wrap_main(code, std::move(args));
155   };
156   register_function(name, code_factory);
157 }
158
159 /** Registers the main function of an actor that will be launched from the deployment file */
160 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
161 {
162   kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
163     return std::bind(std::move(code), std::move(args));
164   };
165   register_function(name, code_factory);
166 }
167 /** Registers a function as the default main function of actors
168  *
169  * It will be used as fallback when the function requested from the deployment file was not registered.
170  * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
171  */
172 void Engine::register_default(const std::function<void(int, char**)>& code)
173 {
174   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
175 }
176 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
177 {
178   simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
179 }
180
181 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
182 {
183   simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
184 }
185
186 /** Load a deployment file and launch the actors that it contains
187  *
188  * @beginrst
189  * See also: :ref:`deploy`.
190  * @endrst
191  */
192 void Engine::load_deployment(const std::string& deploy) const
193 {
194   pimpl->load_deployment(deploy);
195 }
196
197 /** Returns the amount of hosts in the platform */
198 size_t Engine::get_host_count() const
199 {
200   return pimpl->hosts_.size();
201 }
202
203 std::vector<Host*> Engine::get_all_hosts() const
204 {
205   std::vector<Host*> res;
206   for (auto const& kv : pimpl->hosts_)
207     res.push_back(kv.second);
208   return res;
209 }
210
211 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
212 {
213   std::vector<Host*> hosts;
214   for (auto const& kv : pimpl->hosts_) {
215     if (filter(kv.second))
216       hosts.push_back(kv.second);
217   }
218
219   return hosts;
220 }
221
222 void Engine::host_register(const std::string& name, Host* host)
223 {
224   pimpl->hosts_[name] = host;
225 }
226
227 void Engine::host_unregister(const std::string& name)
228 {
229   pimpl->hosts_.erase(name);
230 }
231
232 /** @brief Find a host from its name.
233  *
234  *  @throw std::invalid_argument if the searched host does not exist.
235  */
236 Host* Engine::host_by_name(const std::string& name) const
237 {
238   auto host = pimpl->hosts_.find(name);
239   if (host == pimpl->hosts_.end())
240     throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
241   return host->second;
242 }
243
244 /** @brief Find a host from its name (or nullptr if that host does not exist) */
245 Host* Engine::host_by_name_or_null(const std::string& name) const
246 {
247   auto host = pimpl->hosts_.find(name);
248   return host == pimpl->hosts_.end() ? nullptr : host->second;
249 }
250
251 /** @brief Find a link from its name.
252  *
253  *  @throw std::invalid_argument if the searched link does not exist.
254  */
255 Link* Engine::link_by_name(const std::string& name) const
256 {
257   auto link = pimpl->links_.find(name);
258   if (link == pimpl->links_.end())
259     throw std::invalid_argument(std::string("Link not found: ") + name);
260   return link->second->get_iface();
261 }
262
263 /** @brief Find a link from its name (or nullptr if that link does not exist) */
264 Link* Engine::link_by_name_or_null(const std::string& name) const
265 {
266   auto link = pimpl->links_.find(name);
267   return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
268 }
269
270 /** @brief Find a mailox from its name or create one if it does not exist) */
271 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
272 {
273   /* two actors may have pushed the same mbox_create simcall at the same time */
274   kernel::activity::MailboxImpl* mbox = kernel::actor::simcall([&name, this] {
275     auto m = pimpl->mailboxes_.emplace(name, nullptr);
276     if (m.second) {
277       m.first->second = new kernel::activity::MailboxImpl(name);
278       XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
279     }
280     return m.first->second;
281   });
282   return mbox->get_iface();
283 }
284
285 void Engine::link_register(const std::string& name, const Link* link)
286 {
287   pimpl->links_[name] = link->get_impl();
288 }
289
290 void Engine::link_unregister(const std::string& name)
291 {
292   pimpl->links_.erase(name);
293 }
294
295 /** @brief Returns the amount of links in the platform */
296 size_t Engine::get_link_count() const
297 {
298   return pimpl->links_.size();
299 }
300
301 /** @brief Returns the list of all links found in the platform */
302 std::vector<Link*> Engine::get_all_links() const
303 {
304   std::vector<Link*> res;
305   for (auto const& kv : pimpl->links_)
306     res.push_back(kv.second->get_iface());
307   return res;
308 }
309
310 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
311 {
312   std::vector<Link*> filtered_list;
313   for (auto const& kv : pimpl->links_) {
314     Link* l = kv.second->get_iface();
315     if (filter(l))
316       filtered_list.push_back(l);
317   }
318   return filtered_list;
319 }
320
321 size_t Engine::get_actor_count() const
322 {
323   return pimpl->get_actor_count();
324 }
325
326 std::vector<ActorPtr> Engine::get_all_actors() const
327 {
328   std::vector<ActorPtr> actor_list;
329   for (auto const& kv : pimpl->get_actor_list()) {
330     actor_list.push_back(kv.second->get_iface());
331   }
332   return actor_list;
333 }
334
335 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
336 {
337   std::vector<ActorPtr> actor_list;
338   for (auto const& kv : pimpl->get_actor_list()) {
339     if (filter(kv.second->get_iface()))
340       actor_list.push_back(kv.second->get_iface());
341   }
342   return actor_list;
343 }
344
345 void Engine::run() const
346 {
347   /* sealing resources before run: links */
348   for (auto* link : get_all_links())
349     link->seal();
350   /* seal netzone root, recursively seal children netzones, hosts and disks */
351   get_netzone_root()->seal();
352
353   /* Clean IO before the run */
354   fflush(stdout);
355   fflush(stderr);
356
357   if (MC_is_active()) {
358     MC_run();
359   } else {
360     pimpl->run();
361   }
362 }
363
364 /** @brief Retrieve the root netzone, containing all others */
365 s4u::NetZone* Engine::get_netzone_root() const
366 {
367   if (pimpl->netzone_root_)
368     return pimpl->netzone_root_->get_iface();
369   return nullptr;
370 }
371 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
372 void Engine::set_netzone_root(const s4u::NetZone* netzone)
373 {
374   xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
375   pimpl->netzone_root_ = netzone->get_impl();
376 }
377
378 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
379 {
380   if (current->get_name() == name)
381     return current;
382
383   for (auto const& elem : current->get_children()) {
384     NetZone* tmp = netzone_by_name_recursive(elem, name);
385     if (tmp != nullptr) {
386       return tmp;
387     }
388   }
389   return nullptr;
390 }
391
392 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
393 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
394 {
395   return netzone_by_name_recursive(get_netzone_root(), name);
396 }
397
398 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
399 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
400 {
401   auto netp = pimpl->netpoints_.find(name);
402   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
403 }
404
405 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
406 {
407   auto netp = netpoint_by_name_or_null(name);
408   if (netp == nullptr) {
409     throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
410   }
411   return netp;
412 }
413
414 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
415 {
416   std::vector<kernel::routing::NetPoint*> res;
417   for (auto const& kv : pimpl->netpoints_)
418     res.push_back(kv.second);
419   return res;
420 }
421
422 /** @brief Register a new netpoint to the system */
423 void Engine::netpoint_register(kernel::routing::NetPoint* point)
424 {
425   simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
426 }
427
428 /** @brief Unregister a given netpoint */
429 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
430 {
431   kernel::actor::simcall([this, point] {
432     pimpl->netpoints_.erase(point->get_name());
433     delete point;
434   });
435 }
436
437 bool Engine::is_initialized()
438 {
439   return Engine::instance_ != nullptr;
440 }
441 void Engine::set_config(const std::string& str)
442 {
443   config::set_parse(str);
444 }
445 void Engine::set_config(const std::string& name, int value)
446 {
447   config::set_value(name.c_str(), value);
448 }
449 void Engine::set_config(const std::string& name, double value)
450 {
451   config::set_value(name.c_str(), value);
452 }
453 void Engine::set_config(const std::string& name, bool value)
454 {
455   config::set_value(name.c_str(), value);
456 }
457 void Engine::set_config(const std::string& name, const std::string& value)
458 {
459   config::set_value(name.c_str(), value);
460 }
461
462 } // namespace s4u
463 } // namespace simgrid
464
465 /* **************************** Public C interface *************************** */
466 void simgrid_init(int* argc, char** argv)
467 {
468   simgrid::s4u::Engine e(argc, argv);
469 }
470 void simgrid_load_platform(const char* file)
471 {
472   simgrid::s4u::Engine::get_instance()->load_platform(file);
473 }
474
475 void simgrid_load_deployment(const char* file)
476 {
477   simgrid::s4u::Engine::get_instance()->load_deployment(file);
478 }
479 void simgrid_run()
480 {
481   simgrid::s4u::Engine::get_instance()->run();
482 }
483 void simgrid_register_function(const char* name, void (*code)(int, char**))
484 {
485   simgrid::s4u::Engine::get_instance()->register_function(name, code);
486 }
487 void simgrid_register_default(void (*code)(int, char**))
488 {
489   simgrid::s4u::Engine::get_instance()->register_default(code);
490 }
491 double simgrid_get_clock()
492 {
493   return simgrid::s4u::Engine::get_clock();
494 }
495
496 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
497 {
498   return simgrid::s4u::Engine::get_instance()->get_actor_count();
499 }