Logo AND Algorithmique Numérique Distribuée

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