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. */
9 #include "simgrid/kernel/routing/NetPoint.hpp"
10 #include "simgrid/kernel/routing/NetZoneImpl.hpp"
11 #include "simgrid/s4u/Disk.hpp"
12 #include "simgrid/s4u/Engine.hpp"
13 #include "simgrid/s4u/Host.hpp"
14 #include "simgrid/s4u/Mailbox.hpp"
15 #include "simgrid/s4u/NetZone.hpp"
16 #include "simgrid/simix.h"
17 #include "src/instr/instr_private.hpp"
18 #include "src/kernel/EngineImpl.hpp"
19 #include "src/kernel/activity/CommImpl.hpp"
20 #include "src/kernel/actor/ActorImpl.hpp"
21 #include "src/mc/mc_replay.hpp"
22 #include "src/surf/network_interface.hpp"
23 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
24 #include <simgrid/Exception.hpp>
29 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
32 static simgrid::kernel::actor::ActorCode maestro_code;
36 xbt::signal<void()> Engine::on_platform_creation;
37 xbt::signal<void()> Engine::on_platform_created;
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);
70 Engine::instance_ = nullptr;
73 /** @brief Retrieve the engine singleton */
74 Engine* Engine::get_instance()
76 return get_instance(nullptr, nullptr);
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()
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 surf_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([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);
124 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v330
126 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
127 return xbt::wrap_main(code, std::move(args));
129 register_function(name, code_factory);
131 void Engine::register_default(int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v330
133 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
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([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([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 pimpl->hosts_.size();
189 std::vector<Host*> Engine::get_all_hosts() const
191 std::vector<Host*> res;
192 for (auto const& kv : pimpl->hosts_)
193 res.push_back(kv.second);
197 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
199 std::vector<Host*> hosts;
200 for (auto const& kv : pimpl->hosts_) {
201 if (filter(kv.second))
202 hosts.push_back(kv.second);
208 void Engine::host_register(const std::string& name, Host* host)
210 pimpl->hosts_[name] = host;
213 void Engine::host_unregister(const std::string& name)
215 pimpl->hosts_.erase(name);
218 /** @brief Find a host from its name.
220 * @throw std::invalid_argument if the searched host does not exist.
222 Host* Engine::host_by_name(const std::string& name) const
224 auto host = pimpl->hosts_.find(name);
225 if (host == pimpl->hosts_.end())
226 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
230 /** @brief Find a host from its name (or nullptr if that host does not exist) */
231 Host* Engine::host_by_name_or_null(const std::string& name) const
233 auto host = pimpl->hosts_.find(name);
234 return host == pimpl->hosts_.end() ? nullptr : host->second;
237 /** @brief Find a link from its name.
239 * @throw std::invalid_argument if the searched link does not exist.
241 Link* Engine::link_by_name(const std::string& name) const
243 auto link = pimpl->links_.find(name);
244 if (link == pimpl->links_.end())
245 throw std::invalid_argument(std::string("Link not found: ") + name);
246 return link->second->get_iface();
249 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
251 auto link = pimpl->split_duplex_links_.find(name);
252 if (link == pimpl->split_duplex_links_.end())
253 throw std::invalid_argument(std::string("Link not found: ") + name);
254 return link->second->get_iface();
257 /** @brief Find a link from its name (or nullptr if that link does not exist) */
258 Link* Engine::link_by_name_or_null(const std::string& name) const
260 auto link = pimpl->links_.find(name);
261 return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
264 /** @brief Find a mailox from its name or create one if it does not exist) */
265 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
267 /* two actors may have pushed the same mbox_create simcall at the same time */
268 kernel::activity::MailboxImpl* mbox = kernel::actor::simcall([&name, this] {
269 auto m = pimpl->mailboxes_.emplace(name, nullptr);
271 m.first->second = new kernel::activity::MailboxImpl(name);
272 XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
274 return m.first->second;
276 return mbox->get_iface();
279 void Engine::link_register(const std::string& name, const Link* link)
281 pimpl->links_[name] = link->get_impl();
284 void Engine::link_unregister(const std::string& name)
286 pimpl->links_.erase(name);
289 /** @brief Returns the amount of links in the platform */
290 size_t Engine::get_link_count() const
292 return pimpl->links_.size();
295 /** @brief Returns the list of all links found in the platform */
296 std::vector<Link*> Engine::get_all_links() const
298 std::vector<Link*> res;
299 for (auto const& kv : pimpl->links_)
300 res.push_back(kv.second->get_iface());
304 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
306 std::vector<Link*> filtered_list;
307 for (auto const& kv : pimpl->links_) {
308 Link* l = kv.second->get_iface();
310 filtered_list.push_back(l);
312 return filtered_list;
315 size_t Engine::get_actor_count() const
317 return pimpl->get_actor_count();
320 std::vector<ActorPtr> Engine::get_all_actors() const
322 std::vector<ActorPtr> actor_list;
323 for (auto const& kv : pimpl->get_actor_list()) {
324 actor_list.push_back(kv.second->get_iface());
329 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
331 std::vector<ActorPtr> actor_list;
332 for (auto const& kv : pimpl->get_actor_list()) {
333 if (filter(kv.second->get_iface()))
334 actor_list.push_back(kv.second->get_iface());
339 void Engine::run() const
341 /* sealing resources before run: links */
342 for (auto* link : get_all_links())
344 /* seal netzone root, recursively seal children netzones, hosts and disks */
345 get_netzone_root()->seal();
347 /* Clean IO before the run */
351 if (MC_is_active()) {
358 /** @brief Retrieve the root netzone, containing all others */
359 s4u::NetZone* Engine::get_netzone_root() const
361 if (pimpl->netzone_root_)
362 return pimpl->netzone_root_->get_iface();
365 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
366 void Engine::set_netzone_root(const s4u::NetZone* netzone)
368 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
369 pimpl->netzone_root_ = netzone->get_impl();
372 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
374 if (current->get_name() == name)
377 for (auto const& elem : current->get_children()) {
378 NetZone* tmp = netzone_by_name_recursive(elem, name);
379 if (tmp != nullptr) {
386 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
387 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
389 return netzone_by_name_recursive(get_netzone_root(), name);
392 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
393 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
395 auto netp = pimpl->netpoints_.find(name);
396 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
399 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
401 auto netp = netpoint_by_name_or_null(name);
402 if (netp == nullptr) {
403 throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
408 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
410 std::vector<kernel::routing::NetPoint*> res;
411 for (auto const& kv : pimpl->netpoints_)
412 res.push_back(kv.second);
416 /** @brief Register a new netpoint to the system */
417 void Engine::netpoint_register(kernel::routing::NetPoint* point)
419 simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
422 /** @brief Unregister a given netpoint */
423 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
425 kernel::actor::simcall([this, point] {
426 pimpl->netpoints_.erase(point->get_name());
431 bool Engine::is_initialized()
433 return Engine::instance_ != nullptr;
435 void Engine::set_config(const std::string& str)
437 config::set_parse(str);
439 void Engine::set_config(const std::string& name, int value)
441 config::set_value(name.c_str(), value);
443 void Engine::set_config(const std::string& name, double value)
445 config::set_value(name.c_str(), value);
447 void Engine::set_config(const std::string& name, bool value)
449 config::set_value(name.c_str(), value);
451 void Engine::set_config(const std::string& name, const std::string& value)
453 config::set_value(name.c_str(), value);
456 Engine* Engine::set_default_comm_data_copy_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t))
458 kernel::activity::CommImpl::set_copy_data_callback(callback);
462 } // namespace simgrid
464 /* **************************** Public C interface *************************** */
465 void simgrid_init(int* argc, char** argv)
467 static simgrid::s4u::Engine e(argc, argv);
469 void simgrid_load_platform(const char* file)
471 simgrid::s4u::Engine::get_instance()->load_platform(file);
474 void simgrid_load_deployment(const char* file)
476 simgrid::s4u::Engine::get_instance()->load_deployment(file);
480 simgrid::s4u::Engine::get_instance()->run();
482 void simgrid_register_function(const char* name, void (*code)(int, char**))
484 simgrid::s4u::Engine::get_instance()->register_function(name, code);
486 void simgrid_register_default(void (*code)(int, char**))
488 simgrid::s4u::Engine::get_instance()->register_default(code);
490 double simgrid_get_clock()
492 return simgrid::s4u::Engine::get_clock();
495 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
497 return simgrid::s4u::Engine::get_instance()->get_actor_count();
500 void simgrid_set_maestro(void (*code)(void*), void* data)
503 XBT_INFO("WARNING, SIMIX_set_maestro is believed to not work on windows. Please help us investigating this issue if "
504 "you need that feature");
506 maestro_code = std::bind(code, data);
508 void SIMIX_set_maestro(void (*code)(void*), void* data) // XBT_ATTRIB_DEPRECATED_v333
510 simgrid_set_maestro(code, data);