Logo AND Algorithmique Numérique Distribuée

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