Logo AND Algorithmique Numérique Distribuée

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