Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge branches
[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 (name == NULL || root == NULL) return NULL;
237   if (strcmp (root->name, name) == 0) return root;
238
239   xbt_dict_cursor_t cursor = NULL;
240   container_t child;
241   char *child_name;
242   xbt_dict_foreach(root->children, cursor, child_name, child) {
243     container_t ret = recursiveGetContainer(name, child);
244     if (ret) return ret;
245   }
246   return NULL;
247 }
248
249 container_t getContainer (const char *name)
250 {
251   if (name == NULL) return NULL;
252   return recursiveGetContainer(name, rootContainer);
253 }
254
255 int knownContainerWithName (const char *name)
256 {
257   if (xbt_dict_get_or_null (allContainers, name)){
258     return 1;
259   }else{
260     return 0;
261   }
262 }
263
264 container_t getContainerByName (const char *name)
265 {
266   return (container_t)xbt_dict_get (allContainers, name);
267 }
268
269 char *getContainerIdByName (const char *name)
270 {
271   return getContainerByName(name)->id;
272 }
273
274 container_t getRootContainer ()
275 {
276   return rootContainer;
277 }
278
279 static type_t recursiveGetType (const char *name, type_t root)
280 {
281   if (strcmp (root->name, name) == 0) return root;
282
283   xbt_dict_cursor_t cursor = NULL;
284   type_t child;
285   char *child_name;
286   xbt_dict_foreach(root->children, cursor, child_name, child) {
287     type_t ret = recursiveGetType(name, child);
288     if (ret) return ret;
289   }
290   return NULL;
291 }
292
293 type_t getType (const char *name, type_t father)
294 {
295   return recursiveGetType (name, father);
296 }
297
298 void destroyContainer (container_t container)
299 {
300   //remove me from my father
301   if (container->father){
302     xbt_dict_remove(container->father->children, container->name);
303   }
304
305   XBT_DEBUG("destroy container %s", container->name);
306
307   //obligation to dump previous events because they might
308   //reference the container that is about to be destroyed
309   TRACE_last_timestamp_to_dump = surf_get_clock();
310   TRACE_paje_dump_buffer(1);
311
312   //trace my destruction
313   if (!TRACE_disable_destroy()){
314     //do not trace the container destruction if user requests
315     new_pajeDestroyContainer(container);
316   }
317
318   //free
319   xbt_free (container->name);
320   xbt_free (container->id);
321   xbt_free (container->children);
322   xbt_free (container);
323   container = NULL;
324 }
325
326 static void recursiveDestroyContainer (container_t container)
327 {
328   xbt_dict_cursor_t cursor = NULL;
329   container_t child;
330   char *child_name;
331   xbt_dict_foreach(container->children, cursor, child_name, child) {
332     recursiveDestroyContainer (child);
333   }
334   destroyContainer (container);
335 }
336
337 static void recursiveDestroyType (type_t type)
338 {
339   xbt_dict_cursor_t cursor = NULL;
340   type_t child;
341   char *child_name;
342   xbt_dict_foreach(type->children, cursor, child_name, child) {
343     recursiveDestroyType (child);
344   }
345   xbt_free (type->name);
346   xbt_free (type->id);
347   xbt_free (type->children);
348   xbt_free (type->values);
349   xbt_free (type);
350   type = NULL;
351 }
352
353 void destroyAllContainers ()
354 {
355   if (getRootContainer()) recursiveDestroyContainer (getRootContainer());
356   if (getRootType()) recursiveDestroyType (getRootType());
357   rootContainer = NULL;
358   rootType = NULL;
359 }
360
361
362 #endif /* HAVE_TRACING */