Logo AND Algorithmique Numérique Distribuée

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