Logo AND Algorithmique Numérique Distribuée

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