Logo AND Algorithmique Numérique Distribuée

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