Logo AND Algorithmique Numérique Distribuée

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