Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
replace SIMIX_process_count by simgrid_get_actor_count
[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/Disk.hpp"
12 #include "simgrid/s4u/Engine.hpp"
13 #include "simgrid/s4u/Host.hpp"
14 #include "simgrid/s4u/Mailbox.hpp"
15 #include "simgrid/s4u/NetZone.hpp"
16 #include "simgrid/s4u/Storage.hpp"
17 #include "simgrid/simix.h"
18 #include "src/instr/instr_private.hpp"
19 #include "src/kernel/EngineImpl.hpp"
20 #include "src/simix/smx_private.hpp" // For access to simix_global->process_list
21 #include "src/surf/network_interface.hpp"
22 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
23 #include <simgrid/Exception.hpp>
24
25 #include <string>
26
27 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
29
30 namespace simgrid {
31 namespace s4u {
32 xbt::signal<void()> Engine::on_platform_creation;
33 xbt::signal<void()> Engine::on_platform_created;
34 xbt::signal<void()> Engine::on_simulation_end;
35 xbt::signal<void(double)> Engine::on_time_advance;
36 xbt::signal<void(void)> Engine::on_deadlock;
37
38 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
39
40 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
41 {
42   xbt_assert(Engine::instance_ == nullptr, "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   Engine::instance_ = this;
47 }
48
49 Engine::~Engine()
50 {
51   delete pimpl;
52   Engine::instance_ = nullptr;
53 }
54
55 /** @brief Retrieve the engine singleton */
56 Engine* Engine::get_instance()
57 {
58   if (Engine::instance_ == nullptr) {
59     auto e = new Engine(0, nullptr);
60     xbt_assert(Engine::instance_ == e);
61   }
62   return Engine::instance_;
63 }
64
65 void Engine::shutdown()
66 {
67   delete Engine::instance_;
68   Engine::instance_ = nullptr;
69 }
70
71 double Engine::get_clock()
72 {
73   return SIMIX_get_clock();
74 }
75
76 /**
77  * Creates a new platform, including hosts, links, and the routing table.
78  *
79  * \rst
80  * See also: :ref:`platform`.
81  * \endrst
82  */
83 void Engine::load_platform(const std::string& platf)
84 {
85   double start = xbt_os_time();
86   try {
87     parse_platform_file(platf);
88   } catch (const Exception& e) {
89     xbt_die("Error while loading %s: %s", platf.c_str(), e.what());
90   }
91
92   double end = xbt_os_time();
93   XBT_DEBUG("PARSE TIME: %g", (end - start));
94 }
95
96 /** Registers the main function of an actor that will be launched from the deployment file */
97 void Engine::register_function(const std::string& name, int (*code)(int, char**))
98 {
99   SIMIX_function_register(name, code);
100 }
101 /** Registers the main function of an actor that will be launched from the deployment file */
102 void Engine::register_function(const std::string& name, void (*code)(std::vector<std::string>))
103 {
104   SIMIX_function_register(name, code);
105 }
106 /** Registers a function as the default main function of actors
107  *
108  * It will be used as fallback when the function requested from the deployment file was not registered.
109  * It is used for trace-based simulations (see examples/s4u/replay-comms and similar).
110  */
111 void Engine::register_default(int (*code)(int, char**))
112 {
113   SIMIX_function_register_default(code);
114 }
115 /** Load a deployment file and launch the actors that it contains
116  *
117  * \rst
118  * See also: :ref:`deploy`.
119  * \endrst
120  */
121 void Engine::load_deployment(const std::string& deploy)
122 {
123   SIMIX_launch_application(deploy);
124 }
125 /** Returns the amount of hosts in the platform */
126 size_t Engine::get_host_count()
127 {
128   return pimpl->hosts_.size();
129 }
130
131 std::vector<Host*> Engine::get_all_hosts()
132 {
133   std::vector<Host*> res;
134   for (auto const& kv : pimpl->hosts_)
135     res.push_back(kv.second);
136   return res;
137 }
138
139 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter)
140 {
141   std::vector<Host*> hosts;
142   for (auto const& kv : pimpl->hosts_) {
143     if (filter(kv.second))
144       hosts.push_back(kv.second);
145   }
146
147   return hosts;
148 }
149
150 void Engine::host_register(const std::string& name, Host* host)
151 {
152   pimpl->hosts_[name] = host;
153 }
154
155 void Engine::host_unregister(const std::string& name)
156 {
157   pimpl->hosts_.erase(name);
158 }
159
160 /** @brief Find a host from its name.
161  *
162  *  @throw std::invalid_argument if the searched host does not exist.
163  */
164 Host* Engine::host_by_name(const std::string& name)
165 {
166   if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
167     throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
168   return pimpl->hosts_.at(name);
169 }
170
171 /** @brief Find a host from its name (or nullptr if that host does not exist) */
172 Host* Engine::host_by_name_or_null(const std::string& name)
173 {
174   auto host = pimpl->hosts_.find(name);
175   return host == pimpl->hosts_.end() ? nullptr : host->second;
176 }
177
178 /** @brief Find a link from its name.
179  *
180  *  @throw std::invalid_argument if the searched link does not exist.
181  */
182 Link* Engine::link_by_name(const std::string& name)
183 {
184   if (pimpl->links_.find(name) == pimpl->links_.end())
185     throw std::invalid_argument(std::string("Link not found: ") + name);
186
187   return pimpl->links_.at(name);
188 }
189
190 /** @brief Find an link from its name (or nullptr if that link does not exist) */
191 Link* Engine::link_by_name_or_null(const std::string& name)
192 {
193   auto link = pimpl->links_.find(name);
194   return link == pimpl->links_.end() ? nullptr : link->second;
195 }
196
197 void Engine::link_register(const std::string& name, Link* link)
198 {
199   pimpl->links_[name] = link;
200 }
201
202 void Engine::link_unregister(const std::string& name)
203 {
204   pimpl->links_.erase(name);
205 }
206
207 /** @brief Returns the amount of storages in the platform */
208 size_t Engine::get_storage_count()
209 {
210   return pimpl->storages_.size();
211 }
212
213 /** @brief Returns the list of all storages found in the platform */
214 std::vector<Storage*> Engine::get_all_storages()
215 {
216   std::vector<Storage*> res;
217   for (auto const& kv : pimpl->storages_)
218     res.push_back(kv.second);
219   return res;
220 }
221
222 /** @brief Find a storage from its name.
223  *
224  *  @throw std::invalid_argument if the searched storage does not exist.
225  */
226 Storage* Engine::storage_by_name(const std::string& name)
227 {
228   if (pimpl->storages_.find(name) == pimpl->storages_.end())
229     throw std::invalid_argument(std::string("Storage not found: ") + name);
230
231   return pimpl->storages_.at(name);
232 }
233
234 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
235 Storage* Engine::storage_by_name_or_null(const std::string& name)
236 {
237   auto storage = pimpl->storages_.find(name);
238   return storage == pimpl->storages_.end() ? nullptr : storage->second;
239 }
240
241 void Engine::storage_register(const std::string& name, Storage* storage)
242 {
243   pimpl->storages_[name] = storage;
244 }
245
246 void Engine::storage_unregister(const std::string& name)
247 {
248   pimpl->storages_.erase(name);
249 }
250
251 /** @brief Returns the amount of links in the platform */
252 size_t Engine::get_link_count()
253 {
254   return pimpl->links_.size();
255 }
256
257 /** @brief Returns the list of all links found in the platform */
258 std::vector<Link*> Engine::get_all_links()
259 {
260   std::vector<Link*> res;
261   for (auto const& kv : pimpl->links_)
262     res.push_back(kv.second);
263   return res;
264 }
265
266 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter)
267 {
268   std::vector<Link*> filtered_list;
269   for (auto const& kv : pimpl->links_)
270     if (filter(kv.second))
271       filtered_list.push_back(kv.second);
272   return filtered_list;
273 }
274
275 size_t Engine::get_actor_count()
276 {
277   return simix_global->process_list.size();
278 }
279
280 std::vector<ActorPtr> Engine::get_all_actors()
281 {
282   std::vector<ActorPtr> actor_list;
283   actor_list.push_back(simgrid::s4u::Actor::self());
284   for (auto& kv : simix_global->process_list) {
285     actor_list.push_back(kv.second->iface());
286   }
287   return actor_list;
288 }
289
290 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter)
291 {
292   std::vector<ActorPtr> actor_list;
293   for (auto& kv : simix_global->process_list) {
294     if (filter(kv.second->iface()))
295       actor_list.push_back(kv.second->iface());
296   }
297   return actor_list;
298 }
299
300 void Engine::run()
301 {
302   /* Clean IO before the run */
303   fflush(stdout);
304   fflush(stderr);
305
306   if (MC_is_active()) {
307     MC_run();
308   } else {
309     SIMIX_run();
310   }
311 }
312
313 /** @brief Retrieve the root netzone, containing all others */
314 s4u::NetZone* Engine::get_netzone_root()
315 {
316   return pimpl->netzone_root_->get_iface();
317 }
318 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
319 void Engine::set_netzone_root(s4u::NetZone* netzone)
320 {
321   xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
322   pimpl->netzone_root_ = netzone->get_impl();
323 }
324
325 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
326 {
327   if (current->get_name() == name)
328     return current;
329
330   for (auto const& elem : current->get_children()) {
331     NetZone* tmp = netzone_by_name_recursive(elem, name);
332     if (tmp != nullptr) {
333       return tmp;
334     }
335   }
336   return nullptr;
337 }
338
339 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
340 NetZone* Engine::netzone_by_name_or_null(const std::string& name)
341 {
342   return netzone_by_name_recursive(get_netzone_root(), name);
343 }
344
345 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
346 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name)
347 {
348   auto netp = pimpl->netpoints_.find(name);
349   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
350 }
351
352 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints()
353 {
354   std::vector<kernel::routing::NetPoint*> res;
355   for (auto const& kv : pimpl->netpoints_)
356     res.push_back(kv.second);
357   return res;
358 }
359
360 /** @brief Register a new netpoint to the system */
361 void Engine::netpoint_register(kernel::routing::NetPoint* point)
362 {
363   // simgrid::kernel::actor::simcall([&]{ FIXME: this segfaults in set_thread
364   pimpl->netpoints_[point->get_name()] = point;
365   // });
366 }
367
368 /** @brief Unregister a given netpoint */
369 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
370 {
371   kernel::actor::simcall([this, point] {
372     pimpl->netpoints_.erase(point->get_name());
373     delete point;
374   });
375 }
376
377 bool Engine::is_initialized()
378 {
379   return Engine::instance_ != nullptr;
380 }
381 void Engine::set_config(const std::string& str)
382 {
383   config::set_parse(str);
384 }
385 } // namespace s4u
386 } // namespace simgrid
387
388 /* **************************** Public C interface *************************** */
389 void simgrid_init(int* argc, char** argv)
390 {
391   simgrid::s4u::Engine e(argc, argv);
392 }
393 void simgrid_load_platform(const char* file)
394 {
395   simgrid::s4u::Engine::get_instance()->load_platform(file);
396 }
397
398 void simgrid_load_deployment(const char* file)
399 {
400   simgrid::s4u::Engine::get_instance()->load_deployment(file);
401 }
402 void simgrid_run()
403 {
404   simgrid::s4u::Engine::get_instance()->run();
405 }
406 void simgrid_register_function(const char* name, int (*code)(int, char**))
407 {
408   simgrid::s4u::Engine::get_instance()->register_function(name, code);
409 }
410 void simgrid_register_default(int (*code)(int, char**))
411 {
412   simgrid::s4u::Engine::get_instance()->register_default(code);
413 }
414 double simgrid_get_clock()
415 {
416   return simgrid::s4u::Engine::get_clock();
417 }
418 int simgrid_get_actor_count()
419 {
420   return simgrid::s4u::Engine::get_instance()->get_actor_count();
421 }