1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2022. 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 #define SIMIX_H_NO_DEPRECATED_WARNING // avoid deprecation warning on include (remove with XBT_ATTRIB_DEPRECATED_v333)
13 #include <simgrid/simix.h>
16 #include "src/instr/instr_private.hpp"
17 #include "src/kernel/EngineImpl.hpp"
18 #include "src/kernel/resource/NetworkModel.hpp"
19 #include "src/kernel/resource/SplitDuplexLinkImpl.hpp"
20 #include "src/kernel/resource/StandardLinkImpl.hpp"
21 #include "src/mc/mc_replay.hpp"
22 #include "src/surf/HostImpl.hpp"
23 #include "xbt/config.hpp"
28 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
31 static simgrid::kernel::actor::ActorCode maestro_code;
35 xbt::signal<void()> Engine::on_platform_creation;
36 xbt::signal<void()> Engine::on_platform_created;
37 xbt::signal<void()> Engine::on_simulation_start;
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; /* This 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 pimpl->initialize(argc, argv);
50 // Either create a new context with maestro or create
51 // a context object with the current context maestro):
52 kernel::actor::create_maestro(maestro_code);
55 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl())
58 char* argv = &name[0];
59 initialize(&argc, &argv);
62 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
64 initialize(argc, argv);
69 kernel::EngineImpl::shutdown();
70 Engine::instance_ = nullptr;
73 /** @brief Retrieve the engine singleton */
74 Engine* Engine::get_instance()
78 return get_instance(&argc, &argv);
80 Engine* Engine::get_instance(int* argc, char** argv)
82 if (Engine::instance_ == nullptr) {
83 auto e = new Engine(argc, argv);
84 xbt_assert(Engine::instance_ == e);
86 return Engine::instance_;
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();
115 * Creates a new platform, including hosts, links, and the routing table.
118 * See also: :ref:`platform`.
121 void Engine::load_platform(const std::string& platf) const
123 pimpl->load_platform(platf);
127 * @brief Seals the platform, finishing the creation of its resources.
129 * This method is optional. The seal() is done automatically when you call Engine::run.
131 void Engine::seal_platform() const
133 pimpl->seal_platform();
136 /** Registers the main function of an actor that will be launched from the deployment file */
137 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
139 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
140 return xbt::wrap_main(code, std::move(args));
142 register_function(name, code_factory);
145 /** Registers the main function of an actor that will be launched from the deployment file */
146 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
148 kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
149 return std::bind(std::move(code), std::move(args));
151 register_function(name, code_factory);
153 /** Registers a function as the default main function of actors
155 * It will be used as fallback when the function requested from the deployment file was not registered.
156 * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
158 void Engine::register_default(const std::function<void(int, char**)>& code)
160 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
162 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
164 simgrid::kernel::actor::simcall_answered([this, &code]() { pimpl->register_default(code); });
167 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
169 simgrid::kernel::actor::simcall_answered([this, name, &code]() { pimpl->register_function(name, code); });
172 /** Load a deployment file and launch the actors that it contains
175 * See also: :ref:`deploy`.
178 void Engine::load_deployment(const std::string& deploy) const
180 pimpl->load_deployment(deploy);
183 /** Returns the amount of hosts in the platform */
184 size_t Engine::get_host_count() const
186 return get_all_hosts().size();
189 std::vector<Host*> Engine::get_all_hosts() const
191 return get_filtered_hosts([](const Host*) { return true; });
194 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
196 std::vector<Host*> hosts;
197 if (pimpl->netzone_root_) {
198 hosts = pimpl->netzone_root_->get_filtered_hosts(filter);
200 /* Sort hosts in lexicographical order: keep same behavior when the hosts were saved on Engine
201 * Some tests do a get_all_hosts() and selects hosts in this order */
202 std::sort(hosts.begin(), hosts.end(), [](const auto* h1, const auto* h2) { return h1->get_name() < h2->get_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 = host_by_name_or_null(name);
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 Host* host = nullptr;
223 if (pimpl->netzone_root_) {
224 auto* host_impl = pimpl->netzone_root_->get_host_by_name_or_null(name);
226 host = host_impl->get_iface();
231 /** @brief Find a link from its name.
233 * @throw std::invalid_argument if the searched link does not exist.
235 Link* Engine::link_by_name(const std::string& name) const
237 auto* link = link_by_name_or_null(name);
239 throw std::invalid_argument(std::string("Link not found: ") + name);
243 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
245 auto* link_impl = pimpl->netzone_root_ ? pimpl->netzone_root_->get_split_duplex_link_by_name_or_null(name) : nullptr;
247 throw std::invalid_argument(std::string("Link not found: ") + name);
248 return link_impl->get_iface();
251 /** @brief Find a link from its name (or nullptr if that link does not exist) */
252 Link* Engine::link_by_name_or_null(const std::string& name) const
254 Link* link = nullptr;
255 if (pimpl->netzone_root_) {
256 /* keep behavior where internal __loopback__ link from network model is given to user */
257 if (name == "__loopback__")
258 return pimpl->netzone_root_->get_network_model()->loopback_->get_iface();
259 auto* link_impl = pimpl->netzone_root_->get_link_by_name_or_null(name);
261 link = link_impl->get_iface();
266 /** @brief Find a mailbox from its name or create one if it does not exist) */
267 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
269 /* two actors may have pushed the same mbox_create simcall at the same time */
270 kernel::activity::MailboxImpl* mbox = kernel::actor::simcall_answered([&name, this] {
271 auto m = pimpl->mailboxes_.try_emplace(name, nullptr);
273 m.first->second = new kernel::activity::MailboxImpl(name);
274 XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
276 return m.first->second;
278 return mbox->get_iface();
281 /** @brief Returns the amount of links in the platform */
282 size_t Engine::get_link_count() const
285 if (pimpl->netzone_root_) {
286 count += pimpl->netzone_root_->get_link_count();
287 /* keep behavior where internal __loopback__ link from network model is given to user */
288 count += pimpl->netzone_root_->get_network_model()->loopback_ ? 1 : 0;
293 /** @brief Returns the list of all links found in the platform */
294 std::vector<Link*> Engine::get_all_links() const
296 return get_filtered_links([](const Link*) { return true; });
299 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
301 std::vector<Link*> res;
302 if (pimpl->netzone_root_) {
303 res = pimpl->netzone_root_->get_filtered_links(filter);
304 /* keep behavior where internal __loopback__ link from network model is given to user */
305 if (pimpl->netzone_root_->get_network_model()->loopback_ &&
306 filter(pimpl->netzone_root_->get_network_model()->loopback_->get_iface()))
307 res.push_back(pimpl->netzone_root_->get_network_model()->loopback_->get_iface());
312 size_t Engine::get_actor_count() const
314 return pimpl->get_actor_count();
317 std::vector<ActorPtr> Engine::get_all_actors() const
319 std::vector<ActorPtr> actor_list;
320 for (auto const& kv : pimpl->get_actor_list()) {
321 actor_list.push_back(kv.second->get_iface());
326 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
328 std::vector<ActorPtr> actor_list;
329 for (auto const& kv : pimpl->get_actor_list()) {
330 if (filter(kv.second->get_iface()))
331 actor_list.push_back(kv.second->get_iface());
336 void Engine::run() const
340 void Engine::run_until(double max_date) const
342 static bool callback_called = false;
343 if (not callback_called) {
344 on_simulation_start();
345 callback_called = true;
347 /* Clean IO before the run */
351 pimpl->run(max_date);
354 void Engine::track_vetoed_activities(std::set<Activity*>* vetoed_activities) const
356 Activity::set_vetoed_activities(vetoed_activities);
359 /** @brief Retrieve the root netzone, containing all others */
360 s4u::NetZone* Engine::get_netzone_root() const
362 if (pimpl->netzone_root_)
363 return pimpl->netzone_root_->get_iface();
366 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
367 void Engine::set_netzone_root(const s4u::NetZone* netzone)
369 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
370 pimpl->netzone_root_ = netzone->get_impl();
373 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
375 if (current->get_name() == name)
378 for (auto const& elem : current->get_children()) {
379 NetZone* tmp = netzone_by_name_recursive(elem, name);
380 if (tmp != nullptr) {
387 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
388 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
390 return netzone_by_name_recursive(get_netzone_root(), name);
393 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
394 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
396 auto netp = pimpl->netpoints_.find(name);
397 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
400 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
402 auto netp = netpoint_by_name_or_null(name);
403 if (netp == nullptr) {
404 throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
409 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
411 std::vector<kernel::routing::NetPoint*> res;
412 for (auto const& kv : pimpl->netpoints_)
413 res.push_back(kv.second);
417 /** @brief Register a new netpoint to the system */
418 void Engine::netpoint_register(kernel::routing::NetPoint* point)
420 simgrid::kernel::actor::simcall_answered([this, point] { pimpl->netpoints_[point->get_name()] = point; });
423 /** @brief Unregister a given netpoint */
424 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
426 kernel::actor::simcall_answered([this, point] {
427 pimpl->netpoints_.erase(point->get_name());
432 bool Engine::is_initialized()
434 return Engine::instance_ != nullptr;
436 void Engine::set_config(const std::string& str)
438 config::set_parse(str);
440 void Engine::set_config(const std::string& name, int value)
442 config::set_value(name.c_str(), value);
444 void Engine::set_config(const std::string& name, double value)
446 config::set_value(name.c_str(), value);
448 void Engine::set_config(const std::string& name, bool value)
450 config::set_value(name.c_str(), value);
452 void Engine::set_config(const std::string& name, const std::string& value)
454 config::set_value(name.c_str(), value);
457 Engine* Engine::set_default_comm_data_copy_callback(
458 const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback)
460 kernel::activity::CommImpl::set_copy_data_callback(callback);
465 } // namespace simgrid
467 /* **************************** Public C interface *************************** */
468 void simgrid_init(int* argc, char** argv)
470 static simgrid::s4u::Engine e(argc, argv);
472 void simgrid_load_platform(const char* file)
474 simgrid::s4u::Engine::get_instance()->load_platform(file);
477 void simgrid_load_deployment(const char* file)
479 simgrid::s4u::Engine::get_instance()->load_deployment(file);
483 simgrid::s4u::Engine::get_instance()->run();
485 void simgrid_run_until(double max_date)
487 simgrid::s4u::Engine::get_instance()->run_until(max_date);
489 void simgrid_register_function(const char* name, void (*code)(int, char**))
491 simgrid::s4u::Engine::get_instance()->register_function(name, code);
493 void simgrid_register_default(void (*code)(int, char**))
495 simgrid::s4u::Engine::get_instance()->register_default(code);
497 double simgrid_get_clock()
499 return simgrid::s4u::Engine::get_clock();
502 void simgrid_set_maestro(void (*code)(void*), void* data)
505 XBT_WARN("simgrid_set_maestro is believed to not work on windows. Please help us investigating this issue if "
506 "you need that feature");
508 maestro_code = std::bind(code, data);
510 void SIMIX_set_maestro(void (*code)(void*), void* data) // XBT_ATTRIB_DEPRECATED_v333
512 simgrid_set_maestro(code, data);