1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2019. 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/Engine.hpp"
12 #include "simgrid/s4u/Host.hpp"
13 #include "simgrid/s4u/Mailbox.hpp"
14 #include "simgrid/s4u/NetZone.hpp"
15 #include "simgrid/s4u/Storage.hpp"
16 #include "simgrid/simix.h"
17 #include "src/instr/instr_private.hpp"
18 #include "src/kernel/EngineImpl.hpp"
19 #include "src/simix/smx_private.hpp" // For access to simix_global->process_list
20 #include "src/surf/network_interface.hpp"
21 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
22 #include <simgrid/Exception.hpp>
26 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
31 xbt::signal<void()> on_platform_creation;
32 xbt::signal<void()> on_platform_created;
33 xbt::signal<void()> on_simulation_end;
34 xbt::signal<void(double)> on_time_advance;
35 xbt::signal<void(void)> on_deadlock;
37 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
39 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
41 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
43 SIMIX_global_init(argc, argv);
45 Engine::instance_ = this;
51 Engine::instance_ = nullptr;
54 /** @brief Retrieve the engine singleton */
55 Engine* Engine::get_instance()
57 if (Engine::instance_ == nullptr) {
58 auto e = new Engine(0, nullptr);
59 xbt_assert(Engine::instance_ == e);
61 return Engine::instance_;
64 void Engine::shutdown()
66 delete Engine::instance_;
67 Engine::instance_ = nullptr;
70 double Engine::get_clock()
72 return SIMIX_get_clock();
76 * @brief A platform loader.
78 * Creates a new platform, including hosts, links, and the routing_table.
79 * @param platf a filename of the XML description of a platform. This file follows this DTD :
81 * @include src/surf/xml/simgrid.dtd
83 * Here is a small example of such a platform
85 * @include examples/platforms/small_platform.xml
87 void Engine::load_platform(const std::string& platf)
89 double start = xbt_os_time();
91 parse_platform_file(platf);
93 xbt_die("Error while loading %s: %s", platf.c_str(), e.what());
96 double end = xbt_os_time();
97 XBT_DEBUG("PARSE TIME: %g", (end - start));
100 void Engine::register_function(const std::string& name, int (*code)(int, char**))
102 SIMIX_function_register(name, code);
104 void Engine::register_function(const std::string& name, void (*code)(std::vector<std::string>))
106 SIMIX_function_register(name, code);
108 void Engine::register_default(int (*code)(int, char**))
110 SIMIX_function_register_default(code);
112 void Engine::load_deployment(const std::string& deploy)
114 SIMIX_launch_application(deploy);
116 /** @brief Returns the amount of hosts in the platform */
117 size_t Engine::get_host_count()
119 return pimpl->hosts_.size();
122 std::vector<Host*> Engine::get_all_hosts()
124 std::vector<Host*> res;
125 for (auto const& kv : pimpl->hosts_)
126 res.push_back(kv.second);
130 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter)
132 std::vector<Host*> hosts;
133 for (auto const& kv : pimpl->hosts_) {
134 if (filter(kv.second))
135 hosts.push_back(kv.second);
141 void Engine::host_register(const std::string& name, Host* host)
143 pimpl->hosts_[name] = host;
146 void Engine::host_unregister(const std::string& name)
148 pimpl->hosts_.erase(name);
151 /** @brief Find a host from its name.
153 * @throw std::invalid_argument if the searched host does not exist.
155 Host* Engine::host_by_name(const std::string& name)
157 if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
158 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
159 return pimpl->hosts_.at(name);
162 /** @brief Find a host from its name (or nullptr if that host does not exist) */
163 Host* Engine::host_by_name_or_null(const std::string& name)
165 auto host = pimpl->hosts_.find(name);
166 return host == pimpl->hosts_.end() ? nullptr : host->second;
169 /** @brief Find a link from its name.
171 * @throw std::invalid_argument if the searched link does not exist.
173 Link* Engine::link_by_name(const std::string& name)
175 if (pimpl->links_.find(name) == pimpl->links_.end())
176 throw std::invalid_argument(std::string("Link not found: ") + name);
178 return pimpl->links_.at(name);
181 /** @brief Find an link from its name (or nullptr if that link does not exist) */
182 Link* Engine::link_by_name_or_null(const std::string& name)
184 auto link = pimpl->links_.find(name);
185 return link == pimpl->links_.end() ? nullptr : link->second;
188 void Engine::link_register(const std::string& name, Link* link)
190 pimpl->links_[name] = link;
193 void Engine::link_unregister(const std::string& name)
195 pimpl->links_.erase(name);
198 /** @brief Returns the amount of storages in the platform */
199 size_t Engine::get_storage_count()
201 return pimpl->storages_.size();
204 /** @brief Returns the list of all storages found in the platform */
205 std::vector<Storage*> Engine::get_all_storages()
207 std::vector<Storage*> res;
208 for (auto const& kv : pimpl->storages_)
209 res.push_back(kv.second);
213 /** @brief Find a storage from its name.
215 * @throw std::invalid_argument if the searched storage does not exist.
217 Storage* Engine::storage_by_name(const std::string& name)
219 if (pimpl->storages_.find(name) == pimpl->storages_.end())
220 throw std::invalid_argument(std::string("Storage not found: ") + name);
222 return pimpl->storages_.at(name);
225 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
226 Storage* Engine::storage_by_name_or_null(const std::string& name)
228 auto storage = pimpl->storages_.find(name);
229 return storage == pimpl->storages_.end() ? nullptr : storage->second;
232 void Engine::storage_register(const std::string& name, Storage* storage)
234 pimpl->storages_[name] = storage;
237 void Engine::storage_unregister(const std::string& name)
239 pimpl->storages_.erase(name);
242 /** @brief Returns the amount of links in the platform */
243 size_t Engine::get_link_count()
245 return pimpl->links_.size();
248 /** @brief Returns the list of all links found in the platform */
249 std::vector<Link*> Engine::get_all_links()
251 std::vector<Link*> res;
252 for (auto const& kv : pimpl->links_)
253 res.push_back(kv.second);
257 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter)
259 std::vector<Link*> filtered_list;
260 for (auto const& kv : pimpl->links_)
261 if (filter(kv.second))
262 filtered_list.push_back(kv.second);
263 return filtered_list;
266 size_t Engine::get_actor_count()
268 return simix_global->process_list.size();
271 std::vector<ActorPtr> Engine::get_all_actors()
273 std::vector<ActorPtr> actor_list;
274 actor_list.push_back(simgrid::s4u::Actor::self());
275 for (auto& kv : simix_global->process_list) {
276 actor_list.push_back(kv.second->iface());
281 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter)
283 std::vector<ActorPtr> actor_list;
284 for (auto& kv : simix_global->process_list) {
285 if (filter(kv.second->iface()))
286 actor_list.push_back(kv.second->iface());
293 /* Clean IO before the run */
297 if (MC_is_active()) {
304 /** @brief Retrieve the root netzone, containing all others */
305 s4u::NetZone* Engine::get_netzone_root()
307 return pimpl->netzone_root_->get_iface();
309 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
310 void Engine::set_netzone_root(s4u::NetZone* netzone)
312 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
313 pimpl->netzone_root_ = netzone->get_impl();
316 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
318 if (current->get_name() == name)
321 for (auto const& elem : current->get_children()) {
322 NetZone* tmp = netzone_by_name_recursive(elem, name);
323 if (tmp != nullptr) {
330 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
331 NetZone* Engine::netzone_by_name_or_null(const std::string& name)
333 return netzone_by_name_recursive(get_netzone_root(), name);
336 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
337 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name)
339 auto netp = pimpl->netpoints_.find(name);
340 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
343 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints()
345 std::vector<kernel::routing::NetPoint*> res;
346 for (auto const& kv : pimpl->netpoints_)
347 res.push_back(kv.second);
351 /** @brief Register a new netpoint to the system */
352 void Engine::netpoint_register(kernel::routing::NetPoint* point)
354 // simgrid::simix::simcall([&]{ FIXME: this segfaults in set_thread
355 pimpl->netpoints_[point->get_name()] = point;
359 /** @brief Unregister a given netpoint */
360 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
362 simix::simcall([this, point] {
363 pimpl->netpoints_.erase(point->get_name());
368 bool Engine::is_initialized()
370 return Engine::instance_ != nullptr;
372 void Engine::set_config(const std::string& str)
374 config::set_parse(str);
377 } // namespace simgrid
379 /* **************************** Public C interface *************************** */
380 void simgrid_init(int* argc, char** argv)
382 simgrid::s4u::Engine e(argc, argv);
384 void simgrid_load_platform(const char* file)
386 simgrid::s4u::Engine::get_instance()->load_platform(file);
389 void simgrid_load_deployment(const char* file)
391 simgrid::s4u::Engine::get_instance()->load_deployment(file);
395 simgrid::s4u::Engine::get_instance()->run();
397 void simgrid_register_function(const char* name, int (*code)(int, char**))
399 simgrid::s4u::Engine::get_instance()->register_function(name, code);
401 void simgrid_register_default(int (*code)(int, char**))
403 simgrid::s4u::Engine::get_instance()->register_default(code);
405 double simgrid_get_clock()
407 return simgrid::s4u::Engine::get_clock();