Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new signal: s4u::onDeadlock()
[simgrid.git] / src / s4u / s4u_engine.cpp
1 /* s4u::Engine Simulation Engine and global functions. */
2
3 /* Copyright (c) 2006-2017. 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 "instr/instr_interface.h"
9 #include "mc/mc.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.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()> onPlatformCreated;
27 xbt::signal<void()> onSimulationEnd;
28 xbt::signal<void(double)> onTimeAdvance;
29 xbt::signal<void(void)> onDeadlock;
30
31 Engine *Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
32
33
34 Engine::Engine(int *argc, char **argv) {
35   xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
36   TRACE_global_init();
37   SIMIX_global_init(argc, argv);
38
39   pimpl                  = new kernel::EngineImpl();
40   s4u::Engine::instance_ = this;
41 }
42
43 Engine::~Engine()
44 {
45   delete pimpl;
46   s4u::Engine::instance_ = nullptr;
47 }
48
49 Engine* Engine::getInstance()
50 {
51   if (s4u::Engine::instance_ == nullptr)
52     return new Engine(0, nullptr);
53   else
54     return s4u::Engine::instance_;
55 }
56
57 void Engine::shutdown() {
58   delete s4u::Engine::instance_;
59   s4u::Engine::instance_ = nullptr;
60 }
61
62 double Engine::getClock()
63 {
64   return SIMIX_get_clock();
65 }
66
67 void Engine::loadPlatform(const char *platf)
68 {
69   SIMIX_create_environment(platf);
70 }
71
72 void Engine::registerFunction(const char*name, int (*code)(int,char**))
73 {
74   SIMIX_function_register(name,code);
75 }
76 void Engine::registerDefault(int (*code)(int,char**))
77 {
78   SIMIX_function_register_default(code);
79 }
80 void Engine::loadDeployment(const char *deploy)
81 {
82   SIMIX_launch_application(deploy);
83 }
84 // FIXME: The following duplicates the content of s4u::Host
85 extern std::map<std::string, simgrid::s4u::Host*> host_list;
86 /** @brief Returns the amount of hosts in the platform */
87 size_t Engine::getHostCount()
88 {
89   return host_list.size();
90 }
91 /** @brief Fills the passed list with all hosts found in the platform */
92 void Engine::getHostList(std::vector<Host*>* list)
93 {
94   for (auto const& kv : host_list)
95     list->push_back(kv.second);
96 }
97 /** @brief Returns the amount of links in the platform */
98 size_t Engine::getLinkCount()
99 {
100   return simgrid::surf::LinkImpl::linksCount();
101 }
102 /** @brief Fills the passed list with all hosts found in the platform */
103 void Engine::getLinkList(std::vector<Link*>* list)
104 {
105   simgrid::surf::LinkImpl::linksList(list);
106 }
107
108 void Engine::run() {
109   if (MC_is_active()) {
110     MC_run();
111   } else {
112     SIMIX_run();
113   }
114 }
115
116 s4u::NetZone* Engine::getNetRoot()
117 {
118   return pimpl->netRoot_;
119 }
120
121 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
122 {
123   if (not strcmp(current->getCname(), name))
124     return current;
125
126   for (auto const& elem : *(current->getChildren())) {
127     simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
128     if (tmp != nullptr) {
129       return tmp;
130     }
131   }
132   return nullptr;
133 }
134
135 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
136 NetZone* Engine::getNetzoneByNameOrNull(const char* name)
137 {
138   return netzoneByNameRecursive(getNetRoot(), name);
139 }
140
141 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
142 simgrid::kernel::routing::NetPoint* Engine::getNetpointByNameOrNull(std::string name)
143 {
144   auto netp = pimpl->netpoints_.find(name);
145   return netp == pimpl->netpoints_.end() ? nullptr : netp->second;
146 }
147
148 /** @brief Fill the provided vector with all existing netpoints */
149 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
150 {
151   for (auto const& kv : pimpl->netpoints_)
152     list->push_back(kv.second);
153 }
154 /** @brief Register a new netpoint to the system */
155 void Engine::netpointRegister(simgrid::kernel::routing::NetPoint* point)
156 {
157   // simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
158   pimpl->netpoints_[point->getName()] = point;
159   // });
160 }
161 /** @brief Unregister a given netpoint */
162 void Engine::netpointUnregister(simgrid::kernel::routing::NetPoint* point)
163 {
164   simgrid::simix::kernelImmediate([this, point] {
165     pimpl->netpoints_.erase(point->getName());
166     delete point;
167   });
168 }
169
170 bool Engine::isInitialized()
171 {
172   return Engine::instance_ != nullptr;
173 }
174 void Engine::setConfig(std::string str)
175 {
176   xbt_cfg_set_parse(str.c_str());
177 }
178 }
179 } // namespace