Logo AND Algorithmique Numérique Distribuée

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