Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] recursive extraction of connections between hosts/link/routers
[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         }
221       }
222     }
223   }
224 }
225
226 /*
227  * Callbacks
228  */
229 static void instr_routing_parse_start_AS ()
230 {
231   if (rootContainer == NULL){
232     rootContainer = xbt_new0(s_container_t, 1);
233     rootContainer->name = xbt_strdup ("0");
234     rootContainer->type = xbt_strdup ("0");
235     rootContainer->typename = xbt_strdup ("0");
236     rootContainer->level = 0;
237     rootContainer->father = NULL;
238     rootContainer->children = xbt_dict_new();
239     rootContainer->kind = INSTR_AS;
240
241     currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
242     xbt_dynar_push (currentContainer, rootContainer);
243
244     allContainers = xbt_dict_new ();
245     hosts_types = xbt_dict_new ();
246     links_types = xbt_dict_new ();
247   }
248
249   container_t newContainer = xbt_new0(s_container_t, 1);
250   newContainer->name = xbt_strdup (A_surfxml_AS_id);
251   newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
252   newContainer->level = newContainer->father->level+1;
253   newContainer->type = instr_AS_type (newContainer->level);
254   newContainer->typename = instr_AS_type (newContainer->level);
255   newContainer->children = xbt_dict_new();
256   newContainer->kind = INSTR_AS;
257   xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
258
259   //trace
260   newContainerType (newContainer->type, newContainer->father->type, newContainer->type);
261   pajeCreateContainer (0, newContainer->name, newContainer->type, newContainer->father->name, newContainer->name);
262
263   //push
264   xbt_dynar_push (currentContainer, newContainer);
265 }
266
267 static void instr_routing_parse_end_AS ()
268 {
269   xbt_dynar_pop_ptr (currentContainer);
270 }
271
272 static void instr_routing_parse_start_link ()
273 {
274   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
275   char type[INSTR_DEFAULT_STR_SIZE];
276   snprintf (type, INSTR_DEFAULT_STR_SIZE, "LINK-%s", father->type);
277   container_t new = newContainer (A_surfxml_link_id, type, INSTR_LINK);
278
279   //bandwidth and latency
280   char bandwidth_type[INSTR_DEFAULT_STR_SIZE], latency_type[INSTR_DEFAULT_STR_SIZE];
281   snprintf (bandwidth_type, INSTR_DEFAULT_STR_SIZE, "bandwidth-%s", type);
282   snprintf (latency_type, INSTR_DEFAULT_STR_SIZE, "latency-%s", type);
283   newVariableType (bandwidth_type, type, "bandwidth", NULL);
284   newVariableType (latency_type, type, "latency", NULL);
285   pajeSetVariable(0, bandwidth_type, new->name, A_surfxml_link_bandwidth);
286   pajeSetVariable(0, latency_type, new->name, A_surfxml_link_latency);
287
288   if (TRACE_uncategorized()){
289     //bandwidth_used
290     char bandwidth_used_type[INSTR_DEFAULT_STR_SIZE];
291     snprintf (bandwidth_used_type, INSTR_DEFAULT_STR_SIZE, "bandwidth_used-%s", type);
292     newVariableType (bandwidth_used_type, type, "bandwidth_used", "0.5 0.5 0.5");
293   }
294
295   //register created link on the dictionary
296   xbt_dict_set (allContainers, A_surfxml_link_id, new, NULL);
297
298   //register this link type
299   xbt_dict_set (links_types, type, xbt_strdup("1"), xbt_free);
300 }
301
302 static void instr_routing_parse_end_link ()
303 {
304 }
305
306 static void instr_routing_parse_start_host ()
307 {
308   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
309   char type[INSTR_DEFAULT_STR_SIZE];
310   snprintf (type, INSTR_DEFAULT_STR_SIZE, "HOST-%s", father->type);
311   container_t new = newContainer (A_surfxml_host_id, type, INSTR_HOST);
312
313   //power
314   char power_type[INSTR_DEFAULT_STR_SIZE];
315   snprintf (power_type, INSTR_DEFAULT_STR_SIZE, "power-%s", type);
316   newVariableType (power_type, type, "power", NULL);
317   pajeSetVariable(0, power_type, new->name, A_surfxml_host_power);
318
319   if (TRACE_uncategorized()){
320     //power_used
321     char power_used_type[INSTR_DEFAULT_STR_SIZE];
322     snprintf (power_used_type, INSTR_DEFAULT_STR_SIZE, "power_used-%s", type);
323     newVariableType (power_used_type, type, "power_used", "0.5 0.5 0.5");
324   }
325
326   //register created host on the dictionary
327   xbt_dict_set (allContainers, A_surfxml_host_id, new, NULL);
328
329   //register this link type
330   xbt_dict_set (hosts_types, type, xbt_strdup("1"), xbt_free);
331 }
332
333 static void instr_routing_parse_end_host ()
334 {
335 }
336
337 static void instr_routing_parse_start_router ()
338 {
339   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
340   char type[INSTR_DEFAULT_STR_SIZE];
341   snprintf (type, INSTR_DEFAULT_STR_SIZE, "ROUTER-%s", father->type);
342   container_t new = newContainer (A_surfxml_router_id, type, INSTR_ROUTER);
343
344   //register created host on the dictionary
345   xbt_dict_set (allContainers, A_surfxml_router_id, new, NULL);
346 }
347
348 static void instr_routing_parse_end_router ()
349 {
350 }
351
352 static void instr_routing_parse_end_platform ()
353 {
354   currentContainer = NULL;
355   recursiveGraphExtraction (rootContainer);
356 }
357
358 /*
359  * Support functions
360  */
361 int instr_link_is_traced (const char *name)
362 {
363   if (((container_t)xbt_dict_get_or_null (allContainers, name))){
364     return 1;
365   } else {
366     return 0;
367   }
368 }
369
370 char *instr_link_type (const char *name)
371 {
372   return ((container_t)xbt_dict_get (allContainers, name))->type;
373 }
374
375 char *instr_host_type (const char *name)
376 {
377   return ((container_t)xbt_dict_get (allContainers, name))->type;
378 }
379
380 void instr_destroy_platform ()
381 {
382   if (rootContainer) recursiveDestroyContainer (rootContainer);
383 }
384
385 #endif /* HAVE_TRACING */
386