1 /* s4u::Engine Simulation Engine and global functions. */
3 /* Copyright (c) 2006-2017. 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. */
8 #include "instr/instr_interface.h"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "simgrid/s4u/Mailbox.hpp"
13 #include "simgrid/s4u/NetZone.hpp"
14 #include "simgrid/s4u/Storage.hpp"
15 #include "simgrid/simix.h"
16 #include "src/kernel/EngineImpl.hpp"
17 #include "src/kernel/routing/NetPoint.hpp"
18 #include "src/kernel/routing/NetZoneImpl.hpp"
19 #include "src/surf/network_interface.hpp"
20 #include "surf/surf.h" // routing_platf. FIXME:KILLME. SOON
22 XBT_LOG_NEW_CATEGORY(s4u,"Log channels of the S4U (Simgrid for you) interface");
26 xbt::signal<void()> onPlatformCreated;
27 xbt::signal<void()> onSimulationEnd;
28 xbt::signal<void(double)> onTimeAdvance;
30 Engine *Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
33 Engine::Engine(int *argc, char **argv) {
34 xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
36 SIMIX_global_init(argc, argv);
38 pimpl = new kernel::EngineImpl();
39 s4u::Engine::instance_ = this;
45 s4u::Engine::instance_ = nullptr;
48 Engine* Engine::getInstance()
50 if (s4u::Engine::instance_ == nullptr)
51 new Engine(0,nullptr);
52 return s4u::Engine::instance_;
55 void Engine::shutdown() {
56 delete s4u::Engine::instance_;
57 s4u::Engine::instance_ = nullptr;
60 double Engine::getClock()
62 return SIMIX_get_clock();
65 void Engine::loadPlatform(const char *platf)
67 SIMIX_create_environment(platf);
70 void Engine::registerFunction(const char*name, int (*code)(int,char**))
72 SIMIX_function_register(name,code);
74 void Engine::registerDefault(int (*code)(int,char**))
76 SIMIX_function_register_default(code);
78 void Engine::loadDeployment(const char *deploy)
80 SIMIX_launch_application(deploy);
82 // FIXME: The following duplicates the content of s4u::Host
83 extern std::map<std::string, simgrid::s4u::Host*> host_list;
84 /** @brief Returns the amount of hosts in the platform */
85 size_t Engine::getHostCount()
87 return host_list.size();
89 /** @brief Fills the passed list with all hosts found in the platform */
90 void Engine::getHostList(std::vector<Host*>* list)
92 for (auto const& kv : host_list)
93 list->push_back(kv.second);
104 s4u::NetZone* Engine::getNetRoot()
106 return pimpl->netRoot_;
109 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
111 if (not strcmp(current->getCname(), name))
114 for (auto const& elem : *(current->getChildren())) {
115 simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
116 if (tmp != nullptr) {
123 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
124 NetZone* Engine::getNetzoneByNameOrNull(const char* name)
126 return netzoneByNameRecursive(getNetRoot(), name);
129 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
130 simgrid::kernel::routing::NetPoint* Engine::getNetpointByNameOrNull(std::string name)
132 auto netp = pimpl->netpoints_.find(name);
133 return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
136 /** @brief Fill the provided vector with all existing netpoints */
137 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
139 for (auto const& kv : pimpl->netpoints_)
140 list->push_back(kv.second);
142 /** @brief Register a new netpoint to the system */
143 void Engine::netpointRegister(simgrid::kernel::routing::NetPoint* point)
145 // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
146 pimpl->netpoints_[point->getName()] = point;
149 /** @brief Unregister a given netpoint */
150 void Engine::netpointUnregister(simgrid::kernel::routing::NetPoint* point)
152 simgrid::simix::kernelImmediate([this, point] {
153 pimpl->netpoints_.erase(point->getName());
158 bool Engine::isInitialized()
160 return Engine::instance_ != nullptr;
162 void Engine::setConfig(std::string str)
164 xbt_cfg_set_parse(str.c_str());