Logo AND Algorithmique Numérique Distribuée

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