Logo AND Algorithmique Numérique Distribuée

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