1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2020. 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/s4u/Storage.hpp"
17 #include "simgrid/simix.h"
18 #include "src/instr/instr_private.hpp"
19 #include "src/kernel/EngineImpl.hpp"
20 #include "src/simix/smx_private.hpp" // For access to simix_global->process_list
21 #include "src/surf/StorageImpl.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)");
34 xbt::signal<void()> Engine::on_platform_creation;
35 xbt::signal<void()> Engine::on_platform_created;
36 xbt::signal<void()> Engine::on_simulation_end;
37 xbt::signal<void(double)> Engine::on_time_advance;
38 xbt::signal<void(void)> Engine::on_deadlock;
40 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
42 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
44 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
46 SIMIX_global_init(argc, argv);
48 Engine::instance_ = this;
54 Engine::instance_ = nullptr;
57 /** @brief Retrieve the engine singleton */
58 Engine* Engine::get_instance()
60 if (Engine::instance_ == nullptr) {
61 auto e = new Engine(nullptr, nullptr);
62 xbt_assert(Engine::instance_ == e);
64 return Engine::instance_;
67 void Engine::shutdown()
69 delete Engine::instance_;
70 Engine::instance_ = nullptr;
73 double Engine::get_clock()
75 return SIMIX_get_clock();
79 * Creates a new platform, including hosts, links, and the routing table.
82 * See also: :ref:`platform`.
85 void Engine::load_platform(const std::string& platf) const
87 double start = xbt_os_time();
88 parse_platform_file(platf);
90 double end = xbt_os_time();
91 XBT_DEBUG("PARSE TIME: %g", (end - start));
94 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
96 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
97 return xbt::wrap_main(code, std::move(args));
99 register_function(name, code_factory);
101 void Engine::register_default(int (*code)(int, char**)) // XBT_ATTRIB_DEPRECATED_v329
103 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
106 /** Registers the main function of an actor that will be launched from the deployment file */
107 void Engine::register_function(const std::string& name, const std::function<void(int, char**)>& code)
109 kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
110 return xbt::wrap_main(code, std::move(args));
112 register_function(name, code_factory);
115 /** Registers the main function of an actor that will be launched from the deployment file */
116 void Engine::register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code)
118 kernel::actor::ActorCodeFactory code_factory = [code{code}](std::vector<std::string> args) mutable {
119 return std::bind(std::move(code), std::move(args));
121 register_function(name, code_factory);
123 /** Registers a function as the default main function of actors
125 * It will be used as fallback when the function requested from the deployment file was not registered.
126 * It is used for trace-based simulations (see examples/s4u/replay-comms and similar).
128 void Engine::register_default(const std::function<void(int, char**)>& code)
130 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
132 void Engine::register_default(const kernel::actor::ActorCodeFactory& code)
134 simgrid::kernel::actor::simcall([this, &code]() { pimpl->register_default(code); });
137 void Engine::register_function(const std::string& name, const kernel::actor::ActorCodeFactory& code)
139 simgrid::kernel::actor::simcall([this, name, &code]() { pimpl->register_function(name, code); });
142 /** Load a deployment file and launch the actors that it contains
145 * See also: :ref:`deploy`.
148 void Engine::load_deployment(const std::string& deploy) const
150 pimpl->load_deployment(deploy);
153 /** Returns the amount of hosts in the platform */
154 size_t Engine::get_host_count() const
156 return pimpl->hosts_.size();
159 std::vector<Host*> Engine::get_all_hosts() const
161 std::vector<Host*> res;
162 for (auto const& kv : pimpl->hosts_)
163 res.push_back(kv.second);
167 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter) const
169 std::vector<Host*> hosts;
170 for (auto const& kv : pimpl->hosts_) {
171 if (filter(kv.second))
172 hosts.push_back(kv.second);
178 void Engine::host_register(const std::string& name, Host* host)
180 pimpl->hosts_[name] = host;
183 void Engine::host_unregister(const std::string& name)
185 pimpl->hosts_.erase(name);
188 /** @brief Find a host from its name.
190 * @throw std::invalid_argument if the searched host does not exist.
192 Host* Engine::host_by_name(const std::string& name) const
194 auto host = pimpl->hosts_.find(name);
195 if (host == pimpl->hosts_.end())
196 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
200 /** @brief Find a host from its name (or nullptr if that host does not exist) */
201 Host* Engine::host_by_name_or_null(const std::string& name) const
203 auto host = pimpl->hosts_.find(name);
204 return host == pimpl->hosts_.end() ? nullptr : host->second;
207 /** @brief Find a link from its name.
209 * @throw std::invalid_argument if the searched link does not exist.
211 Link* Engine::link_by_name(const std::string& name) const
213 auto link = pimpl->links_.find(name);
214 if (link == pimpl->links_.end())
215 throw std::invalid_argument(std::string("Link not found: ") + name);
216 return link->second->get_iface();
219 /** @brief Find a link from its name (or nullptr if that link does not exist) */
220 Link* Engine::link_by_name_or_null(const std::string& name) const
222 auto link = pimpl->links_.find(name);
223 return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
226 void Engine::link_register(const std::string& name, const Link* link)
228 pimpl->links_[name] = link->get_impl();
231 void Engine::link_unregister(const std::string& name)
233 pimpl->links_.erase(name);
236 /** @brief Returns the amount of storages in the platform */
237 size_t Engine::get_storage_count() const
239 return pimpl->storages_.size();
242 /** @brief Returns the list of all storages found in the platform */
243 std::vector<Storage*> Engine::get_all_storages() const
245 std::vector<Storage*> res;
246 for (auto const& kv : pimpl->storages_)
247 res.push_back(kv.second->get_iface());
251 /** @brief Find a storage from its name.
253 * @throw std::invalid_argument if the searched storage does not exist.
255 Storage* Engine::storage_by_name(const std::string& name) const
257 auto storage = pimpl->storages_.find(name);
258 if (storage == pimpl->storages_.end())
259 throw std::invalid_argument(std::string("Storage not found: ") + name);
260 return storage->second->get_iface();
263 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
264 Storage* Engine::storage_by_name_or_null(const std::string& name) const
266 auto storage = pimpl->storages_.find(name);
267 return storage == pimpl->storages_.end() ? nullptr : storage->second->get_iface();
270 void Engine::storage_register(const std::string& name, const Storage* storage)
272 pimpl->storages_[name] = storage->get_impl();
275 void Engine::storage_unregister(const std::string& name)
277 pimpl->storages_.erase(name);
280 /** @brief Returns the amount of links in the platform */
281 size_t Engine::get_link_count() const
283 return pimpl->links_.size();
286 /** @brief Returns the list of all links found in the platform */
287 std::vector<Link*> Engine::get_all_links() const
289 std::vector<Link*> res;
290 for (auto const& kv : pimpl->links_)
291 res.push_back(kv.second->get_iface());
295 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter) const
297 std::vector<Link*> filtered_list;
298 for (auto const& kv : pimpl->links_) {
299 Link* l = kv.second->get_iface();
301 filtered_list.push_back(l);
303 return filtered_list;
306 size_t Engine::get_actor_count() const
308 return simix_global->process_list.size();
311 std::vector<ActorPtr> Engine::get_all_actors() const
313 std::vector<ActorPtr> actor_list;
314 for (auto const& kv : simix_global->process_list) {
315 actor_list.push_back(kv.second->get_iface());
320 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const
322 std::vector<ActorPtr> actor_list;
323 for (auto const& kv : simix_global->process_list) {
324 if (filter(kv.second->get_iface()))
325 actor_list.push_back(kv.second->get_iface());
330 void Engine::run() const
332 /* Clean IO before the run */
336 if (MC_is_active()) {
343 /** @brief Retrieve the root netzone, containing all others */
344 s4u::NetZone* Engine::get_netzone_root() const
346 return pimpl->netzone_root_->get_iface();
348 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
349 void Engine::set_netzone_root(const s4u::NetZone* netzone)
351 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
352 pimpl->netzone_root_ = netzone->get_impl();
355 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
357 if (current->get_name() == name)
360 for (auto const& elem : current->get_children()) {
361 NetZone* tmp = netzone_by_name_recursive(elem, name);
362 if (tmp != nullptr) {
369 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
370 NetZone* Engine::netzone_by_name_or_null(const std::string& name) const
372 return netzone_by_name_recursive(get_netzone_root(), name);
375 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
376 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name) const
378 auto netp = pimpl->netpoints_.find(name);
379 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
382 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints() const
384 std::vector<kernel::routing::NetPoint*> res;
385 for (auto const& kv : pimpl->netpoints_)
386 res.push_back(kv.second);
390 /** @brief Register a new netpoint to the system */
391 void Engine::netpoint_register(kernel::routing::NetPoint* point)
393 simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
396 /** @brief Unregister a given netpoint */
397 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
399 kernel::actor::simcall([this, point] {
400 pimpl->netpoints_.erase(point->get_name());
405 bool Engine::is_initialized()
407 return Engine::instance_ != nullptr;
409 void Engine::set_config(const std::string& str)
411 config::set_parse(str);
413 void Engine::set_config(const std::string& name, int value)
415 config::set_value(name.c_str(), value);
417 void Engine::set_config(const std::string& name, double value)
419 config::set_value(name.c_str(), value);
421 void Engine::set_config(const std::string& name, bool value)
423 config::set_value(name.c_str(), value);
425 void Engine::set_config(const std::string& name, const std::string& value)
427 config::set_value(name.c_str(), value);
431 } // namespace simgrid
433 /* **************************** Public C interface *************************** */
434 void simgrid_init(int* argc, char** argv)
436 simgrid::s4u::Engine e(argc, argv);
438 void simgrid_load_platform(const char* file)
440 simgrid::s4u::Engine::get_instance()->load_platform(file);
443 void simgrid_load_deployment(const char* file)
445 simgrid::s4u::Engine::get_instance()->load_deployment(file);
449 simgrid::s4u::Engine::get_instance()->run();
451 void simgrid_register_function(const char* name, void (*code)(int, char**))
453 simgrid::s4u::Engine::get_instance()->register_function(name, code);
455 void simgrid_register_default(void (*code)(int, char**))
457 simgrid::s4u::Engine::get_instance()->register_default(code);
459 double simgrid_get_clock()
461 return simgrid::s4u::Engine::get_clock();
464 int simgrid_get_actor_count() // XBT_ATTRIB_DEPRECATED_v330
466 return simgrid::s4u::Engine::get_instance()->get_actor_count();