Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
03663d5b2230a227c78480ed289c47733312cf56
[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   TYPE_VARIABLE,
19   TYPE_LINK,
20   TYPE_CONTAINER,
21 } e_entity_types;
22
23 typedef struct s_type *type_t;
24 typedef struct s_type {
25   char *id;
26   char *name;
27   e_entity_types kind;
28   struct s_type *father;
29   xbt_dict_t children;
30 }s_type_t;
31
32 typedef enum {
33   INSTR_HOST,
34   INSTR_LINK,
35   INSTR_ROUTER,
36   INSTR_AS,
37 } e_container_types;
38
39 typedef struct s_container *container_t;
40 typedef struct s_container {
41   char *name;     /* Unique name of this container */
42   char *id;       /* Unique id of this container */
43   type_t type;    /* Type of this container */
44   int level;      /* Level in the hierarchy, root level is 0 */
45   e_container_types kind; /* This container is of what kind */
46   struct s_container *father;
47   xbt_dict_t children;
48 }s_container_t;
49
50 static type_t rootType = NULL;              /* the root type */
51 static container_t rootContainer = NULL;    /* the root container */
52 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
53 static xbt_dict_t allContainers = NULL;     /* all created containers indexed by name */
54 xbt_dynar_t allLinkTypes = NULL;     /* all link types defined */
55 xbt_dynar_t allHostTypes = NULL;     /* all host types defined */
56
57 static void instr_routing_parse_start_AS (void);
58 static void instr_routing_parse_end_AS (void);
59 static void instr_routing_parse_start_link (void);
60 static void instr_routing_parse_end_link (void);
61 static void instr_routing_parse_start_host (void);
62 static void instr_routing_parse_end_host (void);
63 static void instr_routing_parse_start_router (void);
64 static void instr_routing_parse_end_router (void);
65 static void instr_routing_parse_end_platform (void);
66
67 static long long int newTypeId ()
68 {
69   static long long int counter = 0;
70   return counter++;
71 }
72
73 static type_t newType (const char *typename, e_entity_types kind, type_t father)
74 {
75   type_t ret = xbt_new0(s_type_t, 1);
76   ret->name = xbt_strdup (typename);
77   ret->father = father;
78   ret->kind = kind;
79   ret->children = xbt_dict_new ();
80
81   long long int id = newTypeId();
82   char str_id[INSTR_DEFAULT_STR_SIZE];
83   snprintf (str_id, INSTR_DEFAULT_STR_SIZE, "%lld", id);
84   ret->id = xbt_strdup (str_id);
85
86   if (father != NULL){
87     xbt_dict_set (father->children, typename, ret, NULL);
88   }
89   return ret;
90 }
91
92 static type_t newContainerType (const char *typename, e_entity_types kind, type_t father)
93 {
94   type_t ret = newType (typename, kind, father);
95 //  if (father) INFO4("ContainerType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
96   if (father) pajeDefineContainerType(ret->id, ret->father->id, ret->name);
97   return ret;
98 }
99
100 static type_t newVariableType (const char *typename, e_entity_types kind, const char *color, type_t father)
101 {
102   type_t ret = newType (typename, kind, father);
103 //  INFO4("VariableType %s(%s), child of %s(%s)", ret->name, ret->id, father->name, father->id);
104   if (color){
105     pajeDefineVariableTypeWithColor(ret->id, ret->father->id, ret->name, color);
106   }else{
107     pajeDefineVariableType(ret->id, ret->father->id, ret->name);
108   }
109   return ret;
110 }
111
112 static type_t newLinkType (const char *typename, e_entity_types kind, type_t father, type_t source, type_t dest)
113 {
114   type_t ret = newType (typename, kind, father);
115 //  INFO8("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);
116   pajeDefineLinkType(ret->id, ret->father->id, source->id, dest->id, ret->name);
117   return ret;
118 }
119
120 static type_t getContainerType (const char *typename, type_t father)
121 {
122   type_t ret;
123   if (father == NULL){
124     ret = newContainerType (typename, TYPE_CONTAINER, father);
125     rootType = ret;
126   }else{
127     //check if my father type already has my typename
128     ret = (type_t)xbt_dict_get_or_null (father->children, typename);
129     if (ret == NULL){
130       ret = newContainerType (typename, TYPE_CONTAINER, father);
131     }
132   }
133   return ret;
134 }
135
136 static type_t getVariableType (const char *typename, const char *color, type_t father)
137 {
138   type_t ret = xbt_dict_get_or_null (father->children, typename);
139   if (ret == NULL){
140     ret = newVariableType (typename, TYPE_VARIABLE, color, father);
141   }
142   return ret;
143 }
144
145 static type_t getLinkType (const char *typename, type_t father, type_t source, type_t dest)
146 {
147   type_t ret = xbt_dict_get_or_null (father->children, typename);
148   if (ret == NULL){
149     ret = newLinkType (typename, TYPE_LINK, father, source, dest);
150   }
151   return ret;
152 }
153
154 void instr_routing_define_callbacks ()
155 {
156   if (!TRACE_is_active())
157     return;
158   surfxml_add_callback(STag_surfxml_AS_cb_list, &instr_routing_parse_start_AS);
159   surfxml_add_callback(ETag_surfxml_AS_cb_list, &instr_routing_parse_end_AS);
160   surfxml_add_callback(STag_surfxml_link_cb_list, &instr_routing_parse_start_link);
161   surfxml_add_callback(ETag_surfxml_link_cb_list, &instr_routing_parse_end_link);
162   surfxml_add_callback(STag_surfxml_host_cb_list, &instr_routing_parse_start_host);
163   surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
164   surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
165   surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
166   surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
167 }
168
169 static long long int newContainedId ()
170 {
171   static long long counter = 0;
172   return counter++;
173 }
174
175 static container_t newContainer (const char *name, e_container_types kind, container_t father)
176 {
177   long long int counter = newContainedId();
178   char id_str[INSTR_DEFAULT_STR_SIZE];
179   snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", counter);
180
181   container_t new = xbt_new0(s_container_t, 1);
182   new->name = xbt_strdup (name); // name of the container
183   new->id = xbt_strdup (id_str); // id (or alias) of the container
184   new->father = father;
185   // father of this container
186   if (new->father){
187     new->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
188   }else{
189     new->father = NULL;
190   }
191   // level depends on level of father
192   if (new->father){
193     new->level = new->father->level+1;
194   }else{
195     new->level = 0;
196   }
197   // type definition (method depends on kind of this new container)
198   new->kind = kind;
199   if (new->kind == INSTR_AS){
200     //if this container is of an AS, its type name depends on its level
201     char as_typename[INSTR_DEFAULT_STR_SIZE];
202     snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
203     if (new->father){
204       new->type = getContainerType (as_typename, new->father->type);
205     }else{
206       new->type = getContainerType ("0", NULL);
207     }
208   }else{
209     //otherwise, the name is its kind
210     switch (new->kind){
211       case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
212       case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
213       case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
214       default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
215     }
216   }
217   new->children = xbt_dict_new();
218   if (new->father){
219     xbt_dict_set(new->father->children, new->name, new, NULL);
220     pajeCreateContainer (0, new->id, new->type->id, new->father->id, new->name);
221   }
222
223   //register hosts, routers, links containers
224   if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
225     xbt_dict_set (allContainers, new->name, new, NULL);
226   }
227
228   //register the host container types
229   if (new->kind == INSTR_HOST){
230     xbt_dynar_push_as (allHostTypes, type_t, new->type);
231   }
232
233   //register the link container types
234   if (new->kind == INSTR_LINK){
235     xbt_dynar_push_as(allLinkTypes, type_t, new->type);
236   }
237   return new;
238 }
239
240 static container_t findChild (container_t root, container_t a1)
241 {
242   if (root == a1) return root;
243
244   xbt_dict_cursor_t cursor = NULL;
245   container_t child;
246   char *child_name;
247   xbt_dict_foreach(root->children, cursor, child_name, child) {
248     if (findChild (child, a1)) return child;
249   }
250   return NULL;
251 }
252
253 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
254 {
255   if (a1->father == a2->father) return a1->father;
256
257   xbt_dict_cursor_t cursor = NULL;
258   container_t child;
259   char *child_name;
260   container_t a1_try = NULL;
261   container_t a2_try = NULL;
262   xbt_dict_foreach(root->children, cursor, child_name, child) {
263     a1_try = findChild (child, a1);
264     a2_try = findChild (child, a2);
265     if (a1_try && a2_try) return child;
266   }
267   return NULL;
268 }
269
270 static void linkContainers (const char *a1, const char *a2)
271 {
272   //ignore loopback
273   if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
274     return;
275
276   container_t a1_container = ((container_t)xbt_dict_get (allContainers, a1));
277   type_t a1_type = a1_container->type;
278
279   container_t a2_container = ((container_t)xbt_dict_get (allContainers, a2));
280   type_t a2_type = a2_container->type;
281
282   container_t container = findCommonFather (rootContainer, a1_container, a2_container);
283   xbt_assert0 (container != NULL, "common father not found");
284
285   //declare type
286   char link_typename[INSTR_DEFAULT_STR_SIZE];
287   snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
288   type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
289
290   //create the link
291   static long long counter = 0;
292   char key[INSTR_DEFAULT_STR_SIZE];
293   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
294   pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
295   pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
296 }
297
298 static void recursiveGraphExtraction (container_t container)
299 {
300   if (xbt_dict_length(container->children)){
301     xbt_dict_cursor_t cursor = NULL;
302     container_t child;
303     char *child_name;
304     //bottom-up recursion
305     xbt_dict_foreach(container->children, cursor, child_name, child) {
306       recursiveGraphExtraction (child);
307     }
308
309     //let's get routes
310     xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
311     container_t child1, child2;
312     const char *child_name1, *child_name2;
313
314     xbt_dict_t filter = xbt_dict_new ();
315
316     xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
317       xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
318         //check if we already register this pair (we only need one direction)
319         char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
320         snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
321         snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
322         if (xbt_dict_get_or_null (filter, aux1)) continue;
323         if (xbt_dict_get_or_null (filter, aux2)) continue;
324
325         //ok, not found, register it
326         xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
327         xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
328
329         if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
330             (child2->kind == INSTR_HOST  || child2->kind == INSTR_ROUTER)){
331
332           //getting route
333           xbt_dynar_t route;
334           xbt_ex_t exception;
335           TRY {
336             route = global_routing->get_route (child_name1, child_name2);
337           }CATCH(exception) {
338             //no route between them, that's possible
339             continue;
340           }
341
342           //link the route members
343           unsigned int cpt;
344           void *link;
345           char *previous_entity_name = (char*)child_name1;
346           xbt_dynar_foreach (route, cpt, link) {
347             char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
348             linkContainers (previous_entity_name, link_name);
349             previous_entity_name = link_name;
350           }
351           linkContainers (previous_entity_name, child_name2);
352         }else if (child1->kind == INSTR_AS &&
353                   child2->kind == INSTR_AS &&
354                   strcmp(child_name1, child_name2) != 0){
355
356           //getting route
357           routing_component_t root = global_routing->root;
358           route_extended_t route;
359           xbt_ex_t exception;
360           TRY {
361             route = root->get_route (root, child_name1, child_name2);
362           }CATCH(exception) {
363             //no route between them, that's possible
364             continue;
365           }
366           xbt_assert2(route!=NULL,
367               "there is no ASroute between %s and %s", child_name1, child_name2);
368           unsigned int cpt;
369           void *link;
370           char *previous_entity_name = route->src_gateway;
371           xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
372             char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
373             linkContainers (previous_entity_name, link_name);
374             previous_entity_name = link_name;
375           }
376           linkContainers (previous_entity_name, route->dst_gateway);
377         }
378       }
379     }
380     xbt_dict_free(&filter);
381   }
382 }
383
384 /*
385  * Callbacks
386  */
387 static void instr_routing_parse_start_AS ()
388 {
389   if (rootContainer == NULL){
390     currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
391     allContainers = xbt_dict_new ();
392     allLinkTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
393     allHostTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
394
395     rootContainer = newContainer ("0", INSTR_AS, NULL);
396     xbt_dynar_push (currentContainer, rootContainer);
397
398   }
399   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
400   container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
401
402   //push
403   xbt_dynar_push (currentContainer, new);
404 }
405
406 static void instr_routing_parse_end_AS ()
407 {
408   xbt_dynar_pop_ptr (currentContainer);
409 }
410
411 static void instr_routing_parse_start_link ()
412 {
413   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
414   container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
415
416   type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
417   type_t latency = getVariableType ("latency", NULL, new->type);
418   pajeSetVariable (0, bandwidth->id, new->id, A_surfxml_link_bandwidth);
419   pajeSetVariable (0, latency->id, new->id, A_surfxml_link_latency);
420   if (TRACE_uncategorized()){
421     getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
422   }
423 }
424
425 static void instr_routing_parse_end_link ()
426 {
427 }
428
429 static void instr_routing_parse_start_host ()
430 {
431   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
432   container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
433
434   type_t power = getVariableType ("power", NULL, new->type);
435   pajeSetVariable (0, power->id, new->id, A_surfxml_host_power);
436   if (TRACE_uncategorized()){
437     getVariableType ("power_used", "0.5 0.5 0.5", new->type);
438   }
439 }
440
441 static void instr_routing_parse_end_host ()
442 {
443 }
444
445 static void instr_routing_parse_start_router ()
446 {
447   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
448   newContainer (A_surfxml_router_id, INSTR_ROUTER, father);
449 }
450
451 static void instr_routing_parse_end_router ()
452 {
453 }
454
455 static void instr_routing_parse_end_platform ()
456 {
457   currentContainer = NULL;
458   recursiveGraphExtraction (rootContainer);
459 }
460
461 /*
462  * Support functions
463  */
464 int instr_link_is_traced (const char *name)
465 {
466   if (((container_t)xbt_dict_get_or_null (allContainers, name))){
467     return 1;
468   } else {
469     return 0;
470   }
471 }
472
473 char *instr_variable_type (const char *name, const char *resource)
474 {
475   container_t container = (container_t)xbt_dict_get (allContainers, resource);
476   xbt_dict_cursor_t cursor = NULL;
477   type_t type;
478   char *type_name;
479   xbt_dict_foreach(container->type->children, cursor, type_name, type) {
480     if (strcmp (name, type->name) == 0) return type->id;
481   }
482   return NULL;
483 }
484
485 char *instr_resource_type (const char *resource_name)
486 {
487   return ((container_t)xbt_dict_get_or_null (allContainers, resource_name))->id;
488 }
489
490 static void recursiveDestroyContainer (container_t container)
491 {
492   xbt_dict_cursor_t cursor = NULL;
493   container_t child;
494   char *child_name;
495   xbt_dict_foreach(container->children, cursor, child_name, child) {
496     recursiveDestroyContainer (child);
497   }
498
499   pajeDestroyContainer(SIMIX_get_clock(), container->type->id, container->id);
500
501   xbt_free (container->name);
502   xbt_free (container->id);
503   xbt_free (container->children);
504   xbt_free (container);
505   container = NULL;
506 }
507
508 static void recursiveDestroyType (type_t type)
509 {
510   xbt_dict_cursor_t cursor = NULL;
511   type_t child;
512   char *child_name;
513   xbt_dict_foreach(type->children, cursor, child_name, child) {
514     recursiveDestroyType (child);
515   }
516   xbt_free (type->name);
517   xbt_free (type->id);
518   xbt_free (type->children);
519   xbt_free (type);
520   type = NULL;
521 }
522
523 void instr_destroy_platform ()
524 {
525   if (rootContainer) recursiveDestroyContainer (rootContainer);
526   if (rootType) recursiveDestroyType (rootType);
527 }
528
529 /*
530  * user categories support
531  */
532 static void recursiveNewUserVariableType (const char *new_typename, const char *color, type_t root)
533 {
534   if (!strcmp (root->name, "HOST") || !strcmp (root->name, "LINK")){
535     newVariableType(new_typename, TYPE_VARIABLE, color, root);
536   }
537   xbt_dict_cursor_t cursor = NULL;
538   type_t child_type;
539   char *name;
540   xbt_dict_foreach(root->children, cursor, name, child_type) {
541     recursiveNewUserVariableType (new_typename, color, child_type);
542   }
543 }
544
545 void instr_new_user_variable_type (const char *new_typename, const char *color)
546 {
547   recursiveNewUserVariableType (new_typename, color, rootType);
548 }
549
550 static void recursiveNewUserLinkVariableType (const char *new_typename, const char *color, type_t root)
551 {
552   if (!strcmp (root->name, "LINK")){
553     newVariableType(new_typename, TYPE_VARIABLE, color, root);
554   }
555   xbt_dict_cursor_t cursor = NULL;
556   type_t child_type;
557   char *name;
558   xbt_dict_foreach(root->children, cursor, name, child_type) {
559     recursiveNewUserLinkVariableType (new_typename, color, child_type);
560   }
561 }
562
563 void instr_new_user_link_variable_type  (const char *new_typename, const char *color)
564 {
565   recursiveNewUserLinkVariableType (new_typename, color, rootType);
566 }
567
568
569 static void recursiveNewUserHostVariableType (const char *new_typename, const char *color, type_t root)
570 {
571   if (!strcmp (root->name, "HOST")){
572     newVariableType(new_typename, TYPE_VARIABLE, color, root);
573   }
574   xbt_dict_cursor_t cursor = NULL;
575   type_t child_type;
576   char *name;
577   xbt_dict_foreach(root->children, cursor, name, child_type) {
578     recursiveNewUserHostVariableType (new_typename, color, child_type);
579   }
580 }
581
582 void instr_new_user_host_variable_type  (const char *new_typename, const char *color)
583 {
584   recursiveNewUserHostVariableType (new_typename, color, rootType);
585 }
586
587 #endif /* HAVE_TRACING */
588