Logo AND Algorithmique Numérique Distribuée

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