Logo AND Algorithmique Numérique Distribuée

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