Logo AND Algorithmique Numérique Distribuée

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