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 "xbt/config.hpp"
27 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
30 static simgrid::kernel::actor::ActorCode maestro_code;
34 xbt::signal<void()> Engine::on_platform_creation;
35 xbt::signal<void()> Engine::on_platform_created;
36 xbt::signal<void()> Engine::on_simulation_start;
37 xbt::signal<void()> Engine::on_simulation_end;
38 xbt::signal<void(double)> Engine::on_time_advance;
39 xbt::signal<void(void)> Engine::on_deadlock;
41 Engine* Engine::instance_ = nullptr; /* This singleton is awful, but I don't see no other solution right now. */
43 void Engine::initialize(int* argc, char** argv)
45 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
46 Engine::instance_ = this;
48 pimpl->initialize(argc, argv);
49 // Either create a new context with maestro or create
50 // a context object with the current context maestro):
51 kernel::actor::create_maestro(maestro_code);
54 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl())
57 char* argv = &name[0];
58 initialize(&argc, &argv);
61 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
63 initialize(argc, argv);
68 kernel::EngineImpl::shutdown();
69 Engine::instance_ = nullptr;
72 /** @brief Retrieve the engine singleton */
73 Engine* Engine::get_instance()
77 return get_instance(&argc, &argv);
79 Engine* Engine::get_instance(int* argc, char** argv)
81 if (Engine::instance_ == nullptr) {
82 auto e = new Engine(argc, argv);
83 xbt_assert(Engine::instance_ == e);
85 return Engine::instance_;
88 void Engine::shutdown() // XBT_ATTRIB_DEPRECATED_v335
90 delete Engine::instance_;
93 double Engine::get_clock()
95 if (MC_is_active() || MC_record_replay_is_active()) {
96 return MC_process_clock_get(kernel::actor::ActorImpl::self());
98 return kernel::EngineImpl::get_clock();
102 void Engine::add_model(std::shared_ptr<kernel::resource::Model> model,
103 const std::vector<kernel::resource::Model*>& dependencies)
105 kernel::actor::simcall_answered([this, &model, &dependencies] { pimpl->add_model(std::move(model), dependencies); });
108 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
110 return pimpl->get_all_models();
114 * Creates a new platform, including hosts, links, and the routing table.
117 * See also: :ref:`platform`.
120 void Engine::load_platform(const std::string& platf) const
122 pimpl->load_platform(platf);
126 * @brief Seals the platform, finishing the creation of its resources.
128 * This method is optional. The seal() is done automatically when you call Engine::run.
130 void Engine::seal_platform() const
132 pimpl->seal_platform();
135 /** Registers the main function of an actor that will be launched from the deployment file */
136 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
138 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
139 return xbt::wrap_main(code, std::move(args));
141 register_function(name, code_factory);
144 /** Registers the main function of an actor that will be launched from the deployment file */
145 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
147 kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
148 return std::bind(std::move(code), std::move(args));
150 register_function(name, code_factory);
152 /** Registers a function as the default main function of actors
154 * It will be used as fallback when the function requested from the deployment file was not registered.
155 * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
157 void Engine::register_default(const std::function<void(int, char**)>& code)
159 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
161 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
163 simgrid::kernel::actor::simcall_answered([this, &code]() { pimpl->register_default(code); });
166 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
168 simgrid::kernel::actor::simcall_answered([this, name, &code]() { pimpl->register_function(name, code); });
171 /** Load a deployment file and launch the actors that it contains
174 * See also: :ref:`deploy`.
177 void Engine::load_deployment(const std::string& deploy) const
179 pimpl->load_deployment(deploy);
182 /** Returns the amount of hosts in the platform */
183 size_t Engine::get_host_count() const
185 return pimpl->hosts_.size();
188 std::vector<Host*> Engine::get_all_hosts() const
190 std::vector<Host*> res;
191 for (auto const& kv : pimpl->hosts_)
192 res.push_back(kv.second);
196 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
198 std::vector<Host*> hosts;
199 for (auto const& kv : pimpl->hosts_) {
200 if (filter(kv.second))
201 hosts.push_back(kv.second);
207 void Engine::host_register(const std::string& name, Host* host)
209 pimpl->hosts_[name] = host;
212 void Engine::host_unregister(const std::string& name)
214 pimpl->hosts_.erase(name);
217 /** @brief Find a host from its name.
219 * @throw std::invalid_argument if the searched host does not exist.
221 Host* Engine::host_by_name(const std::string& name) const
223 auto host = pimpl->hosts_.find(name);
224 if (host == pimpl->hosts_.end())
225 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
229 /** @brief Find a host from its name (or nullptr if that host does not exist) */
230 Host* Engine::host_by_name_or_null(const std::string& name) const
232 auto host = pimpl->hosts_.find(name);
233 return host == pimpl->hosts_.end() ? nullptr : host->second;
236 /** @brief Find a link from its name.
238 * @throw std::invalid_argument if the searched link does not exist.
240 Link* Engine::link_by_name(const std::string& name) const
242 auto* link = link_by_name_or_null(name);
244 throw std::invalid_argument(std::string("Link not found: ") + name);
248 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
250 auto* link_impl = pimpl->netzone_root_ ? pimpl->netzone_root_->get_split_duplex_link_by_name_or_null(name) : nullptr;
252 throw std::invalid_argument(std::string("Link not found: ") + name);
253 return link_impl->get_iface();
256 /** @brief Find a link from its name (or nullptr if that link does not exist) */
257 Link* Engine::link_by_name_or_null(const std::string& name) const
259 Link* link = nullptr;
260 if (pimpl->netzone_root_) {
261 /* keep behavior where internal __loopback__ link from network model is given to user */
262 if (name == "__loopback__")
263 return pimpl->netzone_root_->get_network_model()->loopback_->get_iface();
264 auto* link_impl = pimpl->netzone_root_->get_link_by_name_or_null(name);
266 link = link_impl->get_iface();
271 /** @brief Find a mailbox from its name or create one if it does not exist) */
272 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
274 /* two actors may have pushed the same mbox_create simcall at the same time */
275 kernel::activity::MailboxImpl* mbox = kernel::actor::simcall_answered([&name, this] {
276 auto m = pimpl->mailboxes_.emplace(name, nullptr);
278 m.first->second = new kernel::activity::MailboxImpl(name);
279 XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
281 return m.first->second;
283 return mbox->get_iface();
286 /** @brief Returns the amount of links in the platform */
287 size_t Engine::get_link_count() const
290 if (pimpl->netzone_root_) {
291 count += pimpl->netzone_root_->get_link_count();
292 /* keep behavior where internal __loopback__ link from network model is given to user */
293 count += pimpl->netzone_root_->get_network_model()->loopback_ ? 1 : 0;
298 /** @brief Returns the list of all links found in the platform */
299 std::vector<Link*> Engine::get_all_links() const
301 return get_filtered_links([](const Link*) { return true; });
304 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
306 std::vector<Link*> res;
307 if (pimpl->netzone_root_) {
308 res = pimpl->netzone_root_->get_filtered_links(filter);
309 /* keep behavior where internal __loopback__ link from network model is given to user */
310 if (pimpl->netzone_root_->get_network_model()->loopback_ &&
311 filter(pimpl->netzone_root_->get_network_model()->loopback_->get_iface()))
312 res.push_back(pimpl->netzone_root_->get_network_model()->loopback_->get_iface());
317 size_t Engine::get_actor_count() const
319 return pimpl->get_actor_count();
322 std::vector<ActorPtr> Engine::get_all_actors() const
324 std::vector<ActorPtr> actor_list;
325 for (auto const& kv : pimpl->get_actor_list()) {
326 actor_list.push_back(kv.second->get_iface());
331 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
333 std::vector<ActorPtr> actor_list;
334 for (auto const& kv : pimpl->get_actor_list()) {
335 if (filter(kv.second->get_iface()))
336 actor_list.push_back(kv.second->get_iface());
341 void Engine::run() const
345 void Engine::run_until(double max_date) const
347 static bool callback_called = false;
348 if (not callback_called) {
349 on_simulation_start();
350 callback_called = true;
352 /* Clean IO before the run */
356 pimpl->run(max_date);
359 void Engine::track_vetoed_activities(std::set<Activity*>* vetoed_activities) const
361 Activity::set_vetoed_activities(vetoed_activities);
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_answered([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_answered([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);
462 Engine* Engine::set_default_comm_data_copy_callback(
463 const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback)
465 kernel::activity::CommImpl::set_copy_data_callback(callback);
470 } // namespace simgrid
472 /* **************************** Public C interface *************************** */
473 void simgrid_init(int* argc, char** argv)
475 static simgrid::s4u::Engine e(argc, argv);
477 void simgrid_load_platform(const char* file)
479 simgrid::s4u::Engine::get_instance()->load_platform(file);
482 void simgrid_load_deployment(const char* file)
484 simgrid::s4u::Engine::get_instance()->load_deployment(file);
488 simgrid::s4u::Engine::get_instance()->run();
490 void simgrid_run_until(double max_date)
492 simgrid::s4u::Engine::get_instance()->run_until(max_date);
494 void simgrid_register_function(const char* name, void (*code)(int, char**))
496 simgrid::s4u::Engine::get_instance()->register_function(name, code);
498 void simgrid_register_default(void (*code)(int, char**))
500 simgrid::s4u::Engine::get_instance()->register_default(code);
502 double simgrid_get_clock()
504 return simgrid::s4u::Engine::get_clock();
507 void simgrid_set_maestro(void (*code)(void*), void* data)
510 XBT_WARN("simgrid_set_maestro is believed to not work on windows. Please help us investigating this issue if "
511 "you need that feature");
513 maestro_code = std::bind(code, data);
515 void SIMIX_set_maestro(void (*code)(void*), void* data) // XBT_ATTRIB_DEPRECATED_v333
517 simgrid_set_maestro(code, data);