Logo AND Algorithmique Numérique Distribuée

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