1 /* Copyright (c) 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
18 #include "simdag/simdag.h"
22 #include "xbt/graph.h"
23 #include "surf/surf.h"
24 #include "surf/surf_private.h"
26 XBT_LOG_NEW_DEFAULT_CATEGORY(graphicator,
27 "Graphicator Logging System");
29 static int name_compare_links(const void *n1, const void *n2)
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)));
35 return strcmp(name1, name2);
38 static const char *node_name(xbt_node_t n)
40 return xbt_graph_node_get_data(n);
43 static const char *edge_name(xbt_edge_t n)
45 return xbt_graph_edge_get_data(n);
48 static xbt_node_t xbt_graph_search_node (xbt_graph_t graph, void *data, int (*compare_function)(const char *, const char *))
50 unsigned int cursor = 0;
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;
61 static xbt_edge_t xbt_graph_search_edge (xbt_graph_t graph, xbt_node_t n1, xbt_node_t n2)
63 unsigned int cursor = 0;
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)){
78 int main(int argc, char **argv)
80 char *platformFile = NULL;
81 char *graphvizFile = NULL;
87 xbt_lib_cursor_t cursor,cursor_src,cursor_dst;
94 XBT_INFO("Usage: %s <platform_file.xml> <graphviz_file.dot>", argv[0]);
97 platformFile = argv[1];
98 graphvizFile = argv[2];
101 SD_create_environment(platformFile);
103 xbt_die("Error while loading %s: %s",platformFile,e.msg);
106 //creating the graph structure
107 xbt_graph_t graph = xbt_graph_new_graph (0, NULL);
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));
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])));
125 xbt_lib_foreach(host_lib,cursor_src,src,data){
126 xbt_lib_foreach(host_lib,cursor_dst,dst,data){
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++)
136 void *link = xbt_dynar_get_as(route,i,void *);
137 char *link_name = xbt_strdup(((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);
141 link_node = xbt_graph_new_node (graph, strdup(link_name));
143 xbt_edge_t edge = xbt_graph_search_edge (graph, previous, link_node);
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);
148 previous = link_node;
151 xbt_edge_t edge = xbt_graph_search_edge (graph, previous, dst_node);
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);
159 xbt_graph_export_graphviz (graph, graphvizFile, node_name, edge_name);
160 xbt_graph_free_graph (graph, NULL, NULL, NULL);