Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove forgotten debug println
[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 "xbt/xbt_os_time.h"
23 #include "surf/surf.h"
24 #include "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_timer_start(parse_time);
78     SD_create_environment(platformFile);
79     xbt_os_timer_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_routing_edge_t value1;
101   sg_routing_edge_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   setvbuf(stdout, NULL, _IOLBF, 0);
108
109   SD_init(&argc, argv);
110
111   if (parse_cmdline(&timings, &downgrade, &platformFile, argc, argv) || !platformFile) {
112     xbt_die("Invalid command line arguments: expected [--timings|--downgrade] platformFile");
113   }
114  
115   XBT_DEBUG("%d,%d,%s", timings, downgrade, platformFile);
116
117   if (downgrade) {
118     version = 2;
119     link_ctn = link_ctn_v2;
120   }
121
122   create_environment(parse_time, platformFile);
123
124   if (timings) {
125     XBT_INFO("Parsing time: %fs (%d hosts, %d links)",
126           xbt_os_timer_elapsed(parse_time),SD_workstation_get_number(),SD_link_get_number());
127   } else {
128     printf("<?xml version='1.0'?>\n");
129     printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid.dtd\">\n");
130     printf("<platform version=\"%d\">\n", version);
131     if (!downgrade)
132       printf("<AS id=\"AS0\" routing=\"Full\">\n");
133
134     // Hosts
135     totalHosts = SD_workstation_get_number();
136     hosts = SD_workstation_get_list();
137     qsort((void *) hosts, totalHosts, sizeof(SD_workstation_t),
138         name_compare_hosts);
139
140     for (i = 0; i < totalHosts; i++) {
141       printf("  <host id=\"%s\" power=\"%.0f\"",
142           SD_workstation_get_name(hosts[i]),
143           SD_workstation_get_power(hosts[i]));
144       props = SD_workstation_get_properties(hosts[i]);
145       if (props && !xbt_dict_is_empty(props)) {
146         printf(">\n");
147         xbt_dict_foreach(props, cursor, key, data) {
148           printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
149         }
150         printf("  </host>\n");
151       } else {
152         printf("/>\n");
153       }
154     }
155
156     // Routers
157     xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
158       if(((sg_routing_edge_t)xbt_lib_get_or_null(as_router_lib, key,
159           ROUTING_ASR_LEVEL))->rc_type == SURF_NETWORK_ELEMENT_ROUTER)
160       {
161         printf("  <router id=\"%s\"/>\n",key);
162       }
163     }
164
165     // Links
166     totalLinks = SD_link_get_number();
167     links = SD_link_get_list();
168
169     qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
170
171     for (i = 0; i < totalLinks; i++) {
172       printf("  <link id=\"");
173
174       printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"",
175           SD_link_get_name(links[i]),
176           SD_link_get_current_bandwidth(links[i]),
177           SD_link_get_current_latency(links[i]));
178       if (SD_link_get_sharing_policy(links[i]) == SD_LINK_SHARED) {
179         printf("/>\n");
180       } else {
181         printf(" sharing_policy=\"FATPIPE\"/>\n");
182       }
183     }
184
185
186     xbt_lib_foreach(host_lib, cursor_src, src, value1) // Routes from host
187     {
188       value1 = xbt_lib_get_or_null(host_lib,src,ROUTING_HOST_LEVEL);
189       xbt_lib_foreach(host_lib, cursor_dst, dst, value2) //to host
190       {
191         printf("  <route src=\"%s\" dst=\"%s\">\n  "
192             ,src
193             ,dst);
194         xbt_dynar_t route=NULL;
195         value2 = xbt_lib_get_or_null(host_lib,dst,ROUTING_HOST_LEVEL);
196         routing_get_route_and_latency(value1,value2,&route,NULL);
197         for(i=0;i<xbt_dynar_length(route) ;i++)
198         {
199           void *link = xbt_dynar_get_as(route,i,void *);
200
201           char *link_name = xbt_strdup(((surf_resource_t)link)->name);
202           printf("<%s id=\"%s\"/>",link_ctn,link_name);
203           free(link_name);
204         }
205         printf("\n  </route>\n");
206       }
207       xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
208       {
209         if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
210           printf("  <route src=\"%s\" dst=\"%s\">\n  "
211               ,src
212               ,dst);
213           xbt_dynar_t route=NULL;
214           value2 = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
215           routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route,NULL);
216           for(i=0;i<xbt_dynar_length(route) ;i++)
217           {
218             void *link = xbt_dynar_get_as(route,i,void *);
219
220             char *link_name = xbt_strdup(((surf_resource_t)link)->name);
221             printf("<%s id=\"%s\"/>",link_ctn,link_name);
222             free(link_name);
223           }
224           printf("\n  </route>\n");
225         }
226       }
227     }
228
229     xbt_lib_foreach(as_router_lib, cursor_src, src, value1) // Routes from router
230     {
231       value1 = xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
232       if(routing_get_network_element_type(src) == SURF_NETWORK_ELEMENT_ROUTER){
233         xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
234           {
235           if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
236             printf("  <route src=\"%s\" dst=\"%s\">\n  "
237                 ,src
238                 ,dst);
239             xbt_dynar_t route=NULL;
240             value2 = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
241             routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route,NULL);
242             for(i=0;i<xbt_dynar_length(route) ;i++)
243             {
244               void *link = xbt_dynar_get_as(route,i,void *);
245
246               char *link_name = xbt_strdup(((surf_resource_t)link)->name);
247               printf("<%s id=\"%s\"/>",link_ctn,link_name);
248               free(link_name);
249             }
250             printf("\n  </route>\n");
251           }
252           }
253         xbt_lib_foreach(host_lib, cursor_dst, dst, value2) //to host
254         {
255           printf("  <route src=\"%s\" dst=\"%s\">\n  "
256               ,src, dst);
257           xbt_dynar_t route=NULL;
258           value2 = xbt_lib_get_or_null(host_lib,dst,ROUTING_HOST_LEVEL);
259           routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route, NULL);
260           for(i=0;i<xbt_dynar_length(route) ;i++)
261           {
262             void *link = xbt_dynar_get_as(route,i,void *);
263
264             char *link_name = xbt_strdup(((surf_resource_t)link)->name);
265             printf("<%s id=\"%s\"/>",link_ctn,link_name);
266             free(link_name);
267           }
268           printf("\n  </route>\n");
269         }
270       }
271     }
272
273     if (!downgrade)
274       printf("</AS>\n");
275     printf("</platform>\n");
276   }
277   SD_exit();
278
279   return 0;
280 }