Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9e5bb84728ea093be2c42214fc92bac3831ee633
[simgrid.git] / teshsuite / simdag / platforms / flatifier.cpp
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, sg_link_name(*((SD_link_t *) n1)));
45   strcpy(name2, sg_link_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   unsigned 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_count(),
132           sg_link_count());
133   } else {
134     printf("<?xml version='1.0'?>\n");
135     printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid.dtd\">\n");
136     printf("<platform version=\"%d\">\n", version);
137     if (!downgrade)
138       printf("<AS id=\"AS0\" routing=\"Full\">\n");
139
140     // Hosts
141     totalHosts = SD_workstation_get_count();
142     hosts = SD_workstation_get_list();
143     qsort((void *) hosts, totalHosts, sizeof(SD_workstation_t),
144         name_compare_hosts);
145
146     for (i = 0; i < totalHosts; i++) {
147       printf("  <host id=\"%s\" power=\"%.0f\"",
148           SD_workstation_get_name(hosts[i]),
149           SD_workstation_get_power(hosts[i]));
150       props = SD_workstation_get_properties(hosts[i]);
151       if (SD_workstation_get_cores(hosts[i])>1) {
152         printf(" core=\"%d\"", SD_workstation_get_cores(hosts[i]));
153       }
154       if (props && !xbt_dict_is_empty(props)) {
155         printf(">\n");
156         xbt_dict_foreach(props, cursor, key, data) {
157           printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
158         }
159         printf("  </host>\n");
160       } else {
161         printf("/>\n");
162       }
163     }
164
165     // Routers
166     xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
167       if(surf_routing_edge_get_rc_type((sg_netcard_t)xbt_lib_get_or_null(as_router_lib, key,
168           ROUTING_ASR_LEVEL)) == SURF_NETWORK_ELEMENT_ROUTER)
169       {
170         printf("  <router id=\"%s\"/>\n",key);
171       }
172     }
173
174     // Links
175     totalLinks = sg_link_count();
176     links = SD_link_get_list();
177
178     qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
179
180     for (i = 0; i < totalLinks; i++) {
181       printf("  <link id=\"");
182
183       printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"",
184           sg_link_name(links[i]),
185           sg_link_bandwidth(links[i]),
186           sg_link_latency(links[i]));
187       if (sg_link_is_shared(links[i])) {
188         printf("/>\n");
189       } else {
190         printf(" sharing_policy=\"FATPIPE\"/>\n");
191       }
192     }
193
194     sg_host_t host1, host2;
195     xbt_dict_foreach(host_list, cursor_src, src, host1) // Routes from host
196     {
197       value1 = sg_host_by_name(src)->pimpl_netcard;
198       xbt_dict_foreach(host_list, cursor_dst, dst, host2) //to host
199       {
200         printf("  <route src=\"%s\" dst=\"%s\">\n  "
201             ,src
202             ,dst);
203         xbt_dynar_t route=NULL;
204         value2 = sg_host_by_name(dst)->pimpl_netcard;
205         routing_get_route_and_latency(value1, value2, &route,NULL);
206         for(i=0;i<xbt_dynar_length(route) ;i++)
207         {
208           void *link = xbt_dynar_get_as(route,i,void *);
209
210           char *link_name = xbt_strdup(surf_resource_name((surf_cpp_resource_t)link));
211           printf("<%s id=\"%s\"/>",link_ctn,link_name);
212           free(link_name);
213         }
214         printf("\n  </route>\n");
215       }
216       xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
217       {
218         if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
219           printf("  <route src=\"%s\" dst=\"%s\">\n  "
220               ,src
221               ,dst);
222           xbt_dynar_t route=NULL;
223           value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
224           routing_get_route_and_latency((sg_netcard_t)value1,(sg_netcard_t)value2,&route,NULL);
225           for(i=0;i<xbt_dynar_length(route) ;i++)
226           {
227             void *link = xbt_dynar_get_as(route,i,void *);
228
229             char *link_name = xbt_strdup(surf_resource_name((surf_cpp_resource_t)link));
230             printf("<%s id=\"%s\"/>",link_ctn,link_name);
231             free(link_name);
232           }
233           printf("\n  </route>\n");
234         }
235       }
236     }
237
238     xbt_lib_foreach(as_router_lib, cursor_src, src, value1) // Routes from router
239     {
240       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
241       if(routing_get_network_element_type(src) == SURF_NETWORK_ELEMENT_ROUTER){
242         xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
243           {
244           if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
245             printf("  <route src=\"%s\" dst=\"%s\">\n  "
246                 ,src
247                 ,dst);
248             xbt_dynar_t route=NULL;
249             value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
250             routing_get_route_and_latency((sg_netcard_t)value1,(sg_netcard_t)value2,&route,NULL);
251             for(i=0;i<xbt_dynar_length(route) ;i++)
252             {
253               void *link = xbt_dynar_get_as(route,i,void *);
254
255               char *link_name = xbt_strdup(surf_resource_name((surf_cpp_resource_t)link));
256               printf("<%s id=\"%s\"/>",link_ctn,link_name);
257               free(link_name);
258             }
259             printf("\n  </route>\n");
260           }
261           }
262         xbt_dict_foreach(host_list, cursor_dst, dst, value2) //to host
263         {
264           printf("  <route src=\"%s\" dst=\"%s\">\n  "
265               ,src, dst);
266           xbt_dynar_t route=NULL;
267           value2 = sg_host_by_name(dst)->pimpl_netcard;
268           routing_get_route_and_latency((sg_netcard_t)value1,(sg_netcard_t)value2,&route, NULL);
269           for(i=0;i<xbt_dynar_length(route) ;i++)
270           {
271             void *link = xbt_dynar_get_as(route,i,void *);
272
273             char *link_name = xbt_strdup(surf_resource_name((surf_cpp_resource_t)link));
274             printf("<%s id=\"%s\"/>",link_ctn,link_name);
275             free(link_name);
276           }
277           printf("\n  </route>\n");
278         }
279       }
280     }
281
282     if (!downgrade)
283       printf("</AS>\n");
284     printf("</platform>\n");
285   }
286   SD_exit();
287   xbt_os_timer_free(parse_time);
288
289   return 0;
290 }