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 auto* link_impl = pimpl->netzone_root_->get_link_by_name_or_null(name);
263 link = link_impl->get_iface();
268 /** @brief Find a mailbox from its name or create one if it does not exist) */
269 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
271 /* two actors may have pushed the same mbox_create simcall at the same time */
272 kernel::activity::MailboxImpl* mbox = kernel::actor::simcall_answered([&name, this] {
273 auto m = pimpl->mailboxes_.emplace(name, nullptr);
275 m.first->second = new kernel::activity::MailboxImpl(name);
276 XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
278 return m.first->second;
280 return mbox->get_iface();
283 /** @brief Returns the amount of links in the platform */
284 size_t Engine::get_link_count() const
287 if (pimpl->netzone_root_) {
288 count += pimpl->netzone_root_->get_link_count();
289 /* keep behavior where internal __loopback__ link from network model is given to user */
290 count += pimpl->netzone_root_->get_network_model()->loopback_ ? 1 : 0;
295 /** @brief Returns the list of all links found in the platform */
296 std::vector<Link*> Engine::get_all_links() const
298 return get_filtered_links([](Link*) { return true; });
301 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
303 std::vector<Link*> res;
304 if (pimpl->netzone_root_) {
305 res = pimpl->netzone_root_->get_filtered_links(filter);
306 /* keep behavior where internal __loopback__ link from network model is given to user */
307 if (pimpl->netzone_root_->get_network_model()->loopback_ &&
308 filter(pimpl->netzone_root_->get_network_model()->loopback_->get_iface()))
309 res.push_back(pimpl->netzone_root_->get_network_model()->loopback_->get_iface());
314 size_t Engine::get_actor_count() const
316 return pimpl->get_actor_count();
319 std::vector<ActorPtr> Engine::get_all_actors() const
321 std::vector<ActorPtr> actor_list;
322 for (auto const& kv : pimpl->get_actor_list()) {
323 actor_list.push_back(kv.second->get_iface());
328 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
330 std::vector<ActorPtr> actor_list;
331 for (auto const& kv : pimpl->get_actor_list()) {
332 if (filter(kv.second->get_iface()))
333 actor_list.push_back(kv.second->get_iface());
338 void Engine::run() const
342 void Engine::run_until(double max_date) const
344 static bool callback_called = false;
345 if (not callback_called) {
346 on_simulation_start();
347 callback_called = true;
349 /* Clean IO before the run */
353 pimpl->run(max_date);
356 void Engine::track_vetoed_activities(std::set<Activity*>* vetoed_activities) const
358 Activity::set_vetoed_activities(vetoed_activities);
361 /** @brief Retrieve the root netzone, containing all others */
362 s4u::NetZone* Engine::get_netzone_root() const
364 if (pimpl->netzone_root_)
365 return pimpl->netzone_root_->get_iface();
368 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
369 void Engine::set_netzone_root(const s4u::NetZone* netzone)
371 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
372 pimpl->netzone_root_ = netzone->get_impl();
375 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
377 if (current->get_name() == name)
380 for (auto const& elem : current->get_children()) {
381 NetZone* tmp = netzone_by_name_recursive(elem, name);
382 if (tmp != nullptr) {
389 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
390 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
392 return netzone_by_name_recursive(get_netzone_root(), name);
395 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
396 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
398 auto netp = pimpl->netpoints_.find(name);
399 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
402 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
404 auto netp = netpoint_by_name_or_null(name);
405 if (netp == nullptr) {
406 throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
411 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
413 std::vector<kernel::routing::NetPoint*> res;
414 for (auto const& kv : pimpl->netpoints_)
415 res.push_back(kv.second);
419 /** @brief Register a new netpoint to the system */
420 void Engine::netpoint_register(kernel::routing::NetPoint* point)
422 simgrid::kernel::actor::simcall_answered([this, point] { pimpl->netpoints_[point->get_name()] = point; });
425 /** @brief Unregister a given netpoint */
426 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
428 kernel::actor::simcall_answered([this, point] {
429 pimpl->netpoints_.erase(point->get_name());
434 bool Engine::is_initialized()
436 return Engine::instance_ != nullptr;
438 void Engine::set_config(const std::string& str)
440 config::set_parse(str);
442 void Engine::set_config(const std::string& name, int value)
444 config::set_value(name.c_str(), value);
446 void Engine::set_config(const std::string& name, double value)
448 config::set_value(name.c_str(), value);
450 void Engine::set_config(const std::string& name, bool value)
452 config::set_value(name.c_str(), value);
454 void Engine::set_config(const std::string& name, const std::string& value)
456 config::set_value(name.c_str(), value);
459 Engine* Engine::set_default_comm_data_copy_callback(
460 const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback)
462 kernel::activity::CommImpl::set_copy_data_callback(callback);
467 } // namespace simgrid
469 /* **************************** Public C interface *************************** */
470 void simgrid_init(int* argc, char** argv)
472 static simgrid::s4u::Engine e(argc, argv);
474 void simgrid_load_platform(const char* file)
476 simgrid::s4u::Engine::get_instance()->load_platform(file);
479 void simgrid_load_deployment(const char* file)
481 simgrid::s4u::Engine::get_instance()->load_deployment(file);
485 simgrid::s4u::Engine::get_instance()->run();
487 void simgrid_run_until(double max_date)
489 simgrid::s4u::Engine::get_instance()->run_until(max_date);
491 void simgrid_register_function(const char* name, void (*code)(int, char**))
493 simgrid::s4u::Engine::get_instance()->register_function(name, code);
495 void simgrid_register_default(void (*code)(int, char**))
497 simgrid::s4u::Engine::get_instance()->register_default(code);
499 double simgrid_get_clock()
501 return simgrid::s4u::Engine::get_clock();
504 void simgrid_set_maestro(void (*code)(void*), void* data)
507 XBT_WARN("simgrid_set_maestro is believed to not work on windows. Please help us investigating this issue if "
508 "you need that feature");
510 maestro_code = std::bind(code, data);
512 void SIMIX_set_maestro(void (*code)(void*), void* data) // XBT_ATTRIB_DEPRECATED_v333
514 simgrid_set_maestro(code, data);