Logo AND Algorithmique Numérique Distribuée

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