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
23 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
27 xbt::signal<void()> on_platform_creation;
28 xbt::signal<void()> on_platform_created;
29 xbt::signal<void()> on_simulation_end;
30 xbt::signal<void(double)> on_time_advance;
31 xbt::signal<void(void)> on_deadlock;
33 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
35 Engine::Engine(int* argc, char** argv)
37 xbt_assert(s4u::Engine::instance_ == nullptr,
38 "It is currently forbidden to create more than one instance of s4u::Engine");
40 SIMIX_global_init(argc, argv);
42 pimpl = new kernel::EngineImpl();
43 s4u::Engine::instance_ = this;
49 s4u::Engine::instance_ = nullptr;
52 /** @brief Retrieve the engine singleton */
53 Engine* Engine::get_instance()
55 if (s4u::Engine::instance_ == nullptr)
56 return new Engine(0, nullptr);
58 return s4u::Engine::instance_;
61 void Engine::shutdown()
63 delete s4u::Engine::instance_;
64 s4u::Engine::instance_ = nullptr;
67 double Engine::get_clock()
69 return SIMIX_get_clock();
72 void Engine::load_platform(const char* platf)
74 SIMIX_create_environment(platf);
77 void Engine::register_function(const char* name, int (*code)(int, char**))
79 SIMIX_function_register(name, code);
81 void Engine::register_default(int (*code)(int, char**))
83 SIMIX_function_register_default(code);
85 void Engine::load_deployment(const char* deploy)
87 SIMIX_launch_application(deploy);
89 /** @brief Returns the amount of hosts in the platform */
90 size_t Engine::get_host_count()
92 return pimpl->hosts_.size();
94 /** @brief Fills the passed list with all hosts found in the platform
95 * @deprecated Please prefer Engine::getAllHosts()
97 void Engine::getHostList(std::vector<Host*>* list)
99 for (auto const& kv : pimpl->hosts_)
100 list->push_back(kv.second);
103 /** @brief Returns the list of all hosts found in the platform */
104 std::vector<Host*> Engine::get_all_hosts()
106 std::vector<Host*> res;
107 for (auto const& kv : pimpl->hosts_)
108 res.push_back(kv.second);
112 std::vector<Host*> Engine::get_filtered_hosts(std::function<bool(Host*)> filter)
114 std::vector<Host*> hosts;
115 for (auto const& kv : pimpl->hosts_) {
116 if (filter(kv.second))
117 hosts.push_back(kv.second);
123 void Engine::host_register(std::string name, simgrid::s4u::Host* host)
125 pimpl->hosts_[name] = host;
128 void Engine::host_unregister(std::string name)
130 pimpl->hosts_.erase(name);
133 simgrid::s4u::Host* Engine::host_by_name(std::string name)
135 return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist
138 simgrid::s4u::Host* Engine::host_by_name_or_null(std::string name)
140 auto host = pimpl->hosts_.find(name);
141 return host == pimpl->hosts_.end() ? nullptr : host->second;
144 /** @brief Returns the amount of storages in the platform */
145 size_t Engine::get_storage_count()
147 return pimpl->storages_.size();
150 /** @brief Returns the list of all storages found in the platform */
151 std::vector<Storage*> Engine::get_all_storages()
153 std::vector<Storage*> res;
154 for (auto const& kv : pimpl->storages_)
155 res.push_back(kv.second);
159 simgrid::s4u::Storage* Engine::storage_by_name(std::string name)
161 return pimpl->storages_.at(name); // Will raise a std::out_of_range if the host does not exist
164 simgrid::s4u::Storage* Engine::storage_by_name_or_null(std::string name)
166 auto storage = pimpl->storages_.find(name);
167 return storage == pimpl->storages_.end() ? nullptr : storage->second;
170 void Engine::storage_register(std::string name, simgrid::s4u::Storage* storage)
172 pimpl->storages_[name] = storage;
175 void Engine::storage_unregister(std::string name)
177 pimpl->storages_.erase(name);
180 /** @brief Returns the amount of links in the platform */
181 size_t Engine::get_link_count()
183 return kernel::resource::LinkImpl::linksCount();
186 /** @brief Returns the list of all links found in the platform */
187 std::vector<Link*> Engine::get_all_links()
189 std::vector<Link*> res;
190 kernel::resource::LinkImpl::linksList(&res);
194 std::vector<Link*> Engine::get_filtered_links(std::function<bool(Link*)> filter)
196 // FIXME: This is a terrible implementation and should be done
197 // without getting all links first.
198 std::vector<Link*> res;
199 kernel::resource::LinkImpl::linksList(&res);
200 std::vector<Link*> filtered_list;
201 for (auto& link : res) {
203 filtered_list.push_back(link);
205 return filtered_list;
208 size_t Engine::get_actor_count()
210 return simix_global->process_list.size();
213 std::vector<ActorPtr> Engine::get_all_actors()
215 std::vector<ActorPtr> actor_list;
216 actor_list.push_back(simgrid::s4u::Actor::self());
217 for (auto& kv : simix_global->process_list) {
218 actor_list.push_back(kv.second->iface());
223 std::vector<ActorPtr> Engine::get_filtered_actors(std::function<bool(ActorPtr)> filter)
225 std::vector<ActorPtr> actor_list;
226 for (auto& kv : simix_global->process_list) {
227 if (filter(kv.second->iface()))
228 actor_list.push_back(kv.second->iface());
235 if (MC_is_active()) {
242 /** @brief Retrieve the root netzone, containing all others */
243 s4u::NetZone* Engine::get_netzone_root()
245 return pimpl->netzone_root_;
247 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
248 void Engine::set_netzone_root(s4u::NetZone* netzone)
250 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
251 pimpl->netzone_root_ = static_cast<kernel::routing::NetZoneImpl*>(netzone);
254 static s4u::NetZone* netzone_by_name_recursive(s4u::NetZone* current, const char* name)
256 if (not strcmp(current->get_cname(), name))
259 for (auto const& elem : *(current->getChildren())) {
260 simgrid::s4u::NetZone* tmp = netzone_by_name_recursive(elem, name);
261 if (tmp != nullptr) {
268 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
269 NetZone* Engine::netzone_by_name_or_null(const char* name)
271 return netzone_by_name_recursive(get_netzone_root(), name);
274 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
275 simgrid::kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(std::string name)
277 auto netp = pimpl->netpoints_.find(name);
278 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
281 /** @brief Fill the provided vector with all existing netpoints */
282 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
284 for (auto const& kv : pimpl->netpoints_)
285 list->push_back(kv.second);
287 std::vector<simgrid::kernel::routing::NetPoint*> Engine::get_all_netpoints()
289 std::vector<simgrid::kernel::routing::NetPoint*> res;
290 for (auto const& kv : pimpl->netpoints_)
291 res.push_back(kv.second);
295 /** @brief Register a new netpoint to the system */
296 void Engine::netpoint_register(simgrid::kernel::routing::NetPoint* point)
298 // simgrid::simix::simcall([&]{ FIXME: this segfaults in set_thread
299 pimpl->netpoints_[point->get_name()] = point;
302 /** @brief Unregister a given netpoint */
303 void Engine::netpoint_unregister(simgrid::kernel::routing::NetPoint* point)
305 simgrid::simix::simcall([this, point] {
306 pimpl->netpoints_.erase(point->get_name());
311 bool Engine::is_initialized()
313 return Engine::instance_ != nullptr;
315 void Engine::set_config(std::string str)
317 simgrid::config::set_parse(std::move(str));
320 } // namespace simgrid
322 /* **************************** Public C interface *************************** */
323 void sg_engine_load_platform(const char* file)
325 simgrid::s4u::Engine::get_instance()->load_platform(file);
328 void sg_engine_load_deployment(const char* file)
330 simgrid::s4u::Engine::get_instance()->load_deployment(file);
333 void sg_engine_register_function(const char* name, int (*code)(int, char**))
335 simgrid::s4u::Engine::get_instance()->register_function(name, code);
337 void sg_engine_register_default(int (*code)(int, char**))
339 simgrid::s4u::Engine::get_instance()->register_default(code);
341 double sg_engine_get_clock()
343 return simgrid::s4u::Engine::get_clock();