Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9ab100720cd02d9c7a5e733e7932c4f7083db228
[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.h" // 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
30 Engine *Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
31
32
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");
35   s4u::Engine::instance_ = this;
36   pimpl                  = new kernel::EngineImpl();
37
38   TRACE_global_init(argc, argv);
39   SIMIX_global_init(argc, argv);
40 }
41
42 Engine::~Engine()
43 {
44   delete pimpl;
45 }
46
47 Engine *Engine::instance() {
48   if (s4u::Engine::instance_ == nullptr)
49     new Engine(0,nullptr);
50   return s4u::Engine::instance_;
51 }
52
53 void Engine::shutdown() {
54   delete s4u::Engine::instance_;
55   s4u::Engine::instance_ = nullptr;
56 }
57
58 double Engine::getClock()
59 {
60   return SIMIX_get_clock();
61 }
62
63 void Engine::loadPlatform(const char *platf)
64 {
65   SIMIX_create_environment(platf);
66 }
67
68 void Engine::registerFunction(const char*name, int (*code)(int,char**))
69 {
70   SIMIX_function_register(name,code);
71 }
72 void Engine::registerDefault(int (*code)(int,char**))
73 {
74   SIMIX_function_register_default(code);
75 }
76 void Engine::loadDeployment(const char *deploy)
77 {
78   SIMIX_launch_application(deploy);
79 }
80 // FIXME: The following duplicates the content of s4u::Host
81 extern std::map<std::string, simgrid::s4u::Host*> host_list;
82 /** @brief Returns the amount of hosts in the platform */
83 size_t Engine::hostCount()
84 {
85   return host_list.size();
86 }
87 /** @brief Fills the passed list with all hosts found in the platform */
88 void Engine::hostList(std::vector<Host*>* list)
89 {
90   for (auto kv : host_list)
91     list->push_back(kv.second);
92 }
93
94 void Engine::run() {
95   if (MC_is_active()) {
96     MC_run();
97   } else {
98     SIMIX_run();
99   }
100 }
101
102 s4u::NetZone* Engine::netRoot()
103 {
104   return pimpl->netRoot_;
105 }
106
107 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
108 {
109   if (not strcmp(current->name(), name))
110     return current;
111
112   for (auto elem : *(current->children())) {
113     simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
114     if (tmp != nullptr) {
115       return tmp;
116     }
117   }
118   return nullptr;
119 }
120
121 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
122 NetZone* Engine::netzoneByNameOrNull(const char* name)
123 {
124   return netzoneByNameRecursive(netRoot(), name);
125 }
126
127 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
128 simgrid::kernel::routing::NetPoint* Engine::netpointByNameOrNull(const char* name)
129 {
130   if (pimpl->netpoints_.find(name) == pimpl->netpoints_.end())
131     return nullptr;
132   return pimpl->netpoints_.at(name);
133 }
134 /** @brief Fill the provided vector with all existing netpoints */
135 void Engine::netpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
136 {
137   for (auto kv : pimpl->netpoints_)
138     list->push_back(kv.second);
139 }
140 /** @brief Register a new netpoint to the system */
141 void Engine::netpointRegister(simgrid::kernel::routing::NetPoint* point)
142 {
143 //  simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
144   pimpl->netpoints_[point->name()] = point;
145 //  });
146 }
147 /** @brief Unregister a given netpoint */
148 void Engine::netpointUnregister(simgrid::kernel::routing::NetPoint* point)
149 {
150   simgrid::simix::kernelImmediate([this, point] {
151     pimpl->netpoints_.erase(point->name());
152     delete point;
153   });
154 }
155
156 bool Engine::isInitialized()
157 {
158   return Engine::instance_ != nullptr;
159 }
160 }
161 } // namespace