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/surf/network_interface.hpp"
20 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
21 #include <simgrid/Exception.hpp>
26 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
31 xbt::signal<void()> Engine::on_platform_creation;
32 xbt::signal<void()> Engine::on_platform_created;
33 xbt::signal<void()> Engine::on_simulation_end;
34 xbt::signal<void(double)> Engine::on_time_advance;
35 xbt::signal<void(void)> Engine::on_deadlock;
37 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
39 void Engine::initialize(int* argc, char** argv)
41 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
42 Engine::instance_ = this;
44 SIMIX_global_init(argc, argv);
47 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl())
50 char* argv = &name[0];
51 initialize(&argc, &argv);
54 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
56 initialize(argc, argv);
62 Engine::instance_ = nullptr;
65 /** @brief Retrieve the engine singleton */
66 Engine* Engine::get_instance()
68 if (Engine::instance_ == nullptr) {
69 auto e = new Engine(nullptr, nullptr);
70 xbt_assert(Engine::instance_ == e);
72 return Engine::instance_;
75 void Engine::shutdown()
77 delete Engine::instance_;
78 Engine::instance_ = nullptr;
81 double Engine::get_clock()
83 return SIMIX_get_clock();
86 void Engine::add_model(std::shared_ptr<kernel::resource::Model> model,
87 const std::vector<kernel::resource::Model*>& dependencies)
89 simgrid::kernel::actor::simcall([this, &model, &dependencies] { pimpl->add_model(std::move(model), dependencies); });
92 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
94 return pimpl->get_all_models();
98 * Creates a new platform, including hosts, links, and the routing table.
101 * See also: :ref:`platform`.
104 void Engine::load_platform(const std::string& platf) const
106 double start = xbt_os_time();
107 parse_platform_file(platf);
109 double end = xbt_os_time();
110 XBT_DEBUG("PARSE TIME: %g", (end - start));
113 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
115 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
116 return xbt::wrap_main(code, std::move(args));
118 register_function(name, code_factory);
120 void Engine::register_default(int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
122 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
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(int, char**)>& code)
128 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
129 return xbt::wrap_main(code, std::move(args));
131 register_function(name, code_factory);
134 /** Registers the main function of an actor that will be launched from the deployment file */
135 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
137 kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
138 return std::bind(std::move(code), std::move(args));
140 register_function(name, code_factory);
142 /** Registers a function as the default main function of actors
144 * It will be used as fallback when the function requested from the deployment file was not registered.
145 * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
147 void Engine::register_default(const std::function<void(int, char**)>& code)
149 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
151 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
153 simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
156 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
158 simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
161 /** Load a deployment file and launch the actors that it contains
164 * See also: :ref:`deploy`.
167 void Engine::load_deployment(const std::string& deploy) const
169 pimpl->load_deployment(deploy);
172 /** Returns the amount of hosts in the platform */
173 size_t Engine::get_host_count() const
175 return pimpl->hosts_.size();
178 std::vector<Host*> Engine::get_all_hosts() const
180 std::vector<Host*> res;
181 for (auto const& kv : pimpl->hosts_)
182 res.push_back(kv.second);
186 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
188 std::vector<Host*> hosts;
189 for (auto const& kv : pimpl->hosts_) {
190 if (filter(kv.second))
191 hosts.push_back(kv.second);
197 void Engine::host_register(const std::string& name, Host* host)
199 pimpl->hosts_[name] = host;
202 void Engine::host_unregister(const std::string& name)
204 pimpl->hosts_.erase(name);
207 /** @brief Find a host from its name.
209 * @throw std::invalid_argument if the searched host does not exist.
211 Host* Engine::host_by_name(const std::string& name) const
213 auto host = pimpl->hosts_.find(name);
214 if (host == pimpl->hosts_.end())
215 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
219 /** @brief Find a host from its name (or nullptr if that host does not exist) */
220 Host* Engine::host_by_name_or_null(const std::string& name) const
222 auto host = pimpl->hosts_.find(name);
223 return host == pimpl->hosts_.end() ? nullptr : host->second;
226 /** @brief Find a link from its name.
228 * @throw std::invalid_argument if the searched link does not exist.
230 Link* Engine::link_by_name(const std::string& name) const
232 auto link = pimpl->links_.find(name);
233 if (link == pimpl->links_.end())
234 throw std::invalid_argument(std::string("Link not found: ") + name);
235 return link->second->get_iface();
238 /** @brief Find a link from its name (or nullptr if that link does not exist) */
239 Link* Engine::link_by_name_or_null(const std::string& name) const
241 auto link = pimpl->links_.find(name);
242 return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
245 /** @brief Find a mailox from its name or create one if it does not exist) */
246 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
248 /* two actors may have pushed the same mbox_create simcall at the same time */
249 kernel::activity::MailboxImpl* mbox = kernel::actor::simcall([&name, this] {
250 auto m = pimpl->mailboxes_.emplace(name, nullptr);
252 auto* mbox = new kernel::activity::MailboxImpl(name);
253 XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name.c_str());
254 m.first->second = mbox;
256 return m.first->second;
258 return mbox->get_iface();
261 void Engine::link_register(const std::string& name, const Link* link)
263 pimpl->links_[name] = link->get_impl();
266 void Engine::link_unregister(const std::string& name)
268 pimpl->links_.erase(name);
271 /** @brief Returns the amount of links in the platform */
272 size_t Engine::get_link_count() const
274 return pimpl->links_.size();
277 /** @brief Returns the list of all links found in the platform */
278 std::vector<Link*> Engine::get_all_links() const
280 std::vector<Link*> res;
281 for (auto const& kv : pimpl->links_)
282 res.push_back(kv.second->get_iface());
286 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
288 std::vector<Link*> filtered_list;
289 for (auto const& kv : pimpl->links_) {
290 Link* l = kv.second->get_iface();
292 filtered_list.push_back(l);
294 return filtered_list;
297 size_t Engine::get_actor_count() const
299 return pimpl->get_actor_count();
302 std::vector<ActorPtr> Engine::get_all_actors() const
304 std::vector<ActorPtr> actor_list;
305 for (auto const& kv : pimpl->get_actor_list()) {
306 actor_list.push_back(kv.second->get_iface());
311 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
313 std::vector<ActorPtr> actor_list;
314 for (auto const& kv : pimpl->get_actor_list()) {
315 if (filter(kv.second->get_iface()))
316 actor_list.push_back(kv.second->get_iface());
321 void Engine::run() const
323 /* sealing resources before run: links */
324 for (auto* link : get_all_links())
326 /* seal netzone root, recursively seal children netzones, hosts and disks */
327 get_netzone_root()->seal();
329 /* Clean IO before the run */
333 if (MC_is_active()) {
340 /** @brief Retrieve the root netzone, containing all others */
341 s4u::NetZone* Engine::get_netzone_root() const
343 if (pimpl->netzone_root_)
344 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 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
383 auto netp = netpoint_by_name_or_null(name);
384 if (netp == nullptr) {
385 throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
390 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
392 std::vector<kernel::routing::NetPoint*> res;
393 for (auto const& kv : pimpl->netpoints_)
394 res.push_back(kv.second);
398 /** @brief Register a new netpoint to the system */
399 void Engine::netpoint_register(kernel::routing::NetPoint* point)
401 simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
404 /** @brief Unregister a given netpoint */
405 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
407 kernel::actor::simcall([this, point] {
408 pimpl->netpoints_.erase(point->get_name());
413 bool Engine::is_initialized()
415 return Engine::instance_ != nullptr;
417 void Engine::set_config(const std::string& str)
419 config::set_parse(str);
421 void Engine::set_config(const std::string& name, int value)
423 config::set_value(name.c_str(), value);
425 void Engine::set_config(const std::string& name, double value)
427 config::set_value(name.c_str(), value);
429 void Engine::set_config(const std::string& name, bool value)
431 config::set_value(name.c_str(), value);
433 void Engine::set_config(const std::string& name, const std::string& value)
435 config::set_value(name.c_str(), value);
439 } // namespace simgrid
441 /* **************************** Public C interface *************************** */
442 void simgrid_init(int* argc, char** argv)
444 simgrid::s4u::Engine e(argc, argv);
446 void simgrid_load_platform(const char* file)
448 simgrid::s4u::Engine::get_instance()->load_platform(file);
451 void simgrid_load_deployment(const char* file)
453 simgrid::s4u::Engine::get_instance()->load_deployment(file);
457 simgrid::s4u::Engine::get_instance()->run();
459 void simgrid_register_function(const char* name, void (*code)(int, char**))
461 simgrid::s4u::Engine::get_instance()->register_function(name, code);
463 void simgrid_register_default(void (*code)(int, char**))
465 simgrid::s4u::Engine::get_instance()->register_default(code);
467 double simgrid_get_clock()
469 return simgrid::s4u::Engine::get_clock();
472 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
474 return simgrid::s4u::Engine::get_instance()->get_actor_count();