Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4f59ca7692039720eb11addb5539a1695d0203b6
[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::get_clock()
65 {
66   return SIMIX_get_clock();
67 }
68
69 void Engine::load_platform(const char* platf)
70 {
71   SIMIX_create_environment(platf);
72 }
73
74 void Engine::register_function(const char* name, int (*code)(int, char**))
75 {
76   SIMIX_function_register(name, code);
77 }
78 void Engine::register_default(int (*code)(int, char**))
79 {
80   SIMIX_function_register_default(code);
81 }
82 void Engine::load_deployment(const char* deploy)
83 {
84   SIMIX_launch_application(deploy);
85 }
86 /** @brief Returns the amount of hosts in the platform */
87 size_t Engine::get_host_count()
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 Engine::getHostList(std::vector<Host*>* list)
95 {
96   for (auto const& kv : pimpl->hosts_)
97     list->push_back(kv.second);
98 }
99
100 /** @brief Returns the list of all hosts found in the platform */
101 std::vector<Host*> Engine::get_all_hosts()
102 {
103   std::vector<Host*> res;
104   for (auto const& kv : pimpl->hosts_)
105     res.push_back(kv.second);
106   return res;
107 }
108
109 void Engine::add_host(std::string name, simgrid::s4u::Host* host)
110 {
111   pimpl->hosts_[name] = host;
112 }
113
114 void Engine::del_host(std::string name)
115 {
116   pimpl->hosts_.erase(name);
117 }
118
119 simgrid::s4u::Host* Engine::host_by_name(std::string name)
120 {
121   return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
122 }
123
124 simgrid::s4u::Host* Engine::host_by_name_or_null(std::string name)
125 {
126   auto host = pimpl->hosts_.find(name);
127   return host == pimpl->hosts_.end() ? nullptr : host->second;
128 }
129
130 /** @brief Returns the amount of storages in the platform */
131 size_t Engine::get_storage_count()
132 {
133   return pimpl->storages_.size();
134 }
135
136 /** @brief Returns the list of all storages found in the platform */
137 std::vector<Storage*> Engine::get_all_storages()
138 {
139   std::vector<Storage*> res;
140   for (auto const& kv : pimpl->storages_)
141     res.push_back(kv.second);
142   return res;
143 }
144
145 simgrid::s4u::Storage* Engine::storage_by_name(std::string name)
146 {
147   return pimpl->storages_.at(name); // Will raise a std::out_of_range if the host does not exist
148 }
149
150 simgrid::s4u::Storage* Engine::storage_by_name_or_null(std::string name)
151 {
152   auto storage = pimpl->storages_.find(name);
153   return storage == pimpl->storages_.end() ? nullptr : storage->second;
154 }
155
156 void Engine::add_storage(std::string name, simgrid::s4u::Storage* storage)
157 {
158   pimpl->storages_[name] = storage;
159 }
160
161 void Engine::del_storage(std::string name)
162 {
163   pimpl->storages_.erase(name);
164 }
165
166 /** @brief Returns the amount of links in the platform */
167 size_t Engine::get_link_count()
168 {
169   return kernel::resource::LinkImpl::linksCount();
170 }
171
172 /** @brief Returns the list of all links found in the platform */
173 std::vector<Link*> Engine::get_all_links()
174 {
175   std::vector<Link*> res;
176   kernel::resource::LinkImpl::linksList(&res);
177   return res;
178 }
179
180 void Engine::run()
181 {
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   simgrid::config::set_parse(std::move(str));
250 }
251 } // namespace s4u
252 } // namespace simgrid