Logo AND Algorithmique Numérique Distribuée

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