Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove where_network_elements and add element in host_lib and as_router_lib
[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   char *src;
85   char *dst;
86   xbt_ex_t e;
87   xbt_lib_cursor_t cursor,cursor_src,cursor_dst;
88   char * key;
89   char **data;
90
91   SD_init(&argc, argv);
92
93   if (argc < 3){
94     XBT_INFO("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("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_lib_foreach(host_lib,cursor,key,data){
111           if(get_network_element_type(key) == SURF_NETWORK_ELEMENT_HOST ||
112                           get_network_element_type(key) == SURF_NETWORK_ELEMENT_ROUTER )
113             xbt_graph_new_node (graph, xbt_strdup(key));
114   }
115
116   //adding links
117   int totalLinks = SD_link_get_number();
118   const SD_link_t *links = SD_link_get_list();
119   qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
120   for (i = 0; i < totalLinks; i++) {
121     xbt_graph_new_node (graph, xbt_strdup (SD_link_get_name(links[i])));
122   }
123
124
125   xbt_lib_foreach(host_lib,cursor_src,src,data){
126           xbt_lib_foreach(host_lib,cursor_dst,dst,data){
127
128       xbt_node_t src_node = xbt_graph_search_node (graph, src, strcmp);
129       xbt_node_t dst_node = xbt_graph_search_node (graph, dst, strcmp);
130       if(get_network_element_type(src) != SURF_NETWORK_ELEMENT_AS &&
131                   get_network_element_type(dst) != SURF_NETWORK_ELEMENT_AS ){
132         xbt_dynar_t route = global_routing->get_route(src,dst);
133         xbt_node_t previous = src_node;
134         for(i=0;i<xbt_dynar_length(route) ;i++)
135         {
136           void *link = xbt_dynar_get_as(route,i,void *);
137           char *link_name = bprintf("%s",((surf_resource_t) link)->name);
138           if (strcmp(link_name, "loopback")==0 || strcmp(link_name, "__loopback__")==0) continue;
139           xbt_node_t link_node = xbt_graph_search_node (graph, link_name, strcmp);
140           if (!link_node){
141             link_node = xbt_graph_new_node (graph, strdup(link_name));
142           }
143           xbt_edge_t edge = xbt_graph_search_edge (graph, previous, link_node);
144           if (!edge){
145             XBT_DEBUG("\%s %s", (char*)xbt_graph_node_get_data(previous), (char*)xbt_graph_node_get_data(link_node));
146             xbt_graph_new_edge (graph, previous, link_node, NULL);
147           }
148           previous = link_node;
149           free(link_name);
150         }
151         xbt_edge_t edge = xbt_graph_search_edge (graph, previous, dst_node);
152         if (!edge){
153           XBT_DEBUG("\%s %s", (char*)xbt_graph_node_get_data(previous), (char*)xbt_graph_node_get_data(dst_node));
154           xbt_graph_new_edge (graph, previous, dst_node, NULL);
155         }
156     }
157    }
158   }
159   xbt_graph_export_graphviz (graph, graphvizFile, node_name, edge_name);
160   xbt_graph_free_graph (graph, NULL, NULL, NULL);
161   SD_exit();
162
163   return 0;
164 }