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;
84 xbt_dict_cursor_t cursor_src = NULL;
85 xbt_dict_cursor_t cursor_dst = NULL;
94 INFO1("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(bprintf("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_dict_foreach(global_routing->where_network_elements, cursor_src, src, data) {
111 xbt_graph_new_node (graph, xbt_strdup(src));
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])));
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;
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);
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++)
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);
138 link_node = xbt_graph_new_node (graph, strdup(link_name));
140 xbt_edge_t edge = xbt_graph_search_edge (graph, previous, link_node);
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);
145 previous = link_node;
147 xbt_edge_t edge = xbt_graph_search_edge (graph, previous, dst_node);
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);
155 xbt_graph_export_graphviz (graph, graphvizFile, node_name, edge_name);
156 xbt_graph_free_graph (graph, NULL, NULL, NULL);