Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Set level COORD_HOST_LEVEL and COORD_ASR_LEVEL if there are used.
[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 (container_t root)
20 {
21   allContainers = xbt_dict_new ();
22   trivaNodeTypes = xbt_dict_new ();
23   trivaEdgeTypes = xbt_dict_new ();
24   rootContainer = root;
25 }
26
27 static long long int new_type_id (void)
28 {
29   static long long int type_id = 0;
30   return type_id++;
31 }
32
33 static val_t newValue (const char *valuename, const char *color, type_t father)
34 {
35   val_t ret = xbt_new0(s_val_t, 1);
36   ret->name = xbt_strdup (valuename);
37   ret->father = father;
38   ret->color = xbt_strdup (color);
39
40   char str_id[INSTR_DEFAULT_STR_SIZE];
41   snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", new_type_id());
42   ret->id = xbt_strdup (str_id);
43
44   xbt_dict_set (father->values, valuename, ret, NULL);
45   XBT_DEBUG("new value %s, child of %s", ret->name, ret->father->name);
46   return ret;
47 }
48
49 val_t getValue (const char *valuename, const char *color, type_t father)
50 {
51   if (father->kind == TYPE_VARIABLE) return NULL; //Variables can't have different values
52
53   val_t ret = (val_t)xbt_dict_get_or_null (father->values, valuename);
54   if (ret == NULL){
55     ret = newValue (valuename, color, father);
56     XBT_DEBUG("EntityValue %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
57     new_pajeDefineEntityValue(ret);
58   }
59   return ret;
60 }
61
62 val_t getValueByName (const char *valuename, type_t father)
63 {
64   return getValue (valuename, NULL, father);
65 }
66
67 static type_t newType (const char *typename, const char *key, const char *color, e_entity_types kind, type_t father)
68 {
69   type_t ret = xbt_new0(s_type_t, 1);
70   ret->name = xbt_strdup (typename);
71   ret->father = father;
72   ret->kind = kind;
73   ret->children = xbt_dict_new ();
74   ret->values = xbt_dict_new ();
75   ret->color = xbt_strdup (color);
76
77   char str_id[INSTR_DEFAULT_STR_SIZE];
78   snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", new_type_id());
79   ret->id = xbt_strdup (str_id);
80
81   if (father != NULL){
82     xbt_dict_set (father->children, key, ret, NULL);
83     XBT_DEBUG("new type %s, child of %s", typename, father->name);
84   }
85   return ret;
86 }
87
88 type_t getRootType ()
89 {
90   return rootType;
91 }
92
93 type_t getContainerType (const char *typename, type_t father)
94 {
95   type_t ret;
96   if (father == NULL){
97     ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
98     if (father) new_pajeDefineContainerType (ret);
99     rootType = ret;
100   }else{
101     //check if my father type already has my typename
102     ret = (type_t)xbt_dict_get_or_null (father->children, typename);
103     if (ret == NULL){
104       ret = newType (typename, typename, NULL, TYPE_CONTAINER, father);
105       new_pajeDefineContainerType (ret);
106     }
107   }
108   return ret;
109 }
110
111 type_t getEventType (const char *typename, const char *color, type_t father)
112 {
113   type_t ret = xbt_dict_get_or_null (father->children, typename);
114   if (ret == NULL){
115     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
116     if (!color){
117       ret = newType (typename, typename, white, TYPE_EVENT, father);
118     }else{
119       ret = newType (typename, typename, color, TYPE_EVENT, father);
120     }
121     XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
122     new_pajeDefineEventType(ret);
123   }
124   return ret;
125 }
126
127 type_t getVariableType (const char *typename, const char *color, type_t father)
128 {
129   type_t ret = xbt_dict_get_or_null (father->children, typename);
130   if (ret == NULL){
131     char white[INSTR_DEFAULT_STR_SIZE] = "1 1 1";
132     if (!color){
133       ret = newType (typename, typename, white, TYPE_VARIABLE, father);
134     }else{
135       ret = newType (typename, typename, color, TYPE_VARIABLE, father);
136     }
137     XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
138     new_pajeDefineVariableType (ret);
139   }
140   return ret;
141 }
142
143 char *getVariableTypeIdByName (const char *name, type_t father)
144 {
145   xbt_dict_cursor_t cursor = NULL;
146   type_t type;
147   char *key;
148   xbt_dict_foreach(father->children, cursor, key, type) {
149     if (strcmp (name, type->name) == 0) return type->id;
150   }
151   return NULL;
152 }
153
154 type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
155 {
156   char key[INSTR_DEFAULT_STR_SIZE];
157   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%s-%s-%s", typename, source->id, dest->id);
158   type_t ret = xbt_dict_get_or_null (father->children, key);
159   if (ret == NULL){
160     ret = newType (typename, key, NULL, TYPE_LINK, father);
161     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);
162     new_pajeDefineLinkType(ret, source, dest);
163   }
164   return ret;
165 }
166
167 type_t getStateType (const char *typename, type_t father)
168 {
169   type_t ret = xbt_dict_get_or_null (father->children, typename);
170   if (ret == NULL){
171     ret = newType (typename, typename, NULL, TYPE_STATE, father);
172     XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
173     new_pajeDefineStateType(ret);
174   }
175   return ret;
176 }
177
178 container_t newContainer (const char *name, e_container_types kind, container_t father)
179 {
180   static long long int container_id = 0;
181   char id_str[INSTR_DEFAULT_STR_SIZE];
182   snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
183
184   container_t new = xbt_new0(s_container_t, 1);
185   new->name = xbt_strdup (name); // name of the container
186   new->id = xbt_strdup (id_str); // id (or alias) of the container
187   new->father = father;
188   // level depends on level of father
189   if (new->father){
190     new->level = new->father->level+1;
191     XBT_DEBUG("new container %s, child of %s", name, father->name);
192   }else{
193     new->level = 0;
194   }
195   // type definition (method depends on kind of this new container)
196   new->kind = kind;
197   if (new->kind == INSTR_AS){
198     //if this container is of an AS, its type name depends on its level
199     char as_typename[INSTR_DEFAULT_STR_SIZE];
200     snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
201     if (new->father){
202       new->type = getContainerType (as_typename, new->father->type);
203     }else{
204       new->type = getContainerType ("0", NULL);
205     }
206   }else{
207     //otherwise, the name is its kind
208     switch (new->kind){
209       case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
210       case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
211       case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
212       case INSTR_SMPI: new->type = getContainerType ("MPI", new->father->type); break;
213       case INSTR_MSG_PROCESS: new->type = getContainerType ("MSG_PROCESS", new->father->type); break;
214       case INSTR_MSG_TASK: new->type = getContainerType ("MSG_TASK", new->father->type); break;
215       default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
216     }
217   }
218   new->children = xbt_dict_new();
219   if (new->father){
220     xbt_dict_set(new->father->children, new->name, new, NULL);
221     new_pajeCreateContainer (new);
222   }
223
224   //register hosts, routers, links containers
225   if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
226     xbt_dict_set (allContainers, new->name, new, NULL);
227
228     //register NODE types for triva configuration
229     xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), xbt_free);
230   }
231   return new;
232 }
233
234 static container_t recursiveGetContainer (const char *name, container_t root)
235 {
236   if (strcmp (root->name, name) == 0) return root;
237
238   xbt_dict_cursor_t cursor = NULL;
239   container_t child;
240   char *child_name;
241   xbt_dict_foreach(root->children, cursor, child_name, child) {
242     container_t ret = recursiveGetContainer(name, child);
243     if (ret) return ret;
244   }
245   return NULL;
246 }
247
248 container_t getContainer (const char *name)
249 {
250   return recursiveGetContainer(name, rootContainer);
251 }
252
253 container_t getContainerByName (const char *name)
254 {
255   return (container_t)xbt_dict_get_or_null (allContainers, name);
256 }
257
258 char *getContainerIdByName (const char *name)
259 {
260   return getContainerByName(name)->id;
261 }
262
263 container_t getRootContainer ()
264 {
265   return rootContainer;
266 }
267
268 static type_t recursiveGetType (const char *name, type_t root)
269 {
270   if (strcmp (root->name, name) == 0) return root;
271
272   xbt_dict_cursor_t cursor = NULL;
273   type_t child;
274   char *child_name;
275   xbt_dict_foreach(root->children, cursor, child_name, child) {
276     type_t ret = recursiveGetType(name, child);
277     if (ret) return ret;
278   }
279   return NULL;
280 }
281
282 type_t getType (const char *name, type_t father)
283 {
284   return recursiveGetType (name, father);
285 }
286
287 void destroyContainer (container_t container)
288 {
289   //remove me from my father
290   if (container->father){
291     xbt_dict_remove(container->father->children, container->name);
292   }
293
294   XBT_DEBUG("destroy container %s", container->name);
295
296   //obligation to dump previous events because they might
297   //reference the container that is about to be destroyed
298   TRACE_last_timestamp_to_dump = surf_get_clock();
299   TRACE_paje_dump_buffer(1);
300
301   //trace my destruction
302   new_pajeDestroyContainer(container);
303
304   //free
305   xbt_free (container->name);
306   xbt_free (container->id);
307   xbt_free (container->children);
308   xbt_free (container);
309   container = NULL;
310 }
311
312 static void recursiveDestroyContainer (container_t container)
313 {
314   xbt_dict_cursor_t cursor = NULL;
315   container_t child;
316   char *child_name;
317   xbt_dict_foreach(container->children, cursor, child_name, child) {
318     recursiveDestroyContainer (child);
319   }
320   destroyContainer (container);
321 }
322
323 static void recursiveDestroyType (type_t type)
324 {
325   xbt_dict_cursor_t cursor = NULL;
326   type_t child;
327   char *child_name;
328   xbt_dict_foreach(type->children, cursor, child_name, child) {
329     recursiveDestroyType (child);
330   }
331   xbt_free (type->name);
332   xbt_free (type->id);
333   xbt_free (type->children);
334   xbt_free (type->values);
335   xbt_free (type);
336   type = NULL;
337 }
338
339 void destroyAllContainers ()
340 {
341   if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
342   if (getRootType()) recursiveDestroyType (getRootType());
343   rootContainer = NULL;
344   rootType = NULL;
345 }
346
347
348 #endif /* HAVE_TRACING */