Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8922d12cf2b2bf7bd5c385ea1a5e6375f5c8c863
[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 <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(0, 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)
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**)) // deprecated
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, std::move(code_factory));
100 }
101 void Engine::register_default(int (*code)(int, char**)) // deprecated
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, std::move(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](std::vector<std::string> args) {
119     return std::bind(std::move(code), std::move(args));
120   };
121   register_function(name, std::move(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)
149 {
150   pimpl->load_deployment(deploy);
151 }
152
153 /** Returns the amount of hosts in the platform */
154 size_t Engine::get_host_count()
155 {
156   return pimpl->hosts_.size();
157 }
158
159 std::vector<Host*> Engine::get_all_hosts()
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)
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)
193 {
194   if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
195     throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
196   return pimpl->hosts_.at(name);
197 }
198
199 /** @brief Find a host from its name (or nullptr if that host does not exist) */
200 Host* Engine::host_by_name_or_null(const std::string& name)
201 {
202   auto host = pimpl->hosts_.find(name);
203   return host == pimpl->hosts_.end() ? nullptr : host->second;
204 }
205
206 /** @brief Find a link from its name.
207  *
208  *  @throw std::invalid_argument if the searched link does not exist.
209  */
210 Link* Engine::link_by_name(const std::string& name)
211 {
212   if (pimpl->links_.find(name) == pimpl->links_.end())
213     throw std::invalid_argument(std::string("Link not found: ") + name);
214
215   return pimpl->links_.at(name)->get_iface();
216 }
217
218 /** @brief Find an link from its name (or nullptr if that link does not exist) */
219 Link* Engine::link_by_name_or_null(const std::string& name)
220 {
221   auto link = pimpl->links_.find(name);
222   return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
223 }
224
225 void Engine::link_register(const std::string& name, const Link* link)
226 {
227   pimpl->links_[name] = link->get_impl();
228 }
229
230 void Engine::link_unregister(const std::string& name)
231 {
232   pimpl->links_.erase(name);
233 }
234
235 /** @brief Returns the amount of storages in the platform */
236 size_t Engine::get_storage_count()
237 {
238   return pimpl->storages_.size();
239 }
240
241 /** @brief Returns the list of all storages found in the platform */
242 std::vector<Storage*> Engine::get_all_storages()
243 {
244   std::vector<Storage*> res;
245   for (auto const& kv : pimpl->storages_)
246     res.push_back(kv.second->get_iface());
247   return res;
248 }
249
250 /** @brief Find a storage from its name.
251  *
252  *  @throw std::invalid_argument if the searched storage does not exist.
253  */
254 Storage* Engine::storage_by_name(const std::string& name)
255 {
256   if (pimpl->storages_.find(name) == pimpl->storages_.end())
257     throw std::invalid_argument(std::string("Storage not found: ") + name);
258
259   return pimpl->storages_.at(name)->get_iface();
260 }
261
262 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
263 Storage* Engine::storage_by_name_or_null(const std::string& name)
264 {
265   auto storage = pimpl->storages_.find(name);
266   return storage == pimpl->storages_.end() ? nullptr : storage->second->get_iface();
267 }
268
269 void Engine::storage_register(const std::string& name, const Storage* storage)
270 {
271   pimpl->storages_[name] = storage->get_impl();
272 }
273
274 void Engine::storage_unregister(const std::string& name)
275 {
276   pimpl->storages_.erase(name);
277 }
278
279 /** @brief Returns the amount of links in the platform */
280 size_t Engine::get_link_count()
281 {
282   return pimpl->links_.size();
283 }
284
285 /** @brief Returns the list of all links found in the platform */
286 std::vector<Link*> Engine::get_all_links()
287 {
288   std::vector<Link*> res;
289   for (auto const& kv : pimpl->links_)
290     res.push_back(kv.second->get_iface());
291   return res;
292 }
293
294 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter)
295 {
296   std::vector<Link*> filtered_list;
297   for (auto const& kv : pimpl->links_) {
298     Link* l = kv.second->get_iface();
299     if (filter(l))
300       filtered_list.push_back(l);
301   }
302   return filtered_list;
303 }
304
305 size_t Engine::get_actor_count()
306 {
307   return simix_global->process_list.size();
308 }
309
310 std::vector<ActorPtr> Engine::get_all_actors()
311 {
312   std::vector<ActorPtr> actor_list;
313   for (auto const& kv : simix_global->process_list) {
314     actor_list.push_back(kv.second->iface());
315   }
316   return actor_list;
317 }
318
319 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter)
320 {
321   std::vector<ActorPtr> actor_list;
322   for (auto const& kv : simix_global->process_list) {
323     if (filter(kv.second->iface()))
324       actor_list.push_back(kv.second->iface());
325   }
326   return actor_list;
327 }
328
329 void Engine::run()
330 {
331   /* Clean IO before the run */
332   fflush(stdout);
333   fflush(stderr);
334
335   if (MC_is_active()) {
336     MC_run();
337   } else {
338     SIMIX_run();
339   }
340 }
341
342 /** @brief Retrieve the root netzone, containing all others */
343 s4u::NetZone* Engine::get_netzone_root()
344 {
345   return pimpl->netzone_root_->get_iface();
346 }
347 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
348 void Engine::set_netzone_root(const s4u::NetZone* netzone)
349 {
350   xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
351   pimpl->netzone_root_ = netzone->get_impl();
352 }
353
354 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
355 {
356   if (current->get_name() == name)
357     return current;
358
359   for (auto const& elem : current->get_children()) {
360     NetZone* tmp = netzone_by_name_recursive(elem, name);
361     if (tmp != nullptr) {
362       return tmp;
363     }
364   }
365   return nullptr;
366 }
367
368 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
369 NetZone* Engine::netzone_by_name_or_null(const std::string& name)
370 {
371   return netzone_by_name_recursive(get_netzone_root(), name);
372 }
373
374 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
375 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name)
376 {
377   auto netp = pimpl->netpoints_.find(name);
378   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
379 }
380
381 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints()
382 {
383   std::vector<kernel::routing::NetPoint*> res;
384   for (auto const& kv : pimpl->netpoints_)
385     res.push_back(kv.second);
386   return res;
387 }
388
389 /** @brief Register a new netpoint to the system */
390 void Engine::netpoint_register(kernel::routing::NetPoint* point)
391 {
392   simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
393 }
394
395 /** @brief Unregister a given netpoint */
396 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
397 {
398   kernel::actor::simcall([this, point] {
399     pimpl->netpoints_.erase(point->get_name());
400     delete point;
401   });
402 }
403
404 bool Engine::is_initialized()
405 {
406   return Engine::instance_ != nullptr;
407 }
408 void Engine::set_config(const std::string& str)
409 {
410   config::set_parse(str);
411 }
412 void Engine::set_config(const std::string& name, int value)
413 {
414   config::set_value(name.c_str(), value);
415 }
416 void Engine::set_config(const std::string& name, double value)
417 {
418   config::set_value(name.c_str(), value);
419 }
420 void Engine::set_config(const std::string& name, bool value)
421 {
422   config::set_value(name.c_str(), value);
423 }
424 void Engine::set_config(const std::string& name, const std::string& value)
425 {
426   config::set_value(name.c_str(), value);
427 }
428
429 } // namespace s4u
430 } // namespace simgrid
431
432 /* **************************** Public C interface *************************** */
433 void simgrid_init(int* argc, char** argv)
434 {
435   simgrid::s4u::Engine e(argc, argv);
436 }
437 void simgrid_load_platform(const char* file)
438 {
439   simgrid::s4u::Engine::get_instance()->load_platform(file);
440 }
441
442 void simgrid_load_deployment(const char* file)
443 {
444   simgrid::s4u::Engine::get_instance()->load_deployment(file);
445 }
446 void simgrid_run()
447 {
448   simgrid::s4u::Engine::get_instance()->run();
449 }
450 void simgrid_register_function(const char* name, void (*code)(int, char**))
451 {
452   simgrid::s4u::Engine::get_instance()->register_function(name, code);
453 }
454 void simgrid_register_default(void (*code)(int, char**))
455 {
456   simgrid::s4u::Engine::get_instance()->register_default(code);
457 }
458 double simgrid_get_clock()
459 {
460   return simgrid::s4u::Engine::get_clock();
461 }
462 int simgrid_get_actor_count()
463 {
464   return simgrid::s4u::Engine::get_instance()->get_actor_count();
465 }