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/SplitDuplexLinkImpl.hpp"
19 #include "src/kernel/resource/StandardLinkImpl.hpp"
20 #include "src/mc/mc_replay.hpp"
21 #include "xbt/config.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)");
29 static simgrid::kernel::actor::ActorCode maestro_code;
33 xbt::signal<void()> Engine::on_platform_creation;
34 xbt::signal<void()> Engine::on_platform_created;
35 xbt::signal<void()> Engine::on_simulation_start;
36 xbt::signal<void()> Engine::on_simulation_end;
37 xbt::signal<void(double)> Engine::on_time_advance;
38 xbt::signal<void(void)> Engine::on_deadlock;
40 Engine* Engine::instance_ = nullptr; /* This singleton is awful, but I don't see no other solution right now. */
42 void Engine::initialize(int* argc, char** argv)
44 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
45 Engine::instance_ = this;
47 pimpl->initialize(argc, argv);
48 // Either create a new context with maestro or create
49 // a context object with the current context maestro):
50 kernel::actor::create_maestro(maestro_code);
53 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl())
56 char* argv = &name[0];
57 initialize(&argc, &argv);
60 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
62 initialize(argc, argv);
67 kernel::EngineImpl::shutdown();
68 Engine::instance_ = nullptr;
71 /** @brief Retrieve the engine singleton */
72 Engine* Engine::get_instance()
76 return get_instance(&argc, &argv);
78 Engine* Engine::get_instance(int* argc, char** argv)
80 if (Engine::instance_ == nullptr) {
81 auto e = new Engine(argc, argv);
82 xbt_assert(Engine::instance_ == e);
84 return Engine::instance_;
87 void Engine::shutdown() // XBT_ATTRIB_DEPRECATED_v335
89 delete Engine::instance_;
92 double Engine::get_clock()
94 if (MC_is_active() || MC_record_replay_is_active()) {
95 return MC_process_clock_get(kernel::actor::ActorImpl::self());
97 return kernel::EngineImpl::get_clock();
101 void Engine::add_model(std::shared_ptr<kernel::resource::Model> model,
102 const std::vector<kernel::resource::Model*>& dependencies)
104 kernel::actor::simcall_answered([this, &model, &dependencies] { pimpl->add_model(std::move(model), dependencies); });
107 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
109 return pimpl->get_all_models();
113 * Creates a new platform, including hosts, links, and the routing table.
116 * See also: :ref:`platform`.
119 void Engine::load_platform(const std::string& platf) const
121 pimpl->load_platform(platf);
125 * @brief Seals the platform, finishing the creation of its resources.
127 * This method is optional. The seal() is done automatically when you call Engine::run.
129 void Engine::seal_platform() const
131 pimpl->seal_platform();
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(int, char**)>& code)
137 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
138 return xbt::wrap_main(code, std::move(args));
140 register_function(name, code_factory);
143 /** Registers the main function of an actor that will be launched from the deployment file */
144 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
146 kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
147 return std::bind(std::move(code), std::move(args));
149 register_function(name, code_factory);
151 /** Registers a function as the default main function of actors
153 * It will be used as fallback when the function requested from the deployment file was not registered.
154 * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
156 void Engine::register_default(const std::function<void(int, char**)>& code)
158 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
160 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
162 simgrid::kernel::actor::simcall_answered([this, &code]() { pimpl->register_default(code); });
165 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
167 simgrid::kernel::actor::simcall_answered([this, name, &code]() { pimpl->register_function(name, code); });
170 /** Load a deployment file and launch the actors that it contains
173 * See also: :ref:`deploy`.
176 void Engine::load_deployment(const std::string& deploy) const
178 pimpl->load_deployment(deploy);
181 /** Returns the amount of hosts in the platform */
182 size_t Engine::get_host_count() const
184 return pimpl->hosts_.size();
187 std::vector<Host*> Engine::get_all_hosts() const
189 std::vector<Host*> res;
190 for (auto const& kv : pimpl->hosts_)
191 res.push_back(kv.second);
195 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
197 std::vector<Host*> hosts;
198 for (auto const& kv : pimpl->hosts_) {
199 if (filter(kv.second))
200 hosts.push_back(kv.second);
206 void Engine::host_register(const std::string& name, Host* host)
208 pimpl->hosts_[name] = host;
211 void Engine::host_unregister(const std::string& name)
213 pimpl->hosts_.erase(name);
216 /** @brief Find a host from its name.
218 * @throw std::invalid_argument if the searched host does not exist.
220 Host* Engine::host_by_name(const std::string& name) const
222 auto host = pimpl->hosts_.find(name);
223 if (host == pimpl->hosts_.end())
224 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
228 /** @brief Find a host from its name (or nullptr if that host does not exist) */
229 Host* Engine::host_by_name_or_null(const std::string& name) const
231 auto host = pimpl->hosts_.find(name);
232 return host == pimpl->hosts_.end() ? nullptr : host->second;
235 /** @brief Find a link from its name.
237 * @throw std::invalid_argument if the searched link does not exist.
239 Link* Engine::link_by_name(const std::string& name) const
241 auto* link = link_by_name_or_null(name);
243 throw std::invalid_argument(std::string("Link not found: ") + name);
247 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
249 auto* link_impl = pimpl->netzone_root_ ? pimpl->netzone_root_->get_split_duplex_link_by_name_or_null(name) : nullptr;
251 throw std::invalid_argument(std::string("Link not found: ") + name);
252 return link_impl->get_iface();
255 /** @brief Find a link from its name (or nullptr if that link does not exist) */
256 Link* Engine::link_by_name_or_null(const std::string& name) const
258 Link* link = nullptr;
259 if (pimpl->netzone_root_) {
260 auto* link_impl = pimpl->netzone_root_->get_link_by_name_or_null(name);
262 link = link_impl->get_iface();
267 /** @brief Find a mailbox from its name or create one if it does not exist) */
268 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
270 /* two actors may have pushed the same mbox_create simcall at the same time */
271 kernel::activity::MailboxImpl* mbox = kernel::actor::simcall_answered([&name, this] {
272 auto m = pimpl->mailboxes_.emplace(name, nullptr);
274 m.first->second = new kernel::activity::MailboxImpl(name);
275 XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
277 return m.first->second;
279 return mbox->get_iface();
282 void Engine::link_register(const std::string& name, const Link* link)
284 // pimpl->links_[name] = link->get_impl(); //FIXME
287 void Engine::link_unregister(const std::string& name)
289 // pimpl->links_.erase(name); FIXME
292 /** @brief Returns the amount of links in the platform */
293 size_t Engine::get_link_count() const
296 if (pimpl->netzone_root_) {
297 count += pimpl->netzone_root_->get_link_count();
298 /* keep behavior where internal __loopback__ link from network model is given to user */
299 count += pimpl->netzone_root_->get_network_model()->loopback_ ? 1 : 0;
304 /** @brief Returns the list of all links found in the platform */
305 std::vector<Link*> Engine::get_all_links() const
307 return get_filtered_links([](Link*) { return true; });
310 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
312 std::vector<Link*> res;
313 if (pimpl->netzone_root_) {
314 res = pimpl->netzone_root_->get_filtered_links(filter);
315 /* keep behavior where internal __loopback__ link from network model is given to user */
316 if (pimpl->netzone_root_->get_network_model()->loopback_ &&
317 filter(pimpl->netzone_root_->get_network_model()->loopback_->get_iface()))
318 res.push_back(pimpl->netzone_root_->get_network_model()->loopback_->get_iface());
323 size_t Engine::get_actor_count() const
325 return pimpl->get_actor_count();
328 std::vector<ActorPtr> Engine::get_all_actors() const
330 std::vector<ActorPtr> actor_list;
331 for (auto const& kv : pimpl->get_actor_list()) {
332 actor_list.push_back(kv.second->get_iface());
337 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
339 std::vector<ActorPtr> actor_list;
340 for (auto const& kv : pimpl->get_actor_list()) {
341 if (filter(kv.second->get_iface()))
342 actor_list.push_back(kv.second->get_iface());
347 void Engine::run() const
351 void Engine::run_until(double max_date) const
353 static bool callback_called = false;
354 if (not callback_called) {
355 on_simulation_start();
356 callback_called = true;
358 /* Clean IO before the run */
362 pimpl->run(max_date);
365 void Engine::track_vetoed_activities(std::set<Activity*>* vetoed_activities) const
367 Activity::set_vetoed_activities(vetoed_activities);
370 /** @brief Retrieve the root netzone, containing all others */
371 s4u::NetZone* Engine::get_netzone_root() const
373 if (pimpl->netzone_root_)
374 return pimpl->netzone_root_->get_iface();
377 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
378 void Engine::set_netzone_root(const s4u::NetZone* netzone)
380 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
381 pimpl->netzone_root_ = netzone->get_impl();
384 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
386 if (current->get_name() == name)
389 for (auto const& elem : current->get_children()) {
390 NetZone* tmp = netzone_by_name_recursive(elem, name);
391 if (tmp != nullptr) {
398 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
399 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
401 return netzone_by_name_recursive(get_netzone_root(), name);
404 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
405 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
407 auto netp = pimpl->netpoints_.find(name);
408 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
411 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
413 auto netp = netpoint_by_name_or_null(name);
414 if (netp == nullptr) {
415 throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
420 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
422 std::vector<kernel::routing::NetPoint*> res;
423 for (auto const& kv : pimpl->netpoints_)
424 res.push_back(kv.second);
428 /** @brief Register a new netpoint to the system */
429 void Engine::netpoint_register(kernel::routing::NetPoint* point)
431 simgrid::kernel::actor::simcall_answered([this, point] { pimpl->netpoints_[point->get_name()] = point; });
434 /** @brief Unregister a given netpoint */
435 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
437 kernel::actor::simcall_answered([this, point] {
438 pimpl->netpoints_.erase(point->get_name());
443 bool Engine::is_initialized()
445 return Engine::instance_ != nullptr;
447 void Engine::set_config(const std::string& str)
449 config::set_parse(str);
451 void Engine::set_config(const std::string& name, int value)
453 config::set_value(name.c_str(), value);
455 void Engine::set_config(const std::string& name, double value)
457 config::set_value(name.c_str(), value);
459 void Engine::set_config(const std::string& name, bool value)
461 config::set_value(name.c_str(), value);
463 void Engine::set_config(const std::string& name, const std::string& value)
465 config::set_value(name.c_str(), value);
468 Engine* Engine::set_default_comm_data_copy_callback(
469 const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback)
471 kernel::activity::CommImpl::set_copy_data_callback(callback);
476 } // namespace simgrid
478 /* **************************** Public C interface *************************** */
479 void simgrid_init(int* argc, char** argv)
481 static simgrid::s4u::Engine e(argc, argv);
483 void simgrid_load_platform(const char* file)
485 simgrid::s4u::Engine::get_instance()->load_platform(file);
488 void simgrid_load_deployment(const char* file)
490 simgrid::s4u::Engine::get_instance()->load_deployment(file);
494 simgrid::s4u::Engine::get_instance()->run();
496 void simgrid_run_until(double max_date)
498 simgrid::s4u::Engine::get_instance()->run_until(max_date);
500 void simgrid_register_function(const char* name, void (*code)(int, char**))
502 simgrid::s4u::Engine::get_instance()->register_function(name, code);
504 void simgrid_register_default(void (*code)(int, char**))
506 simgrid::s4u::Engine::get_instance()->register_default(code);
508 double simgrid_get_clock()
510 return simgrid::s4u::Engine::get_clock();
513 void simgrid_set_maestro(void (*code)(void*), void* data)
516 XBT_WARN("simgrid_set_maestro is believed to not work on windows. Please help us investigating this issue if "
517 "you need that feature");
519 maestro_code = std::bind(code, data);
521 void SIMIX_set_maestro(void (*code)(void*), void* data) // XBT_ATTRIB_DEPRECATED_v333
523 simgrid_set_maestro(code, data);