Logo AND Algorithmique Numérique Distribuée

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