Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b47590261de957a2b952948d18b818146896eef9
[simgrid.git] / teshsuite / simdag / platforms / flatifier.c
1 /* Copyright (c) 2008-2014. 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_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_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 #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 (props && !xbt_dict_is_empty(props)) {
151         printf(">\n");
152         xbt_dict_foreach(props, cursor, key, data) {
153           printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
154         }
155         printf("  </host>\n");
156       } else {
157         printf("/>\n");
158       }
159     }
160
161     // Routers
162     xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
163       if(surf_routing_edge_get_rc_type(xbt_lib_get_or_null(as_router_lib, key,
164           ROUTING_ASR_LEVEL)) == SURF_NETWORK_ELEMENT_ROUTER)
165       {
166         printf("  <router id=\"%s\"/>\n",key);
167       }
168     }
169
170     // Links
171     totalLinks = SD_link_get_number();
172     links = SD_link_get_list();
173
174     qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
175
176     for (i = 0; i < totalLinks; i++) {
177       printf("  <link id=\"");
178
179       printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"",
180           SD_link_get_name(links[i]),
181           SD_link_get_current_bandwidth(links[i]),
182           SD_link_get_current_latency(links[i]));
183       if (SD_link_get_sharing_policy(links[i]) == SD_LINK_SHARED) {
184         printf("/>\n");
185       } else {
186         printf(" sharing_policy=\"FATPIPE\"/>\n");
187       }
188     }
189
190
191     xbt_lib_foreach(host_lib, cursor_src, src, value1) // Routes from host
192     {
193       value1 = xbt_lib_get_or_null(host_lib,src,ROUTING_HOST_LEVEL);
194       xbt_lib_foreach(host_lib, cursor_dst, dst, value2) //to host
195       {
196         printf("  <route src=\"%s\" dst=\"%s\">\n  "
197             ,src
198             ,dst);
199         xbt_dynar_t route=NULL;
200         value2 = xbt_lib_get_or_null(host_lib,dst,ROUTING_HOST_LEVEL);
201         routing_get_route_and_latency(value1,value2,&route,NULL);
202         for(i=0;i<xbt_dynar_length(route) ;i++)
203         {
204           void *link = xbt_dynar_get_as(route,i,void *);
205
206           char *link_name = xbt_strdup(surf_resource_name(link));
207           printf("<%s id=\"%s\"/>",link_ctn,link_name);
208           free(link_name);
209         }
210         printf("\n  </route>\n");
211       }
212       xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
213       {
214         if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
215           printf("  <route src=\"%s\" dst=\"%s\">\n  "
216               ,src
217               ,dst);
218           xbt_dynar_t route=NULL;
219           value2 = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
220           routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route,NULL);
221           for(i=0;i<xbt_dynar_length(route) ;i++)
222           {
223             void *link = xbt_dynar_get_as(route,i,void *);
224
225             char *link_name = xbt_strdup(surf_resource_name(link));
226             printf("<%s id=\"%s\"/>",link_ctn,link_name);
227             free(link_name);
228           }
229           printf("\n  </route>\n");
230         }
231       }
232     }
233
234     xbt_lib_foreach(as_router_lib, cursor_src, src, value1) // Routes from router
235     {
236       value1 = xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
237       if(routing_get_network_element_type(src) == SURF_NETWORK_ELEMENT_ROUTER){
238         xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
239           {
240           if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
241             printf("  <route src=\"%s\" dst=\"%s\">\n  "
242                 ,src
243                 ,dst);
244             xbt_dynar_t route=NULL;
245             value2 = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
246             routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route,NULL);
247             for(i=0;i<xbt_dynar_length(route) ;i++)
248             {
249               void *link = xbt_dynar_get_as(route,i,void *);
250
251               char *link_name = xbt_strdup(surf_resource_name(link));
252               printf("<%s id=\"%s\"/>",link_ctn,link_name);
253               free(link_name);
254             }
255             printf("\n  </route>\n");
256           }
257           }
258         xbt_lib_foreach(host_lib, cursor_dst, dst, value2) //to host
259         {
260           printf("  <route src=\"%s\" dst=\"%s\">\n  "
261               ,src, dst);
262           xbt_dynar_t route=NULL;
263           value2 = xbt_lib_get_or_null(host_lib,dst,ROUTING_HOST_LEVEL);
264           routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route, NULL);
265           for(i=0;i<xbt_dynar_length(route) ;i++)
266           {
267             void *link = xbt_dynar_get_as(route,i,void *);
268
269             char *link_name = xbt_strdup(surf_resource_name(link));
270             printf("<%s id=\"%s\"/>",link_ctn,link_name);
271             free(link_name);
272           }
273           printf("\n  </route>\n");
274         }
275       }
276     }
277
278     if (!downgrade)
279       printf("</AS>\n");
280     printf("</platform>\n");
281   }
282   SD_exit();
283   xbt_os_timer_free(parse_time);
284
285   return 0;
286 }