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 /** Registers the main function of an actor that will be launched from the deployment file */
94 void Engine::register_function(const std::string& name, int (*code)(int, char**))
96 pimpl->register_function(name, code);
99 /** Registers the main function of an actor that will be launched from the deployment file */
100 void Engine::register_function(const std::string& name, void (*code)(std::vector<std::string>))
102 pimpl->register_function(name, code);
104 /** Registers a function as the default main function of actors
106 * It will be used as fallback when the function requested from the deployment file was not registered.
107 * It is used for trace-based simulations (see examples/s4u/replay-comms and similar).
109 void Engine::register_default(int (*code)(int, char**))
111 pimpl->register_default(code);
114 /** Load a deployment file and launch the actors that it contains
117 * See also: :ref:`deploy`.
120 void Engine::load_deployment(const std::string& deploy)
122 pimpl->load_deployment(deploy);
125 /** Returns the amount of hosts in the platform */
126 size_t Engine::get_host_count()
128 return pimpl->hosts_.size();
131 std::vector<Host*> Engine::get_all_hosts()
133 std::vector<Host*> res;
134 for (auto const& kv : pimpl->hosts_)
135 res.push_back(kv.second);
139 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter)
141 std::vector<Host*> hosts;
142 for (auto const& kv : pimpl->hosts_) {
143 if (filter(kv.second))
144 hosts.push_back(kv.second);
150 void Engine::host_register(const std::string& name, Host* host)
152 pimpl->hosts_[name] = host;
155 void Engine::host_unregister(const std::string& name)
157 pimpl->hosts_.erase(name);
160 /** @brief Find a host from its name.
162 * @throw std::invalid_argument if the searched host does not exist.
164 Host* Engine::host_by_name(const std::string& name)
166 if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
167 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
168 return pimpl->hosts_.at(name);
171 /** @brief Find a host from its name (or nullptr if that host does not exist) */
172 Host* Engine::host_by_name_or_null(const std::string& name)
174 auto host = pimpl->hosts_.find(name);
175 return host == pimpl->hosts_.end() ? nullptr : host->second;
178 /** @brief Find a link from its name.
180 * @throw std::invalid_argument if the searched link does not exist.
182 Link* Engine::link_by_name(const std::string& name)
184 if (pimpl->links_.find(name) == pimpl->links_.end())
185 throw std::invalid_argument(std::string("Link not found: ") + name);
187 return pimpl->links_.at(name)->get_iface();
190 /** @brief Find an link from its name (or nullptr if that link does not exist) */
191 Link* Engine::link_by_name_or_null(const std::string& name)
193 auto link = pimpl->links_.find(name);
194 return link == pimpl->links_.end() ? nullptr : link->second->get_iface();
197 void Engine::link_register(const std::string& name, const Link* link)
199 pimpl->links_[name] = link->get_impl();
202 void Engine::link_unregister(const std::string& name)
204 pimpl->links_.erase(name);
207 /** @brief Returns the amount of storages in the platform */
208 size_t Engine::get_storage_count()
210 return pimpl->storages_.size();
213 /** @brief Returns the list of all storages found in the platform */
214 std::vector<Storage*> Engine::get_all_storages()
216 std::vector<Storage*> res;
217 for (auto const& kv : pimpl->storages_)
218 res.push_back(kv.second->get_iface());
222 /** @brief Find a storage from its name.
224 * @throw std::invalid_argument if the searched storage does not exist.
226 Storage* Engine::storage_by_name(const std::string& name)
228 if (pimpl->storages_.find(name) == pimpl->storages_.end())
229 throw std::invalid_argument(std::string("Storage not found: ") + name);
231 return pimpl->storages_.at(name)->get_iface();
234 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
235 Storage* Engine::storage_by_name_or_null(const std::string& name)
237 auto storage = pimpl->storages_.find(name);
238 return storage == pimpl->storages_.end() ? nullptr : storage->second->get_iface();
241 void Engine::storage_register(const std::string& name, const Storage* storage)
243 pimpl->storages_[name] = storage->get_impl();
246 void Engine::storage_unregister(const std::string& name)
248 pimpl->storages_.erase(name);
251 /** @brief Returns the amount of links in the platform */
252 size_t Engine::get_link_count()
254 return pimpl->links_.size();
257 /** @brief Returns the list of all links found in the platform */
258 std::vector<Link*> Engine::get_all_links()
260 std::vector<Link*> res;
261 for (auto const& kv : pimpl->links_)
262 res.push_back(kv.second->get_iface());
266 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter)
268 std::vector<Link*> filtered_list;
269 for (auto const& kv : pimpl->links_) {
270 Link* l = kv.second->get_iface();
272 filtered_list.push_back(l);
274 return filtered_list;
277 size_t Engine::get_actor_count()
279 return simix_global->process_list.size();
282 std::vector<ActorPtr> Engine::get_all_actors()
284 std::vector<ActorPtr> actor_list;
285 actor_list.push_back(simgrid::s4u::Actor::self());
286 for (auto const& kv : simix_global->process_list) {
287 actor_list.push_back(kv.second->iface());
292 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter)
294 std::vector<ActorPtr> actor_list;
295 for (auto const& kv : simix_global->process_list) {
296 if (filter(kv.second->iface()))
297 actor_list.push_back(kv.second->iface());
304 /* Clean IO before the run */
308 if (MC_is_active()) {
315 /** @brief Retrieve the root netzone, containing all others */
316 s4u::NetZone* Engine::get_netzone_root()
318 return pimpl->netzone_root_->get_iface();
320 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
321 void Engine::set_netzone_root(const s4u::NetZone* netzone)
323 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
324 pimpl->netzone_root_ = netzone->get_impl();
327 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
329 if (current->get_name() == name)
332 for (auto const& elem : current->get_children()) {
333 NetZone* tmp = netzone_by_name_recursive(elem, name);
334 if (tmp != nullptr) {
341 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
342 NetZone* Engine::netzone_by_name_or_null(const std::string& name)
344 return netzone_by_name_recursive(get_netzone_root(), name);
347 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
348 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name)
350 auto netp = pimpl->netpoints_.find(name);
351 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
354 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints()
356 std::vector<kernel::routing::NetPoint*> res;
357 for (auto const& kv : pimpl->netpoints_)
358 res.push_back(kv.second);
362 /** @brief Register a new netpoint to the system */
363 void Engine::netpoint_register(kernel::routing::NetPoint* point)
365 simgrid::kernel::actor::simcall([this, point] { pimpl->netpoints_[point->get_name()] = point; });
368 /** @brief Unregister a given netpoint */
369 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
371 kernel::actor::simcall([this, point] {
372 pimpl->netpoints_.erase(point->get_name());
377 bool Engine::is_initialized()
379 return Engine::instance_ != nullptr;
381 void Engine::set_config(const std::string& str)
383 config::set_parse(str);
386 } // namespace simgrid
388 /* **************************** Public C interface *************************** */
389 void simgrid_init(int* argc, char** argv)
391 simgrid::s4u::Engine e(argc, argv);
393 void simgrid_load_platform(const char* file)
395 simgrid::s4u::Engine::get_instance()->load_platform(file);
398 void simgrid_load_deployment(const char* file)
400 simgrid::s4u::Engine::get_instance()->load_deployment(file);
404 simgrid::s4u::Engine::get_instance()->run();
406 void simgrid_register_function(const char* name, int (*code)(int, char**))
408 simgrid::s4u::Engine::get_instance()->register_function(name, code);
410 void simgrid_register_default(int (*code)(int, char**))
412 simgrid::s4u::Engine::get_instance()->register_default(code);
414 double simgrid_get_clock()
416 return simgrid::s4u::Engine::get_clock();
418 int simgrid_get_actor_count()
420 return simgrid::s4u::Engine::get_instance()->get_actor_count();