Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
581b75580f594b865c978f58b34971d2d4fb1a60
[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 #ifdef _XBT_WIN32
106   setbuf(stderr, NULL);
107   setbuf(stdout, NULL);
108 #else
109   setvbuf(stdout, NULL, _IOLBF, 0);
110 #endif
111
112   SD_init(&argc, argv);
113
114   if (parse_cmdline(&timings, &downgrade, &platformFile, argc, argv) || !platformFile) {
115     xbt_die("Invalid command line arguments: expected [--timings|--downgrade] platformFile");
116   }
117
118   XBT_DEBUG("%d,%d,%s", timings, downgrade, platformFile);
119
120   if (downgrade) {
121     version = 2;
122     link_ctn = link_ctn_v2;
123   }
124
125   create_environment(parse_time, platformFile);
126
127   if (timings) {
128     XBT_INFO("Parsing time: %fs (%zu hosts, %d links)",
129           xbt_os_timer_elapsed(parse_time),sg_host_count(),
130           sg_link_count());
131   } else {
132     printf("<?xml version='1.0'?>\n");
133     printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid.dtd\">\n");
134     printf("<platform version=\"%d\">\n", version);
135     if (!downgrade)
136       printf("<AS id=\"AS0\" routing=\"Full\">\n");
137
138     // Hosts
139     totalHosts = sg_host_count();
140     hosts = sg_host_list();
141     qsort((void *) hosts, totalHosts, sizeof(sg_host_t),
142         name_compare_hosts);
143
144     for (i = 0; i < totalHosts; i++) {
145       printf("  <host id=\"%s\" power=\"%.0f\"",
146           sg_host_get_name(hosts[i]),
147           sg_host_speed(hosts[i]));
148       props = sg_host_get_properties(hosts[i]);
149       if (sg_host_core_count(hosts[i])>1) {
150         printf(" core=\"%d\"", sg_host_core_count(hosts[i]));
151       }
152       if (props && !xbt_dict_is_empty(props)) {
153         printf(">\n");
154         xbt_dict_foreach(props, cursor, key, data) {
155           printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
156         }
157         printf("  </host>\n");
158       } else {
159         printf("/>\n");
160       }
161     }
162
163     // Routers
164     xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
165       if(surf_routing_edge_get_rc_type((sg_netcard_t)xbt_lib_get_or_null(as_router_lib, key,
166           ROUTING_ASR_LEVEL)) == SURF_NETWORK_ELEMENT_ROUTER)
167       {
168         printf("  <router id=\"%s\"/>\n",key);
169       }
170     }
171
172     // Links
173     totalLinks = sg_link_count();
174     links = sg_link_list();
175
176     qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
177
178     for (i = 0; i < totalLinks; i++) {
179       printf("  <link id=\"");
180
181       printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"",
182           sg_link_name(links[i]),
183           sg_link_bandwidth(links[i]),
184           sg_link_latency(links[i]));
185       if (sg_link_is_shared(links[i])) {
186         printf("/>\n");
187       } else {
188         printf(" sharing_policy=\"FATPIPE\"/>\n");
189       }
190     }
191
192     sg_host_t host1, host2;
193     xbt_dict_foreach(host_list, cursor_src, src, host1) // Routes from host
194     {
195       value1 = sg_host_by_name(src)->pimpl_netcard;
196       xbt_dict_foreach(host_list, cursor_dst, dst, host2) //to host
197       {
198         printf("  <route src=\"%s\" dst=\"%s\">\n  "
199             ,src
200             ,dst);
201         xbt_dynar_t route=NULL;
202         value2 = sg_host_by_name(dst)->pimpl_netcard;
203         routing_platf->getRouteAndLatency(value1, value2, &route,NULL);
204         for(i=0;i<xbt_dynar_length(route) ;i++)
205         {
206           void *link = xbt_dynar_get_as(route,i,void *);
207
208           char *link_name = xbt_strdup(surf_resource_name((surf_cpp_resource_t)link));
209           printf("<%s id=\"%s\"/>",link_ctn,link_name);
210           free(link_name);
211         }
212         printf("\n  </route>\n");
213       }
214       xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
215       {
216         if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
217           printf("  <route src=\"%s\" dst=\"%s\">\n  "
218               ,src
219               ,dst);
220           xbt_dynar_t route=NULL;
221           value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
222           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,&route,NULL);
223           for(i=0;i<xbt_dynar_length(route) ;i++)
224           {
225             void *link = xbt_dynar_get_as(route,i,void *);
226
227             char *link_name = xbt_strdup(surf_resource_name((surf_cpp_resource_t)link));
228             printf("<%s id=\"%s\"/>",link_ctn,link_name);
229             free(link_name);
230           }
231           printf("\n  </route>\n");
232         }
233       }
234     }
235
236     xbt_lib_foreach(as_router_lib, cursor_src, src, value1) // Routes from router
237     {
238       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
239       if(routing_get_network_element_type(src) == SURF_NETWORK_ELEMENT_ROUTER){
240         xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
241           {
242           if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
243             printf("  <route src=\"%s\" dst=\"%s\">\n  "
244                 ,src
245                 ,dst);
246             xbt_dynar_t route=NULL;
247             value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
248             routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,&route,NULL);
249             for(i=0;i<xbt_dynar_length(route) ;i++)
250             {
251               void *link = xbt_dynar_get_as(route,i,void *);
252
253               char *link_name = xbt_strdup(surf_resource_name((surf_cpp_resource_t)link));
254               printf("<%s id=\"%s\"/>",link_ctn,link_name);
255               free(link_name);
256             }
257             printf("\n  </route>\n");
258           }
259           }
260         xbt_dict_foreach(host_list, cursor_dst, dst, value2) //to host
261         {
262           printf("  <route src=\"%s\" dst=\"%s\">\n  "
263               ,src, dst);
264           xbt_dynar_t route=NULL;
265           value2 = sg_host_by_name(dst)->pimpl_netcard;
266           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,&route, NULL);
267           for(i=0;i<xbt_dynar_length(route) ;i++)
268           {
269             void *link = xbt_dynar_get_as(route,i,void *);
270
271             char *link_name = xbt_strdup(surf_resource_name((surf_cpp_resource_t)link));
272             printf("<%s id=\"%s\"/>",link_ctn,link_name);
273             free(link_name);
274           }
275           printf("\n  </route>\n");
276         }
277       }
278     }
279
280     if (!downgrade)
281       printf("</AS>\n");
282     printf("</platform>\n");
283   }
284   SD_exit();
285   xbt_os_timer_free(parse_time);
286
287   return 0;
288 }