Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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 /** @brief Retrieve the engine singleton */
51 Engine* Engine::get_instance()
52 {
53   if (s4u::Engine::instance_ == nullptr)
54     return new Engine(0, nullptr);
55   else
56     return s4u::Engine::instance_;
57 }
58
59 void Engine::shutdown()
60 {
61   delete s4u::Engine::instance_;
62   s4u::Engine::instance_ = nullptr;
63 }
64
65 double Engine::get_clock()
66 {
67   return SIMIX_get_clock();
68 }
69
70 void Engine::load_platform(const char* platf)
71 {
72   SIMIX_create_environment(platf);
73 }
74
75 void Engine::register_function(const char* name, int (*code)(int, char**))
76 {
77   SIMIX_function_register(name, code);
78 }
79 void Engine::register_default(int (*code)(int, char**))
80 {
81   SIMIX_function_register_default(code);
82 }
83 void Engine::load_deployment(const char* deploy)
84 {
85   SIMIX_launch_application(deploy);
86 }
87 /** @brief Returns the amount of hosts in the platform */
88 size_t Engine::get_host_count()
89 {
90   return pimpl->hosts_.size();
91 }
92 /** @brief Fills the passed list with all hosts found in the platform
93  *  @deprecated Please prefer Engine::getAllHosts()
94  */
95 void Engine::getHostList(std::vector<Host*>* list)
96 {
97   for (auto const& kv : pimpl->hosts_)
98     list->push_back(kv.second);
99 }
100
101 /** @brief Returns the list of all hosts found in the platform */
102 std::vector<Host*> Engine::get_all_hosts()
103 {
104   std::vector<Host*> res;
105   for (auto const& kv : pimpl->hosts_)
106     res.push_back(kv.second);
107   return res;
108 }
109
110 void Engine::host_register(std::string name, simgrid::s4u::Host* host)
111 {
112   pimpl->hosts_[name] = host;
113 }
114
115 void Engine::host_unregister(std::string name)
116 {
117   pimpl->hosts_.erase(name);
118 }
119
120 simgrid::s4u::Host* Engine::host_by_name(std::string name)
121 {
122   return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
123 }
124
125 simgrid::s4u::Host* Engine::host_by_name_or_null(std::string name)
126 {
127   auto host = pimpl->hosts_.find(name);
128   return host == pimpl->hosts_.end() ? nullptr : host->second;
129 }
130
131 /** @brief Returns the amount of storages in the platform */
132 size_t Engine::get_storage_count()
133 {
134   return pimpl->storages_.size();
135 }
136
137 /** @brief Returns the list of all storages found in the platform */
138 std::vector<Storage*> Engine::get_all_storages()
139 {
140   std::vector<Storage*> res;
141   for (auto const& kv : pimpl->storages_)
142     res.push_back(kv.second);
143   return res;
144 }
145
146 simgrid::s4u::Storage* Engine::storage_by_name(std::string name)
147 {
148   return pimpl->storages_.at(name); // Will raise a std::out_of_range if the host does not exist
149 }
150
151 simgrid::s4u::Storage* Engine::storage_by_name_or_null(std::string name)
152 {
153   auto storage = pimpl->storages_.find(name);
154   return storage == pimpl->storages_.end() ? nullptr : storage->second;
155 }
156
157 void Engine::storage_register(std::string name, simgrid::s4u::Storage* storage)
158 {
159   pimpl->storages_[name] = storage;
160 }
161
162 void Engine::storage_unregister(std::string name)
163 {
164   pimpl->storages_.erase(name);
165 }
166
167 /** @brief Returns the amount of links in the platform */
168 size_t Engine::get_link_count()
169 {
170   return kernel::resource::LinkImpl::linksCount();
171 }
172
173 /** @brief Returns the list of all links found in the platform */
174 std::vector<Link*> Engine::get_all_links()
175 {
176   std::vector<Link*> res;
177   kernel::resource::LinkImpl::linksList(&res);
178   return res;
179 }
180
181 void Engine::run()
182 {
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->netzone_root_;
193 }
194
195 static s4u::NetZone* netzone_by_name_recursive(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 = netzone_by_name_recursive(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 netzone_by_name_recursive(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 std::vector<simgrid::kernel::routing::NetPoint*> Engine::get_all_netpoints()
229 {
230   std::vector<simgrid::kernel::routing::NetPoint*> res;
231   for (auto const& kv : pimpl->netpoints_)
232     res.push_back(kv.second);
233   return res;
234 }
235
236 /** @brief Register a new netpoint to the system */
237 void Engine::netpoint_register(simgrid::kernel::routing::NetPoint* point)
238 {
239   // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
240   pimpl->netpoints_[point->get_name()] = point;
241   // });
242 }
243 /** @brief Unregister a given netpoint */
244 void Engine::netpoint_unregister(simgrid::kernel::routing::NetPoint* point)
245 {
246   simgrid::simix::kernelImmediate([this, point] {
247     pimpl->netpoints_.erase(point->get_name());
248     delete point;
249   });
250 }
251
252 bool Engine::is_initialized()
253 {
254   return Engine::instance_ != nullptr;
255 }
256 void Engine::set_config(std::string str)
257 {
258   simgrid::config::set_parse(std::move(str));
259 }
260 } // namespace s4u
261 } // namespace simgrid