Logo AND Algorithmique Numérique Distribuée

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