Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] making type_t and container_t available for instr module
[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 static 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   // father of this container
171   if (new->father){
172     new->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
173   }else{
174     new->father = NULL;
175   }
176   // level depends on level of father
177   if (new->father){
178     new->level = new->father->level+1;
179   }else{
180     new->level = 0;
181   }
182   // type definition (method depends on kind of this new container)
183   new->kind = kind;
184   if (new->kind == INSTR_AS){
185     //if this container is of an AS, its type name depends on its level
186     char as_typename[INSTR_DEFAULT_STR_SIZE];
187     snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
188     if (new->father){
189       new->type = getContainerType (as_typename, new->father->type);
190     }else{
191       new->type = getContainerType ("0", NULL);
192     }
193   }else{
194     //otherwise, the name is its kind
195     switch (new->kind){
196       case INSTR_HOST: new->type = getContainerType ("HOST", new->father->type); break;
197       case INSTR_LINK: new->type = getContainerType ("LINK", new->father->type); break;
198       case INSTR_ROUTER: new->type = getContainerType ("ROUTER", new->father->type); break;
199       default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
200     }
201   }
202   new->children = xbt_dict_new();
203   if (new->father){
204     xbt_dict_set(new->father->children, new->name, new, NULL);
205     pajeCreateContainer (0, new->id, new->type->id, new->father->id, new->name);
206   }
207
208   //register hosts, routers, links containers
209   if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
210     xbt_dict_set (allContainers, new->name, new, NULL);
211   }
212
213   //register the host container types
214   if (new->kind == INSTR_HOST){
215     xbt_dynar_push_as (allHostTypes, type_t, new->type);
216   }
217
218   //register the link container types
219   if (new->kind == INSTR_LINK){
220     xbt_dynar_push_as(allLinkTypes, type_t, new->type);
221   }
222   return new;
223 }
224
225 static container_t findChild (container_t root, container_t a1)
226 {
227   if (root == a1) return root;
228
229   xbt_dict_cursor_t cursor = NULL;
230   container_t child;
231   char *child_name;
232   xbt_dict_foreach(root->children, cursor, child_name, child) {
233     if (findChild (child, a1)) return child;
234   }
235   return NULL;
236 }
237
238 static container_t findCommonFather (container_t root, container_t a1, container_t a2)
239 {
240   if (a1->father == a2->father) return a1->father;
241
242   xbt_dict_cursor_t cursor = NULL;
243   container_t child;
244   char *child_name;
245   container_t a1_try = NULL;
246   container_t a2_try = NULL;
247   xbt_dict_foreach(root->children, cursor, child_name, child) {
248     a1_try = findChild (child, a1);
249     a2_try = findChild (child, a2);
250     if (a1_try && a2_try) return child;
251   }
252   return NULL;
253 }
254
255 static void linkContainers (const char *a1, const char *a2)
256 {
257   //ignore loopback
258   if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
259     return;
260
261   container_t a1_container = ((container_t)xbt_dict_get (allContainers, a1));
262   type_t a1_type = a1_container->type;
263
264   container_t a2_container = ((container_t)xbt_dict_get (allContainers, a2));
265   type_t a2_type = a2_container->type;
266
267   container_t container = findCommonFather (rootContainer, a1_container, a2_container);
268   xbt_assert0 (container != NULL, "common father not found");
269
270   //declare type
271   char link_typename[INSTR_DEFAULT_STR_SIZE];
272   snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
273   type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
274
275   //create the link
276   static long long counter = 0;
277   char key[INSTR_DEFAULT_STR_SIZE];
278   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
279   pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
280   pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
281 }
282
283 static void recursiveGraphExtraction (container_t container)
284 {
285   if (xbt_dict_length(container->children)){
286     xbt_dict_cursor_t cursor = NULL;
287     container_t child;
288     char *child_name;
289     //bottom-up recursion
290     xbt_dict_foreach(container->children, cursor, child_name, child) {
291       recursiveGraphExtraction (child);
292     }
293
294     //let's get routes
295     xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
296     container_t child1, child2;
297     const char *child_name1, *child_name2;
298
299     xbt_dict_t filter = xbt_dict_new ();
300
301     xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
302       xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
303         //check if we already register this pair (we only need one direction)
304         char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
305         snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
306         snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
307         if (xbt_dict_get_or_null (filter, aux1)) continue;
308         if (xbt_dict_get_or_null (filter, aux2)) continue;
309
310         //ok, not found, register it
311         xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
312         xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
313
314         if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
315             (child2->kind == INSTR_HOST  || child2->kind == INSTR_ROUTER)){
316
317           //getting route
318           xbt_dynar_t route;
319           xbt_ex_t exception;
320           TRY {
321             route = global_routing->get_route (child_name1, child_name2);
322           }CATCH(exception) {
323             //no route between them, that's possible
324             continue;
325           }
326
327           //link the route members
328           unsigned int cpt;
329           void *link;
330           char *previous_entity_name = (char*)child_name1;
331           xbt_dynar_foreach (route, cpt, link) {
332             char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
333             linkContainers (previous_entity_name, link_name);
334             previous_entity_name = link_name;
335           }
336           linkContainers (previous_entity_name, child_name2);
337         }else if (child1->kind == INSTR_AS &&
338                   child2->kind == INSTR_AS &&
339                   strcmp(child_name1, child_name2) != 0){
340
341           //getting route
342           routing_component_t root = global_routing->root;
343           route_extended_t route;
344           xbt_ex_t exception;
345           TRY {
346             route = root->get_route (root, child_name1, child_name2);
347           }CATCH(exception) {
348             //no route between them, that's possible
349             continue;
350           }
351           xbt_assert2(route!=NULL,
352               "there is no ASroute between %s and %s", child_name1, child_name2);
353           unsigned int cpt;
354           void *link;
355           char *previous_entity_name = route->src_gateway;
356           xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
357             char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
358             linkContainers (previous_entity_name, link_name);
359             previous_entity_name = link_name;
360           }
361           linkContainers (previous_entity_name, route->dst_gateway);
362         }
363       }
364     }
365     xbt_dict_free(&filter);
366   }
367 }
368
369 /*
370  * Callbacks
371  */
372 static void instr_routing_parse_start_AS ()
373 {
374   if (rootContainer == NULL){
375     currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
376     allContainers = xbt_dict_new ();
377     allLinkTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
378     allHostTypes = xbt_dynar_new (sizeof(s_type_t), NULL);
379
380     rootContainer = newContainer ("0", INSTR_AS, NULL);
381     xbt_dynar_push (currentContainer, rootContainer);
382
383     if (TRACE_smpi_is_enabled()) {
384       if (!TRACE_smpi_is_grouped()){
385         container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
386         type_t mpi = getContainerType("MPI_PROCESS", father->type);
387         getStateType ("MPI_STATE", mpi);
388         getLinkType ("MPI_LINK", rootType, mpi, mpi);
389       }
390     }
391   }
392   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
393   container_t new = newContainer (A_surfxml_AS_id, INSTR_AS, father);
394
395   //push
396   xbt_dynar_push (currentContainer, new);
397 }
398
399 static void instr_routing_parse_end_AS ()
400 {
401   xbt_dynar_pop_ptr (currentContainer);
402 }
403
404 static void instr_routing_parse_start_link ()
405 {
406   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
407   container_t new = newContainer (A_surfxml_link_id, INSTR_LINK, father);
408
409   type_t bandwidth = getVariableType ("bandwidth", NULL, new->type);
410   type_t latency = getVariableType ("latency", NULL, new->type);
411   pajeSetVariable (0, bandwidth->id, new->id, A_surfxml_link_bandwidth);
412   pajeSetVariable (0, latency->id, new->id, A_surfxml_link_latency);
413   if (TRACE_uncategorized()){
414     getVariableType ("bandwidth_used", "0.5 0.5 0.5", new->type);
415   }
416 }
417
418 static void instr_routing_parse_end_link ()
419 {
420 }
421
422 static void instr_routing_parse_start_host ()
423 {
424   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
425   container_t new = newContainer (A_surfxml_host_id, INSTR_HOST, father);
426
427   type_t power = getVariableType ("power", NULL, new->type);
428   pajeSetVariable (0, power->id, new->id, A_surfxml_host_power);
429   if (TRACE_uncategorized()){
430     getVariableType ("power_used", "0.5 0.5 0.5", new->type);
431   }
432
433   if (TRACE_smpi_is_enabled()) {
434     if (TRACE_smpi_is_grouped()){
435       type_t mpi = getContainerType("MPI_PROCESS", new->type);
436       getStateType ("MPI_STATE", mpi);
437       getLinkType ("MPI_LINK", rootType, mpi, mpi);
438     }
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