Logo AND Algorithmique Numérique Distribuée

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