Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reindent TRY/TRY_CLEANUP/CATCH constructs.
[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   char **value;
57   xbt_ex_t e;
58
59   const SD_workstation_t *hosts;
60   const SD_link_t *links;
61
62   SD_init(&argc, argv);
63
64   platformFile = argv[1];
65   XBT_DEBUG("%s", platformFile);
66   TRY {
67     SD_create_environment(platformFile);
68   }
69   CATCH(e) {
70     xbt_die("Error while loading %s: %s",platformFile,e.msg);
71   }
72
73   printf("<?xml version='1.0'?>\n");
74   printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid.dtd\">\n");
75   printf("<platform version=\"3\">\n");
76   printf("<AS id=\"AS0\" routing=\"Full\">\n");
77
78   // Hosts
79   totalHosts = SD_workstation_get_number();
80   hosts = SD_workstation_get_list();
81   qsort((void *) hosts, totalHosts, sizeof(SD_workstation_t),
82         name_compare_hosts);
83
84   for (i = 0; i < totalHosts; i++) {
85     printf("  <host id=\"%s\" power=\"%.0f\"",
86            SD_workstation_get_name(hosts[i]),
87            SD_workstation_get_power(hosts[i]));
88     props = SD_workstation_get_properties(hosts[i]);
89     if (props && xbt_dict_length(props) > 0) {
90       printf(">\n");
91       xbt_dict_foreach(props, cursor, key, data) {
92         printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
93       }
94       printf("  </host>\n");
95     } else {
96       printf("/>\n");
97     }
98   }
99
100   // Routers
101   xbt_lib_foreach(as_router_lib, cursor_src, key, value) {
102           if(((network_element_info_t)xbt_lib_get_or_null(as_router_lib, key,
103                           ROUTING_ASR_LEVEL))->rc_type == SURF_NETWORK_ELEMENT_ROUTER)
104           {
105                   printf("  <router id=\"%s\"/>\n",key);
106           }
107   }
108
109   // Links
110   totalLinks = SD_link_get_number();
111   links = SD_link_get_list();
112
113   qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
114
115   for (i = 0; i < totalLinks; i++) {
116     printf("  <link id=\"");
117
118     printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"",
119            SD_link_get_name(links[i]),
120            SD_link_get_current_bandwidth(links[i]),
121            SD_link_get_current_latency(links[i]));
122     if (SD_link_get_sharing_policy(links[i]) == SD_LINK_SHARED) {
123       printf("/>\n");
124     } else {
125       printf(" sharing_policy=\"FATPIPE\"/>\n");
126     }
127   }
128
129
130   xbt_lib_foreach(host_lib, cursor_src, src, value) // Routes from host
131   {
132                   xbt_lib_foreach(host_lib, cursor_dst, dst, value) //to host
133                   {
134                                 printf("  <route src=\"%s\" dst=\"%s\">\n       "
135                                           ,src
136                                           ,dst);
137                                 xbt_dynar_t route = global_routing->get_route(src,dst);
138                                 for(i=0;i<xbt_dynar_length(route) ;i++)
139                                 {
140                                         void *link = xbt_dynar_get_as(route,i,void *);
141
142                                         char *link_name = xbt_strdup(((surf_resource_t)link)->name);
143                                         printf("<link_ctn id=\"%s\"/>",link_name);
144                                         free(link_name);
145                                 }
146                                 printf("\n  </route>\n");
147                   }
148                   xbt_lib_foreach(as_router_lib, cursor_dst, dst, value) //to router
149                   {
150                             if(get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
151                                 printf("  <route src=\"%s\" dst=\"%s\">\n       "
152                                           ,src
153                                           ,dst);
154                                 xbt_dynar_t route = global_routing->get_route(src,dst);
155                                 for(i=0;i<xbt_dynar_length(route) ;i++)
156                                 {
157                                         void *link = xbt_dynar_get_as(route,i,void *);
158
159                                         char *link_name = xbt_strdup(((surf_resource_t)link)->name);
160                                         printf("<link_ctn id=\"%s\"/>",link_name);
161                                         free(link_name);
162                                 }
163                                 printf("\n  </route>\n");
164                             }
165                   }
166   }
167
168   xbt_lib_foreach(as_router_lib, cursor_src, src, value) // Routes from router
169   {
170           if(get_network_element_type(src) == SURF_NETWORK_ELEMENT_ROUTER){
171                   xbt_lib_foreach(as_router_lib, cursor_dst, dst, value) //to router
172                   {
173                                 if(get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
174                                 printf("  <route src=\"%s\" dst=\"%s\">\n       "
175                                           ,src
176                                           ,dst);
177                                 xbt_dynar_t route = global_routing->get_route(src,dst);
178                                 for(i=0;i<xbt_dynar_length(route) ;i++)
179                                 {
180                                         void *link = xbt_dynar_get_as(route,i,void *);
181
182                                         char *link_name = xbt_strdup(((surf_resource_t)link)->name);
183                                         printf("<link_ctn id=\"%s\"/>",link_name);
184                                         free(link_name);
185                                 }
186                                 printf("\n  </route>\n");
187                                 }
188                   }
189                   xbt_lib_foreach(host_lib, cursor_dst, dst, value) //to host
190                   {
191                                 printf("  <route src=\"%s\" dst=\"%s\">\n       "
192                                           ,src
193                                           ,dst);
194                                 xbt_dynar_t route = global_routing->get_route(src,dst);
195                                 for(i=0;i<xbt_dynar_length(route) ;i++)
196                                 {
197                                         void *link = xbt_dynar_get_as(route,i,void *);
198
199                                         char *link_name = xbt_strdup(((surf_resource_t)link)->name);
200                                         printf("<link_ctn id=\"%s\"/>",link_name);
201                                         free(link_name);
202                                 }
203                                 printf("\n  </route>\n");
204                   }
205           }
206   }
207
208   printf("</AS>\n");
209   printf("</platform>\n");
210   SD_exit();
211
212   return 0;
213 }