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(s4u::Engine::instance_ == nullptr,
42 "It is currently forbidden to create more than one instance of s4u::Engine");
44 SIMIX_global_init(argc, argv);
46 s4u::Engine::instance_ = this;
52 s4u::Engine::instance_ = nullptr;
55 /** @brief Retrieve the engine singleton */
56 Engine* Engine::get_instance()
58 if (s4u::Engine::instance_ == nullptr) {
59 auto e = new Engine(0, nullptr);
60 xbt_assert(s4u::Engine::instance_ == e);
62 return s4u::Engine::instance_;
65 void Engine::shutdown()
67 delete s4u::Engine::instance_;
68 s4u::Engine::instance_ = nullptr;
71 double Engine::get_clock()
73 return SIMIX_get_clock();
77 * @brief A platform loader.
79 * Creates a new platform, including hosts, links, and the routing_table.
80 * @param platf a filename of the XML description of a platform. This file follows this DTD :
82 * @include src/surf/xml/simgrid.dtd
84 * Here is a small example of such a platform
86 * @include examples/platforms/small_platform.xml
88 void Engine::load_platform(const std::string& platf)
90 double start = xbt_os_time();
92 parse_platform_file(platf);
94 xbt_die("Error while loading %s: %s", platf.c_str(), e.what());
97 double end = xbt_os_time();
98 XBT_DEBUG("PARSE TIME: %g", (end - start));
101 void Engine::register_function(const std::string& name, int (*code)(int, char**))
103 SIMIX_function_register(name, code);
105 void Engine::register_function(const std::string& name, void (*code)(std::vector<std::string>))
107 SIMIX_function_register(name, code);
109 void Engine::register_default(int (*code)(int, char**))
111 SIMIX_function_register_default(code);
113 void Engine::load_deployment(const std::string& deploy)
115 SIMIX_launch_application(deploy);
117 /** @brief Returns the amount of hosts in the platform */
118 size_t Engine::get_host_count()
120 return pimpl->hosts_.size();
123 std::vector<Host*> Engine::get_all_hosts()
125 std::vector<Host*> res;
126 for (auto const& kv : pimpl->hosts_)
127 res.push_back(kv.second);
131 std::vector<Host*> Engine::get_filtered_hosts(std::function<bool(Host*)> filter)
133 std::vector<Host*> hosts;
134 for (auto const& kv : pimpl->hosts_) {
135 if (filter(kv.second))
136 hosts.push_back(kv.second);
142 void Engine::host_register(const std::string& name, simgrid::s4u::Host* host)
144 pimpl->hosts_[name] = host;
147 void Engine::host_unregister(const std::string& name)
149 pimpl->hosts_.erase(name);
152 /** @brief Find a host from its name.
154 * @throw std::invalid_argument if the searched host does not exist.
156 simgrid::s4u::Host* Engine::host_by_name(const std::string& name)
158 if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
159 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
160 return pimpl->hosts_.at(name);
163 /** @brief Find a host from its name (or nullptr if that host does not exist) */
164 simgrid::s4u::Host* Engine::host_by_name_or_null(const std::string& name)
166 auto host = pimpl->hosts_.find(name);
167 return host == pimpl->hosts_.end() ? nullptr : host->second;
170 /** @brief Find a link from its name.
172 * @throw std::invalid_argument if the searched link does not exist.
174 simgrid::s4u::Link* Engine::link_by_name(const std::string& name)
176 if (pimpl->links_.find(name) == pimpl->links_.end())
177 throw std::invalid_argument(std::string("Link not found: ") + name);
179 return pimpl->links_.at(name);
182 /** @brief Find an link from its name (or nullptr if that link does not exist) */
183 simgrid::s4u::Link* Engine::link_by_name_or_null(const std::string& name)
185 auto link = pimpl->links_.find(name);
186 return link == pimpl->links_.end() ? nullptr : link->second;
189 void Engine::link_register(const std::string& name, simgrid::s4u::Link* link)
191 pimpl->links_[name] = link;
194 void Engine::link_unregister(const std::string& name)
196 pimpl->links_.erase(name);
199 /** @brief Returns the amount of storages in the platform */
200 size_t Engine::get_storage_count()
202 return pimpl->storages_.size();
205 /** @brief Returns the list of all storages found in the platform */
206 std::vector<Storage*> Engine::get_all_storages()
208 std::vector<Storage*> res;
209 for (auto const& kv : pimpl->storages_)
210 res.push_back(kv.second);
214 /** @brief Find a storage from its name.
216 * @throw std::invalid_argument if the searched storage does not exist.
218 simgrid::s4u::Storage* Engine::storage_by_name(const std::string& name)
220 if (pimpl->storages_.find(name) == pimpl->storages_.end())
221 throw std::invalid_argument(std::string("Storage not found: ") + name);
223 return pimpl->storages_.at(name);
226 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
227 simgrid::s4u::Storage* Engine::storage_by_name_or_null(const std::string& name)
229 auto storage = pimpl->storages_.find(name);
230 return storage == pimpl->storages_.end() ? nullptr : storage->second;
233 void Engine::storage_register(const std::string& name, simgrid::s4u::Storage* storage)
235 pimpl->storages_[name] = storage;
238 void Engine::storage_unregister(const std::string& name)
240 pimpl->storages_.erase(name);
243 /** @brief Returns the amount of links in the platform */
244 size_t Engine::get_link_count()
246 return pimpl->links_.size();
249 /** @brief Returns the list of all links found in the platform */
250 std::vector<Link*> Engine::get_all_links()
252 std::vector<Link*> res;
253 for (auto const& kv : pimpl->links_)
254 res.push_back(kv.second);
258 std::vector<Link*> Engine::get_filtered_links(std::function<bool(Link*)> filter)
260 std::vector<Link*> filtered_list;
261 for (auto const& kv : pimpl->links_)
262 if (filter(kv.second))
263 filtered_list.push_back(kv.second);
264 return filtered_list;
267 size_t Engine::get_actor_count()
269 return simix_global->process_list.size();
272 std::vector<ActorPtr> Engine::get_all_actors()
274 std::vector<ActorPtr> actor_list;
275 actor_list.push_back(simgrid::s4u::Actor::self());
276 for (auto& kv : simix_global->process_list) {
277 actor_list.push_back(kv.second->iface());
282 std::vector<ActorPtr> Engine::get_filtered_actors(std::function<bool(ActorPtr)> filter)
284 std::vector<ActorPtr> actor_list;
285 for (auto& kv : simix_global->process_list) {
286 if (filter(kv.second->iface()))
287 actor_list.push_back(kv.second->iface());
294 /* Clean IO before the run */
298 if (MC_is_active()) {
305 /** @brief Retrieve the root netzone, containing all others */
306 s4u::NetZone* Engine::get_netzone_root()
308 return pimpl->netzone_root_->get_iface();
310 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
311 void Engine::set_netzone_root(s4u::NetZone* netzone)
313 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
314 pimpl->netzone_root_ = netzone->get_impl();
317 static s4u::NetZone* netzone_by_name_recursive(s4u::NetZone* current, const std::string& name)
319 if (current->get_name() == name)
322 for (auto const& elem : current->get_children()) {
323 simgrid::s4u::NetZone* tmp = netzone_by_name_recursive(elem, name);
324 if (tmp != nullptr) {
331 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
332 NetZone* Engine::netzone_by_name_or_null(const std::string& name)
334 return netzone_by_name_recursive(get_netzone_root(), name);
337 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
338 simgrid::kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name)
340 auto netp = pimpl->netpoints_.find(name);
341 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
344 /** @brief Fill the provided vector with all existing netpoints */
345 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
347 for (auto const& kv : pimpl->netpoints_)
348 list->push_back(kv.second);
350 std::vector<simgrid::kernel::routing::NetPoint*> Engine::get_all_netpoints()
352 std::vector<simgrid::kernel::routing::NetPoint*> res;
353 for (auto const& kv : pimpl->netpoints_)
354 res.push_back(kv.second);
358 /** @brief Register a new netpoint to the system */
359 void Engine::netpoint_register(simgrid::kernel::routing::NetPoint* point)
361 // simgrid::simix::simcall([&]{ FIXME: this segfaults in set_thread
362 pimpl->netpoints_[point->get_name()] = point;
365 /** @brief Unregister a given netpoint */
366 void Engine::netpoint_unregister(simgrid::kernel::routing::NetPoint* point)
368 simgrid::simix::simcall([this, point] {
369 pimpl->netpoints_.erase(point->get_name());
374 bool Engine::is_initialized()
376 return Engine::instance_ != nullptr;
378 void Engine::set_config(std::string str)
380 simgrid::config::set_parse(std::move(str));
383 } // namespace simgrid
385 /* **************************** Public C interface *************************** */
386 void simgrid_init(int* argc, char** argv)
388 simgrid::s4u::Engine e(argc, argv);
390 void simgrid_load_platform(const char* file)
392 simgrid::s4u::Engine::get_instance()->load_platform(file);
395 void simgrid_load_deployment(const char* file)
397 simgrid::s4u::Engine::get_instance()->load_deployment(file);
401 simgrid::s4u::Engine::get_instance()->run();
403 void simgrid_register_function(const char* name, int (*code)(int, char**))
405 simgrid::s4u::Engine::get_instance()->register_function(name, code);
407 void simgrid_register_default(int (*code)(int, char**))
409 simgrid::s4u::Engine::get_instance()->register_default(code);
411 double simgrid_get_clock()
413 return simgrid::s4u::Engine::get_clock();