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. */
8 #include "instr/instr_interface.hpp"
10 #include "simgrid/kernel/routing/NetPoint.hpp"
11 #include "simgrid/kernel/routing/NetZoneImpl.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/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()> onPlatformCreated;
27 xbt::signal<void()> onSimulationEnd;
28 xbt::signal<void(double)> onTimeAdvance;
29 xbt::signal<void(void)> onDeadlock;
31 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
33 Engine::Engine(int* argc, char** argv)
35 xbt_assert(s4u::Engine::instance_ == nullptr,
36 "It is currently forbidden to create more than one instance of s4u::Engine");
38 SIMIX_global_init(argc, argv);
40 pimpl = new kernel::EngineImpl();
41 s4u::Engine::instance_ = this;
47 s4u::Engine::instance_ = nullptr;
50 /** @brief Retrieve the engine singleton */
51 Engine* Engine::get_instance()
53 if (s4u::Engine::instance_ == nullptr)
54 return new Engine(0, nullptr);
56 return s4u::Engine::instance_;
59 void Engine::shutdown()
61 delete s4u::Engine::instance_;
62 s4u::Engine::instance_ = nullptr;
65 double Engine::get_clock()
67 return SIMIX_get_clock();
70 void Engine::load_platform(const char* platf)
72 SIMIX_create_environment(platf);
75 void Engine::register_function(const char* name, int (*code)(int, char**))
77 SIMIX_function_register(name, code);
79 void Engine::register_default(int (*code)(int, char**))
81 SIMIX_function_register_default(code);
83 void Engine::load_deployment(const char* deploy)
85 SIMIX_launch_application(deploy);
87 /** @brief Returns the amount of hosts in the platform */
88 size_t Engine::get_host_count()
90 return pimpl->hosts_.size();
92 /** @brief Fills the passed list with all hosts found in the platform
93 * @deprecated Please prefer Engine::getAllHosts()
95 void Engine::getHostList(std::vector<Host*>* list)
97 for (auto const& kv : pimpl->hosts_)
98 list->push_back(kv.second);
101 /** @brief Returns the list of all hosts found in the platform */
102 std::vector<Host*> Engine::get_all_hosts()
104 std::vector<Host*> res;
105 for (auto const& kv : pimpl->hosts_)
106 res.push_back(kv.second);
110 void Engine::host_register(std::string name, simgrid::s4u::Host* host)
112 pimpl->hosts_[name] = host;
115 void Engine::host_unregister(std::string name)
117 pimpl->hosts_.erase(name);
120 simgrid::s4u::Host* Engine::host_by_name(std::string name)
122 return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
125 simgrid::s4u::Host* Engine::host_by_name_or_null(std::string name)
127 auto host = pimpl->hosts_.find(name);
128 return host == pimpl->hosts_.end() ? nullptr : host->second;
131 /** @brief Returns the amount of storages in the platform */
132 size_t Engine::get_storage_count()
134 return pimpl->storages_.size();
137 /** @brief Returns the list of all storages found in the platform */
138 std::vector<Storage*> Engine::get_all_storages()
140 std::vector<Storage*> res;
141 for (auto const& kv : pimpl->storages_)
142 res.push_back(kv.second);
146 simgrid::s4u::Storage* Engine::storage_by_name(std::string name)
148 return pimpl->storages_.at(name); // Will raise a std::out_of_range if the host does not exist
151 simgrid::s4u::Storage* Engine::storage_by_name_or_null(std::string name)
153 auto storage = pimpl->storages_.find(name);
154 return storage == pimpl->storages_.end() ? nullptr : storage->second;
157 void Engine::storage_register(std::string name, simgrid::s4u::Storage* storage)
159 pimpl->storages_[name] = storage;
162 void Engine::storage_unregister(std::string name)
164 pimpl->storages_.erase(name);
167 /** @brief Returns the amount of links in the platform */
168 size_t Engine::get_link_count()
170 return kernel::resource::LinkImpl::linksCount();
173 /** @brief Returns the list of all links found in the platform */
174 std::vector<Link*> Engine::get_all_links()
176 std::vector<Link*> res;
177 kernel::resource::LinkImpl::linksList(&res);
183 if (MC_is_active()) {
190 s4u::NetZone* Engine::getNetRoot()
192 return pimpl->netzone_root_;
195 static s4u::NetZone* netzone_by_name_recursive(s4u::NetZone* current, const char* name)
197 if (not strcmp(current->get_cname(), name))
200 for (auto const& elem : *(current->getChildren())) {
201 simgrid::s4u::NetZone* tmp = netzone_by_name_recursive(elem, name);
202 if (tmp != nullptr) {
209 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
210 NetZone* Engine::getNetzoneByNameOrNull(const char* name)
212 return netzone_by_name_recursive(getNetRoot(), name);
215 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
216 simgrid::kernel::routing::NetPoint* Engine::getNetpointByNameOrNull(std::string name)
218 auto netp = pimpl->netpoints_.find(name);
219 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
222 /** @brief Fill the provided vector with all existing netpoints */
223 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
225 for (auto const& kv : pimpl->netpoints_)
226 list->push_back(kv.second);
228 std::vector<simgrid::kernel::routing::NetPoint*> Engine::get_all_netpoints()
230 std::vector<simgrid::kernel::routing::NetPoint*> res;
231 for (auto const& kv : pimpl->netpoints_)
232 res.push_back(kv.second);
236 /** @brief Register a new netpoint to the system */
237 void Engine::netpoint_register(simgrid::kernel::routing::NetPoint* point)
239 // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
240 pimpl->netpoints_[point->get_name()] = point;
243 /** @brief Unregister a given netpoint */
244 void Engine::netpoint_unregister(simgrid::kernel::routing::NetPoint* point)
246 simgrid::simix::kernelImmediate([this, point] {
247 pimpl->netpoints_.erase(point->get_name());
252 bool Engine::is_initialized()
254 return Engine::instance_ != nullptr;
256 void Engine::set_config(std::string str)
258 simgrid::config::set_parse(std::move(str));
261 } // namespace simgrid