Logo AND Algorithmique Numérique Distribuée

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