1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2021. 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>
13 #include "src/instr/instr_private.hpp"
14 #include "src/kernel/EngineImpl.hpp"
15 #include "src/mc/mc_replay.hpp"
16 #include "surf/surf.hpp"
21 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
24 static simgrid::kernel::actor::ActorCode maestro_code;
28 xbt::signal<void()> Engine::on_platform_creation;
29 xbt::signal<void()> Engine::on_platform_created;
30 xbt::signal<void()> Engine::on_simulation_end;
31 xbt::signal<void(double)> Engine::on_time_advance;
32 xbt::signal<void(void)> Engine::on_deadlock;
34 Engine* Engine::instance_ = nullptr; /* This singleton is awful, but I don't see no other solution right now. */
36 void Engine::initialize(int* argc, char** argv)
38 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
39 Engine::instance_ = this;
41 pimpl->initialize(argc, argv);
42 // Either create a new context with maestro or create
43 // a context object with the current context maestro):
44 kernel::actor::create_maestro(maestro_code);
47 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl())
50 char* argv = &name[0];
51 initialize(&argc, &argv);
54 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
56 initialize(argc, argv);
61 kernel::EngineImpl::shutdown();
62 Engine::instance_ = nullptr;
65 /** @brief Retrieve the engine singleton */
66 Engine* Engine::get_instance()
68 return get_instance(nullptr, nullptr);
70 Engine* Engine::get_instance(int* argc, char** argv)
72 if (Engine::instance_ == nullptr) {
73 auto e = new Engine(argc, argv);
74 xbt_assert(Engine::instance_ == e);
76 return Engine::instance_;
79 void Engine::shutdown()
81 delete Engine::instance_;
84 double Engine::get_clock()
86 if (MC_is_active() || MC_record_replay_is_active()) {
87 return MC_process_clock_get(kernel::actor::ActorImpl::self());
89 return surf_get_clock();
93 void Engine::add_model(std::shared_ptr<kernel::resource::Model> model,
94 const std::vector<kernel::resource::Model*>& dependencies)
96 kernel::actor::simcall([this, &model, &dependencies] { pimpl->add_model(std::move(model), dependencies); });
99 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
101 return pimpl->get_all_models();
105 * Creates a new platform, including hosts, links, and the routing table.
108 * See also: :ref:`platform`.
111 void Engine::load_platform(const std::string& platf) const
113 pimpl->load_platform(platf);
116 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v330
118 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
119 return xbt::wrap_main(code, std::move(args));
121 register_function(name, code_factory);
123 void Engine::register_default(int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v330
125 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
128 /** Registers the main function of an actor that will be launched from the deployment file */
129 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
131 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
132 return xbt::wrap_main(code, std::move(args));
134 register_function(name, code_factory);
137 /** Registers the main function of an actor that will be launched from the deployment file */
138 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
140 kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
141 return std::bind(std::move(code), std::move(args));
143 register_function(name, code_factory);
145 /** Registers a function as the default main function of actors
147 * It will be used as fallback when the function requested from the deployment file was not registered.
148 * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
150 void Engine::register_default(const std::function<void(int, char**)>& code)
152 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
154 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
156 simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
159 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
161 simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
164 /** Load a deployment file and launch the actors that it contains
167 * See also: :ref:`deploy`.
170 void Engine::load_deployment(const std::string& deploy) const
172 pimpl->load_deployment(deploy);
175 /** Returns the amount of hosts in the platform */
176 size_t Engine::get_host_count() const
178 return pimpl->hosts_.size();
181 std::vector<Host*> Engine::get_all_hosts() const
183 std::vector<Host*> res;
184 for (auto const& kv : pimpl->hosts_)
185 res.push_back(kv.second);
189 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
191 std::vector<Host*> hosts;
192 for (auto const& kv : pimpl->hosts_) {
193 if (filter(kv.second))
194 hosts.push_back(kv.second);
200 void Engine::host_register(const std::string& name, Host* host)
202 pimpl->hosts_[name] = host;
205 void Engine::host_unregister(const std::string& name)
207 pimpl->hosts_.erase(name);
210 /** @brief Find a host from its name.
212 * @throw std::invalid_argument if the searched host does not exist.
214 Host* Engine::host_by_name(const std::string& name) const
216 auto host = pimpl->hosts_.find(name);
217 if (host == pimpl->hosts_.end())
218 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
222 /** @brief Find a host from its name (or nullptr if that host does not exist) */
223 Host* Engine::host_by_name_or_null(const std::string& name) const
225 auto host = pimpl->hosts_.find(name);
226 return host == pimpl->hosts_.end() ? nullptr : host->second;
229 /** @brief Find a link from its name.
231 * @throw std::invalid_argument if the searched link does not exist.
233 Link* Engine::link_by_name(const std::string& name) const
235 auto link = pimpl->links_.find(name);
236 if (link == pimpl->links_.end())
237 throw std::invalid_argument(std::string("Link not found: ") + name);
238 return link->second->get_iface();
241 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
243 auto link = pimpl->split_duplex_links_.find(name);
244 if (link == pimpl->split_duplex_links_.end())
245 throw std::invalid_argument(std::string("Link not found: ") + name);
246 return link->second->get_iface();
249 /** @brief Find a link from its name (or nullptr if that link does not exist) */
250 Link* Engine::link_by_name_or_null(const std::string& name) const
252 auto link = pimpl->links_.find(name);
253 return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
256 /** @brief Find a mailox from its name or create one if it does not exist) */
257 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
259 /* two actors may have pushed the same mbox_create simcall at the same time */
260 kernel::activity::MailboxImpl* mbox = kernel::actor::simcall([&name, this] {
261 auto m = pimpl->mailboxes_.emplace(name, nullptr);
263 m.first->second = new kernel::activity::MailboxImpl(name);
264 XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
266 return m.first->second;
268 return mbox->get_iface();
271 void Engine::link_register(const std::string& name, const Link* link)
273 pimpl->links_[name] = link->get_impl();
276 void Engine::link_unregister(const std::string& name)
278 pimpl->links_.erase(name);
281 /** @brief Returns the amount of links in the platform */
282 size_t Engine::get_link_count() const
284 return pimpl->links_.size();
287 /** @brief Returns the list of all links found in the platform */
288 std::vector<Link*> Engine::get_all_links() const
290 std::vector<Link*> res;
291 for (auto const& kv : pimpl->links_)
292 res.push_back(kv.second->get_iface());
296 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
298 std::vector<Link*> filtered_list;
299 for (auto const& kv : pimpl->links_) {
300 Link* l = kv.second->get_iface();
302 filtered_list.push_back(l);
304 return filtered_list;
307 size_t Engine::get_actor_count() const
309 return pimpl->get_actor_count();
312 std::vector<ActorPtr> Engine::get_all_actors() const
314 std::vector<ActorPtr> actor_list;
315 for (auto const& kv : pimpl->get_actor_list()) {
316 actor_list.push_back(kv.second->get_iface());
321 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
323 std::vector<ActorPtr> actor_list;
324 for (auto const& kv : pimpl->get_actor_list()) {
325 if (filter(kv.second->get_iface()))
326 actor_list.push_back(kv.second->get_iface());
331 void Engine::run() const
333 /* sealing resources before run: links */
334 for (auto* link : get_all_links())
336 /* seal netzone root, recursively seal children netzones, hosts and disks */
337 get_netzone_root()->seal();
339 /* Clean IO before the run */
343 if (MC_is_active()) {
350 /** @brief Retrieve the root netzone, containing all others */
351 s4u::NetZone* Engine::get_netzone_root() const
353 if (pimpl->netzone_root_)
354 return pimpl->netzone_root_->get_iface();
357 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
358 void Engine::set_netzone_root(const s4u::NetZone* netzone)
360 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
361 pimpl->netzone_root_ = netzone->get_impl();
364 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
366 if (current->get_name() == name)
369 for (auto const& elem : current->get_children()) {
370 NetZone* tmp = netzone_by_name_recursive(elem, name);
371 if (tmp != nullptr) {
378 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
379 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
381 return netzone_by_name_recursive(get_netzone_root(), name);
384 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
385 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
387 auto netp = pimpl->netpoints_.find(name);
388 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
391 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
393 auto netp = netpoint_by_name_or_null(name);
394 if (netp == nullptr) {
395 throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
400 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
402 std::vector<kernel::routing::NetPoint*> res;
403 for (auto const& kv : pimpl->netpoints_)
404 res.push_back(kv.second);
408 /** @brief Register a new netpoint to the system */
409 void Engine::netpoint_register(kernel::routing::NetPoint* point)
411 simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
414 /** @brief Unregister a given netpoint */
415 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
417 kernel::actor::simcall([this, point] {
418 pimpl->netpoints_.erase(point->get_name());
423 bool Engine::is_initialized()
425 return Engine::instance_ != nullptr;
427 void Engine::set_config(const std::string& str)
429 config::set_parse(str);
431 void Engine::set_config(const std::string& name, int value)
433 config::set_value(name.c_str(), value);
435 void Engine::set_config(const std::string& name, double value)
437 config::set_value(name.c_str(), value);
439 void Engine::set_config(const std::string& name, bool value)
441 config::set_value(name.c_str(), value);
443 void Engine::set_config(const std::string& name, const std::string& value)
445 config::set_value(name.c_str(), value);
448 Engine* Engine::set_default_comm_data_copy_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t))
450 kernel::activity::CommImpl::set_copy_data_callback(callback);
454 } // namespace simgrid
456 double SIMIX_get_clock() // XBT_ATTRIB_DEPRECATED_v332
458 return simgrid::s4u::Engine::get_clock();
461 /* **************************** Public C interface *************************** */
462 void simgrid_init(int* argc, char** argv)
464 static simgrid::s4u::Engine e(argc, argv);
466 void simgrid_load_platform(const char* file)
468 simgrid::s4u::Engine::get_instance()->load_platform(file);
471 void simgrid_load_deployment(const char* file)
473 simgrid::s4u::Engine::get_instance()->load_deployment(file);
477 simgrid::s4u::Engine::get_instance()->run();
479 void simgrid_register_function(const char* name, void (*code)(int, char**))
481 simgrid::s4u::Engine::get_instance()->register_function(name, code);
483 void simgrid_register_default(void (*code)(int, char**))
485 simgrid::s4u::Engine::get_instance()->register_default(code);
487 double simgrid_get_clock()
489 return simgrid::s4u::Engine::get_clock();
492 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
494 return simgrid::s4u::Engine::get_instance()->get_actor_count();
497 void simgrid_set_maestro(void (*code)(void*), void* data)
500 XBT_WARN("simgrid_set_maestro is believed to not work on windows. Please help us investigating this issue if "
501 "you need that feature");
503 maestro_code = std::bind(code, data);
505 void SIMIX_set_maestro(void (*code)(void*), void* data) // XBT_ATTRIB_DEPRECATED_v333
507 simgrid_set_maestro(code, data);