Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
We need cmake 2.8 to compile simgrid
[simgrid.git] / tools / graphicator / graphicator.c
1 /* Copyright (c) 2008, 2009, 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 #ifndef _XBT_WIN32
8 #include <unistd.h>
9 #endif
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <time.h>
14 #include <string.h>
15 #include <math.h>
16
17
18 #include "simdag/simdag.h"
19 #include "xbt/log.h"
20 #include "xbt/dict.h"
21 #include "xbt/ex.h"
22 #include "xbt/graph.h"
23 #include "surf/surf.h"
24 #include "surf/surf_private.h"
25
26 XBT_LOG_NEW_DEFAULT_CATEGORY(graphicator,
27                              "Graphicator Logging System");
28
29 static int name_compare_links(const void *n1, const void *n2)
30 {
31   char name1[80], name2[80];
32   strcpy(name1, SD_link_get_name(*((SD_link_t *) n1)));
33   strcpy(name2, SD_link_get_name(*((SD_link_t *) n2)));
34
35   return strcmp(name1, name2);
36 }
37
38 static const char *node_name(xbt_node_t n)
39 {
40   return xbt_graph_node_get_data(n);
41 }
42
43 static const char *edge_name(xbt_edge_t n)
44 {
45   return xbt_graph_edge_get_data(n);
46 }
47
48 static xbt_node_t xbt_graph_search_node (xbt_graph_t graph, void *data,  int (*compare_function)(const char *, const char *))
49 {
50   unsigned int cursor = 0;
51   void *tmp = NULL;
52
53   xbt_dynar_t dynar = xbt_graph_get_nodes (graph);
54   xbt_dynar_foreach(dynar, cursor, tmp) {
55     xbt_node_t node = (xbt_node_t)tmp;
56     if (!compare_function (data, xbt_graph_node_get_data (node))) return node;
57   }
58   return NULL;
59 }
60
61 static xbt_edge_t xbt_graph_search_edge (xbt_graph_t graph, xbt_node_t n1, xbt_node_t n2)
62 {
63   unsigned int cursor = 0;
64   void *tmp = NULL;
65   xbt_dynar_t dynar = xbt_graph_get_edges (graph);
66   xbt_dynar_foreach(dynar, cursor, tmp) {
67     xbt_edge_t edge = (xbt_edge_t)tmp;
68     if (( xbt_graph_edge_get_source(edge) == n1 &&
69           xbt_graph_edge_get_target(edge) == n2) ||
70         ( xbt_graph_edge_get_source(edge) == n2 &&
71           xbt_graph_edge_get_target(edge) == n1)){
72       return edge;
73     }
74   }
75   return NULL;
76 }
77
78 int main(int argc, char **argv)
79 {
80   char *platformFile = NULL;
81   char *graphvizFile = NULL;
82
83   unsigned int i;
84   xbt_dict_cursor_t cursor_src = NULL;
85   xbt_dict_cursor_t cursor_dst = NULL;
86   char *src;
87   char *dst;
88   char *data;
89   xbt_ex_t e;
90
91   SD_init(&argc, argv);
92
93   if (argc < 3){
94     INFO1("Usage: %s <platform_file.xml> <graphviz_file.dot>", argv[0]);
95     return 1;
96   }
97   platformFile = argv[1];
98   graphvizFile = argv[2];
99
100   TRY {
101     SD_create_environment(platformFile);
102   } CATCH(e) {
103     xbt_die(bprintf("Error while loading %s: %s",platformFile,e.msg));     
104   }
105
106   //creating the graph structure
107   xbt_graph_t graph = xbt_graph_new_graph (0, NULL);
108
109   //adding hosts
110   xbt_dict_foreach(global_routing->where_network_elements, cursor_src, src, data) {
111     xbt_graph_new_node (graph, xbt_strdup(src));
112   }
113
114   //adding links
115   int totalLinks = SD_link_get_number();
116   const SD_link_t *links = SD_link_get_list();
117   qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
118   for (i = 0; i < totalLinks; i++) {
119     xbt_graph_new_node (graph, xbt_strdup (SD_link_get_name(links[i])));
120   }
121
122   xbt_dict_foreach(global_routing->where_network_elements, cursor_src, src, data) {
123     xbt_dict_foreach(global_routing->where_network_elements, cursor_dst, dst, data) {
124       if (strcmp(src,"loopback")==0 || strcmp(dst,"loopback")==0) continue;
125
126       xbt_node_t src_node = xbt_graph_search_node (graph, src, strcmp);
127       xbt_node_t dst_node = xbt_graph_search_node (graph, dst, strcmp);
128       TRY {
129         xbt_dynar_t route = global_routing->get_route(src,dst);
130         xbt_node_t previous = src_node;
131         for(i=0;i<xbt_dynar_length(route) ;i++)
132         {
133           void *link = xbt_dynar_get_as(route,i,void *);
134           char *link_name = bprintf("%s",((surf_resource_t) link)->name);
135           if (strcmp(link_name, "loopback")==0 || strcmp(link_name, "__loopback__")==0) continue;
136           xbt_node_t link_node = xbt_graph_search_node (graph, link_name, strcmp);
137           if (!link_node){
138             link_node = xbt_graph_new_node (graph, strdup(link_name));
139           }
140           xbt_edge_t edge = xbt_graph_search_edge (graph, previous, link_node);
141           if (!edge){
142             DEBUG2("\%s %s", (char*)xbt_graph_node_get_data(previous), (char*)xbt_graph_node_get_data(link_node));
143             xbt_graph_new_edge (graph, previous, link_node, NULL);
144           }
145           previous = link_node;
146         }
147         xbt_edge_t edge = xbt_graph_search_edge (graph, previous, dst_node);
148         if (!edge){
149           DEBUG2("\%s %s", (char*)xbt_graph_node_get_data(previous), (char*)xbt_graph_node_get_data(dst_node));
150           xbt_graph_new_edge (graph, previous, dst_node, NULL);
151         }
152       } CATCH(e) {}
153     }
154   }
155   xbt_graph_export_graphviz (graph, graphvizFile, node_name, edge_name);
156   xbt_graph_free_graph (graph, NULL, NULL, NULL);
157   SD_exit();
158
159   return 0;
160 }