Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[S4U] Engine: Add Engine::get_filtered_links
[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 "mc/mc.h"
9 #include "simgrid/kernel/routing/NetPoint.hpp"
10 #include "simgrid/kernel/routing/NetZoneImpl.hpp"
11 #include "simgrid/s4u/Engine.hpp"
12 #include "simgrid/s4u/Host.hpp"
13 #include "simgrid/s4u/Mailbox.hpp"
14 #include "simgrid/s4u/NetZone.hpp"
15 #include "simgrid/s4u/Storage.hpp"
16 #include "simgrid/simix.h"
17 #include "src/instr/instr_private.hpp"
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()> on_platform_creation;
27 xbt::signal<void()> on_platform_created;
28 xbt::signal<void()> on_simulation_end;
29 xbt::signal<void(double)> on_time_advance;
30 xbt::signal<void(void)> on_deadlock;
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 {
36   xbt_assert(s4u::Engine::instance_ == nullptr,
37              "It is currently forbidden to create more than one instance of s4u::Engine");
38   TRACE_global_init();
39   SIMIX_global_init(argc, argv);
40
41   pimpl                  = new kernel::EngineImpl();
42   s4u::Engine::instance_ = this;
43 }
44
45 Engine::~Engine()
46 {
47   delete pimpl;
48   s4u::Engine::instance_ = nullptr;
49 }
50
51 /** @brief Retrieve the engine singleton */
52 Engine* Engine::get_instance()
53 {
54   if (s4u::Engine::instance_ == nullptr)
55     return new Engine(0, nullptr);
56   else
57     return s4u::Engine::instance_;
58 }
59
60 void Engine::shutdown()
61 {
62   delete s4u::Engine::instance_;
63   s4u::Engine::instance_ = nullptr;
64 }
65
66 double Engine::get_clock()
67 {
68   return SIMIX_get_clock();
69 }
70
71 void Engine::load_platform(const char* platf)
72 {
73   SIMIX_create_environment(platf);
74 }
75
76 void Engine::register_function(const char* name, int (*code)(int, char**))
77 {
78   SIMIX_function_register(name, code);
79 }
80 void Engine::register_default(int (*code)(int, char**))
81 {
82   SIMIX_function_register_default(code);
83 }
84 void Engine::load_deployment(const char* deploy)
85 {
86   SIMIX_launch_application(deploy);
87 }
88 /** @brief Returns the amount of hosts in the platform */
89 size_t Engine::get_host_count()
90 {
91   return pimpl->hosts_.size();
92 }
93 /** @brief Fills the passed list with all hosts found in the platform
94  *  @deprecated Please prefer Engine::getAllHosts()
95  */
96 void 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::get_all_hosts()
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 std::vector<Host*> Engine::get_filtered_hosts(std::function<bool(Host*)> filter)
112 {
113   std::vector<Host*> hosts;
114   for (auto const& kv : pimpl->hosts_) {
115     if (filter(kv.second))
116       hosts.push_back(kv.second);
117   }
118
119   return hosts;
120 }
121
122 void Engine::host_register(std::string name, simgrid::s4u::Host* host)
123 {
124   pimpl->hosts_[name] = host;
125 }
126
127 void Engine::host_unregister(std::string name)
128 {
129   pimpl->hosts_.erase(name);
130 }
131
132 simgrid::s4u::Host* Engine::host_by_name(std::string name)
133 {
134   return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
135 }
136
137 simgrid::s4u::Host* Engine::host_by_name_or_null(std::string name)
138 {
139   auto host = pimpl->hosts_.find(name);
140   return host == pimpl->hosts_.end() ? nullptr : host->second;
141 }
142
143 /** @brief Returns the amount of storages in the platform */
144 size_t Engine::get_storage_count()
145 {
146   return pimpl->storages_.size();
147 }
148
149 /** @brief Returns the list of all storages found in the platform */
150 std::vector<Storage*> Engine::get_all_storages()
151 {
152   std::vector<Storage*> res;
153   for (auto const& kv : pimpl->storages_)
154     res.push_back(kv.second);
155   return res;
156 }
157
158 simgrid::s4u::Storage* Engine::storage_by_name(std::string name)
159 {
160   return pimpl->storages_.at(name); // Will raise a std::out_of_range if the host does not exist
161 }
162
163 simgrid::s4u::Storage* Engine::storage_by_name_or_null(std::string name)
164 {
165   auto storage = pimpl->storages_.find(name);
166   return storage == pimpl->storages_.end() ? nullptr : storage->second;
167 }
168
169 void Engine::storage_register(std::string name, simgrid::s4u::Storage* storage)
170 {
171   pimpl->storages_[name] = storage;
172 }
173
174 void Engine::storage_unregister(std::string name)
175 {
176   pimpl->storages_.erase(name);
177 }
178
179 /** @brief Returns the amount of links in the platform */
180 size_t Engine::get_link_count()
181 {
182   return kernel::resource::LinkImpl::linksCount();
183 }
184
185 /** @brief Returns the list of all links found in the platform */
186 std::vector<Link*> Engine::get_all_links()
187 {
188   std::vector<Link*> res;
189   kernel::resource::LinkImpl::linksList(&res);
190   return res;
191 }
192
193 std::vector<Link*> Engine::get_filtered_links(std::function<bool(Link*)> filter)
194 {
195   // FIXME: This is a terrible implementation and should be done
196   // without getting all links first.
197   std::vector<Link*> res;
198   kernel::resource::LinkImpl::linksList(&res);
199   std::vector<Link*> filtered_list;
200   for (auto& link : res) {
201     if (filter(link))
202       filtered_list.push_back(link);
203   }
204   return filtered_list;
205 }
206
207 void Engine::run()
208 {
209   if (MC_is_active()) {
210     MC_run();
211   } else {
212     SIMIX_run();
213   }
214 }
215
216 /** @brief Retrieve the root netzone, containing all others */
217 s4u::NetZone* Engine::get_netzone_root()
218 {
219   return pimpl->netzone_root_;
220 }
221 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
222 void Engine::set_netzone_root(s4u::NetZone* netzone)
223 {
224   xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
225   pimpl->netzone_root_ = static_cast<kernel::routing::NetZoneImpl*>(netzone);
226 }
227
228 static s4u::NetZone* netzone_by_name_recursive(s4u::NetZone* current, const char* name)
229 {
230   if (not strcmp(current->get_cname(), name))
231     return current;
232
233   for (auto const& elem : *(current->getChildren())) {
234     simgrid::s4u::NetZone* tmp = netzone_by_name_recursive(elem, name);
235     if (tmp != nullptr) {
236       return tmp;
237     }
238   }
239   return nullptr;
240 }
241
242 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
243 NetZone* Engine::netzone_by_name_or_null(const char* name)
244 {
245   return netzone_by_name_recursive(get_netzone_root(), name);
246 }
247
248 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
249 simgrid::kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(std::string name)
250 {
251   auto netp = pimpl->netpoints_.find(name);
252   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
253 }
254
255 /** @brief Fill the provided vector with all existing netpoints */
256 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
257 {
258   for (auto const& kv : pimpl->netpoints_)
259     list->push_back(kv.second);
260 }
261 std::vector<simgrid::kernel::routing::NetPoint*> Engine::get_all_netpoints()
262 {
263   std::vector<simgrid::kernel::routing::NetPoint*> res;
264   for (auto const& kv : pimpl->netpoints_)
265     res.push_back(kv.second);
266   return res;
267 }
268
269 /** @brief Register a new netpoint to the system */
270 void Engine::netpoint_register(simgrid::kernel::routing::NetPoint* point)
271 {
272   // simgrid::simix::simcall([&]{ FIXME: this segfaults in set_thread
273   pimpl->netpoints_[point->get_name()] = point;
274   // });
275 }
276 /** @brief Unregister a given netpoint */
277 void Engine::netpoint_unregister(simgrid::kernel::routing::NetPoint* point)
278 {
279   simgrid::simix::simcall([this, point] {
280     pimpl->netpoints_.erase(point->get_name());
281     delete point;
282   });
283 }
284
285 bool Engine::is_initialized()
286 {
287   return Engine::instance_ != nullptr;
288 }
289 void Engine::set_config(std::string str)
290 {
291   simgrid::config::set_parse(std::move(str));
292 }
293 } // namespace s4u
294 } // namespace simgrid
295
296 /* **************************** Public C interface *************************** */
297 void sg_engine_load_platform(const char* file)
298 {
299   simgrid::s4u::Engine::get_instance()->load_platform(file);
300 }
301
302 void sg_engine_load_deployment(const char* file)
303 {
304   simgrid::s4u::Engine::get_instance()->load_deployment(file);
305 }
306
307 void sg_engine_register_function(const char* name, int (*code)(int, char**))
308 {
309   simgrid::s4u::Engine::get_instance()->register_function(name, code);
310 }
311 void sg_engine_register_default(int (*code)(int, char**))
312 {
313   simgrid::s4u::Engine::get_instance()->register_default(code);
314 }
315 double sg_engine_get_clock()
316 {
317   return simgrid::s4u::Engine::get_clock();
318 }