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>
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; /* That singleton is awful, but I don't see no other solution right now. */
41 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
43 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
45 SIMIX_global_init(argc, argv);
47 Engine::instance_ = this;
53 Engine::instance_ = nullptr;
56 /** @brief Retrieve the engine singleton */
57 Engine* Engine::get_instance()
59 if (Engine::instance_ == nullptr) {
60 auto e = new Engine(0, nullptr);
61 xbt_assert(Engine::instance_ == e);
63 return Engine::instance_;
66 void Engine::shutdown()
68 delete Engine::instance_;
69 Engine::instance_ = nullptr;
72 double Engine::get_clock()
74 return SIMIX_get_clock();
78 * Creates a new platform, including hosts, links, and the routing table.
81 * See also: :ref:`platform`.
84 void Engine::load_platform(const std::string& platf)
86 double start = xbt_os_time();
87 parse_platform_file(platf);
89 double end = xbt_os_time();
90 XBT_DEBUG("PARSE TIME: %g", (end - start));
93 void Engine::register_function(const std::string& name, int (*code)(int, char**)) // deprecated
95 register_function(name, [code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
97 void Engine::register_default(int (*code)(int, char**)) // deprecated
99 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
102 /** Registers the main function of an actor that will be launched from the deployment file */
103 void Engine::register_function(const std::string& name, void (*code)(int, char**))
105 register_function(name, [code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
108 /** Registers the main function of an actor that will be launched from the deployment file */
109 void Engine::register_function(const std::string& name, void (*code)(std::vector<std::string>))
111 register_function(name,
112 [code](std::vector<std::string> args) { return std::bind(std::move(code), std::move(args)); });
114 /** Registers a function as the default main function of actors
116 * It will be used as fallback when the function requested from the deployment file was not registered.
117 * It is used for trace-based simulations (see examples/s4u/replay-comms and similar).
119 void Engine::register_default(void (*code)(int, char**))
121 register_default([code](std::vector<std::string> args) { return xbt::wrap_main(code, std::move(args)); });
123 void Engine::register_default(kernel::actor::ActorCodeFactory code)
125 simgrid::kernel::actor::simcall([this, code]() { pimpl->register_default(code); });
128 void Engine::register_function(const std::string& name, kernel::actor::ActorCodeFactory code)
130 simgrid::kernel::actor::simcall([this, name, code]() { pimpl->register_function(name, code); });
133 /** Load a deployment file and launch the actors that it contains
136 * See also: :ref:`deploy`.
139 void Engine::load_deployment(const std::string& deploy)
141 pimpl->load_deployment(deploy);
144 /** Returns the amount of hosts in the platform */
145 size_t Engine::get_host_count()
147 return pimpl->hosts_.size();
150 std::vector<Host*> Engine::get_all_hosts()
152 std::vector<Host*> res;
153 for (auto const& kv : pimpl->hosts_)
154 res.push_back(kv.second);
158 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter)
160 std::vector<Host*> hosts;
161 for (auto const& kv : pimpl->hosts_) {
162 if (filter(kv.second))
163 hosts.push_back(kv.second);
169 void Engine::host_register(const std::string& name, Host* host)
171 pimpl->hosts_[name] = host;
174 void Engine::host_unregister(const std::string& name)
176 pimpl->hosts_.erase(name);
179 /** @brief Find a host from its name.
181 * @throw std::invalid_argument if the searched host does not exist.
183 Host* Engine::host_by_name(const std::string& name)
185 if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
186 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
187 return pimpl->hosts_.at(name);
190 /** @brief Find a host from its name (or nullptr if that host does not exist) */
191 Host* Engine::host_by_name_or_null(const std::string& name)
193 auto host = pimpl->hosts_.find(name);
194 return host == pimpl->hosts_.end() ? nullptr : host->second;
197 /** @brief Find a link from its name.
199 * @throw std::invalid_argument if the searched link does not exist.
201 Link* Engine::link_by_name(const std::string& name)
203 if (pimpl->links_.find(name) == pimpl->links_.end())
204 throw std::invalid_argument(std::string("Link not found: ") + name);
206 return pimpl->links_.at(name)->get_iface();
209 /** @brief Find an link from its name (or nullptr if that link does not exist) */
210 Link* Engine::link_by_name_or_null(const std::string& name)
212 auto link = pimpl->links_.find(name);
213 return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
216 void Engine::link_register(const std::string& name, const Link* link)
218 pimpl->links_[name] = link->get_impl();
221 void Engine::link_unregister(const std::string& name)
223 pimpl->links_.erase(name);
226 /** @brief Returns the amount of storages in the platform */
227 size_t Engine::get_storage_count()
229 return pimpl->storages_.size();
232 /** @brief Returns the list of all storages found in the platform */
233 std::vector<Storage*> Engine::get_all_storages()
235 std::vector<Storage*> res;
236 for (auto const& kv : pimpl->storages_)
237 res.push_back(kv.second->get_iface());
241 /** @brief Find a storage from its name.
243 * @throw std::invalid_argument if the searched storage does not exist.
245 Storage* Engine::storage_by_name(const std::string& name)
247 if (pimpl->storages_.find(name) == pimpl->storages_.end())
248 throw std::invalid_argument(std::string("Storage not found: ") + name);
250 return pimpl->storages_.at(name)->get_iface();
253 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
254 Storage* Engine::storage_by_name_or_null(const std::string& name)
256 auto storage = pimpl->storages_.find(name);
257 return storage == pimpl->storages_.end() ? nullptr : storage->second->get_iface();
260 void Engine::storage_register(const std::string& name, const Storage* storage)
262 pimpl->storages_[name] = storage->get_impl();
265 void Engine::storage_unregister(const std::string& name)
267 pimpl->storages_.erase(name);
270 /** @brief Returns the amount of links in the platform */
271 size_t Engine::get_link_count()
273 return pimpl->links_.size();
276 /** @brief Returns the list of all links found in the platform */
277 std::vector<Link*> Engine::get_all_links()
279 std::vector<Link*> res;
280 for (auto const& kv : pimpl->links_)
281 res.push_back(kv.second->get_iface());
285 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter)
287 std::vector<Link*> filtered_list;
288 for (auto const& kv : pimpl->links_) {
289 Link* l = kv.second->get_iface();
291 filtered_list.push_back(l);
293 return filtered_list;
296 size_t Engine::get_actor_count()
298 return simix_global->process_list.size();
301 std::vector<ActorPtr> Engine::get_all_actors()
303 std::vector<ActorPtr> actor_list;
304 actor_list.push_back(simgrid::s4u::Actor::self());
305 for (auto const& kv : simix_global->process_list) {
306 actor_list.push_back(kv.second->iface());
311 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter)
313 std::vector<ActorPtr> actor_list;
314 for (auto const& kv : simix_global->process_list) {
315 if (filter(kv.second->iface()))
316 actor_list.push_back(kv.second->iface());
323 /* Clean IO before the run */
327 if (MC_is_active()) {
334 /** @brief Retrieve the root netzone, containing all others */
335 s4u::NetZone* Engine::get_netzone_root()
337 return pimpl->netzone_root_->get_iface();
339 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
340 void Engine::set_netzone_root(const s4u::NetZone* netzone)
342 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
343 pimpl->netzone_root_ = netzone->get_impl();
346 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
348 if (current->get_name() == name)
351 for (auto const& elem : current->get_children()) {
352 NetZone* tmp = netzone_by_name_recursive(elem, name);
353 if (tmp != nullptr) {
360 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
361 NetZone* Engine::netzone_by_name_or_null(const std::string& name)
363 return netzone_by_name_recursive(get_netzone_root(), name);
366 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
367 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name)
369 auto netp = pimpl->netpoints_.find(name);
370 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
373 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints()
375 std::vector<kernel::routing::NetPoint*> res;
376 for (auto const& kv : pimpl->netpoints_)
377 res.push_back(kv.second);
381 /** @brief Register a new netpoint to the system */
382 void Engine::netpoint_register(kernel::routing::NetPoint* point)
384 simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
387 /** @brief Unregister a given netpoint */
388 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
390 kernel::actor::simcall([this, point] {
391 pimpl->netpoints_.erase(point->get_name());
396 bool Engine::is_initialized()
398 return Engine::instance_ != nullptr;
400 void Engine::set_config(const std::string& str)
402 config::set_parse(str);
405 } // namespace simgrid
407 /* **************************** Public C interface *************************** */
408 void simgrid_init(int* argc, char** argv)
410 simgrid::s4u::Engine e(argc, argv);
412 void simgrid_load_platform(const char* file)
414 simgrid::s4u::Engine::get_instance()->load_platform(file);
417 void simgrid_load_deployment(const char* file)
419 simgrid::s4u::Engine::get_instance()->load_deployment(file);
423 simgrid::s4u::Engine::get_instance()->run();
425 void simgrid_register_function(const char* name, void (*code)(int, char**))
427 simgrid::s4u::Engine::get_instance()->register_function(name, code);
429 void simgrid_register_default(void (*code)(int, char**))
431 simgrid::s4u::Engine::get_instance()->register_default(code);
433 double simgrid_get_clock()
435 return simgrid::s4u::Engine::get_clock();
437 int simgrid_get_actor_count()
439 return simgrid::s4u::Engine::get_instance()->get_actor_count();