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/mc/mc_replay.hpp"
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 void Engine::initialize(int* argc, char** argv)
42 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
43 Engine::instance_ = this;
45 SIMIX_global_init(argc, argv);
48 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl())
51 char* argv = &name[0];
52 initialize(&argc, &argv);
55 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
57 initialize(argc, argv);
63 Engine::instance_ = nullptr;
66 /** @brief Retrieve the engine singleton */
67 Engine* Engine::get_instance()
69 if (Engine::instance_ == nullptr) {
70 auto e = new Engine(nullptr, nullptr);
71 xbt_assert(Engine::instance_ == e);
73 return Engine::instance_;
76 void Engine::shutdown()
78 delete Engine::instance_;
79 Engine::instance_ = nullptr;
82 double Engine::get_clock()
84 if (MC_is_active() || MC_record_replay_is_active()) {
85 return MC_process_clock_get(SIMIX_process_self());
87 return surf_get_clock();
91 void Engine::add_model(std::shared_ptr<kernel::resource::Model> model,
92 const std::vector<kernel::resource::Model*>& dependencies)
94 simgrid::kernel::actor::simcall([this, &model, &dependencies] { pimpl->add_model(std::move(model), dependencies); });
97 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
99 return pimpl->get_all_models();
103 * Creates a new platform, including hosts, links, and the routing table.
106 * See also: :ref:`platform`.
109 void Engine::load_platform(const std::string& platf) const
111 pimpl->load_platform(platf);
114 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v330
116 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
117 return xbt::wrap_main(code, std::move(args));
119 register_function(name, code_factory);
121 void Engine::register_default(int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v330
123 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
126 /** Registers the main function of an actor that will be launched from the deployment file */
127 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
129 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
130 return xbt::wrap_main(code, std::move(args));
132 register_function(name, code_factory);
135 /** Registers the main function of an actor that will be launched from the deployment file */
136 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
138 kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
139 return std::bind(std::move(code), std::move(args));
141 register_function(name, code_factory);
143 /** Registers a function as the default main function of actors
145 * It will be used as fallback when the function requested from the deployment file was not registered.
146 * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
148 void Engine::register_default(const std::function<void(int, char**)>& code)
150 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
152 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
154 simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
157 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
159 simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
162 /** Load a deployment file and launch the actors that it contains
165 * See also: :ref:`deploy`.
168 void Engine::load_deployment(const std::string& deploy) const
170 pimpl->load_deployment(deploy);
173 /** Returns the amount of hosts in the platform */
174 size_t Engine::get_host_count() const
176 return pimpl->hosts_.size();
179 std::vector<Host*> Engine::get_all_hosts() const
181 std::vector<Host*> res;
182 for (auto const& kv : pimpl->hosts_)
183 res.push_back(kv.second);
187 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
189 std::vector<Host*> hosts;
190 for (auto const& kv : pimpl->hosts_) {
191 if (filter(kv.second))
192 hosts.push_back(kv.second);
198 void Engine::host_register(const std::string& name, Host* host)
200 pimpl->hosts_[name] = host;
203 void Engine::host_unregister(const std::string& name)
205 pimpl->hosts_.erase(name);
208 /** @brief Find a host from its name.
210 * @throw std::invalid_argument if the searched host does not exist.
212 Host* Engine::host_by_name(const std::string& name) const
214 auto host = pimpl->hosts_.find(name);
215 if (host == pimpl->hosts_.end())
216 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
220 /** @brief Find a host from its name (or nullptr if that host does not exist) */
221 Host* Engine::host_by_name_or_null(const std::string& name) const
223 auto host = pimpl->hosts_.find(name);
224 return host == pimpl->hosts_.end() ? nullptr : host->second;
227 /** @brief Find a link from its name.
229 * @throw std::invalid_argument if the searched link does not exist.
231 Link* Engine::link_by_name(const std::string& name) const
233 auto link = pimpl->links_.find(name);
234 if (link == pimpl->links_.end())
235 throw std::invalid_argument(std::string("Link not found: ") + name);
236 return link->second->get_iface();
239 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
241 auto link = pimpl->split_duplex_links_.find(name);
242 if (link == pimpl->split_duplex_links_.end())
243 throw std::invalid_argument(std::string("Link not found: ") + name);
244 return link->second->get_iface();
247 /** @brief Find a link from its name (or nullptr if that link does not exist) */
248 Link* Engine::link_by_name_or_null(const std::string& name) const
250 auto link = pimpl->links_.find(name);
251 return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
254 /** @brief Find a mailox from its name or create one if it does not exist) */
255 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
257 /* two actors may have pushed the same mbox_create simcall at the same time */
258 kernel::activity::MailboxImpl* mbox = kernel::actor::simcall([&name, this] {
259 auto m = pimpl->mailboxes_.emplace(name, nullptr);
261 m.first->second = new kernel::activity::MailboxImpl(name);
262 XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
264 return m.first->second;
266 return mbox->get_iface();
269 void Engine::link_register(const std::string& name, const Link* link)
271 pimpl->links_[name] = link->get_impl();
274 void Engine::link_unregister(const std::string& name)
276 pimpl->links_.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 pimpl->get_actor_count();
310 std::vector<ActorPtr> Engine::get_all_actors() const
312 std::vector<ActorPtr> actor_list;
313 for (auto const& kv : pimpl->get_actor_list()) {
314 actor_list.push_back(kv.second->get_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 : pimpl->get_actor_list()) {
323 if (filter(kv.second->get_iface()))
324 actor_list.push_back(kv.second->get_iface());
329 void Engine::run() const
331 /* sealing resources before run: links */
332 for (auto* link : get_all_links())
334 /* seal netzone root, recursively seal children netzones, hosts and disks */
335 get_netzone_root()->seal();
337 /* Clean IO before the run */
341 if (MC_is_active()) {
348 /** @brief Retrieve the root netzone, containing all others */
349 s4u::NetZone* Engine::get_netzone_root() const
351 if (pimpl->netzone_root_)
352 return pimpl->netzone_root_->get_iface();
355 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
356 void Engine::set_netzone_root(const s4u::NetZone* netzone)
358 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
359 pimpl->netzone_root_ = netzone->get_impl();
362 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
364 if (current->get_name() == name)
367 for (auto const& elem : current->get_children()) {
368 NetZone* tmp = netzone_by_name_recursive(elem, name);
369 if (tmp != nullptr) {
376 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
377 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
379 return netzone_by_name_recursive(get_netzone_root(), name);
382 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
383 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
385 auto netp = pimpl->netpoints_.find(name);
386 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
389 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
391 auto netp = netpoint_by_name_or_null(name);
392 if (netp == nullptr) {
393 throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
398 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
400 std::vector<kernel::routing::NetPoint*> res;
401 for (auto const& kv : pimpl->netpoints_)
402 res.push_back(kv.second);
406 /** @brief Register a new netpoint to the system */
407 void Engine::netpoint_register(kernel::routing::NetPoint* point)
409 simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
412 /** @brief Unregister a given netpoint */
413 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
415 kernel::actor::simcall([this, point] {
416 pimpl->netpoints_.erase(point->get_name());
421 bool Engine::is_initialized()
423 return Engine::instance_ != nullptr;
425 void Engine::set_config(const std::string& str)
427 config::set_parse(str);
429 void Engine::set_config(const std::string& name, int value)
431 config::set_value(name.c_str(), value);
433 void Engine::set_config(const std::string& name, double value)
435 config::set_value(name.c_str(), value);
437 void Engine::set_config(const std::string& name, bool value)
439 config::set_value(name.c_str(), value);
441 void Engine::set_config(const std::string& name, const std::string& value)
443 config::set_value(name.c_str(), value);
447 } // namespace simgrid
449 /* **************************** Public C interface *************************** */
450 void simgrid_init(int* argc, char** argv)
452 simgrid::s4u::Engine e(argc, argv);
454 void simgrid_load_platform(const char* file)
456 simgrid::s4u::Engine::get_instance()->load_platform(file);
459 void simgrid_load_deployment(const char* file)
461 simgrid::s4u::Engine::get_instance()->load_deployment(file);
465 simgrid::s4u::Engine::get_instance()->run();
467 void simgrid_register_function(const char* name, void (*code)(int, char**))
469 simgrid::s4u::Engine::get_instance()->register_function(name, code);
471 void simgrid_register_default(void (*code)(int, char**))
473 simgrid::s4u::Engine::get_instance()->register_default(code);
475 double simgrid_get_clock()
477 return simgrid::s4u::Engine::get_clock();
480 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
482 return simgrid::s4u::Engine::get_instance()->get_actor_count();