1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2023. 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. */
8 #include <simgrid/kernel/routing/NetPoint.hpp>
9 #include <simgrid/modelchecker.h>
10 #include <simgrid/s4u/Engine.hpp>
12 #include "src/instr/instr_private.hpp"
13 #include "src/kernel/EngineImpl.hpp"
14 #include "src/kernel/resource/HostImpl.hpp"
15 #include "src/kernel/resource/NetworkModel.hpp"
16 #include "src/kernel/resource/SplitDuplexLinkImpl.hpp"
17 #include "src/kernel/resource/StandardLinkImpl.hpp"
18 #include "src/mc/mc.h"
19 #include "src/mc/mc_replay.hpp"
20 #include "xbt/config.hpp"
25 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
28 static simgrid::kernel::actor::ActorCode maestro_code;
30 namespace simgrid::s4u {
31 xbt::signal<void()> Engine::on_platform_creation;
32 xbt::signal<void()> Engine::on_platform_created;
33 xbt::signal<void()> Engine::on_simulation_start;
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; /* This 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 pimpl_->initialize(argc, argv);
46 // Either create a new context with maestro or create
47 // a context object with the current context maestro):
48 kernel::actor::create_maestro(maestro_code);
51 Engine::Engine(std::string name) : pimpl_(new kernel::EngineImpl())
54 char* argv = &name[0];
55 initialize(&argc, &argv);
58 Engine::Engine(int* argc, char** argv) : pimpl_(new kernel::EngineImpl())
60 initialize(argc, argv);
65 kernel::EngineImpl::shutdown();
66 Engine::instance_ = nullptr;
69 /** @brief Retrieve the engine singleton */
70 Engine* Engine::get_instance()
74 return get_instance(&argc, &argv);
76 Engine* Engine::get_instance(int* argc, char** argv)
78 if (Engine::instance_ == nullptr) {
79 auto* e = new Engine(argc, argv);
80 xbt_assert(Engine::instance_ == e);
82 return Engine::instance_;
84 const std::vector<std::string>& Engine::get_cmdline() const
86 return pimpl_->get_cmdline();
89 void Engine::shutdown() // XBT_ATTRIB_DEPRECATED_v335
91 delete Engine::instance_;
94 double Engine::get_clock()
96 if (MC_is_active() || MC_record_replay_is_active()) {
97 return MC_process_clock_get(kernel::actor::ActorImpl::self());
99 return kernel::EngineImpl::get_clock();
103 void Engine::add_model(std::shared_ptr<kernel::resource::Model> model,
104 const std::vector<kernel::resource::Model*>& dependencies)
106 kernel::actor::simcall_answered([this, &model, &dependencies] { pimpl_->add_model(std::move(model), dependencies); });
109 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
111 return pimpl_->get_all_models();
114 void Engine::load_platform(const std::string& platf) const
116 pimpl_->load_platform(platf);
119 void Engine::seal_platform() const
121 pimpl_->seal_platform();
124 static void flatify_hosts(Engine const& engine, std::stringstream& ss)
127 std::vector<Host*> hosts = engine.get_all_hosts();
129 for (auto const* h : hosts) {
130 ss << " <host id=\"" << h->get_name() << "\" speed=\"" << h->get_speed() << "\"";
131 const std::unordered_map<std::string, std::string>* props = h->get_properties();
132 if (h->get_core_count() > 1)
133 ss << " core=\"" << h->get_core_count() << "\"";
135 // Sort the properties before displaying them, so that the tests are perfectly reproducible
136 std::vector<std::string> keys;
137 for (auto const& [key, _] : *props)
139 if (not keys.empty()) {
141 std::sort(keys.begin(), keys.end());
142 for (const std::string& key : keys)
143 ss << " <prop id=\"" << key << "\" value=\"" << props->at(key) << "\"/>\n";
151 std::vector<simgrid::kernel::routing::NetPoint*> netpoints = engine.get_all_netpoints();
152 std::sort(netpoints.begin(), netpoints.end(),
153 [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
154 return a->get_name() < b->get_name();
157 for (auto const& src : netpoints)
158 if (src->is_router())
159 ss << " <router id=\"" << src->get_name() << "\"/>\n";
162 static void flatify_links(Engine const& engine, std::stringstream& ss)
164 std::vector<Link*> links = engine.get_all_links();
166 std::sort(links.begin(), links.end(), [](const Link* a, const Link* b) { return a->get_name() < b->get_name(); });
168 for (auto const* link : links) {
169 ss << " <link id=\"" << link->get_name() << "\"";
170 ss << " bandwidth=\"" << link->get_bandwidth() << "\"";
171 ss << " latency=\"" << link->get_latency() << "\"";
172 if (link->get_concurrency_limit() != -1)
173 ss << " concurrency=\"" << link->get_concurrency_limit() << "\"";
174 if (link->is_shared()) {
177 ss << " sharing_policy=\"FATPIPE\"/>\n";
182 static void flatify_routes(Engine const& engine, std::stringstream& ss)
184 auto hosts = engine.get_all_hosts();
185 auto netpoints = engine.get_all_netpoints();
186 std::sort(netpoints.begin(), netpoints.end(),
187 [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
188 return a->get_name() < b->get_name();
191 for (auto const* src_host : hosts) { // Routes from host
192 const simgrid::kernel::routing::NetPoint* src = src_host->get_netpoint();
193 for (auto const* dst_host : hosts) { // Routes to host
194 std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
195 const simgrid::kernel::routing::NetPoint* dst = dst_host->get_netpoint();
196 simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
199 ss << " <route src=\"" << src_host->get_name() << "\" dst=\"" << dst_host->get_name() << "\">\n ";
200 for (auto const& link : route)
201 ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
202 ss << "\n </route>\n";
205 for (auto const& dst : netpoints) { // to router
206 if (not dst->is_router())
208 ss << " <route src=\"" << src_host->get_name() << "\" dst=\"" << dst->get_name() << "\">\n ";
209 std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
210 simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
211 for (auto const& link : route)
212 ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
213 ss << "\n </route>\n";
217 for (auto const& value1 : netpoints) { // Routes from router
218 if (not value1->is_router())
220 for (auto const& value2 : netpoints) { // to router
221 if (not value2->is_router())
223 std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
224 simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
227 ss << " <route src=\"" << value1->get_name() << "\" dst=\"" << value2->get_name() << "\">\n ";
228 for (auto const& link : route)
229 ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
230 ss << "\n </route>\n";
232 for (auto const* dst_host : hosts) { // Routes to host
233 ss << " <route src=\"" << value1->get_name() << "\" dst=\"" << dst_host->get_name() << "\">\n ";
234 std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
235 const simgrid::kernel::routing::NetPoint* netcardDst = dst_host->get_netpoint();
236 simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
237 for (auto const& link : route)
238 ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
239 ss << "\n </route>\n";
243 std::string Engine::flatify_platform() const
245 std::string version = "4.1";
246 std::stringstream ss;
248 ss << "<?xml version='1.0'?>\n";
249 ss << "<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n";
250 ss << "<platform version=\"" << version << "\">\n";
251 ss << "<AS id=\"" << get_netzone_root()->get_name() << "\" routing=\"Full\">\n";
253 flatify_hosts(*this, ss);
254 flatify_links(*this, ss);
255 flatify_routes(*this, ss);
258 ss << "</platform>\n";
262 /** Registers the main function of an actor that will be launched from the deployment file */
263 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
265 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
266 return xbt::wrap_main(code, std::move(args));
268 register_function(name, code_factory);
271 /** Registers the main function of an actor that will be launched from the deployment file */
272 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
274 kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
275 return std::bind(std::move(code), std::move(args));
277 register_function(name, code_factory);
279 /** Registers a function as the default main function of actors
281 * It will be used as fallback when the function requested from the deployment file was not registered.
282 * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
284 void Engine::register_default(const std::function<void(int, char**)>& code)
286 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
288 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
290 simgrid::kernel::actor::simcall_answered([this, &code]() { pimpl_->register_default(code); });
293 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
295 simgrid::kernel::actor::simcall_answered([this, name, &code]() { pimpl_->register_function(name, code); });
298 /** Load a deployment file and launch the actors that it contains
301 * See also: :ref:`deploy`.
304 void Engine::load_deployment(const std::string& deploy) const
306 pimpl_->load_deployment(deploy);
309 /** Returns the amount of hosts in the platform */
310 size_t Engine::get_host_count() const
312 return get_all_hosts().size();
315 std::vector<Host*> Engine::get_all_hosts() const
317 return get_filtered_hosts([](const Host*) { return true; });
320 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
322 std::vector<Host*> hosts;
323 if (pimpl_->netzone_root_) {
324 hosts = pimpl_->netzone_root_->get_filtered_hosts(filter);
326 /* Sort hosts in lexicographical order: keep same behavior when the hosts were saved on Engine
327 * Some tests do a get_all_hosts() and selects hosts in this order */
328 std::sort(hosts.begin(), hosts.end(), [](const auto* h1, const auto* h2) { return h1->get_name() < h2->get_name(); });
333 /** @brief Find a host from its name.
335 * @throw std::invalid_argument if the searched host does not exist.
337 Host* Engine::host_by_name(const std::string& name) const
339 auto* host = host_by_name_or_null(name);
341 throw std::invalid_argument("Host not found: '" + name + "'");
345 /** @brief Find a host from its name (or nullptr if that host does not exist) */
346 Host* Engine::host_by_name_or_null(const std::string& name) const
348 Host* host = nullptr;
349 if (pimpl_->netzone_root_) {
350 auto* host_impl = pimpl_->netzone_root_->get_host_by_name_or_null(name);
352 host = host_impl->get_iface();
357 /** @brief Find a link from its name.
359 * @throw std::invalid_argument if the searched link does not exist.
361 Link* Engine::link_by_name(const std::string& name) const
363 auto* link = link_by_name_or_null(name);
365 throw std::invalid_argument("Link not found: " + name);
369 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
372 pimpl_->netzone_root_ ? pimpl_->netzone_root_->get_split_duplex_link_by_name_or_null(name) : nullptr;
374 throw std::invalid_argument("Link not found: " + name);
375 return link_impl->get_iface();
378 /** @brief Find a link from its name (or nullptr if that link does not exist) */
379 Link* Engine::link_by_name_or_null(const std::string& name) const
381 Link* link = nullptr;
382 if (pimpl_->netzone_root_) {
383 /* keep behavior where internal __loopback__ link from network model is given to user */
384 if (name == "__loopback__")
385 return pimpl_->netzone_root_->get_network_model()->loopback_->get_iface();
386 auto* link_impl = pimpl_->netzone_root_->get_link_by_name_or_null(name);
388 link = link_impl->get_iface();
393 /** @brief Find a mailbox from its name or create one if it does not exist) */
394 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
396 /* two actors may have pushed the same mbox_create simcall at the same time */
397 kernel::activity::MailboxImpl* mbox = kernel::actor::simcall_answered([&name, this] {
398 auto [m, inserted] = pimpl_->mailboxes_.try_emplace(name, nullptr);
400 m->second = new kernel::activity::MailboxImpl(name);
401 XBT_DEBUG("Creating a mailbox at %p with name %s", m->second, name.c_str());
405 return mbox->get_iface();
408 /** @brief Returns the amount of links in the platform */
409 size_t Engine::get_link_count() const
412 if (pimpl_->netzone_root_) {
413 count += pimpl_->netzone_root_->get_link_count();
414 /* keep behavior where internal __loopback__ link from network model is given to user */
415 count += pimpl_->netzone_root_->get_network_model()->loopback_ ? 1 : 0;
420 /** @brief Returns the list of all links found in the platform */
421 std::vector<Link*> Engine::get_all_links() const
423 return get_filtered_links([](const Link*) { return true; });
426 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
428 std::vector<Link*> res;
429 if (pimpl_->netzone_root_) {
430 res = pimpl_->netzone_root_->get_filtered_links(filter);
431 /* keep behavior where internal __loopback__ link from network model is given to user */
432 if (pimpl_->netzone_root_->get_network_model()->loopback_ &&
433 filter(pimpl_->netzone_root_->get_network_model()->loopback_->get_iface()))
434 res.push_back(pimpl_->netzone_root_->get_network_model()->loopback_->get_iface());
439 size_t Engine::get_actor_count() const
441 return pimpl_->get_actor_count();
444 std::vector<ActorPtr> Engine::get_all_actors() const
446 std::vector<ActorPtr> actor_list;
447 for (auto const& [_, actor] : pimpl_->get_actor_list()) {
448 actor_list.push_back(actor->get_iface());
453 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
455 std::vector<ActorPtr> actor_list;
456 for (auto const& [_, actor] : pimpl_->get_actor_list()) {
457 if (filter(actor->get_iface()))
458 actor_list.push_back(actor->get_iface());
463 void Engine::run() const
467 void Engine::run_until(double max_date) const
469 if (static bool callback_called = false; not callback_called) {
470 on_simulation_start();
471 callback_called = true;
473 /* Clean IO before the run */
477 pimpl_->run(max_date);
480 void Engine::track_vetoed_activities(std::set<Activity*>* vetoed_activities) const
482 Activity::set_vetoed_activities(vetoed_activities);
485 /** @brief Retrieve the root netzone, containing all others */
486 s4u::NetZone* Engine::get_netzone_root() const
488 if (pimpl_->netzone_root_)
489 return pimpl_->netzone_root_->get_iface();
492 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
493 void Engine::set_netzone_root(const s4u::NetZone* netzone)
495 xbt_assert(pimpl_->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
496 pimpl_->netzone_root_ = netzone->get_impl();
499 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
501 if (current->get_name() == name)
504 for (auto const& elem : current->get_children()) {
505 NetZone* tmp = netzone_by_name_recursive(elem, name);
506 if (tmp != nullptr) {
513 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
514 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
516 return netzone_by_name_recursive(get_netzone_root(), name);
519 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
520 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
522 auto netp = pimpl_->netpoints_.find(name);
523 return netp == pimpl_->netpoints_.end() ? nullptr : netp->second;
526 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
528 auto* netp = netpoint_by_name_or_null(name);
529 if (netp == nullptr) {
530 throw std::invalid_argument("Netpoint not found: " + name);
535 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
537 std::vector<kernel::routing::NetPoint*> res;
538 for (auto const& [_, netpoint] : pimpl_->netpoints_)
539 res.push_back(netpoint);
543 /** @brief Register a new netpoint to the system */
544 void Engine::netpoint_register(kernel::routing::NetPoint* point)
546 simgrid::kernel::actor::simcall_answered([this, point] { pimpl_->netpoints_[point->get_name()] = point; });
549 /** @brief Unregister a given netpoint */
550 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
552 kernel::actor::simcall_answered([this, point] {
553 pimpl_->netpoints_.erase(point->get_name());
558 bool Engine::is_initialized()
560 return Engine::instance_ != nullptr;
562 void Engine::set_config(const std::string& str)
564 config::set_parse(str);
566 void Engine::set_config(const std::string& name, int value)
568 config::set_value(name.c_str(), value);
570 void Engine::set_config(const std::string& name, double value)
572 config::set_value(name.c_str(), value);
574 void Engine::set_config(const std::string& name, bool value)
576 config::set_value(name.c_str(), value);
578 void Engine::set_config(const std::string& name, const std::string& value)
580 config::set_value(name.c_str(), value);
583 Engine* Engine::set_default_comm_data_copy_callback(
584 const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback)
586 kernel::activity::CommImpl::set_copy_data_callback(callback);
590 } // namespace simgrid::s4u
592 /* **************************** Public C interface *************************** */
593 void simgrid_init(int* argc, char** argv)
595 static simgrid::s4u::Engine e(argc, argv);
597 void simgrid_load_platform(const char* file)
599 simgrid::s4u::Engine::get_instance()->load_platform(file);
602 void simgrid_load_deployment(const char* file)
604 simgrid::s4u::Engine::get_instance()->load_deployment(file);
608 simgrid::s4u::Engine::get_instance()->run();
610 void simgrid_run_until(double max_date)
612 simgrid::s4u::Engine::get_instance()->run_until(max_date);
614 void simgrid_register_function(const char* name, void (*code)(int, char**))
616 simgrid::s4u::Engine::get_instance()->register_function(name, code);
618 void simgrid_register_default(void (*code)(int, char**))
620 simgrid::s4u::Engine::get_instance()->register_default(code);
622 double simgrid_get_clock()
624 return simgrid::s4u::Engine::get_clock();
627 void simgrid_set_maestro(void (*code)(void*), void* data)
629 maestro_code = std::bind(code, data);