Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename the instr::Container class to its proper name
[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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
14
15 static container_t rootContainer = nullptr;    /* the root container */
16 static xbt_dict_t allContainers = nullptr;     /* all created containers indexed by name */
17 std::set<std::string> trivaNodeTypes;           /* all host types defined */
18 std::set<std::string> trivaEdgeTypes;           /* all link types defined */
19
20 long long int instr_new_paje_id ()
21 {
22   static long long int type_id = 0;
23   return type_id++;
24 }
25
26 void PJ_container_alloc ()
27 {
28   allContainers = xbt_dict_new_homogeneous(nullptr);
29 }
30
31 void PJ_container_release ()
32 {
33   xbt_dict_free (&allContainers);
34 }
35
36 void PJ_container_set_root (container_t root)
37 {
38   rootContainer = root;
39 }
40
41 container_t PJ_container_new(const char* name, simgrid::instr::e_container_types kind, container_t father)
42 {
43   if (name == nullptr){
44     THROWF (tracing_error, 0, "can't create a container with a nullptr name");
45   }
46
47   static long long int container_id = 0;
48   char id_str[INSTR_DEFAULT_STR_SIZE];
49   snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id);
50   container_id++;
51
52   container_t newContainer = xbt_new0(simgrid::instr::Container, 1);
53   newContainer->name_      = xbt_strdup(name);   // name of the container
54   newContainer->id_        = xbt_strdup(id_str); // id (or alias) of the container
55   newContainer->father_    = father;
56   sg_host_t sg_host = sg_host_by_name(name);
57
58   //Search for network_element_t
59   switch (kind){
60     case simgrid::instr::INSTR_HOST:
61       newContainer->netpoint_ = sg_host->pimpl_netpoint;
62       xbt_assert(newContainer->netpoint_, "Element '%s' not found", name);
63       break;
64     case simgrid::instr::INSTR_ROUTER:
65       newContainer->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
66       xbt_assert(newContainer->netpoint_, "Element '%s' not found", name);
67       break;
68     case simgrid::instr::INSTR_AS:
69       newContainer->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
70       xbt_assert(newContainer->netpoint_, "Element '%s' not found", name);
71       break;
72     default:
73       newContainer->netpoint_ = nullptr;
74       break;
75   }
76
77   // level depends on level of father
78   if (newContainer->father_) {
79     newContainer->level_ = newContainer->father_->level_ + 1;
80     XBT_DEBUG("new container %s, child of %s", name, father->name_);
81   }else{
82     newContainer->level_ = 0;
83   }
84   // type definition (method depends on kind of this new container)
85   newContainer->kind_ = kind;
86   if (newContainer->kind_ == simgrid::instr::INSTR_AS) {
87     //if this container is of an AS, its type name depends on its level
88     char as_typename[INSTR_DEFAULT_STR_SIZE];
89     snprintf(as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", newContainer->level_);
90     if (newContainer->father_) {
91       newContainer->type_ = simgrid::instr::Type::getOrNull(as_typename, newContainer->father_->type_);
92       if (newContainer->type_ == nullptr) {
93         newContainer->type_ = simgrid::instr::Type::containerNew(as_typename, newContainer->father_->type_);
94       }
95     }else{
96       newContainer->type_ = simgrid::instr::Type::containerNew("0", nullptr);
97     }
98   }else{
99     //otherwise, the name is its kind
100     char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
101     switch (newContainer->kind_) {
102       case simgrid::instr::INSTR_HOST:
103         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
104         break;
105       case simgrid::instr::INSTR_LINK:
106         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
107         break;
108       case simgrid::instr::INSTR_ROUTER:
109         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
110         break;
111       case simgrid::instr::INSTR_SMPI:
112         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
113         break;
114       case simgrid::instr::INSTR_MSG_PROCESS:
115         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
116         break;
117       case simgrid::instr::INSTR_MSG_VM:
118         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
119         break;
120       case simgrid::instr::INSTR_MSG_TASK:
121         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
122         break;
123       default:
124         THROWF (tracing_error, 0, "new container kind is unknown.");
125         break;
126     }
127     simgrid::instr::Type* type = simgrid::instr::Type::getOrNull(typeNameBuff, newContainer->father_->type_);
128     if (type == nullptr){
129       newContainer->type_ = simgrid::instr::Type::containerNew(typeNameBuff, newContainer->father_->type_);
130     }else{
131       newContainer->type_ = type;
132     }
133   }
134   newContainer->children_ = xbt_dict_new_homogeneous(nullptr);
135   if (newContainer->father_) {
136     xbt_dict_set(newContainer->father_->children_, newContainer->name_, newContainer, nullptr);
137     LogContainerCreation(newContainer);
138   }
139
140   //register all kinds by name
141   if (xbt_dict_get_or_null(allContainers, newContainer->name_) != nullptr) {
142     THROWF(tracing_error, 1, "container %s already present in allContainers data structure", newContainer->name_);
143   }
144
145   xbt_dict_set(allContainers, newContainer->name_, newContainer, nullptr);
146   XBT_DEBUG("Add container name '%s'", newContainer->name_);
147
148   //register NODE types for triva configuration
149   if (newContainer->kind_ == simgrid::instr::INSTR_HOST || newContainer->kind_ == simgrid::instr::INSTR_LINK ||
150       newContainer->kind_ == simgrid::instr::INSTR_ROUTER) {
151     trivaNodeTypes.insert(newContainer->type_->name_);
152   }
153   return newContainer;
154 }
155
156 container_t PJ_container_get (const char *name)
157 {
158   container_t ret = PJ_container_get_or_null (name);
159   if (ret == nullptr){
160     THROWF(tracing_error, 1, "container with name %s not found", name);
161   }
162   return ret;
163 }
164
165 container_t PJ_container_get_or_null (const char *name)
166 {
167   return static_cast<container_t>(name != nullptr ? xbt_dict_get_or_null(allContainers, name) : nullptr);
168 }
169
170 container_t PJ_container_get_root ()
171 {
172   return rootContainer;
173 }
174
175 void PJ_container_remove_from_parent (container_t child)
176 {
177   if (child == nullptr){
178     THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
179   }
180
181   container_t parent = child->father_;
182   if (parent){
183     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", child->name_, parent->name_);
184     xbt_dict_remove(parent->children_, child->name_);
185   }
186 }
187
188 void PJ_container_free (container_t container)
189 {
190   if (container == nullptr){
191     THROWF (tracing_error, 0, "trying to free a nullptr container");
192   }
193   XBT_DEBUG("destroy container %s", container->name_);
194
195   //obligation to dump previous events because they might
196   //reference the container that is about to be destroyed
197   TRACE_last_timestamp_to_dump = surf_get_clock();
198   TRACE_paje_dump_buffer(1);
199
200   //trace my destruction
201   if (not TRACE_disable_destroy() && container != PJ_container_get_root()) {
202     //do not trace the container destruction if user requests
203     //or if the container is root
204     LogContainerDestruction(container);
205   }
206
207   //remove it from allContainers data structure
208   xbt_dict_remove(allContainers, container->name_);
209
210   //free
211   xbt_free(container->name_);
212   xbt_free(container->id_);
213   xbt_dict_free(&container->children_);
214   xbt_free (container);
215   container = nullptr;
216 }
217
218 static void recursiveDestroyContainer (container_t container)
219 {
220   if (container == nullptr){
221     THROWF (tracing_error, 0, "trying to recursively destroy a nullptr container");
222   }
223   XBT_DEBUG("recursiveDestroyContainer %s", container->name_);
224   xbt_dict_cursor_t cursor = nullptr;
225   container_t child;
226   char *child_name;
227   xbt_dict_foreach (container->children_, cursor, child_name, child) {
228     recursiveDestroyContainer (child);
229   }
230   PJ_container_free (container);
231 }
232
233 void PJ_container_free_all ()
234 {
235   container_t root = PJ_container_get_root();
236   if (root == nullptr){
237     THROWF (tracing_error, 0, "trying to free all containers, but root is nullptr");
238   }
239   recursiveDestroyContainer (root);
240   rootContainer = nullptr;
241
242   //checks
243   if (not xbt_dict_is_empty(allContainers)) {
244     THROWF(tracing_error, 0, "some containers still present even after destroying all of them");
245   }
246 }