Logo AND Algorithmique Numérique Distribuée

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