Logo AND Algorithmique Numérique Distribuée

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