Logo AND Algorithmique Numérique Distribuée

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