Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] new field id to make the final trace file smaller
[simgrid.git] / src / instr / instr_routing.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 #include "surf/surf_private.h"
11 #include "surf/network_private.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
14
15 extern xbt_dict_t defined_types; /* from instr_interface.c */
16
17 typedef enum {
18   INSTR_HOST,
19   INSTR_LINK,
20   INSTR_ROUTER,
21   INSTR_AS,
22 } e_container_types;
23
24 typedef struct s_container *container_t;
25 typedef struct s_container {
26   char *name;     /* Unique name of this container */
27   char *id;       /* Unique id of this container */
28   char *type;     /* Type of this container */
29   char *typename; /* Type name of this container */
30   int level;      /* Level in the hierarchy, root level is 0 */
31   e_container_types kind; /* This container is of what kind */
32   struct s_container *father;
33   xbt_dict_t children;
34 }s_container_t;
35
36 static container_t rootContainer = NULL;    /* the root container */
37 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
38 static xbt_dict_t allContainers = NULL;     /* all created containers indexed by name */
39 xbt_dict_t hosts_types = NULL;
40 xbt_dict_t links_types = NULL;
41
42 static void instr_routing_parse_start_AS (void);
43 static void instr_routing_parse_end_AS (void);
44 static void instr_routing_parse_start_link (void);
45 static void instr_routing_parse_end_link (void);
46 static void instr_routing_parse_start_host (void);
47 static void instr_routing_parse_end_host (void);
48 static void instr_routing_parse_start_router (void);
49 static void instr_routing_parse_end_router (void);
50 static void instr_routing_parse_end_platform (void);
51 static char *instr_AS_type (int level);
52
53 static char *instr_AS_type (int level)
54 {
55   char *ret = xbt_new (char, INSTR_DEFAULT_STR_SIZE);
56   if (level == 0){
57     snprintf (ret, INSTR_DEFAULT_STR_SIZE, "0");
58   }else{
59     snprintf (ret, INSTR_DEFAULT_STR_SIZE, "L%d", level);
60   }
61   return ret;
62 }
63
64 static void newContainerType (const char *type, const char *parentType, const char *name)
65 {
66   char *defined = xbt_dict_get_or_null (defined_types, type);
67   if (!defined){
68     pajeDefineContainerType(type, parentType, name);
69     xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
70   }
71 }
72
73
74 static void newVariableType (const char *type, const char *parentType, const char *name, const char *color)
75 {
76   char *defined = xbt_dict_get_or_null (defined_types, type);
77   if (!defined){
78     if (color){
79       pajeDefineVariableTypeWithColor(type, parentType, name, color);
80     }else{
81       pajeDefineVariableType(type, parentType, name);
82     }
83     xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
84   }
85 }
86
87 static void newLinkType (const char *type, const char *parentType, const char *sourceType, const char *destType, const char *name)
88 {
89   char *defined = xbt_dict_get_or_null (defined_types, type);
90   if (!defined){
91     pajeDefineLinkType(type, parentType, sourceType, destType, name);
92     xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
93   }
94 }
95
96 void instr_routing_define_callbacks ()
97 {
98   if (!TRACE_is_active())
99     return;
100   surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
101   surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
102   surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
103   surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
104   surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
105   surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
106   surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
107   surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
108   surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
109 }
110
111 static long long int newContainedId ()
112 {
113   static long long counter = 0;
114   return counter++;
115 }
116
117 static container_t newContainer (const char *name, const char *type, e_container_types kind)
118 {
119   long long int counter = newContainedId();
120   char id_str[INSTR_DEFAULT_STR_SIZE];
121   snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
122
123   container_t newContainer = xbt_new0(s_container_t, 1);
124   newContainer->name = xbt_strdup (name);
125   newContainer->id = xbt_strdup (id_str);
126   newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
127   newContainer->level = newContainer->father->level+1;
128   newContainer->type = xbt_strdup (type);
129   switch (kind){
130     case INSTR_HOST: newContainer->typename = xbt_strdup ("HOST"); break;
131     case INSTR_LINK: newContainer->typename = xbt_strdup ("LINK"); break;
132     case INSTR_ROUTER: newContainer->typename = xbt_strdup ("ROUTER"); break;
133     case INSTR_AS: newContainer->typename = xbt_strdup ("AS"); break;
134     default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
135   }
136   newContainer->kind = kind;
137   newContainer->children = xbt_dict_new();
138   xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
139
140   newContainerType (newContainer->type, newContainer->father->type, newContainer->typename);
141   pajeCreateContainer (0, newContainer->id, newContainer->type, newContainer->father->id, newContainer->name);
142
143   return newContainer;
144 }
145
146 static void recursiveDestroyContainer (container_t container)
147 {
148   xbt_dict_cursor_t cursor = NULL;
149   container_t child;
150   char *child_name;
151   xbt_dict_foreach(container->children, cursor, child_name, child) {
152     recursiveDestroyContainer (child);
153   }
154
155   pajeDestroyContainer(SIMIX_get_clock(), container->type, container->id);
156
157   xbt_free (container->name);
158   xbt_free (container->id);
159   xbt_free (container->type);
160   xbt_free (container->children);
161   xbt_free (container);
162   container = NULL;
163 }
164
165 static void linkContainers (container_t container, const char *a1, const char *a2)
166 {
167   //ignore loopback
168   if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
169     return;
170
171   char *a1_id = ((container_t)xbt_dict_get (allContainers, a1))->id;
172   char *a1_type = ((container_t)xbt_dict_get (allContainers, a1))->type;
173   char *a1_typename = ((container_t)xbt_dict_get (allContainers, a1))->typename;
174
175   char *a2_id = ((container_t)xbt_dict_get (allContainers, a2))->id;
176   char *a2_type = ((container_t)xbt_dict_get (allContainers, a2))->type;
177   char *a2_typename = ((container_t)xbt_dict_get (allContainers, a2))->typename;
178
179   //declare type
180   char new_link_type[INSTR_DEFAULT_STR_SIZE], new_link_typename[INSTR_DEFAULT_STR_SIZE];
181   snprintf (new_link_type, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type, a2_type);
182   snprintf (new_link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_typename, a2_typename);
183   newLinkType (new_link_type, container->type, a1_type, a2_type, new_link_typename);
184
185   //create the link
186   static long long counter = 0;
187   char key[INSTR_DEFAULT_STR_SIZE];
188   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
189   pajeStartLink(SIMIX_get_clock(), new_link_type, container->id, "G", a1_id, key);
190   pajeEndLink(SIMIX_get_clock(), new_link_type, container->id, "G", a2_id, key);
191 }
192
193 static void recursiveGraphExtraction (container_t container)
194 {
195   if (xbt_dict_length(container->children)){
196     xbt_dict_cursor_t cursor = NULL;
197     container_t child;
198     char *child_name;
199     //bottom-up recursion
200     xbt_dict_foreach(container->children, cursor, child_name, child) {
201       recursiveGraphExtraction (child);
202     }
203
204     //let's get routes
205     xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
206     container_t child1, child2;
207     const char *child_name1, *child_name2;
208
209     xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
210       xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
211         if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
212             (child2->kind == INSTR_HOST  || child2->kind == INSTR_ROUTER)){
213
214           //getting route
215           xbt_dynar_t route;
216           xbt_ex_t exception;
217           TRY {
218             route = global_routing->get_route (child_name1, child_name2);
219           }CATCH(exception) {
220             //no route between them, that's possible
221             continue;
222           }
223
224           //link the route members
225           unsigned int cpt;
226           void *link;
227           char *previous_entity_name = (char*)child_name1;
228           xbt_dynar_foreach (route, cpt, link) {
229             char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
230             linkContainers (container, previous_entity_name, link_name);
231             previous_entity_name = link_name;
232           }
233           linkContainers (container, previous_entity_name, child_name2);
234         }else if (child1->kind == INSTR_AS &&
235                   child2->kind == INSTR_AS &&
236                   strcmp(child_name1, child_name2) != 0){
237
238           //getting route
239           routing_component_t root = global_routing->root;
240           route_extended_t route;
241           xbt_ex_t exception;
242           TRY {
243             route = root->get_route (root, child_name1, child_name2);
244           }CATCH(exception) {
245             //no route between them, that's possible
246             continue;
247           }
248           unsigned int cpt;
249           void *link;
250           char *previous_entity_name = route->src_gateway;
251           xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
252             char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
253             linkContainers (container, previous_entity_name, link_name);
254             previous_entity_name = link_name;
255           }
256           linkContainers (container, previous_entity_name, route->dst_gateway);
257         }
258       }
259     }
260   }
261 }
262
263 /*
264  * Callbacks
265  */
266 static void instr_routing_parse_start_AS ()
267 {
268   if (rootContainer == NULL){
269     rootContainer = xbt_new0(s_container_t, 1);
270     rootContainer->name = xbt_strdup ("0");
271     rootContainer->id = xbt_strdup (rootContainer->name);
272     rootContainer->type = xbt_strdup ("0");
273     rootContainer->typename = xbt_strdup ("0");
274     rootContainer->level = 0;
275     rootContainer->father = NULL;
276     rootContainer->children = xbt_dict_new();
277     rootContainer->kind = INSTR_AS;
278
279     currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
280     xbt_dynar_push (currentContainer, rootContainer);
281
282     allContainers = xbt_dict_new ();
283     hosts_types = xbt_dict_new ();
284     links_types = xbt_dict_new ();
285   }
286
287   long long int counter = newContainedId();
288   char id_str[INSTR_DEFAULT_STR_SIZE];
289   snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
290
291   container_t newContainer = xbt_new0(s_container_t, 1);
292   newContainer->name = xbt_strdup (A_surfxml_AS_id);
293   newContainer->id = xbt_strdup (id_str);
294   newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
295   newContainer->level = newContainer->father->level+1;
296   newContainer->type = instr_AS_type (newContainer->level);
297   newContainer->typename = instr_AS_type (newContainer->level);
298   newContainer->children = xbt_dict_new();
299   newContainer->kind = INSTR_AS;
300   xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
301
302   //trace
303   newContainerType (newContainer->type, newContainer->father->type, newContainer->type);
304   pajeCreateContainer (0, newContainer->id, newContainer->type, newContainer->father->id, newContainer->name);
305
306   //push
307   xbt_dynar_push (currentContainer, newContainer);
308 }
309
310 static void instr_routing_parse_end_AS ()
311 {
312   xbt_dynar_pop_ptr (currentContainer);
313 }
314
315 static void instr_routing_parse_start_link ()
316 {
317   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
318   char type[INSTR_DEFAULT_STR_SIZE];
319   snprintf (type, INSTR_DEFAULT_STR_SIZE, "LINK-%s", father->type);
320   container_t new = newContainer (A_surfxml_link_id, type, INSTR_LINK);
321
322   //bandwidth and latency
323   char bandwidth_type[INSTR_DEFAULT_STR_SIZE], latency_type[INSTR_DEFAULT_STR_SIZE];
324   snprintf (bandwidth_type, INSTR_DEFAULT_STR_SIZE, "bandwidth-%s", type);
325   snprintf (latency_type, INSTR_DEFAULT_STR_SIZE, "latency-%s", type);
326   newVariableType (bandwidth_type, type, "bandwidth", NULL);
327   newVariableType (latency_type, type, "latency", NULL);
328   pajeSetVariable(0, bandwidth_type, new->id, A_surfxml_link_bandwidth);
329   pajeSetVariable(0, latency_type, new->id, A_surfxml_link_latency);
330
331   if (TRACE_uncategorized()){
332     //bandwidth_used
333     char bandwidth_used_type[INSTR_DEFAULT_STR_SIZE];
334     snprintf (bandwidth_used_type, INSTR_DEFAULT_STR_SIZE, "bandwidth_used-%s", type);
335     newVariableType (bandwidth_used_type, type, "bandwidth_used", "0.5 0.5 0.5");
336   }
337
338   //register created link on the dictionary
339   xbt_dict_set (allContainers, A_surfxml_link_id, new, NULL);
340
341   //register this link type
342   xbt_dict_set (links_types, type, xbt_strdup("1"), xbt_free);
343 }
344
345 static void instr_routing_parse_end_link ()
346 {
347 }
348
349 static void instr_routing_parse_start_host ()
350 {
351   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
352   char type[INSTR_DEFAULT_STR_SIZE];
353   snprintf (type, INSTR_DEFAULT_STR_SIZE, "HOST-%s", father->type);
354   container_t new = newContainer (A_surfxml_host_id, type, INSTR_HOST);
355
356   //power
357   char power_type[INSTR_DEFAULT_STR_SIZE];
358   snprintf (power_type, INSTR_DEFAULT_STR_SIZE, "power-%s", type);
359   newVariableType (power_type, type, "power", NULL);
360   pajeSetVariable(0, power_type, new->id, A_surfxml_host_power);
361
362   if (TRACE_uncategorized()){
363     //power_used
364     char power_used_type[INSTR_DEFAULT_STR_SIZE];
365     snprintf (power_used_type, INSTR_DEFAULT_STR_SIZE, "power_used-%s", type);
366     newVariableType (power_used_type, type, "power_used", "0.5 0.5 0.5");
367   }
368
369   //register created host on the dictionary
370   xbt_dict_set (allContainers, A_surfxml_host_id, new, NULL);
371
372   //register this link type
373   xbt_dict_set (hosts_types, type, xbt_strdup("1"), xbt_free);
374 }
375
376 static void instr_routing_parse_end_host ()
377 {
378 }
379
380 static void instr_routing_parse_start_router ()
381 {
382   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
383   char type[INSTR_DEFAULT_STR_SIZE];
384   snprintf (type, INSTR_DEFAULT_STR_SIZE, "ROUTER-%s", father->type);
385   container_t new = newContainer (A_surfxml_router_id, type, INSTR_ROUTER);
386
387   //register created host on the dictionary
388   xbt_dict_set (allContainers, A_surfxml_router_id, new, NULL);
389 }
390
391 static void instr_routing_parse_end_router ()
392 {
393 }
394
395 static void instr_routing_parse_end_platform ()
396 {
397   currentContainer = NULL;
398   recursiveGraphExtraction (rootContainer);
399 }
400
401 /*
402  * Support functions
403  */
404 int instr_link_is_traced (const char *name)
405 {
406   if (((container_t)xbt_dict_get_or_null (allContainers, name))){
407     return 1;
408   } else {
409     return 0;
410   }
411 }
412
413 char *instr_link_type (const char *name)
414 {
415   return ((container_t)xbt_dict_get (allContainers, name))->type;
416 }
417
418 char *instr_host_type (const char *name)
419 {
420   return ((container_t)xbt_dict_get (allContainers, name))->type;
421 }
422
423 char *instr_id (const char *name)
424 {
425   return ((container_t)xbt_dict_get (allContainers, name))->id;
426 }
427
428 void instr_destroy_platform ()
429 {
430   if (rootContainer) recursiveDestroyContainer (rootContainer);
431 }
432
433 #endif /* HAVE_TRACING */
434