1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2021. 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/simix.h"
17 #include "src/instr/instr_private.hpp"
18 #include "src/kernel/EngineImpl.hpp"
19 #include "src/simix/smx_private.hpp" // For access to simix_global->process_list
20 #include "src/surf/network_interface.hpp"
21 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
22 #include <simgrid/Exception.hpp>
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)");
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;
38 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
40 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
42 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
44 SIMIX_global_init(argc, argv);
46 Engine::instance_ = this;
52 Engine::instance_ = nullptr;
55 /** @brief Retrieve the engine singleton */
56 Engine* Engine::get_instance()
58 if (Engine::instance_ == nullptr) {
59 auto e = new Engine(nullptr, nullptr);
60 xbt_assert(Engine::instance_ == e);
62 return Engine::instance_;
65 void Engine::shutdown()
67 delete Engine::instance_;
68 Engine::instance_ = nullptr;
71 double Engine::get_clock()
73 return SIMIX_get_clock();
76 void Engine::add_model(std::shared_ptr<simgrid::kernel::resource::Model> model, std::vector<std::string>&& dep_models)
78 simgrid::kernel::actor::simcall([this, &model, &dep_models] {
79 pimpl->add_model(std::move(model), std::forward<decltype(dep_models)>(dep_models));
83 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
85 return pimpl->get_all_models();
89 * Creates a new platform, including hosts, links, and the routing table.
92 * See also: :ref:`platform`.
95 void Engine::load_platform(const std::string& platf) const
97 double start = xbt_os_time();
98 parse_platform_file(platf);
100 double end = xbt_os_time();
101 XBT_DEBUG("PARSE TIME: %g", (end - start));
104 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
106 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
107 return xbt::wrap_main(code, std::move(args));
109 register_function(name, code_factory);
111 void Engine::register_default(int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
113 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
116 /** Registers the main function of an actor that will be launched from the deployment file */
117 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
119 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
120 return xbt::wrap_main(code, std::move(args));
122 register_function(name, code_factory);
125 /** Registers the main function of an actor that will be launched from the deployment file */
126 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
128 kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
129 return std::bind(std::move(code), std::move(args));
131 register_function(name, code_factory);
133 /** Registers a function as the default main function of actors
135 * It will be used as fallback when the function requested from the deployment file was not registered.
136 * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
138 void Engine::register_default(const std::function<void(int, char**)>& code)
140 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
142 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
144 simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
147 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
149 simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
152 /** Load a deployment file and launch the actors that it contains
155 * See also: :ref:`deploy`.
158 void Engine::load_deployment(const std::string& deploy) const
160 pimpl->load_deployment(deploy);
163 /** Returns the amount of hosts in the platform */
164 size_t Engine::get_host_count() const
166 return pimpl->hosts_.size();
169 std::vector<Host*> Engine::get_all_hosts() const
171 std::vector<Host*> res;
172 for (auto const& kv : pimpl->hosts_)
173 res.push_back(kv.second);
177 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
179 std::vector<Host*> hosts;
180 for (auto const& kv : pimpl->hosts_) {
181 if (filter(kv.second))
182 hosts.push_back(kv.second);
188 void Engine::host_register(const std::string& name, Host* host)
190 pimpl->hosts_[name] = host;
193 void Engine::host_unregister(const std::string& name)
195 pimpl->hosts_.erase(name);
198 /** @brief Find a host from its name.
200 * @throw std::invalid_argument if the searched host does not exist.
202 Host* Engine::host_by_name(const std::string& name) const
204 auto host = pimpl->hosts_.find(name);
205 if (host == pimpl->hosts_.end())
206 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
210 /** @brief Find a host from its name (or nullptr if that host does not exist) */
211 Host* Engine::host_by_name_or_null(const std::string& name) const
213 auto host = pimpl->hosts_.find(name);
214 return host == pimpl->hosts_.end() ? nullptr : host->second;
217 /** @brief Find a link from its name.
219 * @throw std::invalid_argument if the searched link does not exist.
221 Link* Engine::link_by_name(const std::string& name) const
223 auto link = pimpl->links_.find(name);
224 if (link == pimpl->links_.end())
225 throw std::invalid_argument(std::string("Link not found: ") + name);
226 return link->second->get_iface();
229 /** @brief Find a link from its name (or nullptr if that link does not exist) */
230 Link* Engine::link_by_name_or_null(const std::string& name) const
232 auto link = pimpl->links_.find(name);
233 return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
236 void Engine::link_register(const std::string& name, const Link* link)
238 pimpl->links_[name] = link->get_impl();
241 void Engine::link_unregister(const std::string& name)
243 pimpl->links_.erase(name);
246 /** @brief Returns the amount of links in the platform */
247 size_t Engine::get_link_count() const
249 return pimpl->links_.size();
252 /** @brief Returns the list of all links found in the platform */
253 std::vector<Link*> Engine::get_all_links() const
255 std::vector<Link*> res;
256 for (auto const& kv : pimpl->links_)
257 res.push_back(kv.second->get_iface());
261 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
263 std::vector<Link*> filtered_list;
264 for (auto const& kv : pimpl->links_) {
265 Link* l = kv.second->get_iface();
267 filtered_list.push_back(l);
269 return filtered_list;
272 size_t Engine::get_actor_count() const
274 return simix_global->process_list.size();
277 std::vector<ActorPtr> Engine::get_all_actors() const
279 std::vector<ActorPtr> actor_list;
280 for (auto const& kv : simix_global->process_list) {
281 actor_list.push_back(kv.second->get_iface());
286 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
288 std::vector<ActorPtr> actor_list;
289 for (auto const& kv : simix_global->process_list) {
290 if (filter(kv.second->get_iface()))
291 actor_list.push_back(kv.second->get_iface());
296 void Engine::run() const
298 /* Clean IO before the run */
302 if (MC_is_active()) {
309 /** @brief Retrieve the root netzone, containing all others */
310 s4u::NetZone* Engine::get_netzone_root() const
312 return pimpl->netzone_root_->get_iface();
314 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
315 void Engine::set_netzone_root(const s4u::NetZone* netzone)
317 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
318 pimpl->netzone_root_ = netzone->get_impl();
321 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
323 if (current->get_name() == name)
326 for (auto const& elem : current->get_children()) {
327 NetZone* tmp = netzone_by_name_recursive(elem, name);
328 if (tmp != nullptr) {
335 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
336 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
338 return netzone_by_name_recursive(get_netzone_root(), name);
341 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
342 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
344 auto netp = pimpl->netpoints_.find(name);
345 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
348 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
350 std::vector<kernel::routing::NetPoint*> res;
351 for (auto const& kv : pimpl->netpoints_)
352 res.push_back(kv.second);
356 /** @brief Register a new netpoint to the system */
357 void Engine::netpoint_register(kernel::routing::NetPoint* point)
359 simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
362 /** @brief Unregister a given netpoint */
363 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
365 kernel::actor::simcall([this, point] {
366 pimpl->netpoints_.erase(point->get_name());
371 bool Engine::is_initialized()
373 return Engine::instance_ != nullptr;
375 void Engine::set_config(const std::string& str)
377 config::set_parse(str);
379 void Engine::set_config(const std::string& name, int value)
381 config::set_value(name.c_str(), value);
383 void Engine::set_config(const std::string& name, double value)
385 config::set_value(name.c_str(), value);
387 void Engine::set_config(const std::string& name, bool value)
389 config::set_value(name.c_str(), value);
391 void Engine::set_config(const std::string& name, const std::string& value)
393 config::set_value(name.c_str(), value);
397 } // namespace simgrid
399 /* **************************** Public C interface *************************** */
400 void simgrid_init(int* argc, char** argv)
402 simgrid::s4u::Engine e(argc, argv);
404 void simgrid_load_platform(const char* file)
406 simgrid::s4u::Engine::get_instance()->load_platform(file);
409 void simgrid_load_deployment(const char* file)
411 simgrid::s4u::Engine::get_instance()->load_deployment(file);
415 simgrid::s4u::Engine::get_instance()->run();
417 void simgrid_register_function(const char* name, void (*code)(int, char**))
419 simgrid::s4u::Engine::get_instance()->register_function(name, code);
421 void simgrid_register_default(void (*code)(int, char**))
423 simgrid::s4u::Engine::get_instance()->register_default(code);
425 double simgrid_get_clock()
427 return simgrid::s4u::Engine::get_clock();
430 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
432 return simgrid::s4u::Engine::get_instance()->get_actor_count();