Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar by writing lighter C++ code
[simgrid.git] / src / s4u / s4u_engine.cpp
1 /* s4u::Engine Simulation Engine and global functions. */
2
3 /* Copyright (c) 2006-2015. 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/Mailbox.hpp"
11 #include "simgrid/s4u/NetZone.hpp"
12 #include "simgrid/s4u/engine.hpp"
13 #include "simgrid/s4u/host.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
29 Engine *Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
30
31
32 Engine::Engine(int *argc, char **argv) {
33   xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
34   s4u::Engine::instance_ = this;
35   pimpl                  = new kernel::EngineImpl();
36
37   TRACE_global_init(argc, argv);
38   SIMIX_global_init(argc, argv);
39 }
40
41 Engine::~Engine()
42 {
43   delete pimpl;
44 }
45
46 Engine *Engine::instance() {
47   if (s4u::Engine::instance_ == nullptr)
48     new Engine(0,nullptr);
49   return s4u::Engine::instance_;
50 }
51
52 void Engine::shutdown() {
53   delete s4u::Engine::instance_;
54   s4u::Engine::instance_ = nullptr;
55   delete s4u::Storage::storages_;
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
81 void Engine::run() {
82   if (MC_is_active()) {
83     MC_run();
84   } else {
85     SIMIX_run();
86   }
87 }
88
89 s4u::NetZone* Engine::netRoot()
90 {
91   return pimpl->netRoot_;
92 }
93
94 static s4u::NetZone* netzoneByNameRecursive(s4u::NetZone* current, const char* name)
95 {
96   if(!strcmp(current->name(), name))
97     return current;
98
99   xbt_dict_cursor_t cursor = nullptr;
100   char *key;
101   NetZone_t elem;
102   xbt_dict_foreach(current->children(), cursor, key, elem) {
103     simgrid::s4u::NetZone* tmp = netzoneByNameRecursive(elem, name);
104     if (tmp != nullptr )
105         return tmp;
106   }
107   return nullptr;
108 }
109
110 /** @brief Retrieve the NetZone of the given name (or nullptr if not found) */
111 NetZone* Engine::netzoneByNameOrNull(const char* name)
112 {
113   return netzoneByNameRecursive(netRoot(), name);
114 }
115
116 /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */
117 simgrid::kernel::routing::NetPoint* Engine::netpointByNameOrNull(const char* name)
118 {
119   if (pimpl->netpoints_.find(name) == pimpl->netpoints_.end())
120     return nullptr;
121   return pimpl->netpoints_.at(name);
122 }
123 /** @brief Fill the provided vector with all existing netpoints */
124 void Engine::netpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
125 {
126   for (auto kv : pimpl->netpoints_)
127     list->push_back(kv.second);
128 }
129 /** @brief Register a new netpoint to the system */
130 void Engine::netpointRegister(simgrid::kernel::routing::NetPoint* point)
131 {
132 //  simgrid::simix::kernelImmediate([&]{ FIXME: this segfaults in set_thread
133   pimpl->netpoints_[point->name()] = point;
134 //  });
135 }
136 /** @brief Unregister a given netpoint */
137 void Engine::netpointUnregister(simgrid::kernel::routing::NetPoint* point)
138 {
139   simgrid::simix::kernelImmediate([&] {
140     pimpl->netpoints_.erase(point->name());
141     delete point;
142   });
143 }
144 }
145 }