Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Convert xbt_cfg_set_parse -> simgrid::config::set_parse.
[simgrid.git] / src / s4u / s4u_engine.cpp
1 /* s4u::Engine Simulation Engine and global functions. */
2
3 /* Copyright (c) 2006-2018. 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.hpp"
9 #include "mc/mc.h"
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"
22
23 XBT_LOG_NEW_CATEGORY(s4u,"Log channels of the S4U (Simgrid for you) interface");
24
25 namespace simgrid {
26 namespace s4u {
27 xbt::signal<void()> onPlatformCreated;
28 xbt::signal<void()> onSimulationEnd;
29 xbt::signal<void(double)> onTimeAdvance;
30 xbt::signal<void(void)> onDeadlock;
31
32 Engine *Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
33
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");
36   TRACE_global_init();
37   SIMIX_global_init(argc, argv);
38
39   pimpl                  = new kernel::EngineImpl();
40   s4u::Engine::instance_ = this;
41 }
42
43 Engine::~Engine()
44 {
45   delete pimpl;
46   s4u::Engine::instance_ = nullptr;
47 }
48
49 Engine* Engine::getInstance()
50 {
51   if (s4u::Engine::instance_ == nullptr)
52     return new Engine(0, nullptr);
53   else
54     return s4u::Engine::instance_;
55 }
56
57 void Engine::shutdown() {
58   delete s4u::Engine::instance_;
59   s4u::Engine::instance_ = nullptr;
60 }
61
62 double Engine::getClock()
63 {
64   return SIMIX_get_clock();
65 }
66
67 void Engine::loadPlatform(const char *platf)
68 {
69   SIMIX_create_environment(platf);
70 }
71
72 void Engine::registerFunction(const char*name, int (*code)(int,char**))
73 {
74   SIMIX_function_register(name,code);
75 }
76 void Engine::registerDefault(int (*code)(int,char**))
77 {
78   SIMIX_function_register_default(code);
79 }
80 void Engine::loadDeployment(const char *deploy)
81 {
82   SIMIX_launch_application(deploy);
83 }
84 /** @brief Returns the amount of hosts in the platform */
85 size_t Engine::getHostCount()
86 {
87   return pimpl->hosts_.size();
88 }
89 /** @brief Fills the passed list with all hosts found in the platform
90  *  @deprecated Please prefer Engine::getAllHosts()
91  */
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)
94 {
95   for (auto const& kv : pimpl->hosts_)
96     list->push_back(kv.second);
97 }
98
99 /** @brief Returns the list of all hosts found in the platform */
100 std::vector<Host*> Engine::getAllHosts()
101 {
102   std::vector<Host*> res;
103   for (auto const& kv : pimpl->hosts_)
104     res.push_back(kv.second);
105   return res;
106 }
107
108 void Engine::addHost(std::string name, simgrid::s4u::Host* host)
109 {
110   pimpl->hosts_[name] = host;
111 }
112
113 void Engine::delHost(std::string name)
114 {
115   pimpl->hosts_.erase(name);
116 }
117
118 simgrid::s4u::Host* Engine::hostByName(std::string name)
119 {
120   return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
121 }
122
123 simgrid::s4u::Host* Engine::hostByNameOrNull(std::string name)
124 {
125   auto host = pimpl->hosts_.find(name);
126   return host == pimpl->hosts_.end() ? nullptr : host->second;
127 }
128
129 /** @brief Returns the list of all storages found in the platform */
130 std::vector<Storage*> Engine::getAllStorages()
131 {
132   std::vector<Storage*> res;
133   for (auto const& kv : pimpl->storages_)
134     res.push_back(kv.second);
135   return res;
136 }
137
138 simgrid::s4u::Storage* Engine::storageByName(std::string name)
139 {
140   return pimpl->storages_.at(name); // Will raise a std::out_of_range if the host does not exist
141 }
142
143 simgrid::s4u::Storage* Engine::storageByNameOrNull(std::string name)
144 {
145   auto storage = pimpl->storages_.find(name);
146   return storage == pimpl->storages_.end() ? nullptr : storage->second;
147 }
148
149 void Engine::addStorage(std::string name, simgrid::s4u::Storage* storage)
150 {
151   pimpl->storages_[name] = storage;
152 }
153
154 void Engine::delStorage(std::string name)
155 {
156   pimpl->storages_.erase(name);
157 }
158
159 /** @brief Returns the amount of links in the platform */
160 size_t Engine::getLinkCount()
161 {
162   return kernel::resource::LinkImpl::linksCount();
163 }
164
165 /** @brief Fills the passed list with all links found in the platform
166  *
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)
170 {
171   kernel::resource::LinkImpl::linksList(list);
172 }
173
174 /** @brief Returns the list of all links found in the platform */
175 std::vector<Link*> Engine::getAllLinks()
176 {
177   std::vector<Link*> res;
178   kernel::resource::LinkImpl::linksList(&res);
179   return res;
180 }
181
182 void Engine::run() {
183   if (MC_is_active()) {
184     MC_run();
185   } else {
186     SIMIX_run();
187   }
188 }
189
190 s4u::NetZone* Engine::getNetRoot()
191 {
192   return pimpl->netRoot_;
193 }
194
195 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
196 {
197   if (not strcmp(current->get_cname(), name))
198     return current;
199
200   for (auto const& elem : *(current->getChildren())) {
201     simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
202     if (tmp != nullptr) {
203       return tmp;
204     }
205   }
206   return nullptr;
207 }
208
209 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
210 NetZone* Engine::getNetzoneByNameOrNull(const char* name)
211 {
212   return netzoneByNameRecursive(getNetRoot(), name);
213 }
214
215 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
216 simgrid::kernel::routing::NetPoint* Engine::getNetpointByNameOrNull(std::string name)
217 {
218   auto netp = pimpl->netpoints_.find(name);
219   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
220 }
221
222 /** @brief Fill the provided vector with all existing netpoints */
223 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
224 {
225   for (auto const& kv : pimpl->netpoints_)
226     list->push_back(kv.second);
227 }
228 /** @brief Register a new netpoint to the system */
229 void Engine::netpointRegister(simgrid::kernel::routing::NetPoint* point)
230 {
231   // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
232   pimpl->netpoints_[point->get_name()] = point;
233   // });
234 }
235 /** @brief Unregister a given netpoint */
236 void Engine::netpointUnregister(simgrid::kernel::routing::NetPoint* point)
237 {
238   simgrid::simix::kernelImmediate([this, point] {
239     pimpl->netpoints_.erase(point->get_name());
240     delete point;
241   });
242 }
243
244 bool Engine::isInitialized()
245 {
246   return Engine::instance_ != nullptr;
247 }
248 void Engine::setConfig(std::string str)
249 {
250   simgrid::config::set_parse(std::move(str));
251 }
252 }
253 } // namespace