Logo AND Algorithmique Numérique Distribuée

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