Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mv is-router and flatifier to their own dir
[simgrid.git] / teshsuite / simdag / flatifier / flatifier.cpp
1 /* Copyright (c) 2008-2015. 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 "src/surf/network_interface.hpp"
8 #include "simgrid/simdag.h"
9 #include "xbt/xbt_os_time.h"
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
12
13 static int name_compare_hosts(const void *n1, const void *n2)
14 {
15   return strcmp(sg_host_get_name(*(sg_host_t *) n1), sg_host_get_name(*(sg_host_t *) n2));
16 }
17
18 static int name_compare_links(const void *n1, const void *n2)
19 {
20   return strcmp(sg_link_name(*(SD_link_t *) n1),sg_link_name(*(SD_link_t *) n2));
21 }
22
23 static int parse_cmdline(int *timings, char **platformFile, int argc, char **argv)
24 {
25   int wrong_option = 0;
26   for (int i = 1; i < argc; i++) {
27     if (strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
28       if (!strcmp(argv[i], "--timings")) {
29         *timings = 1;
30       } else {
31           wrong_option = 1;
32           break;
33       }
34     } else {
35       *platformFile = argv[i];
36     }
37   }
38   return wrong_option;
39 }
40
41 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
42 {
43   xbt_ex_t e;
44   TRY {
45     xbt_os_cputimer_start(parse_time);
46     SD_create_environment(platformFile);
47     xbt_os_cputimer_stop(parse_time);
48   }
49   CATCH(e) {
50     xbt_die("Error while loading %s: %s", platformFile, e.msg);
51   }
52 }
53
54 int main(int argc, char **argv)
55 {
56   char *platformFile = NULL;
57   unsigned int totalHosts, totalLinks;
58   int timings=0;
59   int version = 4;
60   const char *link_ctn = "link_ctn";
61   unsigned int i;
62   xbt_dict_t props = NULL;
63   xbt_dict_cursor_t cursor = NULL;
64   xbt_lib_cursor_t cursor_src = NULL;
65   xbt_lib_cursor_t cursor_dst = NULL;
66   char *src,*dst,*key,*data;
67   sg_netcard_t value1;
68   sg_netcard_t value2;
69
70   const sg_host_t *hosts;
71   const SD_link_t *links;
72   xbt_os_timer_t parse_time = xbt_os_timer_new();
73
74   SD_init(&argc, argv);
75
76   if (parse_cmdline(&timings, &platformFile, argc, argv) || !platformFile) {
77     xbt_die("Invalid command line arguments: expected [--timings] platformFile");
78   }
79
80   XBT_DEBUG("%d,%s", timings, platformFile);
81
82   create_environment(parse_time, platformFile);
83
84   if (timings) {
85     XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time),
86              sg_host_count(), sg_link_count());
87   } else {
88     printf("<?xml version='1.0'?>\n");
89     printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\">\n");
90     printf("<platform version=\"%d\">\n", version);
91     printf("<AS id=\"AS0\" routing=\"Full\">\n");
92
93     // Hosts
94     totalHosts = sg_host_count();
95     hosts = sg_host_list();
96     qsort((void *) hosts, totalHosts, sizeof(sg_host_t), name_compare_hosts);
97
98     for (i = 0; i < totalHosts; i++) {
99       printf("  <host id=\"%s\" speed=\"%.0f\"", sg_host_get_name(hosts[i]), sg_host_speed(hosts[i]));
100       props = sg_host_get_properties(hosts[i]);
101       if (sg_host_core_count(hosts[i])>1) {
102         printf(" core=\"%d\"", sg_host_core_count(hosts[i]));
103       }
104       if (props && !xbt_dict_is_empty(props)) {
105         printf(">\n");
106         xbt_dict_foreach(props, cursor, key, data) {
107           printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
108         }
109         printf("  </host>\n");
110       } else {
111         printf("/>\n");
112       }
113     }
114
115     // Routers
116     xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
117       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, key, ROUTING_ASR_LEVEL);
118       if(value1->isRouter()) {
119         printf("  <router id=\"%s\"/>\n",key);
120       }
121     }
122
123     // Links
124     totalLinks = sg_link_count();
125     links = sg_link_list();
126
127     qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
128
129     for (i = 0; i < totalLinks; i++) {
130       printf("  <link id=\"");
131
132       printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", sg_link_name(links[i]),
133              sg_link_bandwidth(links[i]), sg_link_latency(links[i]));
134       if (sg_link_is_shared(links[i])) {
135         printf("/>\n");
136       } else {
137         printf(" sharing_policy=\"FATPIPE\"/>\n");
138       }
139     }
140
141     sg_host_t host1, host2;
142     xbt_dict_foreach(host_list, cursor_src, src, host1){ // Routes from host
143       value1 = sg_host_by_name(src)->pimpl_netcard;
144       xbt_dict_foreach(host_list, cursor_dst, dst, host2){ //to host
145         std::vector<Link*> *route = new std::vector<Link*>();
146         value2 = sg_host_by_name(dst)->pimpl_netcard;
147         routing_platf->getRouteAndLatency(value1, value2, route,NULL);
148         if (! route->empty()){
149           printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
150           for (auto link: *route)
151             printf("<%s id=\"%s\"/>",link_ctn,link->getName());
152           printf("\n  </route>\n");
153         }
154         delete route;
155       }
156       xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2){ //to router
157         value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
158         if(value2->isRouter()){
159           printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
160           std::vector<Link*> *route = new std::vector<Link*>();
161           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route,NULL);
162           for (auto link : *route)
163             printf("<%s id=\"%s\"/>",link_ctn,link->getName());
164           delete route;
165           printf("\n  </route>\n");
166         }
167       }
168     }
169
170     xbt_lib_foreach(as_router_lib, cursor_src, src, value1){ // Routes from router
171       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
172       if (value1->isRouter()){
173         xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2){ //to router
174           value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
175           if(value2->isRouter()){
176             printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
177             std::vector<Link*> *route = new std::vector<Link*>();
178             routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route,NULL);
179             for(auto link :*route)
180               printf("<%s id=\"%s\"/>",link_ctn,link->getName());
181             delete route;
182             printf("\n  </route>\n");
183           }
184         }
185         xbt_dict_foreach(host_list, cursor_dst, dst, value2){ //to host
186           printf("  <route src=\"%s\" dst=\"%s\">\n  ",src, dst);
187           std::vector<Link*> *route = new std::vector<Link*>();
188           value2 = sg_host_by_name(dst)->pimpl_netcard;
189           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route, NULL);
190           for(auto link : *route)
191             printf("<%s id=\"%s\"/>",link_ctn,link->getName());
192           delete route;
193           printf("\n  </route>\n");
194         }
195       }
196     }
197
198     printf("</AS>\n");
199     printf("</platform>\n");
200   }
201   SD_exit();
202   xbt_os_timer_free(parse_time);
203
204   return 0;
205 }