Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] some checks in tracing functions
[simgrid.git] / src / instr / instr_paje.c
1 /* Copyright (c) 2010. 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 "instr/instr_private.h"
8
9 #ifdef HAVE_TRACING
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje, instr, "Paje tracing event system (data structures)");
12
13 static type_t rootType = NULL;              /* the root type */
14 static container_t rootContainer = NULL;    /* the root container */
15 static xbt_dict_t allContainers = NULL;     /* all created containers indexed by name */
16 xbt_dict_t trivaNodeTypes = NULL;     /* all link types defined */
17 xbt_dict_t trivaEdgeTypes = NULL;     /* all host types defined */
18
19 void instr_paje_init (void)
20 {
21   allContainers = xbt_dict_new_homogeneous(NULL);
22   trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free);
23   trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free);
24 }
25
26 void instr_paje_set_root (container_t root)
27 {
28   rootContainer = root;
29 }
30
31 void instr_paje_free (void)
32 {
33   xbt_dict_free (&allContainers);
34   xbt_dict_free (&trivaNodeTypes);
35   xbt_dict_free (&trivaEdgeTypes);
36 }
37
38 static long long int new_type_id (void)
39 {
40   static long long int type_id = 0;
41   return type_id++;
42 }
43
44 static void destroyValue (void *value)
45 {
46   xbt_free(((val_t)value)->name);
47   xbt_free(((val_t)value)->color);
48   xbt_free(((val_t)value)->id);
49   xbt_free(value);
50 }
51
52 static val_t newValue (const char *valuename, const char *color, type_t father)
53 {
54   val_t ret = xbt_new0(s_val_t, 1);
55   ret->name = xbt_strdup (valuename);
56   ret->father = father;
57   ret->color = xbt_strdup (color);
58
59   char str_id[INSTR_DEFAULT_STR_SIZE];
60   snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", new_type_id());
61   ret->id = xbt_strdup (str_id);
62
63   xbt_dict_set (father->values, valuename, ret, NULL);
64   XBT_DEBUG("new value %s, child of %s", ret->name, ret->father->name);
65   return ret;
66 }
67
68 val_t getValue (const char *valuename, const char *color, type_t father)
69 {
70   if (father->kind == TYPE_VARIABLE) return NULL; //Variables can't have different values
71
72   val_t ret = (val_t)xbt_dict_get_or_null (father->values, valuename);
73   if (ret == NULL){
74     ret = newValue (valuename, color, father);
75     XBT_DEBUG("EntityValue %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
76     new_pajeDefineEntityValue(ret);
77   }
78   return ret;
79 }
80
81 val_t getValueByName (const char *valuename, type_t father)
82 {
83   return getValue (valuename, NULL, father);
84 }
85
86 static type_t newType (const char *typename, const char *key, const char *color, e_entity_types kind, type_t father)
87 {
88   type_t ret = xbt_new0(s_type_t, 1);
89   ret->name = xbt_strdup (typename);
90   ret->father = father;
91   ret->kind = kind;
92   ret->children = xbt_dict_new_homogeneous(NULL);
93   ret->values = xbt_dict_new_homogeneous(NULL);
94   ret->color = xbt_strdup (color);
95
96   char str_id[INSTR_DEFAULT_STR_SIZE];
97   snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", new_type_id());
98   ret->id = xbt_strdup (str_id);
99
100   if (father != NULL){
101     xbt_dict_set (father->children, key, ret, NULL);
102     XBT_DEBUG("new type %s, child of %s", typename, father->name);
103   }
104   return ret;
105 }
106
107 type_t getRootType ()
108 {
109   return rootType;
110 }
111
112 type_t getContainerType (const char *typename, type_t father)
113 {
114   type_t ret;
115   if (father == NULL){
116     ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
117     if (father) new_pajeDefineContainerType (ret);
118     rootType = ret;
119   }else{
120     //check if my father type already has my typename
121     ret = (type_t)xbt_dict_get_or_null (father->children, typename);
122     if (ret == NULL){
123       ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
124       new_pajeDefineContainerType (ret);
125     }
126   }
127   return ret;
128 }
129
130 type_t getEventType (const char *typename, const char *color, type_t father)
131 {
132   type_t ret = xbt_dict_get_or_null (father->children, typename);
133   if (ret == NULL){
134     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
135     if (!color){
136       ret = newType (typename, typename, white, TYPE_EVENT, father);
137     }else{
138       ret = newType (typename, typename, color, TYPE_EVENT, father);
139     }
140     XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
141     new_pajeDefineEventType(ret);
142   }
143   return ret;
144 }
145
146 type_t getVariableType (const char *typename, const char *color, type_t father)
147 {
148   type_t ret = xbt_dict_get_or_null (father->children, typename);
149   if (ret == NULL){
150     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
151     if (!color){
152       ret = newType (typename, typename, white, TYPE_VARIABLE, father);
153     }else{
154       ret = newType (typename, typename, color, TYPE_VARIABLE, father);
155     }
156     XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
157     new_pajeDefineVariableType (ret);
158   }
159   return ret;
160 }
161
162 char *getVariableTypeIdByName (const char *name, type_t father)
163 {
164   xbt_dict_cursor_t cursor = NULL;
165   type_t type;
166   char *key;
167   xbt_dict_foreach(father->children, cursor, key, type) {
168     if (strcmp (name, type->name) == 0) return type->id;
169   }
170   return NULL;
171 }
172
173 type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
174 {
175   char key[INSTR_DEFAULT_STR_SIZE];
176   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
177   type_t ret = xbt_dict_get_or_null (father->children, key);
178   if (ret == NULL){
179     ret = newType (typename, key, NULL, TYPE_LINK, father);
180     XBT_DEBUG("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->name, ret->id, father->name, father->id, source->name, source->id, dest->name, dest->id);
181     new_pajeDefineLinkType(ret, source, dest);
182   }
183   return ret;
184 }
185
186 type_t getStateType (const char *typename, type_t father)
187 {
188   type_t ret = xbt_dict_get_or_null (father->children, typename);
189   if (ret == NULL){
190     ret = newType (typename, typename, NULL, TYPE_STATE, father);
191     XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
192     new_pajeDefineStateType(ret);
193   }
194   return ret;
195 }
196
197 container_t newContainer (const char *name, e_container_types kind, container_t father)
198 {
199   static long long int container_id = 0;
200   char id_str[INSTR_DEFAULT_STR_SIZE];
201   snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
202
203   container_t new = xbt_new0(s_container_t, 1);
204   new->name = xbt_strdup (name); // name of the container
205   new->id = xbt_strdup (id_str); // id (or alias) of the container
206   new->father = father;
207   // level depends on level of father
208   if (new->father){
209     new->level = new->father->level+1;
210     XBT_DEBUG("new container %s, child of %s", name, father->name);
211   }else{
212     new->level = 0;
213   }
214   // type definition (method depends on kind of this new container)
215   new->kind = kind;
216   if (new->kind == INSTR_AS){
217     //if this container is of an AS, its type name depends on its level
218     char as_typename[INSTR_DEFAULT_STR_SIZE];
219     snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
220     if (new->father){
221       new->type = getContainerType (as_typename, new->father->type);
222     }else{
223       new->type = getContainerType ("0", NULL);
224     }
225   }else{
226     //otherwise, the name is its kind
227     switch (new->kind){
228       case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
229       case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
230       case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
231       case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
232       case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
233       case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
234       default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
235     }
236   }
237   new->children = xbt_dict_new_homogeneous(NULL);
238   if (new->father){
239     xbt_dict_set(new->father->children, new->name, new, NULL);
240     new_pajeCreateContainer (new);
241   }
242
243   //register hosts, routers, links containers
244   if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
245     xbt_dict_set (allContainers, new->name, new, NULL);
246
247     //register NODE types for triva configuration
248     xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), NULL);
249   }
250   return new;
251 }
252
253 static container_t recursiveGetContainer (const char *name, container_t root)
254 {
255   if (name == NULL || root == NULL) return NULL;
256   if (strcmp (root->name, name) == 0) return root;
257
258   xbt_dict_cursor_t cursor = NULL;
259   container_t child;
260   char *child_name;
261   xbt_dict_foreach(root->children, cursor, child_name, child) {
262     container_t ret = recursiveGetContainer(name, child);
263     if (ret) return ret;
264   }
265   return NULL;
266 }
267
268 container_t getContainer (const char *name)
269 {
270   if (name == NULL) return NULL;
271   return getContainerByName(name);
272 }
273
274 int knownContainerWithName (const char *name)
275 {
276   if (xbt_dict_get_or_null (allContainers, name) == NULL){
277     return 0;
278   }else{
279     return 1;
280   }
281 }
282
283 container_t getContainerByName (const char *name)
284 {
285   container_t ret = xbt_dict_get_or_null (allContainers, name);
286   if (ret == NULL){
287     XBT_CRITICAL("container with name %s not found", name);
288   }
289   return ret;
290 }
291
292 char *getContainerIdByName (const char *name)
293 {
294   return getContainerByName(name)->id;
295 }
296
297 container_t getRootContainer ()
298 {
299   return rootContainer;
300 }
301
302 static type_t recursiveGetType (const char *name, type_t root)
303 {
304   if (strcmp (root->name, name) == 0) return root;
305
306   xbt_dict_cursor_t cursor = NULL;
307   type_t child;
308   char *child_name;
309   type_t ret = NULL;
310   xbt_dict_foreach(root->children, cursor, child_name, child) {
311     type_t found = recursiveGetType(name, child);
312     if (found){
313       if (ret == NULL){
314         ret = found;
315       }else{
316         XBT_CRITICAL("[tracing] found two types with the same name");
317       }
318     }
319   }
320   return ret;
321 }
322
323 type_t getType (const char *name, type_t father)
324 {
325   return recursiveGetType (name, father);
326 }
327
328 void removeContainerFromParent (container_t child)
329 {
330   container_t parent = child->father;
331   if (parent){
332     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
333         child->name,
334         parent->name);
335     xbt_dict_remove (parent->children, child->name);
336   }
337 }
338
339 void destroyContainer (container_t container)
340 {
341   XBT_DEBUG("destroy container %s", container->name);
342
343   //obligation to dump previous events because they might
344   //reference the container that is about to be destroyed
345   TRACE_last_timestamp_to_dump = surf_get_clock();
346   TRACE_paje_dump_buffer(1);
347
348   //trace my destruction
349   if (!TRACE_disable_destroy()){
350     //do not trace the container destruction if user requests
351     new_pajeDestroyContainer(container);
352   }
353
354   //remove it from allContainers data structure
355   xbt_dict_remove (allContainers, container->name);
356
357   //free
358   xbt_free (container->name);
359   xbt_free (container->id);
360   xbt_dict_free (&container->children);
361   xbt_free (container);
362   container = NULL;
363 }
364
365 static void recursiveDestroyContainer (container_t container)
366 {
367   XBT_DEBUG("recursiveDestroyContainer %s", container->name);
368   xbt_dict_cursor_t cursor = NULL;
369   container_t child;
370   char *child_name;
371   xbt_dict_foreach(container->children, cursor, child_name, child) {
372     recursiveDestroyContainer (child);
373   }
374   destroyContainer (container);
375 }
376
377 static void recursiveDestroyType (type_t type)
378 {
379   XBT_DEBUG("recursiveDestroyType %s", type->name);
380   xbt_dict_cursor_t cursor = NULL;
381   type_t child;
382   char *child_name;
383   xbt_dict_foreach(type->children, cursor, child_name, child) {
384     recursiveDestroyType (child);
385   }
386   xbt_free (type->name);
387   xbt_free (type->id);
388   xbt_free (type->color);
389   xbt_dict_free (&type->children);
390   val_t value;
391   char *value_name;
392   xbt_dict_foreach(type->values, cursor, value_name, value) {
393     destroyValue (value);
394   }
395   xbt_dict_free (&type->values);
396   xbt_free (type);
397   type = NULL;
398 }
399
400 void destroyAllContainers ()
401 {
402   if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
403   if (getRootType()) recursiveDestroyType (getRootType());
404   rootContainer = NULL;
405   rootType = NULL;
406
407   //checks
408   if (xbt_dict_length(allContainers) != 0){
409     XBT_CRITICAL("some containers still present even after destroying all of them");
410   }
411 }
412
413
414 #endif /* HAVE_TRACING */