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/mc/mc_replay.hpp"
21 #include "src/surf/network_interface.hpp"
22 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
23 #include <simgrid/Exception.hpp>
28 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
33 xbt::signal<void()> Engine::on_platform_creation;
34 xbt::signal<void()> Engine::on_platform_created;
35 xbt::signal<void()> Engine::on_simulation_end;
36 xbt::signal<void(double)> Engine::on_time_advance;
37 xbt::signal<void(void)> Engine::on_deadlock;
39 Engine* Engine::instance_ = nullptr; /* This singleton is awful, but I don't see no other solution right now. */
41 void Engine::initialize(int* argc, char** argv)
43 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
44 Engine::instance_ = this;
46 pimpl->initialize(argc, argv);
47 SIMIX_global_init(argc, argv);
50 Engine::Engine(std::string name) : pimpl(new kernel::EngineImpl(nullptr, nullptr))
53 char* argv = &name[0];
54 initialize(&argc, &argv);
57 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl(argc, argv))
59 initialize(argc, argv);
65 Engine::instance_ = nullptr;
68 /** @brief Retrieve the engine singleton */
69 Engine* Engine::get_instance()
71 return get_instance(nullptr, nullptr);
73 Engine* Engine::get_instance(int* argc, char** argv)
75 if (Engine::instance_ == nullptr) {
76 auto e = new Engine(argc, argv);
77 xbt_assert(Engine::instance_ == e);
79 return Engine::instance_;
82 void Engine::shutdown()
84 delete Engine::instance_;
87 double Engine::get_clock()
89 if (MC_is_active() || MC_record_replay_is_active()) {
90 return MC_process_clock_get(SIMIX_process_self());
92 return surf_get_clock();
96 void Engine::add_model(std::shared_ptr<kernel::resource::Model> model,
97 const std::vector<kernel::resource::Model*>& dependencies)
99 simgrid::kernel::actor::simcall([this, &model, &dependencies] { pimpl->add_model(std::move(model), dependencies); });
102 const std::vector<simgrid::kernel::resource::Model*>& Engine::get_all_models() const
104 return pimpl->get_all_models();
108 * Creates a new platform, including hosts, links, and the routing table.
111 * See also: :ref:`platform`.
114 void Engine::load_platform(const std::string& platf) const
116 pimpl->load_platform(platf);
119 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v330
121 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
122 return xbt::wrap_main(code, std::move(args));
124 register_function(name, code_factory);
126 void Engine::register_default(int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v330
128 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
131 /** Registers the main function of an actor that will be launched from the deployment file */
132 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
134 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
135 return xbt::wrap_main(code, std::move(args));
137 register_function(name, code_factory);
140 /** Registers the main function of an actor that will be launched from the deployment file */
141 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
143 kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
144 return std::bind(std::move(code), std::move(args));
146 register_function(name, code_factory);
148 /** Registers a function as the default main function of actors
150 * It will be used as fallback when the function requested from the deployment file was not registered.
151 * It is used for trace-based simulations (see examples/cpp/replay-comms and similar).
153 void Engine::register_default(const std::function<void(int, char**)>& code)
155 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
157 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
159 simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
162 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
164 simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
167 /** Load a deployment file and launch the actors that it contains
170 * See also: :ref:`deploy`.
173 void Engine::load_deployment(const std::string& deploy) const
175 pimpl->load_deployment(deploy);
178 /** Returns the amount of hosts in the platform */
179 size_t Engine::get_host_count() const
181 return pimpl->hosts_.size();
184 std::vector<Host*> Engine::get_all_hosts() const
186 std::vector<Host*> res;
187 for (auto const& kv : pimpl->hosts_)
188 res.push_back(kv.second);
192 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
194 std::vector<Host*> hosts;
195 for (auto const& kv : pimpl->hosts_) {
196 if (filter(kv.second))
197 hosts.push_back(kv.second);
203 void Engine::host_register(const std::string& name, Host* host)
205 pimpl->hosts_[name] = host;
208 void Engine::host_unregister(const std::string& name)
210 pimpl->hosts_.erase(name);
213 /** @brief Find a host from its name.
215 * @throw std::invalid_argument if the searched host does not exist.
217 Host* Engine::host_by_name(const std::string& name) const
219 auto host = pimpl->hosts_.find(name);
220 if (host == pimpl->hosts_.end())
221 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
225 /** @brief Find a host from its name (or nullptr if that host does not exist) */
226 Host* Engine::host_by_name_or_null(const std::string& name) const
228 auto host = pimpl->hosts_.find(name);
229 return host == pimpl->hosts_.end() ? nullptr : host->second;
232 /** @brief Find a link from its name.
234 * @throw std::invalid_argument if the searched link does not exist.
236 Link* Engine::link_by_name(const std::string& name) const
238 auto link = pimpl->links_.find(name);
239 if (link == pimpl->links_.end())
240 throw std::invalid_argument(std::string("Link not found: ") + name);
241 return link->second->get_iface();
244 SplitDuplexLink* Engine::split_duplex_link_by_name(const std::string& name) const
246 auto link = pimpl->split_duplex_links_.find(name);
247 if (link == pimpl->split_duplex_links_.end())
248 throw std::invalid_argument(std::string("Link not found: ") + name);
249 return link->second->get_iface();
252 /** @brief Find a link from its name (or nullptr if that link does not exist) */
253 Link* Engine::link_by_name_or_null(const std::string& name) const
255 auto link = pimpl->links_.find(name);
256 return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
259 /** @brief Find a mailox from its name or create one if it does not exist) */
260 Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
262 /* two actors may have pushed the same mbox_create simcall at the same time */
263 kernel::activity::MailboxImpl* mbox = kernel::actor::simcall([&name, this] {
264 auto m = pimpl->mailboxes_.emplace(name, nullptr);
266 m.first->second = new kernel::activity::MailboxImpl(name);
267 XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
269 return m.first->second;
271 return mbox->get_iface();
274 void Engine::link_register(const std::string& name, const Link* link)
276 pimpl->links_[name] = link->get_impl();
279 void Engine::link_unregister(const std::string& name)
281 pimpl->links_.erase(name);
284 /** @brief Returns the amount of links in the platform */
285 size_t Engine::get_link_count() const
287 return pimpl->links_.size();
290 /** @brief Returns the list of all links found in the platform */
291 std::vector<Link*> Engine::get_all_links() const
293 std::vector<Link*> res;
294 for (auto const& kv : pimpl->links_)
295 res.push_back(kv.second->get_iface());
299 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
301 std::vector<Link*> filtered_list;
302 for (auto const& kv : pimpl->links_) {
303 Link* l = kv.second->get_iface();
305 filtered_list.push_back(l);
307 return filtered_list;
310 size_t Engine::get_actor_count() const
312 return pimpl->get_actor_count();
315 std::vector<ActorPtr> Engine::get_all_actors() const
317 std::vector<ActorPtr> actor_list;
318 for (auto const& kv : pimpl->get_actor_list()) {
319 actor_list.push_back(kv.second->get_iface());
324 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
326 std::vector<ActorPtr> actor_list;
327 for (auto const& kv : pimpl->get_actor_list()) {
328 if (filter(kv.second->get_iface()))
329 actor_list.push_back(kv.second->get_iface());
334 void Engine::run() const
336 /* sealing resources before run: links */
337 for (auto* link : get_all_links())
339 /* seal netzone root, recursively seal children netzones, hosts and disks */
340 get_netzone_root()->seal();
342 /* Clean IO before the run */
346 if (MC_is_active()) {
353 /** @brief Retrieve the root netzone, containing all others */
354 s4u::NetZone* Engine::get_netzone_root() const
356 if (pimpl->netzone_root_)
357 return pimpl->netzone_root_->get_iface();
360 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
361 void Engine::set_netzone_root(const s4u::NetZone* netzone)
363 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
364 pimpl->netzone_root_ = netzone->get_impl();
367 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
369 if (current->get_name() == name)
372 for (auto const& elem : current->get_children()) {
373 NetZone* tmp = netzone_by_name_recursive(elem, name);
374 if (tmp != nullptr) {
381 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
382 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
384 return netzone_by_name_recursive(get_netzone_root(), name);
387 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
388 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
390 auto netp = pimpl->netpoints_.find(name);
391 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
394 kernel::routing::NetPoint* Engine::netpoint_by_name(const std::string& name) const
396 auto netp = netpoint_by_name_or_null(name);
397 if (netp == nullptr) {
398 throw std::invalid_argument(std::string("Netpoint not found: %s") + name);
403 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
405 std::vector<kernel::routing::NetPoint*> res;
406 for (auto const& kv : pimpl->netpoints_)
407 res.push_back(kv.second);
411 /** @brief Register a new netpoint to the system */
412 void Engine::netpoint_register(kernel::routing::NetPoint* point)
414 simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
417 /** @brief Unregister a given netpoint */
418 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
420 kernel::actor::simcall([this, point] {
421 pimpl->netpoints_.erase(point->get_name());
426 bool Engine::is_initialized()
428 return Engine::instance_ != nullptr;
430 void Engine::set_config(const std::string& str)
432 config::set_parse(str);
434 void Engine::set_config(const std::string& name, int value)
436 config::set_value(name.c_str(), value);
438 void Engine::set_config(const std::string& name, double value)
440 config::set_value(name.c_str(), value);
442 void Engine::set_config(const std::string& name, bool value)
444 config::set_value(name.c_str(), value);
446 void Engine::set_config(const std::string& name, const std::string& value)
448 config::set_value(name.c_str(), value);
451 Engine* Engine::set_default_comm_data_copy_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t))
453 kernel::activity::CommImpl::set_copy_data_callback(callback);
457 } // namespace simgrid
459 /* **************************** Public C interface *************************** */
460 void simgrid_init(int* argc, char** argv)
462 static simgrid::s4u::Engine e(argc, argv);
464 void simgrid_load_platform(const char* file)
466 simgrid::s4u::Engine::get_instance()->load_platform(file);
469 void simgrid_load_deployment(const char* file)
471 simgrid::s4u::Engine::get_instance()->load_deployment(file);
475 simgrid::s4u::Engine::get_instance()->run();
477 void simgrid_register_function(const char* name, void (*code)(int, char**))
479 simgrid::s4u::Engine::get_instance()->register_function(name, code);
481 void simgrid_register_default(void (*code)(int, char**))
483 simgrid::s4u::Engine::get_instance()->register_default(code);
485 double simgrid_get_clock()
487 return simgrid::s4u::Engine::get_clock();
490 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
492 return simgrid::s4u::Engine::get_instance()->get_actor_count();