Logo AND Algorithmique Numérique Distribuée

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