Logo AND Algorithmique Numérique Distribuée

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