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 <boost/algorithm/string/predicate.hpp>
23 #include <simgrid/Exception.hpp>
31 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
36 xbt::signal<void()> Engine::on_platform_creation;
37 xbt::signal<void()> Engine::on_platform_created;
38 xbt::signal<void()> Engine::on_simulation_end;
39 xbt::signal<void(double)> Engine::on_time_advance;
40 xbt::signal<void(void)> Engine::on_deadlock;
42 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
44 void Engine::initialize(int* argc, char** argv)
46 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
47 Engine::instance_ = this;
49 SIMIX_global_init(argc, argv);
52 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl())
55 char* argv = &name[0];
56 initialize(&argc, &argv);
59 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
61 initialize(argc, argv);
67 Engine::instance_ = nullptr;
70 /** @brief Retrieve the engine singleton */
71 Engine* Engine::get_instance()
73 if (Engine::instance_ == nullptr) {
74 auto e = new Engine(nullptr, nullptr);
75 xbt_assert(Engine::instance_ == e);
77 return Engine::instance_;
80 void Engine::shutdown()
82 delete Engine::instance_;
83 Engine::instance_ = nullptr;
86 double Engine::get_clock()
88 if (MC_is_active() || MC_record_replay_is_active()) {
89 return MC_process_clock_get(SIMIX_process_self());
91 return surf_get_clock();
95 void Engine::add_model(std::shared_ptr<kernel::resource::Model> model,
96 const std::vector<kernel::resource::Model*>& dependencies)
98 simgrid::kernel::actor::simcall([this, &model, &dependencies] { pimpl->add_model(std::move(model), dependencies); });
101 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
103 return pimpl->get_all_models();
107 * Creates a new platform, including hosts, links, and the routing table.
110 * See also: :ref:`platform`.
113 void Engine::load_platform(const std::string& platf) const
115 double start = xbt_os_time();
116 if (boost::algorithm::ends_with(platf, ".so") or boost::algorithm::ends_with(platf, ".dylib")) {
118 xbt_die("loading platform through shared library isn't supported on windows");
120 void* handle = dlopen(platf.c_str(), RTLD_LAZY);
121 xbt_assert(handle, "Impossible to open platform file: %s", platf.c_str());
122 typedef void (*load_fct_t)(const Engine&);
124 load_fct_t callable = (load_fct_t)dlsym(handle, "load_platform");
125 const char* dlsym_error = dlerror();
126 xbt_assert(not dlsym_error, "Error: %s", dlsym_error);
131 parse_platform_file(platf);
134 double end = xbt_os_time();
135 XBT_DEBUG("PARSE TIME: %g", (end - start));
138 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
140 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
141 return xbt::wrap_main(code, std::move(args));
143 register_function(name, code_factory);
145 void Engine::register_default(int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
147 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
150 /** Registers the main function of an actor that will be launched from the deployment file */
151 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
153 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
154 return xbt::wrap_main(code, std::move(args));
156 register_function(name, code_factory);
159 /** Registers the main function of an actor that will be launched from the deployment file */
160 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
162 kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
163 return std::bind(std::move(code), std::move(args));
165 register_function(name, code_factory);
167 /** Registers a function as the default main function of actors
169 * It will be used as fallback when the function requested from the deployment file was not registered.
170 * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
172 void Engine::register_default(const std::function<void(int, char**)>& code)
174 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
176 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
178 simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
181 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
183 simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
186 /** Load a deployment file and launch the actors that it contains
189 * See also: :ref:`deploy`.
192 void Engine::load_deployment(const std::string& deploy) const
194 pimpl->load_deployment(deploy);
197 /** Returns the amount of hosts in the platform */
198 size_t Engine::get_host_count() const
200 return pimpl->hosts_.size();
203 std::vector<Host*> Engine::get_all_hosts() const
205 std::vector<Host*> res;
206 for (auto const& kv : pimpl->hosts_)
207 res.push_back(kv.second);
211 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
213 std::vector<Host*> hosts;
214 for (auto const& kv : pimpl->hosts_) {
215 if (filter(kv.second))
216 hosts.push_back(kv.second);
222 void Engine::host_register(const std::string& name, Host* host)
224 pimpl->hosts_[name] = host;
227 void Engine::host_unregister(const std::string& name)
229 pimpl->hosts_.erase(name);
232 /** @brief Find a host from its name.
234 * @throw std::invalid_argument if the searched host does not exist.
236 Host* Engine::host_by_name(const std::string& name) const
238 auto host = pimpl->hosts_.find(name);
239 if (host == pimpl->hosts_.end())
240 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
244 /** @brief Find a host from its name (or nullptr if that host does not exist) */
245 Host* Engine::host_by_name_or_null(const std::string& name) const
247 auto host = pimpl->hosts_.find(name);
248 return host == pimpl->hosts_.end() ? nullptr : host->second;
251 /** @brief Find a link from its name.
253 * @throw std::invalid_argument if the searched link does not exist.
255 Link* Engine::link_by_name(const std::string& name) const
257 auto link = pimpl->links_.find(name);
258 if (link == pimpl->links_.end())
259 throw std::invalid_argument(std::string("Link not found: ") + name);
260 return link->second->get_iface();
263 /** @brief Find a link from its name (or nullptr if that link does not exist) */
264 Link* Engine::link_by_name_or_null(const std::string& name) const
266 auto link = pimpl->links_.find(name);
267 return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
270 /** @brief Find a mailox from its name or create one if it does not exist) */
271 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
273 /* two actors may have pushed the same mbox_create simcall at the same time */
274 kernel::activity::MailboxImpl* mbox = kernel::actor::simcall([&name, this] {
275 auto m = pimpl->mailboxes_.emplace(name, nullptr);
277 m.first->second = new kernel::activity::MailboxImpl(name);
278 XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
280 return m.first->second;
282 return mbox->get_iface();
285 void Engine::link_register(const std::string& name, const Link* link)
287 pimpl->links_[name] = link->get_impl();
290 void Engine::link_unregister(const std::string& name)
292 pimpl->links_.erase(name);
295 /** @brief Returns the amount of links in the platform */
296 size_t Engine::get_link_count() const
298 return pimpl->links_.size();
301 /** @brief Returns the list of all links found in the platform */
302 std::vector<Link*> Engine::get_all_links() const
304 std::vector<Link*> res;
305 for (auto const& kv : pimpl->links_)
306 res.push_back(kv.second->get_iface());
310 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
312 std::vector<Link*> filtered_list;
313 for (auto const& kv : pimpl->links_) {
314 Link* l = kv.second->get_iface();
316 filtered_list.push_back(l);
318 return filtered_list;
321 size_t Engine::get_actor_count() const
323 return pimpl->get_actor_count();
326 std::vector<ActorPtr> Engine::get_all_actors() const
328 std::vector<ActorPtr> actor_list;
329 for (auto const& kv : pimpl->get_actor_list()) {
330 actor_list.push_back(kv.second->get_iface());
335 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
337 std::vector<ActorPtr> actor_list;
338 for (auto const& kv : pimpl->get_actor_list()) {
339 if (filter(kv.second->get_iface()))
340 actor_list.push_back(kv.second->get_iface());
345 void Engine::run() const
347 /* sealing resources before run: links */
348 for (auto* link : get_all_links())
350 /* seal netzone root, recursively seal children netzones, hosts and disks */
351 get_netzone_root()->seal();
353 /* Clean IO before the run */
357 if (MC_is_active()) {
364 /** @brief Retrieve the root netzone, containing all others */
365 s4u::NetZone* Engine::get_netzone_root() const
367 if (pimpl->netzone_root_)
368 return pimpl->netzone_root_->get_iface();
371 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
372 void Engine::set_netzone_root(const s4u::NetZone* netzone)
374 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
375 pimpl->netzone_root_ = netzone->get_impl();
378 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
380 if (current->get_name() == name)
383 for (auto const& elem : current->get_children()) {
384 NetZone* tmp = netzone_by_name_recursive(elem, name);
385 if (tmp != nullptr) {
392 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
393 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
395 return netzone_by_name_recursive(get_netzone_root(), name);
398 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
399 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
401 auto netp = pimpl->netpoints_.find(name);
402 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
405 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
407 auto netp = netpoint_by_name_or_null(name);
408 if (netp == nullptr) {
409 throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
414 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
416 std::vector<kernel::routing::NetPoint*> res;
417 for (auto const& kv : pimpl->netpoints_)
418 res.push_back(kv.second);
422 /** @brief Register a new netpoint to the system */
423 void Engine::netpoint_register(kernel::routing::NetPoint* point)
425 simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
428 /** @brief Unregister a given netpoint */
429 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
431 kernel::actor::simcall([this, point] {
432 pimpl->netpoints_.erase(point->get_name());
437 bool Engine::is_initialized()
439 return Engine::instance_ != nullptr;
441 void Engine::set_config(const std::string& str)
443 config::set_parse(str);
445 void Engine::set_config(const std::string& name, int value)
447 config::set_value(name.c_str(), value);
449 void Engine::set_config(const std::string& name, double value)
451 config::set_value(name.c_str(), value);
453 void Engine::set_config(const std::string& name, bool value)
455 config::set_value(name.c_str(), value);
457 void Engine::set_config(const std::string& name, const std::string& value)
459 config::set_value(name.c_str(), value);
463 } // namespace simgrid
465 /* **************************** Public C interface *************************** */
466 void simgrid_init(int* argc, char** argv)
468 simgrid::s4u::Engine e(argc, argv);
470 void simgrid_load_platform(const char* file)
472 simgrid::s4u::Engine::get_instance()->load_platform(file);
475 void simgrid_load_deployment(const char* file)
477 simgrid::s4u::Engine::get_instance()->load_deployment(file);
481 simgrid::s4u::Engine::get_instance()->run();
483 void simgrid_register_function(const char* name, void (*code)(int, char**))
485 simgrid::s4u::Engine::get_instance()->register_function(name, code);
487 void simgrid_register_default(void (*code)(int, char**))
489 simgrid::s4u::Engine::get_instance()->register_default(code);
491 double simgrid_get_clock()
493 return simgrid::s4u::Engine::get_clock();
496 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
498 return simgrid::s4u::Engine::get_instance()->get_actor_count();