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
21 #include "xbt/config.hpp"
23 XBT_LOG_NEW_CATEGORY(s4u,"Log channels of the S4U (Simgrid for you) interface");
27 xbt::signal<void()> onPlatformCreated;
28 xbt::signal<void()> onSimulationEnd;
29 xbt::signal<void(double)> onTimeAdvance;
30 xbt::signal<void(void)> onDeadlock;
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) {
35 xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
37 SIMIX_global_init(argc, argv);
39 pimpl = new kernel::EngineImpl();
40 s4u::Engine::instance_ = this;
46 s4u::Engine::instance_ = nullptr;
49 Engine* Engine::getInstance()
51 if (s4u::Engine::instance_ == nullptr)
52 return new Engine(0, nullptr);
54 return s4u::Engine::instance_;
57 void Engine::shutdown() {
58 delete s4u::Engine::instance_;
59 s4u::Engine::instance_ = nullptr;
62 double Engine::getClock()
64 return SIMIX_get_clock();
67 void Engine::loadPlatform(const char *platf)
69 SIMIX_create_environment(platf);
72 void Engine::registerFunction(const char*name, int (*code)(int,char**))
74 SIMIX_function_register(name,code);
76 void Engine::registerDefault(int (*code)(int,char**))
78 SIMIX_function_register_default(code);
80 void Engine::loadDeployment(const char *deploy)
82 SIMIX_launch_application(deploy);
84 /** @brief Returns the amount of hosts in the platform */
85 size_t Engine::getHostCount()
87 return pimpl->hosts_.size();
89 /** @brief Fills the passed list with all hosts found in the platform
90 * @deprecated Please prefer Engine::getAllHosts()
92 void XBT_ATTRIB_DEPRECATED_v322("Engine::getHostList() is deprecated in favor of Engine::getAllHosts(). Please switch before v3.22")
93 Engine::getHostList(std::vector<Host*>* list)
95 for (auto const& kv : pimpl->hosts_)
96 list->push_back(kv.second);
99 /** @brief Returns the list of all hosts found in the platform */
100 std::vector<Host*> Engine::getAllHosts()
102 std::vector<Host*> res;
103 for (auto const& kv : pimpl->hosts_)
104 res.push_back(kv.second);
108 void Engine::addHost(std::string name, simgrid::s4u::Host* host)
110 pimpl->hosts_[name] = host;
113 void Engine::delHost(std::string name)
115 pimpl->hosts_.erase(name);
118 simgrid::s4u::Host* Engine::hostByName(std::string name)
120 return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
123 simgrid::s4u::Host* Engine::hostByNameOrNull(std::string name)
125 auto host = pimpl->hosts_.find(name);
126 return host == pimpl->hosts_.end() ? nullptr : host->second;
129 /** @brief Returns the list of all storages found in the platform */
130 std::vector<Storage*> Engine::getAllStorages()
132 std::vector<Storage*> res;
133 for (auto const& kv : pimpl->storages_)
134 res.push_back(kv.second);
138 simgrid::s4u::Storage* Engine::storageByName(std::string name)
140 return pimpl->storages_.at(name); // Will raise a std::out_of_range if the host does not exist
143 simgrid::s4u::Storage* Engine::storageByNameOrNull(std::string name)
145 auto storage = pimpl->storages_.find(name);
146 return storage == pimpl->storages_.end() ? nullptr : storage->second;
149 void Engine::addStorage(std::string name, simgrid::s4u::Storage* storage)
151 pimpl->storages_[name] = storage;
154 void Engine::delStorage(std::string name)
156 pimpl->storages_.erase(name);
159 /** @brief Returns the amount of links in the platform */
160 size_t Engine::getLinkCount()
162 return kernel::resource::LinkImpl::linksCount();
165 /** @brief Fills the passed list with all links found in the platform
167 * @deprecated. Prefer Engine::getAllLinks() */
168 void XBT_ATTRIB_DEPRECATED_v322("Engine::getLinkList() is deprecated in favor of Engine::getAllLinks(). Please switch before v3.22")
169 Engine::getLinkList(std::vector<Link*>* list)
171 kernel::resource::LinkImpl::linksList(list);
174 /** @brief Returns the list of all links found in the platform */
175 std::vector<Link*> Engine::getAllLinks()
177 std::vector<Link*> res;
178 kernel::resource::LinkImpl::linksList(&res);
183 if (MC_is_active()) {
190 s4u::NetZone* Engine::getNetRoot()
192 return pimpl->netRoot_;
195 static s4u::NetZone* netzoneByNameRecursive(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 = netzoneByNameRecursive(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 netzoneByNameRecursive(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 /** @brief Register a new netpoint to the system */
229 void Engine::netpointRegister(simgrid::kernel::routing::NetPoint* point)
231 // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
232 pimpl->netpoints_[point->get_name()] = point;
235 /** @brief Unregister a given netpoint */
236 void Engine::netpointUnregister(simgrid::kernel::routing::NetPoint* point)
238 simgrid::simix::kernelImmediate([this, point] {
239 pimpl->netpoints_.erase(point->get_name());
244 bool Engine::isInitialized()
246 return Engine::instance_ != nullptr;
248 void Engine::setConfig(std::string str)
250 simgrid::config::set_parse(std::move(str));