Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove unneeded extern "C".
[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.h"
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   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  *  @deprecated Please prefer Engine::getAllHosts()
90  */
91 void XBT_ATTRIB_DEPRECATED_v322("Engine::getHostList() is deprecated in favor of Engine::getAllHosts(). Please switch before v3.22")
92 Engine::getHostList(std::vector<Host*>* list)
93 {
94   for (auto const& kv : pimpl->hosts_)
95     list->push_back(kv.second);
96 }
97
98 /** @brief Returns the list of all hosts found in the platform */
99 std::vector<Host*> Engine::getAllHosts()
100 {
101   std::vector<Host*> res;
102   for (auto const& kv : pimpl->hosts_)
103     res.push_back(kv.second);
104   return res;
105 }
106
107 void Engine::addHost(std::string name, simgrid::s4u::Host* host)
108 {
109   pimpl->hosts_[name] = host;
110 }
111
112 void Engine::delHost(std::string name)
113 {
114   pimpl->hosts_.erase(name);
115 }
116
117 simgrid::s4u::Host* Engine::hostByName(std::string name)
118 {
119   return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
120 }
121
122 simgrid::s4u::Host* Engine::hostByNameOrNull(std::string name)
123 {
124   auto host = pimpl->hosts_.find(name);
125   return host == pimpl->hosts_.end() ? nullptr : host->second;
126 }
127
128 /** @brief Returns the list of all storages found in the platform */
129 std::vector<Storage*> Engine::getAllStorages()
130 {
131   std::vector<Storage*> res;
132   for (auto const& kv : pimpl->storages_)
133     res.push_back(kv.second);
134   return res;
135 }
136
137 simgrid::s4u::Storage* Engine::storageByName(std::string name)
138 {
139   return pimpl->storages_.at(name); // Will raise a std::out_of_range if the host does not exist
140 }
141
142 simgrid::s4u::Storage* Engine::storageByNameOrNull(std::string name)
143 {
144   auto storage = pimpl->storages_.find(name);
145   return storage == pimpl->storages_.end() ? nullptr : storage->second;
146 }
147
148 void Engine::addStorage(std::string name, simgrid::s4u::Storage* storage)
149 {
150   pimpl->storages_[name] = storage;
151 }
152
153 void Engine::delStorage(std::string name)
154 {
155   pimpl->storages_.erase(name);
156 }
157
158 /** @brief Returns the amount of links in the platform */
159 size_t Engine::getLinkCount()
160 {
161   return kernel::resource::LinkImpl::linksCount();
162 }
163
164 /** @brief Fills the passed list with all links found in the platform
165  *
166  *  @deprecated. Prefer Engine::getAllLinks() */
167 void XBT_ATTRIB_DEPRECATED_v322("Engine::getLinkList() is deprecated in favor of Engine::getAllLinks(). Please switch before v3.22")
168 Engine::getLinkList(std::vector<Link*>* list)
169 {
170   kernel::resource::LinkImpl::linksList(list);
171 }
172
173 /** @brief Returns the list of all links found in the platform */
174 std::vector<Link*> Engine::getAllLinks()
175 {
176   std::vector<Link*> res;
177   kernel::resource::LinkImpl::linksList(&res);
178   return res;
179 }
180
181 void Engine::run() {
182   if (MC_is_active()) {
183     MC_run();
184   } else {
185     SIMIX_run();
186   }
187 }
188
189 s4u::NetZone* Engine::getNetRoot()
190 {
191   return pimpl->netRoot_;
192 }
193
194 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
195 {
196   if (not strcmp(current->get_cname(), name))
197     return current;
198
199   for (auto const& elem : *(current->getChildren())) {
200     simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
201     if (tmp != nullptr) {
202       return tmp;
203     }
204   }
205   return nullptr;
206 }
207
208 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
209 NetZone* Engine::getNetzoneByNameOrNull(const char* name)
210 {
211   return netzoneByNameRecursive(getNetRoot(), name);
212 }
213
214 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
215 simgrid::kernel::routing::NetPoint* Engine::getNetpointByNameOrNull(std::string name)
216 {
217   auto netp = pimpl->netpoints_.find(name);
218   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
219 }
220
221 /** @brief Fill the provided vector with all existing netpoints */
222 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
223 {
224   for (auto const& kv : pimpl->netpoints_)
225     list->push_back(kv.second);
226 }
227 /** @brief Register a new netpoint to the system */
228 void Engine::netpointRegister(simgrid::kernel::routing::NetPoint* point)
229 {
230   // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
231   pimpl->netpoints_[point->get_name()] = point;
232   // });
233 }
234 /** @brief Unregister a given netpoint */
235 void Engine::netpointUnregister(simgrid::kernel::routing::NetPoint* point)
236 {
237   simgrid::simix::kernelImmediate([this, point] {
238     pimpl->netpoints_.erase(point->get_name());
239     delete point;
240   });
241 }
242
243 bool Engine::isInitialized()
244 {
245   return Engine::instance_ != nullptr;
246 }
247 void Engine::setConfig(std::string str)
248 {
249   xbt_cfg_set_parse(str.c_str());
250 }
251 }
252 } // namespace