Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bf94475198ea55cc388203c1999dc3bf988aa278
[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   try {
44     xbt_os_cputimer_start(parse_time);
45     SD_create_environment(platformFile);
46     xbt_os_cputimer_stop(parse_time);
47   }
48   catch (std::exception& e) {
49     xbt_die("Error while loading %s: %s", platformFile, e.what());
50   }
51 }
52
53 int main(int argc, char **argv)
54 {
55   char *platformFile = nullptr;
56   int timings=0;
57   int version = 4;
58   unsigned int i;
59   xbt_dict_t props = nullptr;
60   xbt_dict_cursor_t cursor = nullptr;
61   xbt_lib_cursor_t cursor_src = nullptr;
62   xbt_lib_cursor_t cursor_dst = nullptr;
63   char *src,*dst,*key,*data;
64   sg_netcard_t value1;
65   sg_netcard_t value2;
66
67   xbt_os_timer_t parse_time = xbt_os_timer_new();
68
69   SD_init(&argc, argv);
70
71   if (parse_cmdline(&timings, &platformFile, argc, argv) || !platformFile) {
72     xbt_die("Invalid command line arguments: expected [--timings] platformFile");
73   }
74
75   XBT_DEBUG("%d,%s", timings, platformFile);
76
77   create_environment(parse_time, platformFile);
78
79   if (timings) {
80     XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time),
81              sg_host_count(), sg_link_count());
82   } else {
83     printf("<?xml version='1.0'?>\n");
84     printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\">\n");
85     printf("<platform version=\"%d\">\n", version);
86     printf("<AS id=\"AS0\" routing=\"Full\">\n");
87
88     // Hosts
89     unsigned int totalHosts = sg_host_count();
90     sg_host_t *hosts = sg_host_list();
91     qsort((void *) hosts, totalHosts, sizeof(sg_host_t), name_compare_hosts);
92
93     for (i = 0; i < totalHosts; i++) {
94       printf("  <host id=\"%s\" speed=\"%.0f\"", sg_host_get_name(hosts[i]), sg_host_speed(hosts[i]));
95       props = sg_host_get_properties(hosts[i]);
96       if (hosts[i]->core_count()>1) {
97         printf(" core=\"%d\"", hosts[i]->core_count());
98       }
99       if (props && !xbt_dict_is_empty(props)) {
100         printf(">\n");
101         xbt_dict_foreach(props, cursor, key, data) {
102           printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
103         }
104         printf("  </host>\n");
105       } else {
106         printf("/>\n");
107       }
108     }
109
110     // Routers
111     xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
112       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, key, ROUTING_ASR_LEVEL);
113       if(value1->isRouter()) {
114         printf("  <router id=\"%s\"/>\n",key);
115       }
116     }
117
118     // Links
119     unsigned int totalLinks = sg_link_count();
120     SD_link_t *links = sg_link_list();
121
122     qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
123
124     for (i = 0; i < totalLinks; i++) {
125       printf("  <link id=\"");
126
127       printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", sg_link_name(links[i]),
128              sg_link_bandwidth(links[i]), sg_link_latency(links[i]));
129       if (sg_link_is_shared(links[i])) {
130         printf("/>\n");
131       } else {
132         printf(" sharing_policy=\"FATPIPE\"/>\n");
133       }
134     }
135
136     sg_host_t host1, host2;
137     xbt_dict_foreach(host_list, cursor_src, src, host1){ // Routes from host
138       value1 = sg_host_by_name(src)->pimpl_netcard;
139       xbt_dict_foreach(host_list, cursor_dst, dst, host2){ //to host
140         std::vector<Link*> *route = new std::vector<Link*>();
141         value2 = sg_host_by_name(dst)->pimpl_netcard;
142         routing_platf->getRouteAndLatency(value1, value2, route,nullptr);
143         if (! route->empty()){
144           printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
145           for (auto link: *route)
146             printf("<link_ctn id=\"%s\"/>",link->getName());
147           printf("\n  </route>\n");
148         }
149         delete route;
150       }
151       xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2){ //to router
152         value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
153         if(value2->isRouter()){
154           printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
155           std::vector<Link*> *route = new std::vector<Link*>();
156           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route,nullptr);
157           for (auto link : *route)
158             printf("<link_ctn id=\"%s\"/>",link->getName());
159           delete route;
160           printf("\n  </route>\n");
161         }
162       }
163     }
164
165     xbt_lib_foreach(as_router_lib, cursor_src, src, value1){ // Routes from router
166       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
167       if (value1->isRouter()){
168         xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2){ //to router
169           value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
170           if(value2->isRouter()){
171             printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
172             std::vector<Link*> *route = new std::vector<Link*>();
173             routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route,nullptr);
174             for(auto link :*route)
175               printf("<link_ctn id=\"%s\"/>",link->getName());
176             delete route;
177             printf("\n  </route>\n");
178           }
179         }
180         xbt_dict_foreach(host_list, cursor_dst, dst, value2){ //to host
181           printf("  <route src=\"%s\" dst=\"%s\">\n  ",src, dst);
182           std::vector<Link*> *route = new std::vector<Link*>();
183           value2 = sg_host_by_name(dst)->pimpl_netcard;
184           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route, nullptr);
185           for(auto link : *route)
186             printf("<link_ctn id=\"%s\"/>",link->getName());
187           delete route;
188           printf("\n  </route>\n");
189         }
190       }
191     }
192
193     printf("</AS>\n");
194     printf("</platform>\n");
195     free(hosts);
196     free(links);
197   }
198
199   SD_exit();
200   xbt_os_timer_free(parse_time);
201
202   return 0;
203 }