Logo AND Algorithmique Numérique Distribuée

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