Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use initialisation lists (and std::string)
[simgrid.git] / src / instr / instr_paje_containers.cpp
1 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/s4u/Engine.hpp"
7 #include "simgrid/s4u/Host.hpp"
8
9 #include "surf/surf.h"
10
11 #include "src/instr/instr_private.h"
12
13 #include <unordered_map>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
16
17 static container_t rootContainer = nullptr;    /* the root container */
18 static std::unordered_map<std::string, simgrid::instr::Container*>
19     allContainers;                              /* all created containers indexed by name */
20 std::set<std::string> trivaNodeTypes;           /* all host types defined */
21 std::set<std::string> trivaEdgeTypes;           /* all link types defined */
22
23 long long int instr_new_paje_id ()
24 {
25   static long long int type_id = 0;
26   return type_id++;
27 }
28
29 void PJ_container_set_root (container_t root)
30 {
31   rootContainer = root;
32 }
33
34 simgrid::instr::Container::Container(const char* name, simgrid::instr::e_container_types kind, Container* father)
35     : name_(xbt_strdup(name)), kind_(kind), father_(father)
36 {
37   xbt_assert(name != nullptr, "Container name cannot be nullptr");
38
39   static long long int container_id = 0;
40   id_                               = bprintf("%lld", container_id); // id (or alias) of the container
41   container_id++;
42
43   //Search for network_element_t
44   switch (kind){
45     case simgrid::instr::INSTR_HOST:
46       this->netpoint_ = sg_host_by_name(name)->pimpl_netpoint;
47       xbt_assert(this->netpoint_, "Element '%s' not found", name);
48       break;
49     case simgrid::instr::INSTR_ROUTER:
50       this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
51       xbt_assert(this->netpoint_, "Element '%s' not found", name);
52       break;
53     case simgrid::instr::INSTR_AS:
54       this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
55       xbt_assert(this->netpoint_, "Element '%s' not found", name);
56       break;
57     default:
58       this->netpoint_ = nullptr;
59       break;
60   }
61
62   if (father_) {
63     this->level_ = father_->level_ + 1;
64     XBT_DEBUG("new container %s, child of %s", name, father->name_);
65   }
66
67   // type definition (method depends on kind of this new container)
68   if (this->kind_ == simgrid::instr::INSTR_AS) {
69     //if this container is of an AS, its type name depends on its level
70     char as_typename[INSTR_DEFAULT_STR_SIZE];
71     snprintf(as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", this->level_);
72     if (this->father_) {
73       this->type_ = this->father_->type_->getChildOrNull(as_typename);
74       if (this->type_ == nullptr) {
75         this->type_ = simgrid::instr::Type::containerNew(as_typename, this->father_->type_);
76       }
77     }else{
78       this->type_ = simgrid::instr::Type::containerNew("0", nullptr);
79     }
80   } else {
81     //otherwise, the name is its kind
82     char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
83     switch (this->kind_) {
84       case simgrid::instr::INSTR_HOST:
85         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
86         break;
87       case simgrid::instr::INSTR_LINK:
88         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
89         break;
90       case simgrid::instr::INSTR_ROUTER:
91         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
92         break;
93       case simgrid::instr::INSTR_SMPI:
94         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
95         break;
96       case simgrid::instr::INSTR_MSG_PROCESS:
97         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
98         break;
99       case simgrid::instr::INSTR_MSG_VM:
100         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
101         break;
102       case simgrid::instr::INSTR_MSG_TASK:
103         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
104         break;
105       default:
106         THROWF (tracing_error, 0, "new container kind is unknown.");
107         break;
108     }
109     simgrid::instr::Type* type = this->father_->type_->getChildOrNull(typeNameBuff);
110     if (type == nullptr){
111       this->type_ = simgrid::instr::Type::containerNew(typeNameBuff, this->father_->type_);
112     }else{
113       this->type_ = type;
114     }
115   }
116   this->children_ = xbt_dict_new_homogeneous(nullptr);
117   if (this->father_) {
118     xbt_dict_set(this->father_->children_, this->name_, this, nullptr);
119     LogContainerCreation(this);
120   }
121
122   //register all kinds by name
123   if (not allContainers.emplace(this->name_, this).second) {
124     THROWF(tracing_error, 1, "container %s already present in allContainers data structure", this->name_);
125   }
126
127   XBT_DEBUG("Add container name '%s'", this->name_);
128
129   //register NODE types for triva configuration
130   if (this->kind_ == simgrid::instr::INSTR_HOST || this->kind_ == simgrid::instr::INSTR_LINK ||
131       this->kind_ == simgrid::instr::INSTR_ROUTER) {
132     trivaNodeTypes.insert(this->type_->name_);
133   }
134 }
135 simgrid::instr::Container::~Container()
136 {
137   XBT_DEBUG("destroy container %s", name_);
138
139   // obligation to dump previous events because they might
140   // reference the container that is about to be destroyed
141   TRACE_last_timestamp_to_dump = surf_get_clock();
142   TRACE_paje_dump_buffer(1);
143
144   // trace my destruction
145   if (not TRACE_disable_destroy() && this != PJ_container_get_root()) {
146     // do not trace the container destruction if user requests
147     // or if the container is root
148     LogContainerDestruction(this);
149   }
150
151   // remove it from allContainers data structure
152   allContainers.erase(name_);
153
154   // free
155   xbt_free(name_);
156   xbt_free(id_);
157   xbt_dict_free(&children_);
158 }
159
160 simgrid::instr::Container* PJ_container_get(const char* name)
161 {
162   container_t ret = PJ_container_get_or_null (name);
163   if (ret == nullptr){
164     THROWF(tracing_error, 1, "container with name %s not found", name);
165   }
166   return ret;
167 }
168
169 simgrid::instr::Container* PJ_container_get_or_null(const char* name)
170 {
171   auto cont = allContainers.find(name);
172   return cont == allContainers.end() ? nullptr : cont->second;
173 }
174
175 simgrid::instr::Container* PJ_container_get_root()
176 {
177   return rootContainer;
178 }
179
180 void PJ_container_remove_from_parent (container_t child)
181 {
182   if (child == nullptr){
183     THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
184   }
185
186   container_t parent = child->father_;
187   if (parent){
188     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", child->name_, parent->name_);
189     xbt_dict_remove(parent->children_, child->name_);
190   }
191 }
192
193 static void recursiveDestroyContainer (container_t container)
194 {
195   if (container == nullptr){
196     THROWF (tracing_error, 0, "trying to recursively destroy a nullptr container");
197   }
198   XBT_DEBUG("recursiveDestroyContainer %s", container->name_);
199   xbt_dict_cursor_t cursor = nullptr;
200   container_t child;
201   char *child_name;
202   xbt_dict_foreach (container->children_, cursor, child_name, child) {
203     recursiveDestroyContainer (child);
204   }
205   delete container;
206 }
207
208 void PJ_container_free_all ()
209 {
210   container_t root = PJ_container_get_root();
211   if (root == nullptr){
212     THROWF (tracing_error, 0, "trying to free all containers, but root is nullptr");
213   }
214   recursiveDestroyContainer (root);
215   rootContainer = nullptr;
216
217   //checks
218   if (not allContainers.empty()) {
219     THROWF(tracing_error, 0, "some containers still present even after destroying all of them");
220   }
221 }