Logo AND Algorithmique Numérique Distribuée

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