Logo AND Algorithmique Numérique Distribuée

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