Logo AND Algorithmique Numérique Distribuée

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