Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
65ef3a1f3be45c0ffb1d9be3999ce221973d5b3d
[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   TRACE_global_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   register_function(name, [code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
97 }
98 void Engine::register_default(int (*code)(int, char**)) // deprecated
99 {
100   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
101 }
102
103 /** Registers the main function of an actor that will be launched from the deployment file */
104 void Engine::register_function(const std::string& name, void (*code)(int, char**))
105 {
106   register_function(name, [code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
107 }
108
109 /** Registers the main function of an actor that will be launched from the deployment file */
110 void Engine::register_function(const std::string& name, void (*code)(std::vector<std::string>))
111 {
112   register_function(name,
113                     [code](std::vector<std::string> args) { return std::bind(std::move(code), std::move(args)); });
114 }
115 /** Registers a function as the default main function of actors
116  *
117  * It will be used as fallback when the function requested from the deployment file was not registered.
118  * It is used for trace-based simulations (see examples/s4u/replay-comms and similar).
119  */
120 void Engine::register_default(void (*code)(int, char**))
121 {
122   register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
123 }
124 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
125 {
126   simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
127 }
128
129 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
130 {
131   simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
132 }
133
134 /** Load a deployment file and launch the actors that it contains
135  *
136  * \rst
137  * See also: :ref:`deploy`.
138  * \endrst
139  */
140 void Engine::load_deployment(const std::string& deploy)
141 {
142   pimpl->load_deployment(deploy);
143 }
144
145 /** Returns the amount of hosts in the platform */
146 size_t Engine::get_host_count()
147 {
148   return pimpl->hosts_.size();
149 }
150
151 std::vector<Host*> Engine::get_all_hosts()
152 {
153   std::vector<Host*> res;
154   for (auto const& kv : pimpl->hosts_)
155     res.push_back(kv.second);
156   return res;
157 }
158
159 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter)
160 {
161   std::vector<Host*> hosts;
162   for (auto const& kv : pimpl->hosts_) {
163     if (filter(kv.second))
164       hosts.push_back(kv.second);
165   }
166
167   return hosts;
168 }
169
170 void Engine::host_register(const std::string& name, Host* host)
171 {
172   pimpl->hosts_[name] = host;
173 }
174
175 void Engine::host_unregister(const std::string& name)
176 {
177   pimpl->hosts_.erase(name);
178 }
179
180 /** @brief Find a host from its name.
181  *
182  *  @throw std::invalid_argument if the searched host does not exist.
183  */
184 Host* Engine::host_by_name(const std::string& name)
185 {
186   if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
187     throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
188   return pimpl->hosts_.at(name);
189 }
190
191 /** @brief Find a host from its name (or nullptr if that host does not exist) */
192 Host* Engine::host_by_name_or_null(const std::string& name)
193 {
194   auto host = pimpl->hosts_.find(name);
195   return host == pimpl->hosts_.end() ? nullptr : host->second;
196 }
197
198 /** @brief Find a link from its name.
199  *
200  *  @throw std::invalid_argument if the searched link does not exist.
201  */
202 Link* Engine::link_by_name(const std::string& name)
203 {
204   if (pimpl->links_.find(name) == pimpl->links_.end())
205     throw std::invalid_argument(std::string("Link not found: ") + name);
206
207   return pimpl->links_.at(name)->get_iface();
208 }
209
210 /** @brief Find an link from its name (or nullptr if that link does not exist) */
211 Link* Engine::link_by_name_or_null(const std::string& name)
212 {
213   auto link = pimpl->links_.find(name);
214   return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
215 }
216
217 void Engine::link_register(const std::string& name, const Link* link)
218 {
219   pimpl->links_[name] = link->get_impl();
220 }
221
222 void Engine::link_unregister(const std::string& name)
223 {
224   pimpl->links_.erase(name);
225 }
226
227 /** @brief Returns the amount of storages in the platform */
228 size_t Engine::get_storage_count()
229 {
230   return pimpl->storages_.size();
231 }
232
233 /** @brief Returns the list of all storages found in the platform */
234 std::vector<Storage*> Engine::get_all_storages()
235 {
236   std::vector<Storage*> res;
237   for (auto const& kv : pimpl->storages_)
238     res.push_back(kv.second->get_iface());
239   return res;
240 }
241
242 /** @brief Find a storage from its name.
243  *
244  *  @throw std::invalid_argument if the searched storage does not exist.
245  */
246 Storage* Engine::storage_by_name(const std::string& name)
247 {
248   if (pimpl->storages_.find(name) == pimpl->storages_.end())
249     throw std::invalid_argument(std::string("Storage not found: ") + name);
250
251   return pimpl->storages_.at(name)->get_iface();
252 }
253
254 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
255 Storage* Engine::storage_by_name_or_null(const std::string& name)
256 {
257   auto storage = pimpl->storages_.find(name);
258   return storage == pimpl->storages_.end() ? nullptr : storage->second->get_iface();
259 }
260
261 void Engine::storage_register(const std::string& name, const Storage* storage)
262 {
263   pimpl->storages_[name] = storage->get_impl();
264 }
265
266 void Engine::storage_unregister(const std::string& name)
267 {
268   pimpl->storages_.erase(name);
269 }
270
271 /** @brief Returns the amount of links in the platform */
272 size_t Engine::get_link_count()
273 {
274   return pimpl->links_.size();
275 }
276
277 /** @brief Returns the list of all links found in the platform */
278 std::vector<Link*> Engine::get_all_links()
279 {
280   std::vector<Link*> res;
281   for (auto const& kv : pimpl->links_)
282     res.push_back(kv.second->get_iface());
283   return res;
284 }
285
286 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter)
287 {
288   std::vector<Link*> filtered_list;
289   for (auto const& kv : pimpl->links_) {
290     Link* l = kv.second->get_iface();
291     if (filter(l))
292       filtered_list.push_back(l);
293   }
294   return filtered_list;
295 }
296
297 size_t Engine::get_actor_count()
298 {
299   return simix_global->process_list.size();
300 }
301
302 std::vector<ActorPtr> Engine::get_all_actors()
303 {
304   std::vector<ActorPtr> actor_list;
305   actor_list.push_back(simgrid::s4u::Actor::self());
306   for (auto const& kv : simix_global->process_list) {
307     actor_list.push_back(kv.second->iface());
308   }
309   return actor_list;
310 }
311
312 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter)
313 {
314   std::vector<ActorPtr> actor_list;
315   for (auto const& kv : simix_global->process_list) {
316     if (filter(kv.second->iface()))
317       actor_list.push_back(kv.second->iface());
318   }
319   return actor_list;
320 }
321
322 void Engine::run()
323 {
324   /* Clean IO before the run */
325   fflush(stdout);
326   fflush(stderr);
327
328   if (MC_is_active()) {
329     MC_run();
330   } else {
331     SIMIX_run();
332   }
333 }
334
335 /** @brief Retrieve the root netzone, containing all others */
336 s4u::NetZone* Engine::get_netzone_root()
337 {
338   return pimpl->netzone_root_->get_iface();
339 }
340 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
341 void Engine::set_netzone_root(const s4u::NetZone* netzone)
342 {
343   xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
344   pimpl->netzone_root_ = netzone->get_impl();
345 }
346
347 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
348 {
349   if (current->get_name() == name)
350     return current;
351
352   for (auto const& elem : current->get_children()) {
353     NetZone* tmp = netzone_by_name_recursive(elem, name);
354     if (tmp != nullptr) {
355       return tmp;
356     }
357   }
358   return nullptr;
359 }
360
361 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
362 NetZone* Engine::netzone_by_name_or_null(const std::string& name)
363 {
364   return netzone_by_name_recursive(get_netzone_root(), name);
365 }
366
367 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
368 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name)
369 {
370   auto netp = pimpl->netpoints_.find(name);
371   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
372 }
373
374 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints()
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 } // namespace s4u
406 } // namespace simgrid
407
408 /* **************************** Public C interface *************************** */
409 void simgrid_init(int* argc, char** argv)
410 {
411   simgrid::s4u::Engine e(argc, argv);
412 }
413 void simgrid_load_platform(const char* file)
414 {
415   simgrid::s4u::Engine::get_instance()->load_platform(file);
416 }
417
418 void simgrid_load_deployment(const char* file)
419 {
420   simgrid::s4u::Engine::get_instance()->load_deployment(file);
421 }
422 void simgrid_run()
423 {
424   simgrid::s4u::Engine::get_instance()->run();
425 }
426 void simgrid_register_function(const char* name, void (*code)(int, char**))
427 {
428   simgrid::s4u::Engine::get_instance()->register_function(name, code);
429 }
430 void simgrid_register_default(void (*code)(int, char**))
431 {
432   simgrid::s4u::Engine::get_instance()->register_default(code);
433 }
434 double simgrid_get_clock()
435 {
436   return simgrid::s4u::Engine::get_clock();
437 }
438 int simgrid_get_actor_count()
439 {
440   return simgrid::s4u::Engine::get_instance()->get_actor_count();
441 }
442
443 void simgrid_get_all_hosts(size_t* host_count, sg_host_t** hosts)
444 {
445   simgrid::s4u::Engine* e               = simgrid::s4u::Engine::get_instance();
446   *host_count                           = e->get_host_count();
447   std::vector<simgrid::s4u::Host*> list = e->get_all_hosts();
448
449   auto last = std::remove_if(begin(list), end(list), [](const simgrid::s4u::Host* host) {
450     return not host || not host->get_netpoint() || not host->get_netpoint()->is_host();
451   });
452   std::sort(begin(list), last,
453             [](const simgrid::s4u::Host* a, const simgrid::s4u::Host* b) { return a->get_name() < b->get_name(); });
454
455   *hosts = static_cast<sg_host_t*>(xbt_malloc(sizeof(sg_host_t) * (*host_count)));
456   for (size_t i = 0; i < *host_count; i++)
457     (*hosts)[i] = list[i];
458 }