Logo AND Algorithmique Numérique Distribuée

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