1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2019. 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/Disk.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/instr/instr_private.hpp"
19 #include "src/kernel/EngineImpl.hpp"
20 #include "src/simix/smx_private.hpp" // For access to simix_global->process_list
21 #include "src/surf/network_interface.hpp"
22 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
23 #include <simgrid/Exception.hpp>
27 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_engine, s4u, "Logging specific to S4U (engine)");
32 xbt::signal<void()> Engine::on_platform_creation;
33 xbt::signal<void()> Engine::on_platform_created;
34 xbt::signal<void()> Engine::on_simulation_end;
35 xbt::signal<void(double)> Engine::on_time_advance;
36 xbt::signal<void(void)> Engine::on_deadlock;
38 Engine* Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
40 Engine::Engine(int* argc, char** argv) : pimpl(new kernel::EngineImpl())
42 xbt_assert(Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
44 SIMIX_global_init(argc, argv);
46 Engine::instance_ = this;
52 Engine::instance_ = nullptr;
55 /** @brief Retrieve the engine singleton */
56 Engine* Engine::get_instance()
58 if (Engine::instance_ == nullptr) {
59 auto e = new Engine(0, nullptr);
60 xbt_assert(Engine::instance_ == e);
62 return Engine::instance_;
65 void Engine::shutdown()
67 delete Engine::instance_;
68 Engine::instance_ = nullptr;
71 double Engine::get_clock()
73 return SIMIX_get_clock();
77 * Creates a new platform, including hosts, links, and the routing table.
80 * See also: :ref:`platform`.
83 void Engine::load_platform(const std::string& platf)
85 double start = xbt_os_time();
87 parse_platform_file(platf);
88 } catch (const Exception& e) {
89 xbt_die("Error while loading %s: %s", platf.c_str(), e.what());
92 double end = xbt_os_time();
93 XBT_DEBUG("PARSE TIME: %g", (end - start));
96 /** Registers the main function of an actor that will be launched from the deployment file */
97 void Engine::register_function(const std::string& name, int (*code)(int, char**))
99 pimpl->register_function(name, code);
102 /** Registers the main function of an actor that will be launched from the deployment file */
103 void Engine::register_function(const std::string& name, void (*code)(std::vector<std::string>))
105 pimpl->register_function(name, code);
107 /** Registers a function as the default main function of actors
109 * It will be used as fallback when the function requested from the deployment file was not registered.
110 * It is used for trace-based simulations (see examples/s4u/replay-comms and similar).
112 void Engine::register_default(int (*code)(int, char**))
114 pimpl->register_default(code);
117 /** Load a deployment file and launch the actors that it contains
120 * See also: :ref:`deploy`.
123 void Engine::load_deployment(const std::string& deploy)
125 pimpl->load_deployment(deploy);
128 /** Returns the amount of hosts in the platform */
129 size_t Engine::get_host_count()
131 return pimpl->hosts_.size();
134 std::vector<Host*> Engine::get_all_hosts()
136 std::vector<Host*> res;
137 for (auto const& kv : pimpl->hosts_)
138 res.push_back(kv.second);
142 std::vector<Host*> Engine::get_filtered_hosts(const std::function<bool(Host*)>& filter)
144 std::vector<Host*> hosts;
145 for (auto const& kv : pimpl->hosts_) {
146 if (filter(kv.second))
147 hosts.push_back(kv.second);
153 void Engine::host_register(const std::string& name, Host* host)
155 pimpl->hosts_[name] = host;
158 void Engine::host_unregister(const std::string& name)
160 pimpl->hosts_.erase(name);
163 /** @brief Find a host from its name.
165 * @throw std::invalid_argument if the searched host does not exist.
167 Host* Engine::host_by_name(const std::string& name)
169 if (pimpl->hosts_.find(name) == pimpl->hosts_.end())
170 throw std::invalid_argument(std::string("Host not found: '") + name + std::string("'"));
171 return pimpl->hosts_.at(name);
174 /** @brief Find a host from its name (or nullptr if that host does not exist) */
175 Host* Engine::host_by_name_or_null(const std::string& name)
177 auto host = pimpl->hosts_.find(name);
178 return host == pimpl->hosts_.end() ? nullptr : host->second;
181 /** @brief Find a link from its name.
183 * @throw std::invalid_argument if the searched link does not exist.
185 Link* Engine::link_by_name(const std::string& name)
187 if (pimpl->links_.find(name) == pimpl->links_.end())
188 throw std::invalid_argument(std::string("Link not found: ") + name);
190 return pimpl->links_.at(name);
193 /** @brief Find an link from its name (or nullptr if that link does not exist) */
194 Link* Engine::link_by_name_or_null(const std::string& name)
196 auto link = pimpl->links_.find(name);
197 return link == pimpl->links_.end() ? nullptr : link->second;
200 void Engine::link_register(const std::string& name, Link* link)
202 pimpl->links_[name] = link;
205 void Engine::link_unregister(const std::string& name)
207 pimpl->links_.erase(name);
210 /** @brief Returns the amount of storages in the platform */
211 size_t Engine::get_storage_count()
213 return pimpl->storages_.size();
216 /** @brief Returns the list of all storages found in the platform */
217 std::vector<Storage*> Engine::get_all_storages()
219 std::vector<Storage*> res;
220 for (auto const& kv : pimpl->storages_)
221 res.push_back(kv.second);
225 /** @brief Find a storage from its name.
227 * @throw std::invalid_argument if the searched storage does not exist.
229 Storage* Engine::storage_by_name(const std::string& name)
231 if (pimpl->storages_.find(name) == pimpl->storages_.end())
232 throw std::invalid_argument(std::string("Storage not found: ") + name);
234 return pimpl->storages_.at(name);
237 /** @brief Find a storage from its name (or nullptr if that storage does not exist) */
238 Storage* Engine::storage_by_name_or_null(const std::string& name)
240 auto storage = pimpl->storages_.find(name);
241 return storage == pimpl->storages_.end() ? nullptr : storage->second;
244 void Engine::storage_register(const std::string& name, Storage* storage)
246 pimpl->storages_[name] = storage;
249 void Engine::storage_unregister(const std::string& name)
251 pimpl->storages_.erase(name);
254 /** @brief Returns the amount of links in the platform */
255 size_t Engine::get_link_count()
257 return pimpl->links_.size();
260 /** @brief Returns the list of all links found in the platform */
261 std::vector<Link*> Engine::get_all_links()
263 std::vector<Link*> res;
264 for (auto const& kv : pimpl->links_)
265 res.push_back(kv.second);
269 std::vector<Link*> Engine::get_filtered_links(const std::function<bool(Link*)>& filter)
271 std::vector<Link*> filtered_list;
272 for (auto const& kv : pimpl->links_)
273 if (filter(kv.second))
274 filtered_list.push_back(kv.second);
275 return filtered_list;
278 size_t Engine::get_actor_count()
280 return simix_global->process_list.size();
283 std::vector<ActorPtr> Engine::get_all_actors()
285 std::vector<ActorPtr> actor_list;
286 actor_list.push_back(simgrid::s4u::Actor::self());
287 for (auto& kv : simix_global->process_list) {
288 actor_list.push_back(kv.second->iface());
293 std::vector<ActorPtr> Engine::get_filtered_actors(const std::function<bool(ActorPtr)>& filter)
295 std::vector<ActorPtr> actor_list;
296 for (auto& kv : simix_global->process_list) {
297 if (filter(kv.second->iface()))
298 actor_list.push_back(kv.second->iface());
305 /* Clean IO before the run */
309 if (MC_is_active()) {
316 /** @brief Retrieve the root netzone, containing all others */
317 s4u::NetZone* Engine::get_netzone_root()
319 return pimpl->netzone_root_->get_iface();
321 /** @brief Set the root netzone, containing all others. Once set, it cannot be changed. */
322 void Engine::set_netzone_root(s4u::NetZone* netzone)
324 xbt_assert(pimpl->netzone_root_ == nullptr, "The root NetZone cannot be changed once set");
325 pimpl->netzone_root_ = netzone->get_impl();
328 static NetZone* netzone_by_name_recursive(NetZone* current, const std::string& name)
330 if (current->get_name() == name)
333 for (auto const& elem : current->get_children()) {
334 NetZone* tmp = netzone_by_name_recursive(elem, name);
335 if (tmp != nullptr) {
342 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
343 NetZone* Engine::netzone_by_name_or_null(const std::string& name)
345 return netzone_by_name_recursive(get_netzone_root(), name);
348 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
349 kernel::routing::NetPoint* Engine::netpoint_by_name_or_null(const std::string& name)
351 auto netp = pimpl->netpoints_.find(name);
352 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
355 std::vector<kernel::routing::NetPoint*> Engine::get_all_netpoints()
357 std::vector<kernel::routing::NetPoint*> res;
358 for (auto const& kv : pimpl->netpoints_)
359 res.push_back(kv.second);
363 /** @brief Register a new netpoint to the system */
364 void Engine::netpoint_register(kernel::routing::NetPoint* point)
366 // simgrid::kernel::actor::simcall([&]{ FIXME: this segfaults in set_thread
367 pimpl->netpoints_[point->get_name()] = point;
371 /** @brief Unregister a given netpoint */
372 void Engine::netpoint_unregister(kernel::routing::NetPoint* point)
374 kernel::actor::simcall([this, point] {
375 pimpl->netpoints_.erase(point->get_name());
380 bool Engine::is_initialized()
382 return Engine::instance_ != nullptr;
384 void Engine::set_config(const std::string& str)
386 config::set_parse(str);
389 } // namespace simgrid
391 /* **************************** Public C interface *************************** */
392 void simgrid_init(int* argc, char** argv)
394 simgrid::s4u::Engine e(argc, argv);
396 void simgrid_load_platform(const char* file)
398 simgrid::s4u::Engine::get_instance()->load_platform(file);
401 void simgrid_load_deployment(const char* file)
403 simgrid::s4u::Engine::get_instance()->load_deployment(file);
407 simgrid::s4u::Engine::get_instance()->run();
409 void simgrid_register_function(const char* name, int (*code)(int, char**))
411 simgrid::s4u::Engine::get_instance()->register_function(name, code);
413 void simgrid_register_default(int (*code)(int, char**))
415 simgrid::s4u::Engine::get_instance()->register_default(code);
417 double simgrid_get_clock()
419 return simgrid::s4u::Engine::get_clock();
421 int simgrid_get_actor_count()
423 return simgrid::s4u::Engine::get_instance()->get_actor_count();