Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
play with (parallel) execs. still not satisfying
[simgrid.git] / src / s4u / s4u_Engine.cpp
1 /* s4u::Engine Simulation Engine and global functions. */
2
3 /* Copyright (c) 2006-2019. 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/Engine.hpp"
12 #include "simgrid/s4u/Host.hpp"
13 #include "simgrid/s4u/Mailbox.hpp"
14 #include "simgrid/s4u/NetZone.hpp"
15 #include "simgrid/s4u/Storage.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 <string>
25
26 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
28
29 namespace simgrid {
30 namespace s4u {
31 xbt::signal<void()> on_platform_creation;
32 xbt::signal<void()> on_platform_created;
33 xbt::signal<void()> on_simulation_end;
34 xbt::signal<void(double)> on_time_advance;
35 xbt::signal<void(void)> on_deadlock;
36
37 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
38
39 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
40 {
41   xbt_assert(s4u::Engine::instance_ == nullptr,
42              "It is currently forbidden to create more than one instance of s4u::Engine");
43   TRACE_global_init();
44   SIMIX_global_init(argc, argv);
45
46   s4u::Engine::instance_ = this;
47 }
48
49 Engine::~Engine()
50 {
51   delete pimpl;
52   s4u::Engine::instance_ = nullptr;
53 }
54
55 /** @brief Retrieve the engine singleton */
56 Engine* Engine::get_instance()
57 {
58   if (s4u::Engine::instance_ == nullptr) {
59     auto e = new Engine(0, nullptr);
60     xbt_assert(s4u::Engine::instance_ == e);
61   }
62   return s4u::Engine::instance_;
63 }
64
65 void Engine::shutdown()
66 {
67   delete s4u::Engine::instance_;
68   s4u::Engine::instance_ = nullptr;
69 }
70
71 double Engine::get_clock()
72 {
73   return SIMIX_get_clock();
74 }
75
76 /**
77  * @brief A platform loader.
78  *
79  * Creates a new platform, including hosts, links, and the routing_table.
80  * @param platf a filename of the XML description of a platform. This file follows this DTD :
81  *
82  *     @include src/surf/xml/simgrid.dtd
83  *
84  * Here is a small example of such a platform
85  *
86  *     @include examples/platforms/small_platform.xml
87  */
88 void Engine::load_platform(const std::string& platf)
89 {
90   double start = xbt_os_time();
91   try {
92     parse_platform_file(platf);
93   } catch (xbt_ex& e) {
94     xbt_die("Error while loading %s: %s", platf.c_str(), e.what());
95   }
96
97   double end = xbt_os_time();
98   XBT_DEBUG("PARSE TIME: %g", (end - start));
99 }
100
101 void Engine::register_function(const std::string& name, int (*code)(int, char**))
102 {
103   SIMIX_function_register(name, code);
104 }
105 void Engine::register_function(const std::string& name, void (*code)(std::vector<std::string>))
106 {
107   SIMIX_function_register(name, code);
108 }
109 void Engine::register_default(int (*code)(int, char**))
110 {
111   SIMIX_function_register_default(code);
112 }
113 void Engine::load_deployment(const std::string& deploy)
114 {
115   SIMIX_launch_application(deploy);
116 }
117 /** @brief Returns the amount of hosts in the platform */
118 size_t Engine::get_host_count()
119 {
120   return pimpl->hosts_.size();
121 }
122
123 std::vector<Host*> Engine::get_all_hosts()
124 {
125   std::vector<Host*> res;
126   for (auto const& kv : pimpl->hosts_)
127     res.push_back(kv.second);
128   return res;
129 }
130
131 std::vector<Host*> Engine::get_filtered_hosts(std::function<bool(Host*)> filter)
132 {
133   std::vector<Host*> hosts;
134   for (auto const& kv : pimpl->hosts_) {
135     if (filter(kv.second))
136       hosts.push_back(kv.second);
137   }
138
139   return hosts;
140 }
141
142 void Engine::host_register(const std::string& name, simgrid::s4u::Host* host)
143 {
144   pimpl->hosts_[name] = host;
145 }
146
147 void Engine::host_unregister(const std::string& name)
148 {
149   pimpl->hosts_.erase(name);
150 }
151
152 /** @brief Find a host from its name.
153  *
154  *  @throw std::invalid_argument if the searched host does not exist.
155  */
156 simgrid::s4u::Host* Engine::host_by_name(const std::string& name)
157 {
158   if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
159     throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
160   return pimpl->hosts_.at(name);
161 }
162
163 /** @brief Find a host from its name (or nullptr if that host does not exist) */
164 simgrid::s4u::Host* Engine::host_by_name_or_null(const std::string& name)
165 {
166   auto host = pimpl->hosts_.find(name);
167   return host == pimpl->hosts_.end() ? nullptr : host->second;
168 }
169
170 /** @brief Find a link from its name.
171  *
172  *  @throw std::invalid_argument if the searched link does not exist.
173  */
174 simgrid::s4u::Link* Engine::link_by_name(const std::string& name)
175 {
176   if (pimpl->links_.find(name) == pimpl->links_.end())
177     throw std::invalid_argument(std::string("Link not found: ") + name);
178
179   return pimpl->links_.at(name);
180 }
181
182 /** @brief Find an link from its name (or nullptr if that link does not exist) */
183 simgrid::s4u::Link* Engine::link_by_name_or_null(const std::string& name)
184 {
185   auto link = pimpl->links_.find(name);
186   return link == pimpl->links_.end() ? nullptr : link->second;
187 }
188
189 void Engine::link_register(const std::string& name, simgrid::s4u::Link* link)
190 {
191   pimpl->links_[name] = link;
192 }
193
194 void Engine::link_unregister(const std::string& name)
195 {
196   pimpl->links_.erase(name);
197 }
198
199 /** @brief Returns the amount of storages in the platform */
200 size_t Engine::get_storage_count()
201 {
202   return pimpl->storages_.size();
203 }
204
205 /** @brief Returns the list of all storages found in the platform */
206 std::vector<Storage*> Engine::get_all_storages()
207 {
208   std::vector<Storage*> res;
209   for (auto const& kv : pimpl->storages_)
210     res.push_back(kv.second);
211   return res;
212 }
213
214 /** @brief Find a storage from its name.
215  *
216  *  @throw std::invalid_argument if the searched storage does not exist.
217  */
218 simgrid::s4u::Storage* Engine::storage_by_name(const std::string& name)
219 {
220   if (pimpl->storages_.find(name) == pimpl->storages_.end())
221     throw std::invalid_argument(std::string("Storage not found: ") + name);
222
223   return pimpl->storages_.at(name);
224 }
225
226 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
227 simgrid::s4u::Storage* Engine::storage_by_name_or_null(const std::string& name)
228 {
229   auto storage = pimpl->storages_.find(name);
230   return storage == pimpl->storages_.end() ? nullptr : storage->second;
231 }
232
233 void Engine::storage_register(const std::string& name, simgrid::s4u::Storage* storage)
234 {
235   pimpl->storages_[name] = storage;
236 }
237
238 void Engine::storage_unregister(const std::string& name)
239 {
240   pimpl->storages_.erase(name);
241 }
242
243 /** @brief Returns the amount of links in the platform */
244 size_t Engine::get_link_count()
245 {
246   return pimpl->links_.size();
247 }
248
249 /** @brief Returns the list of all links found in the platform */
250 std::vector<Link*> Engine::get_all_links()
251 {
252   std::vector<Link*> res;
253   for (auto const& kv : pimpl->links_)
254     res.push_back(kv.second);
255   return res;
256 }
257
258 std::vector<Link*> Engine::get_filtered_links(std::function<bool(Link*)> filter)
259 {
260   std::vector<Link*> filtered_list;
261   for (auto const& kv : pimpl->links_)
262     if (filter(kv.second))
263       filtered_list.push_back(kv.second);
264   return filtered_list;
265 }
266
267 size_t Engine::get_actor_count()
268 {
269   return simix_global->process_list.size();
270 }
271
272 std::vector<ActorPtr> Engine::get_all_actors()
273 {
274   std::vector<ActorPtr> actor_list;
275   actor_list.push_back(simgrid::s4u::Actor::self());
276   for (auto& kv : simix_global->process_list) {
277     actor_list.push_back(kv.second->iface());
278   }
279   return actor_list;
280 }
281
282 std::vector<ActorPtr> Engine::get_filtered_actors(std::function<bool(ActorPtr)> filter)
283 {
284   std::vector<ActorPtr> actor_list;
285   for (auto& kv : simix_global->process_list) {
286     if (filter(kv.second->iface()))
287       actor_list.push_back(kv.second->iface());
288   }
289   return actor_list;
290 }
291
292 void Engine::run()
293 {
294   /* Clean IO before the run */
295   fflush(stdout);
296   fflush(stderr);
297
298   if (MC_is_active()) {
299     MC_run();
300   } else {
301     SIMIX_run();
302   }
303 }
304
305 /** @brief Retrieve the root netzone, containing all others */
306 s4u::NetZone* Engine::get_netzone_root()
307 {
308   return pimpl->netzone_root_->get_iface();
309 }
310 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
311 void Engine::set_netzone_root(s4u::NetZone* netzone)
312 {
313   xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
314   pimpl->netzone_root_ = netzone->get_impl();
315 }
316
317 static s4u::NetZone* netzone_by_name_recursive(s4u::NetZone* current, const std::string& name)
318 {
319   if (current->get_name() == name)
320     return current;
321
322   for (auto const& elem : current->get_children()) {
323     simgrid::s4u::NetZone* tmp = netzone_by_name_recursive(elem, name);
324     if (tmp != nullptr) {
325       return tmp;
326     }
327   }
328   return nullptr;
329 }
330
331 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
332 NetZone* Engine::netzone_by_name_or_null(const std::string& name)
333 {
334   return netzone_by_name_recursive(get_netzone_root(), name);
335 }
336
337 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
338 simgrid::kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name)
339 {
340   auto netp = pimpl->netpoints_.find(name);
341   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
342 }
343
344 /** @brief Fill the provided vector with all existing netpoints */
345 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
346 {
347   for (auto const& kv : pimpl->netpoints_)
348     list->push_back(kv.second);
349 }
350 std::vector<simgrid::kernel::routing::NetPoint*> Engine::get_all_netpoints()
351 {
352   std::vector<simgrid::kernel::routing::NetPoint*> res;
353   for (auto const& kv : pimpl->netpoints_)
354     res.push_back(kv.second);
355   return res;
356 }
357
358 /** @brief Register a new netpoint to the system */
359 void Engine::netpoint_register(simgrid::kernel::routing::NetPoint* point)
360 {
361   // simgrid::simix::simcall([&]{ FIXME: this segfaults in set_thread
362   pimpl->netpoints_[point->get_name()] = point;
363   // });
364 }
365 /** @brief Unregister a given netpoint */
366 void Engine::netpoint_unregister(simgrid::kernel::routing::NetPoint* point)
367 {
368   simgrid::simix::simcall([this, point] {
369     pimpl->netpoints_.erase(point->get_name());
370     delete point;
371   });
372 }
373
374 bool Engine::is_initialized()
375 {
376   return Engine::instance_ != nullptr;
377 }
378 void Engine::set_config(std::string str)
379 {
380   simgrid::config::set_parse(std::move(str));
381 }
382 } // namespace s4u
383 } // namespace simgrid
384
385 /* **************************** Public C interface *************************** */
386 void simgrid_init(int* argc, char** argv)
387 {
388   simgrid::s4u::Engine e(argc, argv);
389 }
390 void simgrid_load_platform(const char* file)
391 {
392   simgrid::s4u::Engine::get_instance()->load_platform(file);
393 }
394
395 void simgrid_load_deployment(const char* file)
396 {
397   simgrid::s4u::Engine::get_instance()->load_deployment(file);
398 }
399 void simgrid_run()
400 {
401   simgrid::s4u::Engine::get_instance()->run();
402 }
403 void simgrid_register_function(const char* name, int (*code)(int, char**))
404 {
405   simgrid::s4u::Engine::get_instance()->register_function(name, code);
406 }
407 void simgrid_register_default(int (*code)(int, char**))
408 {
409   simgrid::s4u::Engine::get_instance()->register_default(code);
410 }
411 double simgrid_get_clock()
412 {
413   return simgrid::s4u::Engine::get_clock();
414 }