Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Compile src/instr with g++ so that we can use C++ constructs
[simgrid.git] / teshsuite / simdag / platforms / flatifier.c
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
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 static const char link_ctn_v2[] =  "link:ctn";
27 static const char link_ctn_v3[] = "link_ctn";
28
29 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier,
30                              "Logging specific to this platform parsing tool");
31
32 static int name_compare_hosts(const void *n1, const void *n2)
33 {
34   char name1[80], name2[80];
35   strcpy(name1, SD_workstation_get_name(*((SD_workstation_t *) n1)));
36   strcpy(name2, SD_workstation_get_name(*((SD_workstation_t *) n2)));
37
38   return strcmp(name1, name2);
39 }
40
41 static int name_compare_links(const void *n1, const void *n2)
42 {
43   char name1[80], name2[80];
44   strcpy(name1, SD_link_get_name(*((SD_link_t *) n1)));
45   strcpy(name2, SD_link_get_name(*((SD_link_t *) n2)));
46
47   return strcmp(name1, name2);
48 }
49
50 static int parse_cmdline(int *timings, int *downgrade, char **platformFile, int argc, char **argv)
51 {
52   int wrong_option = 0;
53   int i;
54   for (i = 1; i < argc; i++) {
55     if (strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
56       if (!strcmp(argv[i], "--timings")) {
57         *timings = 1;
58       } else {
59         if (!strcmp(argv[i], "--downgrade")) {
60           *downgrade = 1;
61         } else {
62           wrong_option = 1;
63           break;
64         }
65       }
66     } else {
67       *platformFile = argv[i];
68     }
69   }
70   return wrong_option;
71 }
72
73 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
74 {
75   xbt_ex_t e;
76   TRY {
77     xbt_os_cputimer_start(parse_time);
78     SD_create_environment(platformFile);
79     xbt_os_cputimer_stop(parse_time);
80   }
81   CATCH(e) {
82     xbt_die("Error while loading %s: %s", platformFile, e.msg);
83   }
84 }
85
86 int main(int argc, char **argv)
87 {
88   char *platformFile = NULL;
89   int totalHosts, totalLinks;
90   int timings=0;
91   int downgrade = 0;
92   int version = 3;
93   const char *link_ctn = link_ctn_v3;
94   unsigned int i;
95   xbt_dict_t props = NULL;
96   xbt_dict_cursor_t cursor = NULL;
97   xbt_lib_cursor_t cursor_src = NULL;
98   xbt_lib_cursor_t cursor_dst = NULL;
99   char *src,*dst,*key,*data;
100   sg_netcard_t value1;
101   sg_netcard_t value2;
102
103   const SD_workstation_t *hosts;
104   const SD_link_t *links;
105   xbt_os_timer_t parse_time = xbt_os_timer_new();
106
107 #ifdef _XBT_WIN32
108   setbuf(stderr, NULL);
109   setbuf(stdout, NULL);
110 #else
111   setvbuf(stdout, NULL, _IOLBF, 0);
112 #endif
113
114   SD_init(&argc, argv);
115
116   if (parse_cmdline(&timings, &downgrade, &platformFile, argc, argv) || !platformFile) {
117     xbt_die("Invalid command line arguments: expected [--timings|--downgrade] platformFile");
118   }
119
120   XBT_DEBUG("%d,%d,%s", timings, downgrade, platformFile);
121
122   if (downgrade) {
123     version = 2;
124     link_ctn = link_ctn_v2;
125   }
126
127   create_environment(parse_time, platformFile);
128
129   if (timings) {
130     XBT_INFO("Parsing time: %fs (%d hosts, %d links)",
131           xbt_os_timer_elapsed(parse_time),SD_workstation_get_number(),SD_link_get_number());
132   } else {
133     printf("<?xml version='1.0'?>\n");
134     printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid.dtd\">\n");
135     printf("<platform version=\"%d\">\n", version);
136     if (!downgrade)
137       printf("<AS id=\"AS0\" routing=\"Full\">\n");
138
139     // Hosts
140     totalHosts = SD_workstation_get_number();
141     hosts = SD_workstation_get_list();
142     qsort((void *) hosts, totalHosts, sizeof(SD_workstation_t),
143         name_compare_hosts);
144
145     for (i = 0; i < totalHosts; i++) {
146       printf("  <host id=\"%s\" power=\"%.0f\"",
147           SD_workstation_get_name(hosts[i]),
148           SD_workstation_get_power(hosts[i]));
149       props = SD_workstation_get_properties(hosts[i]);
150       if (SD_workstation_get_cores(hosts[i])>1) {
151         printf(" core=\"%d\"", SD_workstation_get_cores(hosts[i]));
152       }
153       if (props && !xbt_dict_is_empty(props)) {
154         printf(">\n");
155         xbt_dict_foreach(props, cursor, key, data) {
156           printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
157         }
158         printf("  </host>\n");
159       } else {
160         printf("/>\n");
161       }
162     }
163
164     // Routers
165     xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
166       if(surf_routing_edge_get_rc_type(xbt_lib_get_or_null(as_router_lib, key,
167           ROUTING_ASR_LEVEL)) == SURF_NETWORK_ELEMENT_ROUTER)
168       {
169         printf("  <router id=\"%s\"/>\n",key);
170       }
171     }
172
173     // Links
174     totalLinks = SD_link_get_number();
175     links = SD_link_get_list();
176
177     qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
178
179     for (i = 0; i < totalLinks; i++) {
180       printf("  <link id=\"");
181
182       printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"",
183           SD_link_get_name(links[i]),
184           SD_link_get_current_bandwidth(links[i]),
185           SD_link_get_current_latency(links[i]));
186       if (SD_link_is_shared(links[i])) {
187         printf("/>\n");
188       } else {
189         printf(" sharing_policy=\"FATPIPE\"/>\n");
190       }
191     }
192
193     sg_host_t host1, host2;
194     xbt_dict_foreach(host_list, cursor_src, src, host1) // Routes from host
195     {
196       value1 = sg_host_edge(sg_host_by_name(src));
197       xbt_dict_foreach(host_list, cursor_dst, dst, host2) //to host
198       {
199         printf("  <route src=\"%s\" dst=\"%s\">\n  "
200             ,src
201             ,dst);
202         xbt_dynar_t route=NULL;
203         value2 = sg_host_edge(sg_host_by_name(dst));
204         routing_get_route_and_latency(value1, value2, &route,NULL);
205         for(i=0;i<xbt_dynar_length(route) ;i++)
206         {
207           void *link = xbt_dynar_get_as(route,i,void *);
208
209           char *link_name = xbt_strdup(surf_resource_name(link));
210           printf("<%s id=\"%s\"/>",link_ctn,link_name);
211           free(link_name);
212         }
213         printf("\n  </route>\n");
214       }
215       xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
216       {
217         if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
218           printf("  <route src=\"%s\" dst=\"%s\">\n  "
219               ,src
220               ,dst);
221           xbt_dynar_t route=NULL;
222           value2 = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
223           routing_get_route_and_latency((sg_netcard_t)value1,(sg_netcard_t)value2,&route,NULL);
224           for(i=0;i<xbt_dynar_length(route) ;i++)
225           {
226             void *link = xbt_dynar_get_as(route,i,void *);
227
228             char *link_name = xbt_strdup(surf_resource_name(link));
229             printf("<%s id=\"%s\"/>",link_ctn,link_name);
230             free(link_name);
231           }
232           printf("\n  </route>\n");
233         }
234       }
235     }
236
237     xbt_lib_foreach(as_router_lib, cursor_src, src, value1) // Routes from router
238     {
239       value1 = xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
240       if(routing_get_network_element_type(src) == SURF_NETWORK_ELEMENT_ROUTER){
241         xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
242           {
243           if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
244             printf("  <route src=\"%s\" dst=\"%s\">\n  "
245                 ,src
246                 ,dst);
247             xbt_dynar_t route=NULL;
248             value2 = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
249             routing_get_route_and_latency((sg_netcard_t)value1,(sg_netcard_t)value2,&route,NULL);
250             for(i=0;i<xbt_dynar_length(route) ;i++)
251             {
252               void *link = xbt_dynar_get_as(route,i,void *);
253
254               char *link_name = xbt_strdup(surf_resource_name(link));
255               printf("<%s id=\"%s\"/>",link_ctn,link_name);
256               free(link_name);
257             }
258             printf("\n  </route>\n");
259           }
260           }
261         xbt_dict_foreach(host_list, cursor_dst, dst, value2) //to host
262         {
263           printf("  <route src=\"%s\" dst=\"%s\">\n  "
264               ,src, dst);
265           xbt_dynar_t route=NULL;
266           value2 = sg_host_edge(sg_host_by_name(dst));
267           routing_get_route_and_latency((sg_netcard_t)value1,(sg_netcard_t)value2,&route, NULL);
268           for(i=0;i<xbt_dynar_length(route) ;i++)
269           {
270             void *link = xbt_dynar_get_as(route,i,void *);
271
272             char *link_name = xbt_strdup(surf_resource_name(link));
273             printf("<%s id=\"%s\"/>",link_ctn,link_name);
274             free(link_name);
275           }
276           printf("\n  </route>\n");
277         }
278       }
279     }
280
281     if (!downgrade)
282       printf("</AS>\n");
283     printf("</platform>\n");
284   }
285   SD_exit();
286   xbt_os_timer_free(parse_time);
287
288   return 0;
289 }