1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2018. 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/surf/network_interface.hpp"
20 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
22 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
26 xbt::signal<void()> on_platform_creation;
27 xbt::signal<void()> on_platform_created;
28 xbt::signal<void()> on_simulation_end;
29 xbt::signal<void(double)> on_time_advance;
30 xbt::signal<void(void)> on_deadlock;
32 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
34 Engine::Engine(int* argc, char** argv)
36 xbt_assert(s4u::Engine::instance_ == nullptr,
37 "It is currently forbidden to create more than one instance of s4u::Engine");
39 SIMIX_global_init(argc, argv);
41 pimpl = new kernel::EngineImpl();
42 s4u::Engine::instance_ = this;
48 s4u::Engine::instance_ = nullptr;
51 /** @brief Retrieve the engine singleton */
52 Engine* Engine::get_instance()
54 if (s4u::Engine::instance_ == nullptr)
55 return new Engine(0, nullptr);
57 return s4u::Engine::instance_;
60 void Engine::shutdown()
62 delete s4u::Engine::instance_;
63 s4u::Engine::instance_ = nullptr;
66 double Engine::get_clock()
68 return SIMIX_get_clock();
71 void Engine::load_platform(const char* platf)
73 SIMIX_create_environment(platf);
76 void Engine::register_function(const char* name, int (*code)(int, char**))
78 SIMIX_function_register(name, code);
80 void Engine::register_default(int (*code)(int, char**))
82 SIMIX_function_register_default(code);
84 void Engine::load_deployment(const char* deploy)
86 SIMIX_launch_application(deploy);
88 /** @brief Returns the amount of hosts in the platform */
89 size_t Engine::get_host_count()
91 return pimpl->hosts_.size();
93 /** @brief Fills the passed list with all hosts found in the platform
94 * @deprecated Please prefer Engine::getAllHosts()
96 void Engine::getHostList(std::vector<Host*>* list)
98 for (auto const& kv : pimpl->hosts_)
99 list->push_back(kv.second);
102 /** @brief Returns the list of all hosts found in the platform */
103 std::vector<Host*> Engine::get_all_hosts()
105 std::vector<Host*> res;
106 for (auto const& kv : pimpl->hosts_)
107 res.push_back(kv.second);
111 void Engine::host_register(std::string name, simgrid::s4u::Host* host)
113 pimpl->hosts_[name] = host;
116 void Engine::host_unregister(std::string name)
118 pimpl->hosts_.erase(name);
121 simgrid::s4u::Host* Engine::host_by_name(std::string name)
123 return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
126 simgrid::s4u::Host* Engine::host_by_name_or_null(std::string name)
128 auto host = pimpl->hosts_.find(name);
129 return host == pimpl->hosts_.end() ? nullptr : host->second;
132 /** @brief Returns the amount of storages in the platform */
133 size_t Engine::get_storage_count()
135 return pimpl->storages_.size();
138 /** @brief Returns the list of all storages found in the platform */
139 std::vector<Storage*> Engine::get_all_storages()
141 std::vector<Storage*> res;
142 for (auto const& kv : pimpl->storages_)
143 res.push_back(kv.second);
147 simgrid::s4u::Storage* Engine::storage_by_name(std::string name)
149 return pimpl->storages_.at(name); // Will raise a std::out_of_range if the host does not exist
152 simgrid::s4u::Storage* Engine::storage_by_name_or_null(std::string name)
154 auto storage = pimpl->storages_.find(name);
155 return storage == pimpl->storages_.end() ? nullptr : storage->second;
158 void Engine::storage_register(std::string name, simgrid::s4u::Storage* storage)
160 pimpl->storages_[name] = storage;
163 void Engine::storage_unregister(std::string name)
165 pimpl->storages_.erase(name);
168 /** @brief Returns the amount of links in the platform */
169 size_t Engine::get_link_count()
171 return kernel::resource::LinkImpl::linksCount();
174 /** @brief Returns the list of all links found in the platform */
175 std::vector<Link*> Engine::get_all_links()
177 std::vector<Link*> res;
178 kernel::resource::LinkImpl::linksList(&res);
184 if (MC_is_active()) {
191 /** @brief Retrieve the root netzone, containing all others */
192 s4u::NetZone* Engine::get_netzone_root()
194 return pimpl->netzone_root_;
196 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
197 void Engine::set_netzone_root(s4u::NetZone* netzone)
199 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
200 pimpl->netzone_root_ = static_cast<kernel::routing::NetZoneImpl*>(netzone);
203 static s4u::NetZone* netzone_by_name_recursive(s4u::NetZone* current, const char* name)
205 if (not strcmp(current->get_cname(), name))
208 for (auto const& elem : *(current->getChildren())) {
209 simgrid::s4u::NetZone* tmp = netzone_by_name_recursive(elem, name);
210 if (tmp != nullptr) {
217 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
218 NetZone* Engine::netzone_by_name_or_null(const char* name)
220 return netzone_by_name_recursive(get_netzone_root(), name);
223 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
224 simgrid::kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(std::string name)
226 auto netp = pimpl->netpoints_.find(name);
227 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
230 /** @brief Fill the provided vector with all existing netpoints */
231 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
233 for (auto const& kv : pimpl->netpoints_)
234 list->push_back(kv.second);
236 std::vector<simgrid::kernel::routing::NetPoint*> Engine::get_all_netpoints()
238 std::vector<simgrid::kernel::routing::NetPoint*> res;
239 for (auto const& kv : pimpl->netpoints_)
240 res.push_back(kv.second);
244 /** @brief Register a new netpoint to the system */
245 void Engine::netpoint_register(simgrid::kernel::routing::NetPoint* point)
247 // simgrid::simix::simcall([&]{ FIXME: this segfaults in set_thread
248 pimpl->netpoints_[point->get_name()] = point;
251 /** @brief Unregister a given netpoint */
252 void Engine::netpoint_unregister(simgrid::kernel::routing::NetPoint* point)
254 simgrid::simix::simcall([this, point] {
255 pimpl->netpoints_.erase(point->get_name());
260 bool Engine::is_initialized()
262 return Engine::instance_ != nullptr;
264 void Engine::set_config(std::string str)
266 simgrid::config::set_parse(std::move(str));
269 } // namespace simgrid
271 /* **************************** Public C interface *************************** */
272 void sg_engine_load_platform(const char* file)
274 simgrid::s4u::Engine::get_instance()->load_platform(file);
277 void sg_engine_load_deployment(const char* file)
279 simgrid::s4u::Engine::get_instance()->load_deployment(file);
282 void sg_engine_register_function(const char* name, int (*code)(int, char**))
284 simgrid::s4u::Engine::get_instance()->register_function(name, code);
286 void sg_engine_register_default(int (*code)(int, char**))
288 simgrid::s4u::Engine::get_instance()->register_default(code);
290 double sg_engine_get_clock()
292 return simgrid::s4u::Engine::get_clock();