1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved. */
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. */
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/StorageImpl.hpp"
22 #include "src/surf/network_interface.hpp"
23 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
24 #include <simgrid/Exception.hpp>
29 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
34 xbt::signal<void()> Engine::on_platform_creation;
35 xbt::signal<void()> Engine::on_platform_created;
36 xbt::signal<void()> Engine::on_simulation_end;
37 xbt::signal<void(double)> Engine::on_time_advance;
38 xbt::signal<void(void)> Engine::on_deadlock;
40 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
42 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
44 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
46 SIMIX_global_init(argc, argv);
48 Engine::instance_ = this;
54 Engine::instance_ = nullptr;
57 /** @brief Retrieve the engine singleton */
58 Engine* Engine::get_instance()
60 if (Engine::instance_ == nullptr) {
61 auto e = new Engine(0, nullptr);
62 xbt_assert(Engine::instance_ == e);
64 return Engine::instance_;
67 void Engine::shutdown()
69 delete Engine::instance_;
70 Engine::instance_ = nullptr;
73 double Engine::get_clock()
75 return SIMIX_get_clock();
79 * Creates a new platform, including hosts, links, and the routing table.
82 * See also: :ref:`platform`.
85 void Engine::load_platform(const std::string& platf) const
87 double start = xbt_os_time();
88 parse_platform_file(platf);
90 double end = xbt_os_time();
91 XBT_DEBUG("PARSE TIME: %g", (end - start));
94 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // deprecated
96 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
97 return xbt::wrap_main(code, std::move(args));
99 register_function(name, std::move(code_factory));
101 void Engine::register_default(int (*code)(int, char**)) // deprecated
103 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
106 /** Registers the main function of an actor that will be launched from the deployment file */
107 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
109 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
110 return xbt::wrap_main(code, std::move(args));
112 register_function(name, std::move(code_factory));
115 /** Registers the main function of an actor that will be launched from the deployment file */
116 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
118 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
119 return std::bind(std::move(code), std::move(args));
121 register_function(name, std::move(code_factory));
123 /** Registers a function as the default main function of actors
125 * It will be used as fallback when the function requested from the deployment file was not registered.
126 * It is used for trace-based simulations (see examples/s4u/replay-comms and similar).
128 void Engine::register_default(const std::function<void(int, char**)>& code)
130 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
132 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
134 simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
137 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
139 simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
142 /** Load a deployment file and launch the actors that it contains
145 * See also: :ref:`deploy`.
148 void Engine::load_deployment(const std::string& deploy)
150 pimpl->load_deployment(deploy);
153 /** Returns the amount of hosts in the platform */
154 size_t Engine::get_host_count() const
156 return pimpl->hosts_.size();
159 std::vector<Host*> Engine::get_all_hosts() const
161 std::vector<Host*> res;
162 for (auto const& kv : pimpl->hosts_)
163 res.push_back(kv.second);
167 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
169 std::vector<Host*> hosts;
170 for (auto const& kv : pimpl->hosts_) {
171 if (filter(kv.second))
172 hosts.push_back(kv.second);
178 void Engine::host_register(const std::string& name, Host* host)
180 pimpl->hosts_[name] = host;
183 void Engine::host_unregister(const std::string& name)
185 pimpl->hosts_.erase(name);
188 /** @brief Find a host from its name.
190 * @throw std::invalid_argument if the searched host does not exist.
192 Host* Engine::host_by_name(const std::string& name) const
194 if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
195 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
196 return pimpl->hosts_.at(name);
199 /** @brief Find a host from its name (or nullptr if that host does not exist) */
200 Host* Engine::host_by_name_or_null(const std::string& name) const
202 auto host = pimpl->hosts_.find(name);
203 return host == pimpl->hosts_.end() ? nullptr : host->second;
206 /** @brief Find a link from its name.
208 * @throw std::invalid_argument if the searched link does not exist.
210 Link* Engine::link_by_name(const std::string& name) const
212 if (pimpl->links_.find(name) == pimpl->links_.end())
213 throw std::invalid_argument(std::string("Link not found: ") + name);
215 return pimpl->links_.at(name)->get_iface();
218 /** @brief Find an link from its name (or nullptr if that link does not exist) */
219 Link* Engine::link_by_name_or_null(const std::string& name) const
221 auto link = pimpl->links_.find(name);
222 return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
225 void Engine::link_register(const std::string& name, const Link* link)
227 pimpl->links_[name] = link->get_impl();
230 void Engine::link_unregister(const std::string& name)
232 pimpl->links_.erase(name);
235 /** @brief Returns the amount of storages in the platform */
236 size_t Engine::get_storage_count() const
238 return pimpl->storages_.size();
241 /** @brief Returns the list of all storages found in the platform */
242 std::vector<Storage*> Engine::get_all_storages() const
244 std::vector<Storage*> res;
245 for (auto const& kv : pimpl->storages_)
246 res.push_back(kv.second->get_iface());
250 /** @brief Find a storage from its name.
252 * @throw std::invalid_argument if the searched storage does not exist.
254 Storage* Engine::storage_by_name(const std::string& name) const
256 if (pimpl->storages_.find(name) == pimpl->storages_.end())
257 throw std::invalid_argument(std::string("Storage not found: ") + name);
259 return pimpl->storages_.at(name)->get_iface();
262 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
263 Storage* Engine::storage_by_name_or_null(const std::string& name) const
265 auto storage = pimpl->storages_.find(name);
266 return storage == pimpl->storages_.end() ? nullptr : storage->second->get_iface();
269 void Engine::storage_register(const std::string& name, const Storage* storage)
271 pimpl->storages_[name] = storage->get_impl();
274 void Engine::storage_unregister(const std::string& name)
276 pimpl->storages_.erase(name);
279 /** @brief Returns the amount of links in the platform */
280 size_t Engine::get_link_count() const
282 return pimpl->links_.size();
285 /** @brief Returns the list of all links found in the platform */
286 std::vector<Link*> Engine::get_all_links() const
288 std::vector<Link*> res;
289 for (auto const& kv : pimpl->links_)
290 res.push_back(kv.second->get_iface());
294 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
296 std::vector<Link*> filtered_list;
297 for (auto const& kv : pimpl->links_) {
298 Link* l = kv.second->get_iface();
300 filtered_list.push_back(l);
302 return filtered_list;
305 size_t Engine::get_actor_count() const
307 return simix_global->process_list.size();
310 std::vector<ActorPtr> Engine::get_all_actors() const
312 std::vector<ActorPtr> actor_list;
313 for (auto const& kv : simix_global->process_list) {
314 actor_list.push_back(kv.second->iface());
319 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
321 std::vector<ActorPtr> actor_list;
322 for (auto const& kv : simix_global->process_list) {
323 if (filter(kv.second->iface()))
324 actor_list.push_back(kv.second->iface());
329 void Engine::run() const
331 /* Clean IO before the run */
335 if (MC_is_active()) {
342 /** @brief Retrieve the root netzone, containing all others */
343 s4u::NetZone* Engine::get_netzone_root() const
345 return pimpl->netzone_root_->get_iface();
347 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
348 void Engine::set_netzone_root(const s4u::NetZone* netzone)
350 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
351 pimpl->netzone_root_ = netzone->get_impl();
354 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
356 if (current->get_name() == name)
359 for (auto const& elem : current->get_children()) {
360 NetZone* tmp = netzone_by_name_recursive(elem, name);
361 if (tmp != nullptr) {
368 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
369 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
371 return netzone_by_name_recursive(get_netzone_root(), name);
374 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
375 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
377 auto netp = pimpl->netpoints_.find(name);
378 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
381 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
383 std::vector<kernel::routing::NetPoint*> res;
384 for (auto const& kv : pimpl->netpoints_)
385 res.push_back(kv.second);
389 /** @brief Register a new netpoint to the system */
390 void Engine::netpoint_register(kernel::routing::NetPoint* point)
392 simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
395 /** @brief Unregister a given netpoint */
396 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
398 kernel::actor::simcall([this, point] {
399 pimpl->netpoints_.erase(point->get_name());
404 bool Engine::is_initialized()
406 return Engine::instance_ != nullptr;
408 void Engine::set_config(const std::string& str)
410 config::set_parse(str);
412 void Engine::set_config(const std::string& name, int value)
414 config::set_value(name.c_str(), value);
416 void Engine::set_config(const std::string& name, double value)
418 config::set_value(name.c_str(), value);
420 void Engine::set_config(const std::string& name, bool value)
422 config::set_value(name.c_str(), value);
424 void Engine::set_config(const std::string& name, const std::string& value)
426 config::set_value(name.c_str(), value);
430 } // namespace simgrid
432 /* **************************** Public C interface *************************** */
433 void simgrid_init(int* argc, char** argv)
435 simgrid::s4u::Engine e(argc, argv);
437 void simgrid_load_platform(const char* file)
439 simgrid::s4u::Engine::get_instance()->load_platform(file);
442 void simgrid_load_deployment(const char* file)
444 simgrid::s4u::Engine::get_instance()->load_deployment(file);
448 simgrid::s4u::Engine::get_instance()->run();
450 void simgrid_register_function(const char* name, void (*code)(int, char**))
452 simgrid::s4u::Engine::get_instance()->register_function(name, code);
454 void simgrid_register_default(void (*code)(int, char**))
456 simgrid::s4u::Engine::get_instance()->register_default(code);
458 double simgrid_get_clock()
460 return simgrid::s4u::Engine::get_clock();
463 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
465 return simgrid::s4u::Engine::get_instance()->get_actor_count();