Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7285eaecda63b7c3cd7eacd1a5960e8fcaaadb33
[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, tmp_length;
50   int j, k;
51   unsigned int i;
52   xbt_dict_t props = NULL;
53   xbt_dict_cursor_t cursor = NULL;
54   xbt_dict_cursor_t cursor_src = NULL;
55   xbt_dict_cursor_t cursor_dst = NULL;
56   char *src,*dst,*key,*data;
57   xbt_ex_t e;
58
59   const SD_workstation_t *hosts;
60   const SD_link_t *links, *tmp;
61
62   SD_init(&argc, argv);
63
64   platformFile = argv[1];
65   DEBUG1("%s", platformFile);
66   TRY {
67     SD_create_environment(platformFile);
68   } CATCH(e) {
69     xbt_die(bprintf("Error while loading %s: %s",platformFile,e.msg));     
70   }
71
72   printf("<?xml version='1.0'?>\n");
73   printf("<!DOCTYPE platform SYSTEM \"simgrid.dtd\">\n");
74   printf("<platform version=\"3\">\n");
75   printf("<AS id=\"AS0\" routing=\"Full\">\n");
76
77   // Hosts
78   totalHosts = SD_workstation_get_number();
79   hosts = SD_workstation_get_list();
80   qsort((void *) hosts, totalHosts, sizeof(SD_workstation_t),
81         name_compare_hosts);
82
83   for (i = 0; i < totalHosts; i++) {
84     printf("  <host id=\"%s\" power=\"%.0f\"",
85            SD_workstation_get_name(hosts[i]),
86            SD_workstation_get_power(hosts[i]));
87     props = SD_workstation_get_properties(hosts[i]);
88     if (xbt_dict_length(props) > 0) {
89       printf(">\n");
90       xbt_dict_foreach(props, cursor, key, data) {
91         printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
92       }
93       printf("  </host>\n");
94     } else {
95       printf("/>\n");
96     }
97   }
98
99   // Routers
100   xbt_dict_foreach(global_routing->where_network_elements, cursor, key, data) {
101           if(((network_element_info_t)xbt_dict_get(global_routing->where_network_elements, key))->rc_type
102                           == SURF_NETWORK_ELEMENT_ROUTER)
103           {
104                   printf("  <router id=\"%s\"/>\n",key);
105           }
106   }
107
108   // Links
109   totalLinks = SD_link_get_number();
110   links = SD_link_get_list();
111   qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
112
113   for (i = 0; i < totalLinks; i++) {
114     printf("  <link id=\"");
115
116     printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"",
117            SD_link_get_name(links[i]),
118            SD_link_get_current_bandwidth(links[i]),
119            SD_link_get_current_latency(links[i]));
120     if (SD_link_get_sharing_policy(links[i]) == SD_LINK_SHARED) {
121       printf("/>\n");
122     } else {
123       printf(" sharing_policy=\"FATPIPE\"/>\n");
124     }
125   }
126
127   // Routes
128   xbt_dict_foreach(global_routing->where_network_elements, cursor_src, src, data)
129   {
130           if(((network_element_info_t)xbt_dict_get(global_routing->where_network_elements, src))->rc_type
131                           == SURF_NETWORK_ELEMENT_ROUTER ||
132                           ((network_element_info_t)xbt_dict_get(global_routing->where_network_elements, src))->rc_type
133                           == SURF_NETWORK_ELEMENT_HOST)
134           {
135                   xbt_dict_foreach(global_routing->where_network_elements, cursor_dst, dst, data)
136                   {
137                           if(((network_element_info_t)xbt_dict_get(global_routing->where_network_elements, dst))->rc_type
138                                           == SURF_NETWORK_ELEMENT_ROUTER ||
139                                           ((network_element_info_t)xbt_dict_get(global_routing->where_network_elements, dst))->rc_type
140                                           == SURF_NETWORK_ELEMENT_HOST)
141                           {
142                                 printf("  <route src=\"%s\" dst=\"%s\">\n       "
143                                           ,src
144                                           ,dst);
145                                 xbt_dynar_t route = global_routing->get_route(src,dst);
146                                 for(i=0;i<xbt_dynar_length(route) ;i++)
147                                 {
148                                         void *link = xbt_dynar_get_as(route,i,void *);
149
150                                         char *link_name = bprintf("%s",((surf_resource_t) link)->name);
151                                         printf("<link_ctn id=\"%s\"/>",link_name);
152                                 }
153                                 printf("\n  </route>\n");
154
155                           }
156                   }
157           }
158   }
159
160   printf("</AS>\n");
161   printf("</platform>\n");
162   SD_exit();
163
164   return 0;
165 }