Logo AND Algorithmique Numérique Distribuée

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