Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Runs examples with C++ platform description
[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 #include <dlfcn.h>
27 #include <string>
28
29 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
31
32 namespace simgrid {
33 namespace s4u {
34 xbt::signal<void()> Engine::on_platform_creation;
35 xbt::signal<void()> Engine::on_platform_created;
36 xbt::signal<void()> Engine::on_simulation_end;
37 xbt::signal<void(double)> Engine::on_time_advance;
38 xbt::signal<void(void)> Engine::on_deadlock;
39
40 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
41
42 void Engine::initialize(int* argc, char** argv)
43 {
44   xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
45   Engine::instance_ = this;
46   instr::init();
47   SIMIX_global_init(argc, argv);
48 }
49
50 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl())
51 {
52   int argc   = 1;
53   char* argv = &name[0];
54   initialize(&argc, &argv);
55 }
56
57 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
58 {
59   initialize(argc, argv);
60 }
61
62 Engine::~Engine()
63 {
64   delete pimpl;
65   Engine::instance_ = nullptr;
66 }
67
68 /** @brief Retrieve the engine singleton */
69 Engine* Engine::get_instance()
70 {
71   if (Engine::instance_ == nullptr) {
72     auto e = new Engine(nullptr, nullptr);
73     xbt_assert(Engine::instance_ == e);
74   }
75   return Engine::instance_;
76 }
77
78 void Engine::shutdown()
79 {
80   delete Engine::instance_;
81   Engine::instance_ = nullptr;
82 }
83
84 double Engine::get_clock()
85 {
86   if (MC_is_active() || MC_record_replay_is_active()) {
87     return MC_process_clock_get(SIMIX_process_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   simgrid::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   double start = xbt_os_time();
114   if (boost::algorithm::ends_with(platf, ".so")) {
115     void* handle = dlopen(platf.c_str(), RTLD_LAZY);
116     xbt_assert(handle, "Impossible to open platform file: %s", platf.c_str());
117     typedef void (*load_fct_t)(const Engine&);
118     dlerror();
119     load_fct_t callable     = (load_fct_t)dlsym(handle, "load_platform");
120     const char* dlsym_error = dlerror();
121     xbt_assert(not dlsym_error, "Error: %s", dlsym_error);
122     callable(*this);
123     dlclose(handle);
124   } else {
125     parse_platform_file(platf);
126   }
127
128   double end = xbt_os_time();
129   XBT_DEBUG("PARSE TIME: %g", (end - start));
130 }
131
132 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
133 {
134   kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
135     return xbt::wrap_main(code, std::move(args));
136   };
137   register_function(name, code_factory);
138 }
139 void Engine::register_default(int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
140 {
141   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
142 }
143
144 /** Registers the main function of an actor that will be launched from the deployment file */
145 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
146 {
147   kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
148     return xbt::wrap_main(code, std::move(args));
149   };
150   register_function(name, code_factory);
151 }
152
153 /** Registers the main function of an actor that will be launched from the deployment file */
154 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
155 {
156   kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
157     return std::bind(std::move(code), std::move(args));
158   };
159   register_function(name, code_factory);
160 }
161 /** Registers a function as the default main function of actors
162  *
163  * It will be used as fallback when the function requested from the deployment file was not registered.
164  * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
165  */
166 void Engine::register_default(const std::function<void(int, char**)>& code)
167 {
168   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
169 }
170 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
171 {
172   simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
173 }
174
175 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
176 {
177   simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
178 }
179
180 /** Load a deployment file and launch the actors that it contains
181  *
182  * @beginrst
183  * See also: :ref:`deploy`.
184  * @endrst
185  */
186 void Engine::load_deployment(const std::string& deploy) const
187 {
188   pimpl->load_deployment(deploy);
189 }
190
191 /** Returns the amount of hosts in the platform */
192 size_t Engine::get_host_count() const
193 {
194   return pimpl->hosts_.size();
195 }
196
197 std::vector<Host*> Engine::get_all_hosts() const
198 {
199   std::vector<Host*> res;
200   for (auto const& kv : pimpl->hosts_)
201     res.push_back(kv.second);
202   return res;
203 }
204
205 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
206 {
207   std::vector<Host*> hosts;
208   for (auto const& kv : pimpl->hosts_) {
209     if (filter(kv.second))
210       hosts.push_back(kv.second);
211   }
212
213   return hosts;
214 }
215
216 void Engine::host_register(const std::string& name, Host* host)
217 {
218   pimpl->hosts_[name] = host;
219 }
220
221 void Engine::host_unregister(const std::string& name)
222 {
223   pimpl->hosts_.erase(name);
224 }
225
226 /** @brief Find a host from its name.
227  *
228  *  @throw std::invalid_argument if the searched host does not exist.
229  */
230 Host* Engine::host_by_name(const std::string& name) const
231 {
232   auto host = pimpl->hosts_.find(name);
233   if (host == pimpl->hosts_.end())
234     throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
235   return host->second;
236 }
237
238 /** @brief Find a host from its name (or nullptr if that host does not exist) */
239 Host* Engine::host_by_name_or_null(const std::string& name) const
240 {
241   auto host = pimpl->hosts_.find(name);
242   return host == pimpl->hosts_.end() ? nullptr : host->second;
243 }
244
245 /** @brief Find a link from its name.
246  *
247  *  @throw std::invalid_argument if the searched link does not exist.
248  */
249 Link* Engine::link_by_name(const std::string& name) const
250 {
251   auto link = pimpl->links_.find(name);
252   if (link == pimpl->links_.end())
253     throw std::invalid_argument(std::string("Link not found: ") + name);
254   return link->second->get_iface();
255 }
256
257 /** @brief Find a link from its name (or nullptr if that link does not exist) */
258 Link* Engine::link_by_name_or_null(const std::string& name) const
259 {
260   auto link = pimpl->links_.find(name);
261   return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
262 }
263
264 /** @brief Find a mailox from its name or create one if it does not exist) */
265 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
266 {
267   /* two actors may have pushed the same mbox_create simcall at the same time */
268   kernel::activity::MailboxImpl* mbox = kernel::actor::simcall([&name, this] {
269     auto m = pimpl->mailboxes_.emplace(name, nullptr);
270     if (m.second) {
271       m.first->second = new kernel::activity::MailboxImpl(name);
272       XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
273     }
274     return m.first->second;
275   });
276   return mbox->get_iface();
277 }
278
279 void Engine::link_register(const std::string& name, const Link* link)
280 {
281   pimpl->links_[name] = link->get_impl();
282 }
283
284 void Engine::link_unregister(const std::string& name)
285 {
286   pimpl->links_.erase(name);
287 }
288
289 /** @brief Returns the amount of links in the platform */
290 size_t Engine::get_link_count() const
291 {
292   return pimpl->links_.size();
293 }
294
295 /** @brief Returns the list of all links found in the platform */
296 std::vector<Link*> Engine::get_all_links() const
297 {
298   std::vector<Link*> res;
299   for (auto const& kv : pimpl->links_)
300     res.push_back(kv.second->get_iface());
301   return res;
302 }
303
304 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
305 {
306   std::vector<Link*> filtered_list;
307   for (auto const& kv : pimpl->links_) {
308     Link* l = kv.second->get_iface();
309     if (filter(l))
310       filtered_list.push_back(l);
311   }
312   return filtered_list;
313 }
314
315 size_t Engine::get_actor_count() const
316 {
317   return pimpl->get_actor_count();
318 }
319
320 std::vector<ActorPtr> Engine::get_all_actors() const
321 {
322   std::vector<ActorPtr> actor_list;
323   for (auto const& kv : pimpl->get_actor_list()) {
324     actor_list.push_back(kv.second->get_iface());
325   }
326   return actor_list;
327 }
328
329 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
330 {
331   std::vector<ActorPtr> actor_list;
332   for (auto const& kv : pimpl->get_actor_list()) {
333     if (filter(kv.second->get_iface()))
334       actor_list.push_back(kv.second->get_iface());
335   }
336   return actor_list;
337 }
338
339 void Engine::run() const
340 {
341   /* sealing resources before run: links */
342   for (auto* link : get_all_links())
343     link->seal();
344   /* seal netzone root, recursively seal children netzones, hosts and disks */
345   get_netzone_root()->seal();
346
347   /* Clean IO before the run */
348   fflush(stdout);
349   fflush(stderr);
350
351   if (MC_is_active()) {
352     MC_run();
353   } else {
354     pimpl->run();
355   }
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([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([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 } // namespace s4u
457 } // namespace simgrid
458
459 /* **************************** Public C interface *************************** */
460 void simgrid_init(int* argc, char** argv)
461 {
462   simgrid::s4u::Engine e(argc, argv);
463 }
464 void simgrid_load_platform(const char* file)
465 {
466   simgrid::s4u::Engine::get_instance()->load_platform(file);
467 }
468
469 void simgrid_load_deployment(const char* file)
470 {
471   simgrid::s4u::Engine::get_instance()->load_deployment(file);
472 }
473 void simgrid_run()
474 {
475   simgrid::s4u::Engine::get_instance()->run();
476 }
477 void simgrid_register_function(const char* name, void (*code)(int, char**))
478 {
479   simgrid::s4u::Engine::get_instance()->register_function(name, code);
480 }
481 void simgrid_register_default(void (*code)(int, char**))
482 {
483   simgrid::s4u::Engine::get_instance()->register_default(code);
484 }
485 double simgrid_get_clock()
486 {
487   return simgrid::s4u::Engine::get_clock();
488 }
489
490 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
491 {
492   return simgrid::s4u::Engine::get_instance()->get_actor_count();
493 }