Logo AND Algorithmique Numérique Distribuée

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