Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] re-writing some 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 container_t rootContainer = NULL;    /* the root container */
14 static xbt_dict_t allContainers = NULL;     /* all created containers indexed by name */
15 xbt_dict_t trivaNodeTypes = NULL;     /* all link types defined */
16 xbt_dict_t trivaEdgeTypes = NULL;     /* all host types defined */
17
18 long long int instr_new_paje_id (void)
19 {
20   static long long int type_id = 0;
21   return type_id++;
22 }
23
24 void PJ_container_alloc (void)
25 {
26   allContainers = xbt_dict_new_homogeneous(NULL);
27   trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free);
28   trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free);
29 }
30
31 void PJ_container_release (void)
32 {
33   xbt_dict_free (&allContainers);
34   xbt_dict_free (&trivaNodeTypes);
35   xbt_dict_free (&trivaEdgeTypes);
36 }
37
38 void PJ_container_set_root (container_t root)
39 {
40   rootContainer = root;
41 }
42
43 container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
44 {
45   static long long int container_id = 0;
46   char id_str[INSTR_DEFAULT_STR_SIZE];
47   snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
48
49   container_t new = xbt_new0(s_container_t, 1);
50   new->name = xbt_strdup (name); // name of the container
51   new->id = xbt_strdup (id_str); // id (or alias) of the container
52   new->father = father;
53   // level depends on level of father
54   if (new->father){
55     new->level = new->father->level+1;
56     XBT_DEBUG("new container %s, child of %s", name, father->name);
57   }else{
58     new->level = 0;
59   }
60   // type definition (method depends on kind of this new container)
61   new->kind = kind;
62   if (new->kind == INSTR_AS){
63     //if this container is of an AS, its type name depends on its level
64     char as_typename[INSTR_DEFAULT_STR_SIZE];
65     snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
66     if (new->father){
67       new->type = PJ_type_get (as_typename, new->father->type);
68       if (new->type == NULL){
69         new->type = PJ_type_container_new (as_typename, new->father->type);
70       }
71     }else{
72       new->type = PJ_type_container_new ("0", NULL);
73     }
74   }else{
75     //otherwise, the name is its kind
76     char typename[INSTR_DEFAULT_STR_SIZE];
77     switch (new->kind){
78       case INSTR_HOST:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "HOST");        break;
79       case INSTR_LINK:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "LINK");        break;
80       case INSTR_ROUTER:      snprintf (typename, INSTR_DEFAULT_STR_SIZE, "ROUTER");      break;
81       case INSTR_SMPI:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MPI");         break;
82       case INSTR_MSG_PROCESS: snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS"); break;
83       case INSTR_MSG_TASK:    snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");    break;
84       default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
85     }
86     type_t type = PJ_type_get (typename, new->father->type);
87     if (type == NULL){
88       new->type = PJ_type_container_new (typename, new->father->type);
89     }else{
90       new->type = type;
91     }
92   }
93   new->children = xbt_dict_new_homogeneous(NULL);
94   if (new->father){
95     xbt_dict_set(new->father->children, new->name, new, NULL);
96     new_pajeCreateContainer (new);
97   }
98
99   //register all kinds by name
100   if (xbt_dict_get_or_null(allContainers, new->name) != NULL){
101     THROWF(tracing_error, 1, "container %s already present in allContainers data structure", new->name);
102   }
103   xbt_dict_set (allContainers, new->name, new, NULL);
104
105   //register NODE types for triva configuration
106   if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
107     xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), NULL);
108   }
109   return new;
110 }
111
112 container_t PJ_container_get (const char *name)
113 {
114   container_t ret = PJ_container_get_or_null (name);
115   if (ret == NULL){
116     THROWF(tracing_error, 1, "container with name %s not found", name);
117   }
118   return ret;
119 }
120
121 container_t PJ_container_get_or_null (const char *name)
122 {
123   if (name == NULL) return NULL;
124   container_t ret = xbt_dict_get_or_null (allContainers, name);
125   if (ret == NULL){
126     return NULL;
127   }
128   return ret;
129 }
130
131 container_t PJ_container_get_root ()
132 {
133   return rootContainer;
134 }
135
136 void PJ_container_remove_from_parent (container_t child)
137 {
138   container_t parent = child->father;
139   if (parent){
140     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
141         child->name,
142         parent->name);
143     xbt_dict_remove (parent->children, child->name);
144   }
145 }
146
147 void PJ_container_free (container_t container)
148 {
149   XBT_DEBUG("destroy container %s", container->name);
150
151   //obligation to dump previous events because they might
152   //reference the container that is about to be destroyed
153   TRACE_last_timestamp_to_dump = surf_get_clock();
154   TRACE_paje_dump_buffer(1);
155
156   //trace my destruction
157   if (!TRACE_disable_destroy()){
158     //do not trace the container destruction if user requests
159     new_pajeDestroyContainer(container);
160   }
161
162   //remove it from allContainers data structure
163   xbt_dict_remove (allContainers, container->name);
164
165   //free
166   xbt_free (container->name);
167   xbt_free (container->id);
168   xbt_dict_free (&container->children);
169   xbt_free (container);
170   container = NULL;
171 }
172
173 static void recursiveDestroyContainer (container_t container)
174 {
175   XBT_DEBUG("recursiveDestroyContainer %s", container->name);
176   xbt_dict_cursor_t cursor = NULL;
177   container_t child;
178   char *child_name;
179   xbt_dict_foreach(container->children, cursor, child_name, child) {
180     recursiveDestroyContainer (child);
181   }
182   PJ_container_free (container);
183 }
184
185 void PJ_container_free_all ()
186 {
187   if (PJ_container_get_root()) recursiveDestroyContainer (PJ_container_get_root());
188   rootContainer = NULL;
189
190   //checks
191   if (xbt_dict_length(allContainers) != 0){
192     THROWF(tracing_error, 0, "some containers still present even after destroying all of them");
193   }
194 }
195
196 #endif /* HAVE_TRACING */