Logo AND Algorithmique Numérique Distribuée

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