Logo AND Algorithmique Numérique Distribuée

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