Logo AND Algorithmique Numérique Distribuée

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