Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill old cruft
[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 #include "src/surf/network_interface.hpp"
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   return strcmp(
35       sg_host_get_name(*(sg_host_t *) n1),
36       sg_host_get_name(*(sg_host_t *) n2)
37   );
38 }
39
40 static int name_compare_links(const void *n1, const void *n2)
41 {
42   return strcmp(
43       sg_link_name(*(SD_link_t *) n1),
44       sg_link_name(*(SD_link_t *) n2)
45   );
46 }
47
48 static int parse_cmdline(int *timings, int *downgrade, char **platformFile, int argc, char **argv)
49 {
50   int wrong_option = 0;
51   int i;
52   for (i = 1; i < argc; i++) {
53     if (strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
54       if (!strcmp(argv[i], "--timings")) {
55         *timings = 1;
56       } else {
57         if (!strcmp(argv[i], "--downgrade")) {
58           *downgrade = 1;
59         } else {
60           wrong_option = 1;
61           break;
62         }
63       }
64     } else {
65       *platformFile = argv[i];
66     }
67   }
68   return wrong_option;
69 }
70
71 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
72 {
73   xbt_ex_t e;
74   TRY {
75     xbt_os_cputimer_start(parse_time);
76     SD_create_environment(platformFile);
77     xbt_os_cputimer_stop(parse_time);
78   }
79   CATCH(e) {
80     xbt_die("Error while loading %s: %s", platformFile, e.msg);
81   }
82 }
83
84 int main(int argc, char **argv)
85 {
86   char *platformFile = NULL;
87   unsigned int totalHosts, totalLinks;
88   int timings=0;
89   int downgrade = 0;
90   int version = 3;
91   const char *link_ctn = link_ctn_v3;
92   unsigned int i;
93   xbt_dict_t props = NULL;
94   xbt_dict_cursor_t cursor = NULL;
95   xbt_lib_cursor_t cursor_src = NULL;
96   xbt_lib_cursor_t cursor_dst = NULL;
97   char *src,*dst,*key,*data;
98   sg_netcard_t value1;
99   sg_netcard_t value2;
100
101   const sg_host_t *hosts;
102   const SD_link_t *links;
103   xbt_os_timer_t parse_time = xbt_os_timer_new();
104
105   SD_init(&argc, argv);
106
107   if (parse_cmdline(&timings, &downgrade, &platformFile, argc, argv) || !platformFile) {
108     xbt_die("Invalid command line arguments: expected [--timings|--downgrade] platformFile");
109   }
110
111   XBT_DEBUG("%d,%d,%s", timings, downgrade, platformFile);
112
113   if (downgrade) {
114     version = 2;
115     link_ctn = link_ctn_v2;
116   }
117
118   create_environment(parse_time, platformFile);
119
120   if (timings) {
121     XBT_INFO("Parsing time: %fs (%zu hosts, %d links)",
122           xbt_os_timer_elapsed(parse_time),sg_host_count(),
123           sg_link_count());
124   } else {
125     printf("<?xml version='1.0'?>\n");
126     printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid.dtd\">\n");
127     printf("<platform version=\"%d\">\n", version);
128     if (!downgrade)
129       printf("<AS id=\"AS0\" routing=\"Full\">\n");
130
131     // Hosts
132     totalHosts = sg_host_count();
133     hosts = sg_host_list();
134     qsort((void *) hosts, totalHosts, sizeof(sg_host_t),
135         name_compare_hosts);
136
137     for (i = 0; i < totalHosts; i++) {
138       printf("  <host id=\"%s\" power=\"%.0f\"",
139           sg_host_get_name(hosts[i]),
140           sg_host_speed(hosts[i]));
141       props = sg_host_get_properties(hosts[i]);
142       if (sg_host_core_count(hosts[i])>1) {
143         printf(" core=\"%d\"", sg_host_core_count(hosts[i]));
144       }
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(surf_routing_edge_get_rc_type((sg_netcard_t)xbt_lib_get_or_null(as_router_lib, key,
159           ROUTING_ASR_LEVEL)) == SURF_NETWORK_ELEMENT_ROUTER)
160       {
161         printf("  <router id=\"%s\"/>\n",key);
162       }
163     }
164
165     // Links
166     totalLinks = sg_link_count();
167     links = sg_link_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           sg_link_name(links[i]),
176           sg_link_bandwidth(links[i]),
177           sg_link_latency(links[i]));
178       if (sg_link_is_shared(links[i])) {
179         printf("/>\n");
180       } else {
181         printf(" sharing_policy=\"FATPIPE\"/>\n");
182       }
183     }
184
185     sg_host_t host1, host2;
186     xbt_dict_foreach(host_list, cursor_src, src, host1) // Routes from host
187     {
188       value1 = sg_host_by_name(src)->pimpl_netcard;
189       xbt_dict_foreach(host_list, cursor_dst, dst, host2) //to host
190       {
191         printf("  <route src=\"%s\" dst=\"%s\">\n  "
192             ,src
193             ,dst);
194         xbt_dynar_t route=NULL;
195         value2 = sg_host_by_name(dst)->pimpl_netcard;
196         routing_platf->getRouteAndLatency(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_name((surf_cpp_resource_t)link));
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 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
215           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_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_name((surf_cpp_resource_t)link));
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 = (sg_netcard_t)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 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
241             routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_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_name((surf_cpp_resource_t)link));
247               printf("<%s id=\"%s\"/>",link_ctn,link_name);
248               free(link_name);
249             }
250             printf("\n  </route>\n");
251           }
252           }
253         xbt_dict_foreach(host_list, 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 = sg_host_by_name(dst)->pimpl_netcard;
259           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_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_name((surf_cpp_resource_t)link));
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   xbt_os_timer_free(parse_time);
279
280   return 0;
281 }