Logo AND Algorithmique Numérique Distribuée

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