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 return new Engine(0, nullptr);
61 return s4u::Engine::instance_;
64 void Engine::shutdown()
66 delete s4u::Engine::instance_;
67 s4u::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(std::string platf)
91 if (XBT_LOG_ISENABLED(s4u_engine, xbt_log_priority_debug))
92 start = xbt_os_time();
94 parse_platform_file(platf);
96 xbt_die("Error while loading %s: %s", platf.c_str(), e.what());
99 if (XBT_LOG_ISENABLED(s4u_engine, xbt_log_priority_debug)) {
101 XBT_DEBUG("PARSE TIME: %g", (end - start));
105 void Engine::register_function(std::string name, int (*code)(int, char**))
107 SIMIX_function_register(name, code);
109 void Engine::register_function(std::string name, void (*code)(std::vector<std::string>))
111 SIMIX_function_register(name, code);
113 void Engine::register_default(int (*code)(int, char**))
115 SIMIX_function_register_default(code);
117 void Engine::load_deployment(std::string deploy)
119 SIMIX_launch_application(deploy);
121 /** @brief Returns the amount of hosts in the platform */
122 size_t Engine::get_host_count()
124 return pimpl->hosts_.size();
127 std::vector<Host*> Engine::get_all_hosts()
129 std::vector<Host*> res;
130 for (auto const& kv : pimpl->hosts_)
131 res.push_back(kv.second);
135 std::vector<Host*> Engine::get_filtered_hosts(std::function<bool(Host*)> filter)
137 std::vector<Host*> hosts;
138 for (auto const& kv : pimpl->hosts_) {
139 if (filter(kv.second))
140 hosts.push_back(kv.second);
146 void Engine::host_register(std::string name, simgrid::s4u::Host* host)
148 pimpl->hosts_[name] = host;
151 void Engine::host_unregister(std::string name)
153 pimpl->hosts_.erase(name);
156 /** @brief Find a host from its name.
158 * @throw std::invalid_argument if the searched host does not exist.
160 simgrid::s4u::Host* Engine::host_by_name(std::string name)
162 if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
163 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
164 return pimpl->hosts_.at(name);
167 /** @brief Find a host from its name (or nullptr if that host does not exist) */
168 simgrid::s4u::Host* Engine::host_by_name_or_null(std::string name)
170 auto host = pimpl->hosts_.find(name);
171 return host == pimpl->hosts_.end() ? nullptr : host->second;
174 /** @brief Find a link from its name.
176 * @throw std::invalid_argument if the searched link does not exist.
178 simgrid::s4u::Link* Engine::link_by_name(std::string name)
180 if (pimpl->links_.find(name) == pimpl->links_.end())
181 throw std::invalid_argument(std::string("Link not found: ") + name);
183 return pimpl->links_.at(name);
186 /** @brief Find an link from its name (or nullptr if that link does not exist) */
187 simgrid::s4u::Link* Engine::link_by_name_or_null(std::string name)
189 auto link = pimpl->links_.find(name);
190 return link == pimpl->links_.end() ? nullptr : link->second;
193 void Engine::link_register(std::string name, simgrid::s4u::Link* link)
195 pimpl->links_[name] = link;
198 void Engine::link_unregister(std::string name)
200 pimpl->links_.erase(name);
203 /** @brief Returns the amount of storages in the platform */
204 size_t Engine::get_storage_count()
206 return pimpl->storages_.size();
209 /** @brief Returns the list of all storages found in the platform */
210 std::vector<Storage*> Engine::get_all_storages()
212 std::vector<Storage*> res;
213 for (auto const& kv : pimpl->storages_)
214 res.push_back(kv.second);
218 /** @brief Find a storage from its name.
220 * @throw std::invalid_argument if the searched storage does not exist.
222 simgrid::s4u::Storage* Engine::storage_by_name(std::string name)
224 if (pimpl->storages_.find(name) == pimpl->storages_.end())
225 throw std::invalid_argument(std::string("Storage not found: ") + name);
227 return pimpl->storages_.at(name);
230 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
231 simgrid::s4u::Storage* Engine::storage_by_name_or_null(std::string name)
233 auto storage = pimpl->storages_.find(name);
234 return storage == pimpl->storages_.end() ? nullptr : storage->second;
237 void Engine::storage_register(std::string name, simgrid::s4u::Storage* storage)
239 pimpl->storages_[name] = storage;
242 void Engine::storage_unregister(std::string name)
244 pimpl->storages_.erase(name);
247 /** @brief Returns the amount of links in the platform */
248 size_t Engine::get_link_count()
250 return pimpl->links_.size();
253 /** @brief Returns the list of all links found in the platform */
254 std::vector<Link*> Engine::get_all_links()
256 std::vector<Link*> res;
257 for (auto const& kv : pimpl->links_)
258 res.push_back(kv.second);
262 std::vector<Link*> Engine::get_filtered_links(std::function<bool(Link*)> filter)
264 std::vector<Link*> filtered_list;
265 for (auto const& kv : pimpl->links_)
266 if (filter(kv.second))
267 filtered_list.push_back(kv.second);
268 return filtered_list;
271 size_t Engine::get_actor_count()
273 return simix_global->process_list.size();
276 std::vector<ActorPtr> Engine::get_all_actors()
278 std::vector<ActorPtr> actor_list;
279 actor_list.push_back(simgrid::s4u::Actor::self());
280 for (auto& kv : simix_global->process_list) {
281 actor_list.push_back(kv.second->iface());
286 std::vector<ActorPtr> Engine::get_filtered_actors(std::function<bool(ActorPtr)> filter)
288 std::vector<ActorPtr> actor_list;
289 for (auto& kv : simix_global->process_list) {
290 if (filter(kv.second->iface()))
291 actor_list.push_back(kv.second->iface());
298 /* Clean IO before the run */
302 if (MC_is_active()) {
309 /** @brief Retrieve the root netzone, containing all others */
310 s4u::NetZone* Engine::get_netzone_root()
312 return pimpl->netzone_root_->get_iface();
314 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
315 void Engine::set_netzone_root(s4u::NetZone* netzone)
317 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
318 pimpl->netzone_root_ = netzone->get_impl();
321 static s4u::NetZone* netzone_by_name_recursive(s4u::NetZone* current, std::string name)
323 if (current->get_name() == name)
326 for (auto const& elem : current->get_children()) {
327 simgrid::s4u::NetZone* tmp = netzone_by_name_recursive(elem, name);
328 if (tmp != nullptr) {
335 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
336 NetZone* Engine::netzone_by_name_or_null(std::string name)
338 return netzone_by_name_recursive(get_netzone_root(), name);
341 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
342 simgrid::kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(std::string name)
344 auto netp = pimpl->netpoints_.find(name);
345 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
348 /** @brief Fill the provided vector with all existing netpoints */
349 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
351 for (auto const& kv : pimpl->netpoints_)
352 list->push_back(kv.second);
354 std::vector<simgrid::kernel::routing::NetPoint*> Engine::get_all_netpoints()
356 std::vector<simgrid::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(simgrid::kernel::routing::NetPoint* point)
365 // simgrid::simix::simcall([&]{ FIXME: this segfaults in set_thread
366 pimpl->netpoints_[point->get_name()] = point;
369 /** @brief Unregister a given netpoint */
370 void Engine::netpoint_unregister(simgrid::kernel::routing::NetPoint* point)
372 simgrid::simix::simcall([this, point] {
373 pimpl->netpoints_.erase(point->get_name());
378 bool Engine::is_initialized()
380 return Engine::instance_ != nullptr;
382 void Engine::set_config(std::string str)
384 simgrid::config::set_parse(std::move(str));
387 } // namespace simgrid
389 /* **************************** Public C interface *************************** */
390 void simgrid_init(int* argc, char** argv)
392 simgrid::s4u::Engine e(argc, argv);
394 void simgrid_load_platform(const char* file)
396 simgrid::s4u::Engine::get_instance()->load_platform(file);
399 void simgrid_load_deployment(const char* file)
401 simgrid::s4u::Engine::get_instance()->load_deployment(file);
405 simgrid::s4u::Engine::get_instance()->run();
407 void simgrid_register_function(const char* name, int (*code)(int, char**))
409 simgrid::s4u::Engine::get_instance()->register_function(name, code);
411 void simgrid_register_default(int (*code)(int, char**))
413 simgrid::s4u::Engine::get_instance()->register_default(code);
415 double simgrid_get_clock()
417 return simgrid::s4u::Engine::get_clock();