1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2017. 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.h"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "simgrid/s4u/Mailbox.hpp"
13 #include "simgrid/s4u/NetZone.hpp"
14 #include "simgrid/s4u/Storage.hpp"
15 #include "simgrid/simix.h"
16 #include "src/kernel/EngineImpl.hpp"
17 #include "src/kernel/routing/NetPoint.hpp"
18 #include "src/kernel/routing/NetZoneImpl.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. */
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 void Engine::getHostList(std::vector<Host*>* list)
92 for (auto const& kv : pimpl->hosts_)
93 list->push_back(kv.second);
95 void Engine::addHost(std::string name, simgrid::s4u::Host* host)
97 pimpl->hosts_[name] = host;
99 void Engine::delHost(std::string name)
101 pimpl->hosts_.erase(name);
103 simgrid::s4u::Host* Engine::hostByName(std::string name)
105 return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
107 simgrid::s4u::Host* Engine::hostByNameOrNull(std::string name)
109 auto host = pimpl->hosts_.find(name);
110 return host == pimpl->hosts_.end() ? nullptr : host->second;
113 /** @brief Returns the amount of links in the platform */
114 size_t Engine::getLinkCount()
116 return simgrid::surf::LinkImpl::linksCount();
118 /** @brief Fills the passed list with all hosts found in the platform */
119 void Engine::getLinkList(std::vector<Link*>* list)
121 simgrid::surf::LinkImpl::linksList(list);
125 if (MC_is_active()) {
132 s4u::NetZone* Engine::getNetRoot()
134 return pimpl->netRoot_;
137 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
139 if (not strcmp(current->getCname(), name))
142 for (auto const& elem : *(current->getChildren())) {
143 simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
144 if (tmp != nullptr) {
151 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
152 NetZone* Engine::getNetzoneByNameOrNull(const char* name)
154 return netzoneByNameRecursive(getNetRoot(), name);
157 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
158 simgrid::kernel::routing::NetPoint* Engine::getNetpointByNameOrNull(std::string name)
160 auto netp = pimpl->netpoints_.find(name);
161 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
164 /** @brief Fill the provided vector with all existing netpoints */
165 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
167 for (auto const& kv : pimpl->netpoints_)
168 list->push_back(kv.second);
170 /** @brief Register a new netpoint to the system */
171 void Engine::netpointRegister(simgrid::kernel::routing::NetPoint* point)
173 // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
174 pimpl->netpoints_[point->getName()] = point;
177 /** @brief Unregister a given netpoint */
178 void Engine::netpointUnregister(simgrid::kernel::routing::NetPoint* point)
180 simgrid::simix::kernelImmediate([this, point] {
181 pimpl->netpoints_.erase(point->getName());
186 bool Engine::isInitialized()
188 return Engine::instance_ != nullptr;
190 void Engine::setConfig(std::string str)
192 xbt_cfg_set_parse(str.c_str());