Logo AND Algorithmique Numérique Distribuée

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