Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Also simplify the API of Engine::getLinkList
[simgrid.git] / src / s4u / s4u_engine.cpp
1 /* s4u::Engine Simulation Engine and global functions. */
2
3 /* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 #include "instr/instr_interface.h"
9 #include "mc/mc.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
21
22 XBT_LOG_NEW_CATEGORY(s4u,"Log channels of the S4U (Simgrid for you) interface");
23
24 namespace simgrid {
25 namespace s4u {
26 xbt::signal<void()> onPlatformCreated;
27 xbt::signal<void()> onSimulationEnd;
28 xbt::signal<void(double)> onTimeAdvance;
29 xbt::signal<void(void)> onDeadlock;
30
31 Engine *Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
32
33 Engine::Engine(int *argc, char **argv) {
34   xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
35   TRACE_global_init();
36   SIMIX_global_init(argc, argv);
37
38   pimpl                  = new kernel::EngineImpl();
39   s4u::Engine::instance_ = this;
40 }
41
42 Engine::~Engine()
43 {
44   delete pimpl;
45   s4u::Engine::instance_ = nullptr;
46 }
47
48 Engine* Engine::getInstance()
49 {
50   if (s4u::Engine::instance_ == nullptr)
51     return new Engine(0, nullptr);
52   else
53     return s4u::Engine::instance_;
54 }
55
56 void Engine::shutdown() {
57   delete s4u::Engine::instance_;
58   s4u::Engine::instance_ = nullptr;
59 }
60
61 double Engine::getClock()
62 {
63   return SIMIX_get_clock();
64 }
65
66 void Engine::loadPlatform(const char *platf)
67 {
68   SIMIX_create_environment(platf);
69 }
70
71 void Engine::registerFunction(const char*name, int (*code)(int,char**))
72 {
73   SIMIX_function_register(name,code);
74 }
75 void Engine::registerDefault(int (*code)(int,char**))
76 {
77   SIMIX_function_register_default(code);
78 }
79 void Engine::loadDeployment(const char *deploy)
80 {
81   SIMIX_launch_application(deploy);
82 }
83 /** @brief Returns the amount of hosts in the platform */
84 size_t Engine::getHostCount()
85 {
86   return pimpl->hosts_.size();
87 }
88 /** @brief Fills the passed list with all hosts found in the platform */
89 void Engine::getHostList(std::vector<Host*>* list)
90 {
91   for (auto const& kv : pimpl->hosts_)
92     list->push_back(kv.second);
93 }
94 /** @brief Returns the list of all hosts found in the platform */
95 std::vector<Host*> Engine::getHostList()
96 {
97   std::vector<Host*> res;
98   for (auto const& kv : pimpl->hosts_)
99     res.push_back(kv.second);
100   return res;
101 }
102 void Engine::addHost(std::string name, simgrid::s4u::Host* host)
103 {
104   pimpl->hosts_[name] = host;
105 }
106 void Engine::delHost(std::string name)
107 {
108   pimpl->hosts_.erase(name);
109 }
110 simgrid::s4u::Host* Engine::hostByName(std::string name)
111 {
112   return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
113 }
114 simgrid::s4u::Host* Engine::hostByNameOrNull(std::string name)
115 {
116   auto host = pimpl->hosts_.find(name);
117   return host == pimpl->hosts_.end() ? nullptr : host->second;
118 }
119
120 /** @brief Returns the amount of links in the platform */
121 size_t Engine::getLinkCount()
122 {
123   return simgrid::surf::LinkImpl::linksCount();
124 }
125 /** @brief Fills the passed list with all links found in the platform */
126 void Engine::getLinkList(std::vector<Link*>* list)
127 {
128   simgrid::surf::LinkImpl::linksList(list);
129 }
130 /** @brief Returns the list of all links found in the platform */
131 std::vector<Link*> Engine::getLinkList()
132 {
133   std::vector<Link*> res;
134   simgrid::surf::LinkImpl::linksList(&res);
135   return res;
136 }
137
138 void Engine::run() {
139   if (MC_is_active()) {
140     MC_run();
141   } else {
142     SIMIX_run();
143   }
144 }
145
146 s4u::NetZone* Engine::getNetRoot()
147 {
148   return pimpl->netRoot_;
149 }
150
151 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
152 {
153   if (not strcmp(current->getCname(), name))
154     return current;
155
156   for (auto const& elem : *(current->getChildren())) {
157     simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
158     if (tmp != nullptr) {
159       return tmp;
160     }
161   }
162   return nullptr;
163 }
164
165 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
166 NetZone* Engine::getNetzoneByNameOrNull(const char* name)
167 {
168   return netzoneByNameRecursive(getNetRoot(), name);
169 }
170
171 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
172 simgrid::kernel::routing::NetPoint* Engine::getNetpointByNameOrNull(std::string name)
173 {
174   auto netp = pimpl->netpoints_.find(name);
175   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
176 }
177
178 /** @brief Fill the provided vector with all existing netpoints */
179 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
180 {
181   for (auto const& kv : pimpl->netpoints_)
182     list->push_back(kv.second);
183 }
184 /** @brief Register a new netpoint to the system */
185 void Engine::netpointRegister(simgrid::kernel::routing::NetPoint* point)
186 {
187   // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
188   pimpl->netpoints_[point->getName()] = point;
189   // });
190 }
191 /** @brief Unregister a given netpoint */
192 void Engine::netpointUnregister(simgrid::kernel::routing::NetPoint* point)
193 {
194   simgrid::simix::kernelImmediate([this, point] {
195     pimpl->netpoints_.erase(point->getName());
196     delete point;
197   });
198 }
199
200 bool Engine::isInitialized()
201 {
202   return Engine::instance_ != nullptr;
203 }
204 void Engine::setConfig(std::string str)
205 {
206   xbt_cfg_set_parse(str.c_str());
207 }
208 }
209 } // namespace