Logo AND Algorithmique Numérique Distribuée

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