Logo AND Algorithmique Numérique Distribuée

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