Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add new signal trigerred when some config has been done in the XML
[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_config_in_platform_done;
29 xbt::signal<void()> on_simulation_end;
30 xbt::signal<void(double)> on_time_advance;
31 xbt::signal<void(void)> on_deadlock;
32
33 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
34
35 Engine::Engine(int* argc, char** argv)
36 {
37   xbt_assert(s4u::Engine::instance_ == nullptr,
38              "It is currently forbidden to create more than one instance of s4u::Engine");
39   TRACE_global_init();
40   SIMIX_global_init(argc, argv);
41
42   pimpl                  = new kernel::EngineImpl();
43   s4u::Engine::instance_ = this;
44 }
45
46 Engine::~Engine()
47 {
48   delete pimpl;
49   s4u::Engine::instance_ = nullptr;
50 }
51
52 /** @brief Retrieve the engine singleton */
53 Engine* Engine::get_instance()
54 {
55   if (s4u::Engine::instance_ == nullptr)
56     return new Engine(0, nullptr);
57   else
58     return s4u::Engine::instance_;
59 }
60
61 void Engine::shutdown()
62 {
63   delete s4u::Engine::instance_;
64   s4u::Engine::instance_ = nullptr;
65 }
66
67 double Engine::get_clock()
68 {
69   return SIMIX_get_clock();
70 }
71
72 void Engine::load_platform(const char* platf)
73 {
74   SIMIX_create_environment(platf);
75 }
76
77 void Engine::register_function(const char* name, int (*code)(int, char**))
78 {
79   SIMIX_function_register(name, code);
80 }
81 void Engine::register_default(int (*code)(int, char**))
82 {
83   SIMIX_function_register_default(code);
84 }
85 void Engine::load_deployment(const char* deploy)
86 {
87   SIMIX_launch_application(deploy);
88 }
89 /** @brief Returns the amount of hosts in the platform */
90 size_t Engine::get_host_count()
91 {
92   return pimpl->hosts_.size();
93 }
94 /** @brief Fills the passed list with all hosts found in the platform
95  *  @deprecated Please prefer Engine::getAllHosts()
96  */
97 void Engine::getHostList(std::vector<Host*>* list)
98 {
99   for (auto const& kv : pimpl->hosts_)
100     list->push_back(kv.second);
101 }
102
103 /** @brief Returns the list of all hosts found in the platform */
104 std::vector<Host*> Engine::get_all_hosts()
105 {
106   std::vector<Host*> res;
107   for (auto const& kv : pimpl->hosts_)
108     res.push_back(kv.second);
109   return res;
110 }
111
112 void Engine::host_register(std::string name, simgrid::s4u::Host* host)
113 {
114   pimpl->hosts_[name] = host;
115 }
116
117 void Engine::host_unregister(std::string name)
118 {
119   pimpl->hosts_.erase(name);
120 }
121
122 simgrid::s4u::Host* Engine::host_by_name(std::string name)
123 {
124   return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
125 }
126
127 simgrid::s4u::Host* Engine::host_by_name_or_null(std::string name)
128 {
129   auto host = pimpl->hosts_.find(name);
130   return host == pimpl->hosts_.end() ? nullptr : host->second;
131 }
132
133 /** @brief Returns the amount of storages in the platform */
134 size_t Engine::get_storage_count()
135 {
136   return pimpl->storages_.size();
137 }
138
139 /** @brief Returns the list of all storages found in the platform */
140 std::vector<Storage*> Engine::get_all_storages()
141 {
142   std::vector<Storage*> res;
143   for (auto const& kv : pimpl->storages_)
144     res.push_back(kv.second);
145   return res;
146 }
147
148 simgrid::s4u::Storage* Engine::storage_by_name(std::string name)
149 {
150   return pimpl->storages_.at(name); // Will raise a std::out_of_range if the host does not exist
151 }
152
153 simgrid::s4u::Storage* Engine::storage_by_name_or_null(std::string name)
154 {
155   auto storage = pimpl->storages_.find(name);
156   return storage == pimpl->storages_.end() ? nullptr : storage->second;
157 }
158
159 void Engine::storage_register(std::string name, simgrid::s4u::Storage* storage)
160 {
161   pimpl->storages_[name] = storage;
162 }
163
164 void Engine::storage_unregister(std::string name)
165 {
166   pimpl->storages_.erase(name);
167 }
168
169 /** @brief Returns the amount of links in the platform */
170 size_t Engine::get_link_count()
171 {
172   return kernel::resource::LinkImpl::linksCount();
173 }
174
175 /** @brief Returns the list of all links found in the platform */
176 std::vector<Link*> Engine::get_all_links()
177 {
178   std::vector<Link*> res;
179   kernel::resource::LinkImpl::linksList(&res);
180   return res;
181 }
182
183 void Engine::run()
184 {
185   if (MC_is_active()) {
186     MC_run();
187   } else {
188     SIMIX_run();
189   }
190 }
191
192 s4u::NetZone* Engine::getNetRoot()
193 {
194   return pimpl->netzone_root_;
195 }
196
197 static s4u::NetZone* netzone_by_name_recursive(s4u::NetZone* current, const char* name)
198 {
199   if (not strcmp(current->get_cname(), name))
200     return current;
201
202   for (auto const& elem : *(current->getChildren())) {
203     simgrid::s4u::NetZone* tmp = netzone_by_name_recursive(elem, name);
204     if (tmp != nullptr) {
205       return tmp;
206     }
207   }
208   return nullptr;
209 }
210
211 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
212 NetZone* Engine::getNetzoneByNameOrNull(const char* name)
213 {
214   return netzone_by_name_recursive(getNetRoot(), name);
215 }
216
217 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
218 simgrid::kernel::routing::NetPoint* Engine::getNetpointByNameOrNull(std::string name)
219 {
220   auto netp = pimpl->netpoints_.find(name);
221   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
222 }
223
224 /** @brief Fill the provided vector with all existing netpoints */
225 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
226 {
227   for (auto const& kv : pimpl->netpoints_)
228     list->push_back(kv.second);
229 }
230 std::vector<simgrid::kernel::routing::NetPoint*> Engine::get_all_netpoints()
231 {
232   std::vector<simgrid::kernel::routing::NetPoint*> res;
233   for (auto const& kv : pimpl->netpoints_)
234     res.push_back(kv.second);
235   return res;
236 }
237
238 /** @brief Register a new netpoint to the system */
239 void Engine::netpoint_register(simgrid::kernel::routing::NetPoint* point)
240 {
241   // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
242   pimpl->netpoints_[point->get_name()] = point;
243   // });
244 }
245 /** @brief Unregister a given netpoint */
246 void Engine::netpoint_unregister(simgrid::kernel::routing::NetPoint* point)
247 {
248   simgrid::simix::kernelImmediate([this, point] {
249     pimpl->netpoints_.erase(point->get_name());
250     delete point;
251   });
252 }
253
254 bool Engine::is_initialized()
255 {
256   return Engine::instance_ != nullptr;
257 }
258 void Engine::set_config(std::string str)
259 {
260   simgrid::config::set_parse(std::move(str));
261 }
262 } // namespace s4u
263 } // namespace simgrid