Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
70b4c2efb2c1cb4180d3641551878d781fe1e409
[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 #include "xbt/graph.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
15
16 extern xbt_dict_t defined_types; /* from instr_interface.c */
17
18 static int platform_created = 0;            /* indicate whether the platform file has been traced */
19 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
20
21 static const char *instr_node_name (xbt_node_t node)
22 {
23   void *data = xbt_graph_node_get_data(node);
24   char *str = (char*)data;
25   return str;
26 }
27
28
29 static container_t lowestCommonAncestor (container_t a1, container_t a2)
30 {
31   //this is only an optimization (since most of a1 and a2 share the same parent)
32   if (a1->father == a2->father) return a1->father;
33
34   //create an array with all ancestors of a1
35   xbt_dynar_t ancestors_a1 = xbt_dynar_new(sizeof(container_t), NULL);
36   container_t p;
37   p = a1->father;
38   while (p){
39     xbt_dynar_push_as (ancestors_a1, container_t, p);
40     p = p->father;
41   }
42
43   //create an array with all ancestors of a2
44   xbt_dynar_t ancestors_a2 = xbt_dynar_new(sizeof(container_t), NULL);
45   p = a2->father;
46   while (p){
47     xbt_dynar_push_as (ancestors_a2, container_t, p);
48     p = p->father;
49   }
50
51   //find the lowest ancestor
52   p = NULL;
53   int i = xbt_dynar_length (ancestors_a1) - 1;
54   int j = xbt_dynar_length (ancestors_a2) - 1;
55   while (i >= 0 && j >= 0){
56     container_t a1p = *(container_t*)xbt_dynar_get_ptr (ancestors_a1, i);
57     container_t a2p = *(container_t*)xbt_dynar_get_ptr (ancestors_a2, j);
58     if (a1p == a2p){
59       p = a1p;
60     }else{
61       break;
62     }
63     i--;
64     j--;
65   }
66   xbt_dynar_free (&ancestors_a1);
67   xbt_dynar_free (&ancestors_a2);
68   return p;
69 }
70
71 static void linkContainers (container_t src, container_t dst, xbt_dict_t filter)
72 {
73   //ignore loopback
74   if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0)
75     return;
76
77   //find common father
78   container_t father = lowestCommonAncestor (src, dst);
79   if (!father){
80     xbt_die ("common father unknown, this is a tracing problem");
81   }
82
83   if (filter != NULL){
84     //check if we already register this pair (we only need one direction)
85     char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
86     snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", src->name, dst->name);
87     snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", dst->name, src->name);
88     if (xbt_dict_get_or_null (filter, aux1)) return;
89     if (xbt_dict_get_or_null (filter, aux2)) return;
90
91     //ok, not found, register it
92     xbt_dict_set (filter, aux1, xbt_strdup ("1"), NULL);
93     xbt_dict_set (filter, aux2, xbt_strdup ("1"), NULL);
94   }
95
96   //declare type
97   char link_typename[INSTR_DEFAULT_STR_SIZE];
98   snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s%s-%s%s",
99             father->type->name,
100             src->type->name, src->type->id,
101             dst->type->name, dst->type->id);
102   type_t link_type = PJ_type_get_or_null (link_typename, father->type);
103   if (link_type == NULL){
104     link_type = PJ_type_link_new (link_typename, father->type, src->type, dst->type);
105   }
106
107   //register EDGE types for triva configuration
108   xbt_dict_set (trivaEdgeTypes, link_type->name, xbt_strdup("1"), NULL);
109
110   //create the link
111   static long long counter = 0;
112   char key[INSTR_DEFAULT_STR_SIZE];
113   snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
114   new_pajeStartLink(SIMIX_get_clock(), father, link_type, src, "G", key);
115   new_pajeEndLink(SIMIX_get_clock(), father, link_type, dst, "G", key);
116 }
117
118 static void recursiveGraphExtraction (AS_t rc, container_t container, xbt_dict_t filter)
119 {
120   if (!xbt_dict_is_empty(rc->routing_sons)){
121     xbt_dict_cursor_t cursor = NULL;
122     AS_t rc_son;
123     char *child_name;
124     //bottom-up recursion
125     xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
126       container_t child_container = xbt_dict_get (container->children, rc_son->name);
127       recursiveGraphExtraction (rc_son, child_container, filter);
128     }
129   }
130
131   //let's get routes
132   xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
133   container_t child1, child2;
134   const char *child1_name, *child2_name;
135   xbt_dict_foreach(container->children, cursor1, child1_name, child1) {
136     if (child1->kind == INSTR_LINK) continue;
137     xbt_dict_foreach(container->children, cursor2, child2_name, child2) {
138       if (child2->kind == INSTR_LINK) continue;
139
140       if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
141           (child2->kind == INSTR_HOST  || child2->kind == INSTR_ROUTER) &&
142           strcmp (child1_name, child2_name) != 0){
143
144         xbt_dynar_t route = NULL;
145         xbt_ex_t e;
146
147         TRY {
148           routing_get_route_and_latency((network_element_t)(child1->net_elm),
149                                         (network_element_t)(child2->net_elm),
150                                         &route, NULL);
151         } CATCH(e) {
152           xbt_ex_free(e);
153         }
154         if (route == NULL) continue;
155
156         if (TRACE_onelink_only()){
157           if (xbt_dynar_length (route) > 1) continue;
158         }
159         container_t previous = child1;
160         int i;
161         for (i = 0; i < xbt_dynar_length(route); i++){
162           link_CM02_t *link = ((link_CM02_t*)xbt_dynar_get_ptr (route, i));
163           char *link_name = (*link)->lmm_resource.generic_resource.name;
164           container_t current = PJ_container_get(link_name);
165           linkContainers(previous, current, filter);
166           previous = current;
167         }
168         linkContainers(previous, child2, filter);
169
170       }else if (child1->kind == INSTR_AS &&
171                 child2->kind == INSTR_AS &&
172                 strcmp(child1_name, child2_name) != 0){
173
174         route_t route = xbt_new0(s_route_t,1);
175         route->link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
176         rc->get_route_and_latency (rc,
177                                   (network_element_t)(child1->net_elm),
178                                   (network_element_t)(child2->net_elm), route,NULL);
179         unsigned int cpt;
180         void *link;
181         container_t previous = PJ_container_get(route->src_gateway->name);
182         xbt_dynar_foreach (route->link_list, cpt, link) {
183           char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
184           container_t current = PJ_container_get(link_name);
185           linkContainers (previous, current, filter);
186           previous = current;
187         }
188         container_t last = PJ_container_get(route->dst_gateway->name);
189         linkContainers (previous, last, filter);
190         generic_free_route(route);
191       }
192     }
193   }
194 }
195
196 /*
197  * Callbacks
198  */
199 static void instr_routing_parse_start_AS (const char*id,const char*routing)
200 {
201   if (PJ_container_get_root() == NULL){
202     PJ_container_alloc ();
203     PJ_type_alloc();
204     container_t root = PJ_container_new (id, INSTR_AS, NULL);
205     PJ_container_set_root (root);
206
207     if (TRACE_smpi_is_enabled()) {
208       if (!TRACE_smpi_is_grouped()){
209         type_t mpi = PJ_type_get_or_null ("MPI", root->type);
210         if (mpi == NULL){
211           mpi = PJ_type_container_new("MPI", root->type);
212           PJ_type_state_new ("MPI_STATE", mpi);
213           PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
214         }
215       }
216     }
217
218     if (TRACE_needs_platform()){
219       currentContainer = xbt_dynar_new (sizeof(container_t), NULL);
220       xbt_dynar_push (currentContainer, &root);
221     }
222     return;
223   }
224
225   if (TRACE_needs_platform()){
226     container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
227     container_t new = PJ_container_new (id, INSTR_AS, father);
228     xbt_dynar_push (currentContainer, &new);
229   }
230 }
231
232 static void instr_routing_parse_end_AS ()
233 {
234   if (TRACE_needs_platform()){
235     xbt_dynar_pop_ptr (currentContainer);
236   }
237 }
238
239 static void instr_routing_parse_start_link (sg_platf_link_cbarg_t link)
240 {
241   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
242
243   double bandwidth_value = link->bandwidth;
244   double latency_value = link->latency;
245   xbt_dynar_t links_to_create = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
246
247   if (link->policy == SURF_LINK_FULLDUPLEX){
248     char *up = bprintf("%s_UP", link->id);
249     char *down = bprintf("%s_DOWN", link->id);
250     xbt_dynar_push_as (links_to_create, char*, xbt_strdup(up));
251     xbt_dynar_push_as (links_to_create, char*, xbt_strdup(down));
252     free (up);
253     free (down);
254   }else{
255     xbt_dynar_push_as (links_to_create, char*, strdup(link->id));
256   }
257
258   char *link_name = NULL;
259   unsigned int i;
260   xbt_dynar_foreach (links_to_create, i, link_name){
261
262     container_t new = PJ_container_new (link_name, INSTR_LINK, father);
263
264     if (TRACE_categorized() || TRACE_uncategorized()){
265       type_t bandwidth = PJ_type_get_or_null ("bandwidth", new->type);
266       if (bandwidth == NULL){
267         bandwidth = PJ_type_variable_new ("bandwidth", NULL, new->type);
268       }
269       type_t latency = PJ_type_get_or_null ("latency", new->type);
270       if (latency == NULL){
271         latency = PJ_type_variable_new ("latency", NULL, new->type);
272       }
273       new_pajeSetVariable (0, new, bandwidth, bandwidth_value);
274       new_pajeSetVariable (0, new, latency, latency_value);
275     }
276     if (TRACE_uncategorized()){
277       type_t bandwidth_used = PJ_type_get_or_null ("bandwidth_used", new->type);
278       if (bandwidth_used == NULL){
279         bandwidth_used = PJ_type_variable_new ("bandwidth_used", "0.5 0.5 0.5", new->type);
280       }
281     }
282   }
283
284   xbt_dynar_free (&links_to_create);
285 }
286
287 static void instr_routing_parse_start_host (sg_platf_host_cbarg_t host)
288 {
289   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
290   container_t new = PJ_container_new (host->id, INSTR_HOST, father);
291
292   if (TRACE_categorized() || TRACE_uncategorized()) {
293     type_t power = PJ_type_get_or_null ("power", new->type);
294     if (power == NULL){
295       power = PJ_type_variable_new ("power", NULL, new->type);
296     }
297     new_pajeSetVariable (0, new, power, host->power_peak);
298   }
299   if (TRACE_uncategorized()){
300     type_t power_used = PJ_type_get_or_null ("power_used", new->type);
301     if (power_used == NULL){
302       power_used = PJ_type_variable_new ("power_used", "0.5 0.5 0.5", new->type);
303     }
304   }
305
306   if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()){
307     type_t mpi = PJ_type_get_or_null ("MPI", new->type);
308     if (mpi == NULL){
309       mpi = PJ_type_container_new("MPI", new->type);
310       PJ_type_state_new ("MPI_STATE", mpi);
311       PJ_type_link_new ("MPI_LINK", PJ_type_get_root(), mpi, mpi);
312     }
313   }
314
315   if (TRACE_msg_process_is_enabled()) {
316     type_t msg_process = PJ_type_get_or_null ("MSG_PROCESS", new->type);
317     if (msg_process == NULL){
318       msg_process = PJ_type_container_new("MSG_PROCESS", new->type);
319       type_t state = PJ_type_state_new ("MSG_PROCESS_STATE", msg_process);
320       PJ_value_new ("executing", "0 1 0", state);
321       PJ_value_new ("suspend", "1 0 1", state);
322       PJ_value_new ("sleep", "1 1 0", state);
323       PJ_value_new ("receive", "1 0 0", state);
324       PJ_value_new ("send", "0 0 1", state);
325       PJ_value_new ("task_execute", "0 1 1", state);
326       PJ_type_link_new ("MSG_PROCESS_LINK", PJ_type_get_root(), msg_process, msg_process);
327       PJ_type_link_new ("MSG_PROCESS_TASK_LINK", PJ_type_get_root(), msg_process, msg_process);
328     }
329   }
330 }
331
332 static void instr_routing_parse_start_router (sg_platf_router_cbarg_t router)
333 {
334   container_t father = *(container_t*)xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
335   PJ_container_new (router->id, INSTR_ROUTER, father);
336 }
337
338 static void instr_routing_parse_end_platform ()
339 {
340   xbt_dynar_free(&currentContainer);
341   currentContainer = NULL;
342   xbt_dict_t filter = xbt_dict_new_homogeneous(xbt_free);
343   recursiveGraphExtraction (global_routing->root, PJ_container_get_root(), filter);
344   xbt_dict_free(&filter);
345   platform_created = 1;
346   TRACE_paje_dump_buffer(1);
347 }
348
349 void instr_routing_define_callbacks ()
350 {
351   if (!TRACE_is_enabled()) return;
352   //always need the call backs to ASes (we need only the root AS),
353   //to create the rootContainer and the rootType properly
354   sg_platf_AS_begin_add_cb(instr_routing_parse_start_AS);
355   sg_platf_AS_end_add_cb(instr_routing_parse_end_AS);
356   if (!TRACE_needs_platform()) return;
357   sg_platf_link_add_cb(instr_routing_parse_start_link);
358   sg_platf_host_add_cb(instr_routing_parse_start_host);
359   sg_platf_router_add_cb(instr_routing_parse_start_router);
360
361   sg_platf_postparse_add_cb(instr_routing_parse_end_platform);
362 }
363
364 /*
365  * user categories support
366  */
367 static void recursiveNewVariableType (const char *new_typename, const char *color, type_t root)
368 {
369   if (!strcmp (root->name, "HOST")){
370     char tnstr[INSTR_DEFAULT_STR_SIZE];
371     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "p%s", new_typename);
372     PJ_type_variable_new (tnstr, color, root);
373   }
374   if (!strcmp (root->name, "LINK")){
375     char tnstr[INSTR_DEFAULT_STR_SIZE];
376     snprintf (tnstr, INSTR_DEFAULT_STR_SIZE, "b%s", new_typename);
377     PJ_type_variable_new (tnstr, color, root);
378   }
379   xbt_dict_cursor_t cursor = NULL;
380   type_t child_type;
381   char *name;
382   xbt_dict_foreach(root->children, cursor, name, child_type) {
383     recursiveNewVariableType (new_typename, color, child_type);
384   }
385 }
386
387 void instr_new_variable_type (const char *new_typename, const char *color)
388 {
389   recursiveNewVariableType (new_typename, color, PJ_type_get_root());
390 }
391
392 static void recursiveNewUserVariableType (const char *father_type, const char *new_typename, const char *color, type_t root)
393 {
394   if (!strcmp (root->name, father_type)){
395     PJ_type_variable_new (new_typename, color, root);
396   }
397   xbt_dict_cursor_t cursor = NULL;
398   type_t child_type;
399   char *name;
400   xbt_dict_foreach(root->children, cursor, name, child_type) {
401     recursiveNewUserVariableType (father_type, new_typename, color, child_type);
402   }
403 }
404
405 void instr_new_user_variable_type  (const char *father_type, const char *new_typename, const char *color)
406 {
407   recursiveNewUserVariableType (father_type, new_typename, color, PJ_type_get_root());
408 }
409
410
411
412 int instr_platform_traced ()
413 {
414   return platform_created;
415 }
416
417 #define GRAPHICATOR_SUPPORT_FUNCTIONS
418
419
420 static xbt_node_t new_xbt_graph_node (xbt_graph_t graph, const char *name, xbt_dict_t nodes)
421 {
422   xbt_node_t ret = xbt_dict_get_or_null (nodes, name);
423   if (ret) return ret;
424
425   ret = xbt_graph_new_node (graph, xbt_strdup(name));
426   xbt_dict_set (nodes, name, ret, NULL);
427   return ret;
428 }
429
430 static xbt_edge_t new_xbt_graph_edge (xbt_graph_t graph, xbt_node_t s, xbt_node_t d, xbt_dict_t edges)
431 {
432   xbt_edge_t ret;
433
434   const char *sn = instr_node_name (s);
435   const char *dn = instr_node_name (d);
436   int len = strlen(sn)+strlen(dn)+1;
437   char *name = (char*)malloc(len * sizeof(char));
438
439
440   snprintf (name, len, "%s%s", sn, dn);
441   ret = xbt_dict_get_or_null (edges, name);
442   if (ret == NULL){
443     snprintf (name, len, "%s%s", dn, sn);
444     ret = xbt_dict_get_or_null (edges, name);
445   }
446
447   if (ret == NULL){
448     ret = xbt_graph_new_edge(graph, s, d, NULL);
449     xbt_dict_set (edges, name, ret, NULL);
450   }
451   free (name);
452   return ret;
453 }
454
455 static void recursiveXBTGraphExtraction (xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges,
456     AS_t rc, container_t container)
457 {
458   if (!xbt_dict_is_empty(rc->routing_sons)){
459     xbt_dict_cursor_t cursor = NULL;
460     AS_t rc_son;
461     char *child_name;
462     //bottom-up recursion
463     xbt_dict_foreach(rc->routing_sons, cursor, child_name, rc_son) {
464       container_t child_container = xbt_dict_get (container->children, rc_son->name);
465       recursiveXBTGraphExtraction (graph, nodes, edges, rc_son, child_container);
466     }
467   }
468
469   //let's get routes
470   xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
471   container_t child1, child2;
472   const char *child1_name, *child2_name;
473   xbt_dict_foreach(container->children, cursor1, child1_name, child1) {
474     if (child1->kind == INSTR_LINK) continue;
475     xbt_dict_foreach(container->children, cursor2, child2_name, child2) {
476       if (child2->kind == INSTR_LINK) continue;
477
478       if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
479           (child2->kind == INSTR_HOST  || child2->kind == INSTR_ROUTER) &&
480           strcmp (child1_name, child2_name) != 0){
481
482         // FIXME factorize route creation with else branch below (once possible)
483         xbt_dynar_t route=NULL;
484         routing_get_route_and_latency ((network_element_t)(child1->net_elm),
485                                        (network_element_t)(child2->net_elm),
486                                        &route,NULL);
487         if (TRACE_onelink_only()){
488           if (xbt_dynar_length (route) > 1) continue;
489         }
490         unsigned int cpt;
491         void *link;
492         xbt_node_t current, previous = new_xbt_graph_node(graph, child1_name, nodes);
493         xbt_dynar_foreach (route, cpt, link) {
494           char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
495           current = new_xbt_graph_node(graph, link_name, nodes);
496           new_xbt_graph_edge (graph, previous, current, edges);
497           //previous -> current
498           previous = current;
499         }
500         current = new_xbt_graph_node(graph, child2_name, nodes);
501         new_xbt_graph_edge (graph, previous, current, edges);
502
503       }else if (child1->kind == INSTR_AS &&
504                 child2->kind == INSTR_AS &&
505                 strcmp(child1_name, child2_name) != 0){
506
507         route_t route = xbt_new0(s_route_t,1);
508         route->link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
509         rc->get_route_and_latency (rc,
510                                    (network_element_t)(child1->net_elm),
511                                    (network_element_t)(child2->net_elm),route, NULL);
512         unsigned int cpt;
513         void *link;
514         xbt_node_t current, previous = new_xbt_graph_node(graph, route->src_gateway->name, nodes);
515         xbt_dynar_foreach (route->link_list, cpt, link) {
516           char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
517           current = new_xbt_graph_node(graph, link_name, nodes);
518           new_xbt_graph_edge (graph, previous, current, edges);
519           //previous -> current
520           previous = current;
521         }
522         current = new_xbt_graph_node(graph, route->dst_gateway->name, nodes);
523         new_xbt_graph_edge (graph, previous, current, edges);
524         generic_free_route(route);
525       }
526     }
527   }
528
529 }
530
531 xbt_graph_t instr_routing_platform_graph (void)
532 {
533   xbt_graph_t ret = xbt_graph_new_graph (0, NULL);
534   xbt_dict_t nodes = xbt_dict_new_homogeneous(NULL);
535   xbt_dict_t edges = xbt_dict_new_homogeneous(NULL);
536   recursiveXBTGraphExtraction (ret, nodes, edges, global_routing->root, PJ_container_get_root());
537   xbt_dict_free (&nodes);
538   xbt_dict_free (&edges);
539   return ret;
540 }
541
542 void instr_routing_platform_graph_export_graphviz (xbt_graph_t g, const char *filename)
543 {
544   unsigned int cursor = 0;
545   xbt_node_t node = NULL;
546   xbt_edge_t edge = NULL;
547   FILE *file = NULL;
548
549   file = fopen(filename, "w");
550   xbt_assert(file, "Failed to open %s \n", filename);
551
552   if (g->directed)
553     fprintf(file, "digraph test {\n");
554   else
555     fprintf(file, "graph test {\n");
556
557   fprintf(file, "  graph [overlap=scale]\n");
558
559   fprintf(file, "  node [shape=box, style=filled]\n");
560   fprintf(file,
561           "  node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
562
563   xbt_dynar_foreach(g->nodes, cursor, node) {
564     fprintf(file, "  \"%s\";\n", instr_node_name(node));
565   }
566   xbt_dynar_foreach(g->edges, cursor, edge) {
567     const char *src_s = instr_node_name (edge->src);
568     const char *dst_s = instr_node_name (edge->dst);
569     if (g->directed)
570       fprintf(file, "  \"%s\" -> \"%s\";\n", src_s, dst_s);
571     else
572       fprintf(file, "  \"%s\" -- \"%s\";\n", src_s, dst_s);
573   }
574   fprintf(file, "}\n");
575   fclose(file);
576
577 }
578
579 #endif /* HAVE_TRACING */
580