Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[route parsing] rename network_element_t into sg_routing_edge_t
[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 "surf/surf.h"
23 #include "surf/surf_private.h"
24
25 XBT_LOG_NEW_DEFAULT_CATEGORY(validator,
26                              "Logging specific to this SimDag example");
27
28 static int name_compare_hosts(const void *n1, const void *n2)
29 {
30   char name1[80], name2[80];
31   strcpy(name1, SD_workstation_get_name(*((SD_workstation_t *) n1)));
32   strcpy(name2, SD_workstation_get_name(*((SD_workstation_t *) n2)));
33
34   return strcmp(name1, name2);
35 }
36
37 static int name_compare_links(const void *n1, const void *n2)
38 {
39   char name1[80], name2[80];
40   strcpy(name1, SD_link_get_name(*((SD_link_t *) n1)));
41   strcpy(name2, SD_link_get_name(*((SD_link_t *) n2)));
42
43   return strcmp(name1, name2);
44 }
45
46 int main(int argc, char **argv)
47 {
48   char *platformFile = NULL;
49   int totalHosts, totalLinks;
50   unsigned int i;
51   xbt_dict_t props = NULL;
52   xbt_dict_cursor_t cursor = NULL;
53   xbt_lib_cursor_t cursor_src = NULL;
54   xbt_lib_cursor_t cursor_dst = NULL;
55   char *src,*dst,*key,*data;
56   sg_routing_edge_t value1;
57   sg_routing_edge_t value2;
58   xbt_ex_t e;
59
60   const SD_workstation_t *hosts;
61   const SD_link_t *links;
62
63   setvbuf(stdout, NULL, _IOLBF, 0);
64
65   SD_init(&argc, argv);
66
67   platformFile = argv[1];
68   XBT_DEBUG("%s", platformFile);
69   TRY {
70     SD_create_environment(platformFile);
71   }
72   CATCH(e) {
73     xbt_die("Error while loading %s: %s",platformFile,e.msg);
74   }
75
76   printf("<?xml version='1.0'?>\n");
77   printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid.dtd\">\n");
78   printf("<platform version=\"3\">\n");
79   printf("<AS id=\"AS0\" routing=\"Full\">\n");
80
81   // Hosts
82   totalHosts = SD_workstation_get_number();
83   hosts = SD_workstation_get_list();
84   qsort((void *) hosts, totalHosts, sizeof(SD_workstation_t),
85         name_compare_hosts);
86
87   for (i = 0; i < totalHosts; i++) {
88     printf("  <host id=\"%s\" power=\"%.0f\"",
89            SD_workstation_get_name(hosts[i]),
90            SD_workstation_get_power(hosts[i]));
91     props = SD_workstation_get_properties(hosts[i]);
92     if (props && !xbt_dict_is_empty(props)) {
93       printf(">\n");
94       xbt_dict_foreach(props, cursor, key, data) {
95         printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
96       }
97       printf("  </host>\n");
98     } else {
99       printf("/>\n");
100     }
101   }
102
103   // Routers
104   xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
105           if(((sg_routing_edge_t)xbt_lib_get_or_null(as_router_lib, key,
106                           ROUTING_ASR_LEVEL))->rc_type == SURF_NETWORK_ELEMENT_ROUTER)
107           {
108                   printf("  <router id=\"%s\"/>\n",key);
109           }
110   }
111
112   // Links
113   totalLinks = SD_link_get_number();
114   links = SD_link_get_list();
115
116   qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
117
118   for (i = 0; i < totalLinks; i++) {
119     printf("  <link id=\"");
120
121     printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"",
122            SD_link_get_name(links[i]),
123            SD_link_get_current_bandwidth(links[i]),
124            SD_link_get_current_latency(links[i]));
125     if (SD_link_get_sharing_policy(links[i]) == SD_LINK_SHARED) {
126       printf("/>\n");
127     } else {
128       printf(" sharing_policy=\"FATPIPE\"/>\n");
129     }
130   }
131
132
133   xbt_lib_foreach(host_lib, cursor_src, src, value1) // Routes from host
134   {
135           value1 = xbt_lib_get_or_null(host_lib,src,ROUTING_HOST_LEVEL);
136                   xbt_lib_foreach(host_lib, cursor_dst, dst, value2) //to host
137                   {
138                                 printf("  <route src=\"%s\" dst=\"%s\">\n  "
139                                           ,src
140                                           ,dst);
141                                 xbt_dynar_t route=NULL;
142                                 value2 = xbt_lib_get_or_null(host_lib,dst,ROUTING_HOST_LEVEL);
143                                 routing_get_route_and_latency(value1,value2,&route,NULL);
144                                 for(i=0;i<xbt_dynar_length(route) ;i++)
145                                 {
146                                         void *link = xbt_dynar_get_as(route,i,void *);
147
148                                         char *link_name = xbt_strdup(((surf_resource_t)link)->name);
149                                         printf("<link_ctn id=\"%s\"/>",link_name);
150                                         free(link_name);
151                                 }
152                                 printf("\n  </route>\n");
153                   }
154                   xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
155                   {
156                             if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
157                                 printf("  <route src=\"%s\" dst=\"%s\">\n  "
158                                           ,src
159                                           ,dst);
160                                 xbt_dynar_t route=NULL;
161                                 value2 = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
162                                 routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route,NULL);
163                                 for(i=0;i<xbt_dynar_length(route) ;i++)
164                                 {
165                                         void *link = xbt_dynar_get_as(route,i,void *);
166
167                                         char *link_name = xbt_strdup(((surf_resource_t)link)->name);
168                                         printf("<link_ctn id=\"%s\"/>",link_name);
169                                         free(link_name);
170                                 }
171                                 printf("\n  </route>\n");
172                             }
173                   }
174   }
175
176   xbt_lib_foreach(as_router_lib, cursor_src, src, value1) // Routes from router
177   {
178       value1 = xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
179           if(routing_get_network_element_type(src) == SURF_NETWORK_ELEMENT_ROUTER){
180                   xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
181                   {
182                                 if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
183                                 printf("  <route src=\"%s\" dst=\"%s\">\n  "
184                                           ,src
185                                           ,dst);
186                                 xbt_dynar_t route=NULL;
187                                 value2 = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
188                                 routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route,NULL);
189                                 for(i=0;i<xbt_dynar_length(route) ;i++)
190                                 {
191                                         void *link = xbt_dynar_get_as(route,i,void *);
192
193                                         char *link_name = xbt_strdup(((surf_resource_t)link)->name);
194                                         printf("<link_ctn id=\"%s\"/>",link_name);
195                                         free(link_name);
196                                 }
197                                 printf("\n  </route>\n");
198                                 }
199                   }
200                   xbt_lib_foreach(host_lib, cursor_dst, dst, value2) //to host
201                   {
202                                 printf("  <route src=\"%s\" dst=\"%s\">\n  "
203                                           ,src, dst);
204                                 xbt_dynar_t route=NULL;
205                                 value2 = xbt_lib_get_or_null(host_lib,dst,ROUTING_HOST_LEVEL);
206                                 routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route, NULL);
207                                 for(i=0;i<xbt_dynar_length(route) ;i++)
208                                 {
209                                         void *link = xbt_dynar_get_as(route,i,void *);
210
211                                         char *link_name = xbt_strdup(((surf_resource_t)link)->name);
212                                         printf("<link_ctn id=\"%s\"/>",link_name);
213                                         free(link_name);
214                                 }
215                                 printf("\n  </route>\n");
216                   }
217           }
218   }
219
220   printf("</AS>\n");
221   printf("</platform>\n");
222   SD_exit();
223
224   return 0;
225 }