Logo AND Algorithmique Numérique Distribuée

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