1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2018. The SimGrid Team. All rights reserved. */
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. */
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/simix/smx_private.hpp" // For access to simix_global->process_list
18 #include "src/instr/instr_private.hpp"
19 #include "src/kernel/EngineImpl.hpp"
20 #include "src/surf/network_interface.hpp"
21 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
25 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
29 xbt::signal<void()> on_platform_creation;
30 xbt::signal<void()> on_platform_created;
31 xbt::signal<void()> on_simulation_end;
32 xbt::signal<void(double)> on_time_advance;
33 xbt::signal<void(void)> on_deadlock;
35 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
37 Engine::Engine(int* argc, char** argv)
39 xbt_assert(s4u::Engine::instance_ == nullptr,
40 "It is currently forbidden to create more than one instance of s4u::Engine");
42 SIMIX_global_init(argc, argv);
44 pimpl = new kernel::EngineImpl();
45 s4u::Engine::instance_ = this;
51 s4u::Engine::instance_ = nullptr;
54 /** @brief Retrieve the engine singleton */
55 Engine* Engine::get_instance()
57 if (s4u::Engine::instance_ == nullptr)
58 return new Engine(0, nullptr);
60 return s4u::Engine::instance_;
63 void Engine::shutdown()
65 delete s4u::Engine::instance_;
66 s4u::Engine::instance_ = nullptr;
69 double Engine::get_clock()
71 return SIMIX_get_clock();
74 void Engine::load_platform(std::string platf)
76 SIMIX_create_environment(platf);
79 void Engine::register_function(std::string name, int (*code)(int, char**))
81 SIMIX_function_register(name, code);
83 void Engine::register_function(std::string name, void (*code)(std::vector<std::string>))
85 SIMIX_function_register(name, code);
87 void Engine::register_default(int (*code)(int, char**))
89 SIMIX_function_register_default(code);
91 void Engine::load_deployment(std::string deploy)
93 SIMIX_launch_application(deploy);
95 /** @brief Returns the amount of hosts in the platform */
96 size_t Engine::get_host_count()
98 return pimpl->hosts_.size();
100 /** @brief Fills the passed list with all hosts found in the platform
101 * @deprecated Please prefer Engine::getAllHosts()
103 void Engine::getHostList(std::vector<Host*>* list)
105 for (auto const& kv : pimpl->hosts_)
106 list->push_back(kv.second);
109 std::vector<Host*> Engine::get_all_hosts()
111 std::vector<Host*> res;
112 for (auto const& kv : pimpl->hosts_)
113 res.push_back(kv.second);
117 std::vector<Host*> Engine::get_filtered_hosts(std::function<bool(Host*)> filter)
119 std::vector<Host*> hosts;
120 for (auto const& kv : pimpl->hosts_) {
121 if (filter(kv.second))
122 hosts.push_back(kv.second);
128 void Engine::host_register(std::string name, simgrid::s4u::Host* host)
130 pimpl->hosts_[name] = host;
133 void Engine::host_unregister(std::string name)
135 pimpl->hosts_.erase(name);
138 /** @brief Find an host from its name.
140 * @throw std::invalid_argument if the searched host does not exist.
142 simgrid::s4u::Host* Engine::host_by_name(std::string name)
144 if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
145 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
146 return pimpl->hosts_.at(name);
149 /** @brief Find an host from its name (or nullptr if that host does not exist) */
150 simgrid::s4u::Host* Engine::host_by_name_or_null(std::string name)
152 auto host = pimpl->hosts_.find(name);
153 return host == pimpl->hosts_.end() ? nullptr : host->second;
156 /** @brief Find a link from its name.
158 * @throw std::invalid_argument if the searched link does not exist.
160 simgrid::s4u::Link* Engine::link_by_name(std::string name)
162 if (pimpl->links_.find(name) == pimpl->links_.end())
163 throw std::invalid_argument(std::string("Link not found: ") + name);
165 return pimpl->links_.at(name);
168 /** @brief Find an link from its name (or nullptr if that link does not exist) */
169 simgrid::s4u::Link* Engine::link_by_name_or_null(std::string name)
171 auto link = pimpl->links_.find(name);
172 return link == pimpl->links_.end() ? nullptr : link->second;
175 void Engine::link_register(std::string name, simgrid::s4u::Link* link)
177 pimpl->links_[name] = link;
180 void Engine::link_unregister(std::string name)
182 pimpl->links_.erase(name);
185 /** @brief Returns the amount of storages in the platform */
186 size_t Engine::get_storage_count()
188 return pimpl->storages_.size();
191 /** @brief Returns the list of all storages found in the platform */
192 std::vector<Storage*> Engine::get_all_storages()
194 std::vector<Storage*> res;
195 for (auto const& kv : pimpl->storages_)
196 res.push_back(kv.second);
200 /** @brief Find a storage from its name.
202 * @throw std::invalid_argument if the searched storage does not exist.
204 simgrid::s4u::Storage* Engine::storage_by_name(std::string name)
206 if (pimpl->storages_.find(name) == pimpl->storages_.end())
207 throw std::invalid_argument(std::string("Storage not found: ") + name);
209 return pimpl->storages_.at(name);
212 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
213 simgrid::s4u::Storage* Engine::storage_by_name_or_null(std::string name)
215 auto storage = pimpl->storages_.find(name);
216 return storage == pimpl->storages_.end() ? nullptr : storage->second;
219 void Engine::storage_register(std::string name, simgrid::s4u::Storage* storage)
221 pimpl->storages_[name] = storage;
224 void Engine::storage_unregister(std::string name)
226 pimpl->storages_.erase(name);
229 /** @brief Returns the amount of links in the platform */
230 size_t Engine::get_link_count()
232 return pimpl->links_.size();
235 /** @brief Returns the list of all links found in the platform */
236 std::vector<Link*> Engine::get_all_links()
238 std::vector<Link*> res;
239 for (auto const& kv : pimpl->links_)
240 res.push_back(kv.second);
244 std::vector<Link*> Engine::get_filtered_links(std::function<bool(Link*)> filter)
246 std::vector<Link*> filtered_list;
247 for (auto const& kv : pimpl->links_)
248 if (filter(kv.second))
249 filtered_list.push_back(kv.second);
250 return filtered_list;
253 size_t Engine::get_actor_count()
255 return simix_global->process_list.size();
258 std::vector<ActorPtr> Engine::get_all_actors()
260 std::vector<ActorPtr> actor_list;
261 actor_list.push_back(simgrid::s4u::Actor::self());
262 for (auto& kv : simix_global->process_list) {
263 actor_list.push_back(kv.second->iface());
268 std::vector<ActorPtr> Engine::get_filtered_actors(std::function<bool(ActorPtr)> filter)
270 std::vector<ActorPtr> actor_list;
271 for (auto& kv : simix_global->process_list) {
272 if (filter(kv.second->iface()))
273 actor_list.push_back(kv.second->iface());
280 /* Clean IO before the run */
284 if (MC_is_active()) {
291 /** @brief Retrieve the root netzone, containing all others */
292 s4u::NetZone* Engine::get_netzone_root()
294 return pimpl->netzone_root_->get_iface();
296 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
297 void Engine::set_netzone_root(s4u::NetZone* netzone)
299 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
300 pimpl->netzone_root_ = netzone->get_impl();
303 static s4u::NetZone* netzone_by_name_recursive(s4u::NetZone* current, std::string name)
305 if (current->get_name() == name)
308 for (auto const& elem : current->get_children()) {
309 simgrid::s4u::NetZone* tmp = netzone_by_name_recursive(elem, name);
310 if (tmp != nullptr) {
317 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
318 NetZone* Engine::netzone_by_name_or_null(std::string name)
320 return netzone_by_name_recursive(get_netzone_root(), name);
323 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
324 simgrid::kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(std::string name)
326 auto netp = pimpl->netpoints_.find(name);
327 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
330 /** @brief Fill the provided vector with all existing netpoints */
331 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
333 for (auto const& kv : pimpl->netpoints_)
334 list->push_back(kv.second);
336 std::vector<simgrid::kernel::routing::NetPoint*> Engine::get_all_netpoints()
338 std::vector<simgrid::kernel::routing::NetPoint*> res;
339 for (auto const& kv : pimpl->netpoints_)
340 res.push_back(kv.second);
344 /** @brief Register a new netpoint to the system */
345 void Engine::netpoint_register(simgrid::kernel::routing::NetPoint* point)
347 // simgrid::simix::simcall([&]{ FIXME: this segfaults in set_thread
348 pimpl->netpoints_[point->get_name()] = point;
351 /** @brief Unregister a given netpoint */
352 void Engine::netpoint_unregister(simgrid::kernel::routing::NetPoint* point)
354 simgrid::simix::simcall([this, point] {
355 pimpl->netpoints_.erase(point->get_name());
360 bool Engine::is_initialized()
362 return Engine::instance_ != nullptr;
364 void Engine::set_config(std::string str)
366 simgrid::config::set_parse(std::move(str));
369 } // namespace simgrid
371 /* **************************** Public C interface *************************** */
372 void simgrid_init(int* argc, char** argv)
374 simgrid::s4u::Engine e(argc, argv);
376 void simgrid_load_platform(const char* file)
378 simgrid::s4u::Engine::get_instance()->load_platform(file);
381 void simgrid_load_deployment(const char* file)
383 simgrid::s4u::Engine::get_instance()->load_deployment(file);
387 simgrid::s4u::Engine::get_instance()->run();
389 void simgrid_register_function(const char* name, int (*code)(int, char**))
391 simgrid::s4u::Engine::get_instance()->register_function(name, code);
393 void simgrid_register_default(int (*code)(int, char**))
395 simgrid::s4u::Engine::get_instance()->register_default(code);
397 double simgrid_get_clock()
399 return simgrid::s4u::Engine::get_clock();