Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fec3e126c03f751bf974db8bb18b6a3fcd4ab119
[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 #include "src/internal_config.h"
8 #ifdef HAVE_UNISTD_H
9 #include <unistd.h>
10 #endif
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <time.h>
15 #include <string.h>
16 #include <math.h>
17
18 #include "src/surf/network_interface.hpp"
19 #include "simgrid/simdag.h"
20 #include "xbt/log.h"
21 #include "xbt/dict.h"
22 #include "xbt/ex.h"
23 #include "xbt/xbt_os_time.h"
24 #include "surf/surf.h"
25 #include "src/surf/surf_private.h"
26
27 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
28
29 static int name_compare_hosts(const void *n1, const void *n2)
30 {
31   return strcmp(sg_host_get_name(*(sg_host_t *) n1), sg_host_get_name(*(sg_host_t *) n2));
32 }
33
34 static int name_compare_links(const void *n1, const void *n2)
35 {
36   return strcmp(sg_link_name(*(SD_link_t *) n1),sg_link_name(*(SD_link_t *) n2));
37 }
38
39 static int parse_cmdline(int *timings, char **platformFile, int argc, char **argv)
40 {
41   int wrong_option = 0;
42   int i;
43   for (i = 1; i < argc; i++) {
44     if (strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
45       if (!strcmp(argv[i], "--timings")) {
46         *timings = 1;
47       } else {
48           wrong_option = 1;
49           break;
50       }
51     } else {
52       *platformFile = argv[i];
53     }
54   }
55   return wrong_option;
56 }
57
58 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
59 {
60   xbt_ex_t e;
61   TRY {
62     xbt_os_cputimer_start(parse_time);
63     SD_create_environment(platformFile);
64     xbt_os_cputimer_stop(parse_time);
65   }
66   CATCH(e) {
67     xbt_die("Error while loading %s: %s", platformFile, e.msg);
68   }
69 }
70
71 int main(int argc, char **argv)
72 {
73   char *platformFile = NULL;
74   unsigned int totalHosts, totalLinks;
75   int timings=0;
76   int version = 4;
77   const char *link_ctn = "link_ctn";
78   unsigned int i;
79   xbt_dict_t props = NULL;
80   xbt_dict_cursor_t cursor = NULL;
81   xbt_lib_cursor_t cursor_src = NULL;
82   xbt_lib_cursor_t cursor_dst = NULL;
83   char *src,*dst,*key,*data;
84   sg_netcard_t value1;
85   sg_netcard_t value2;
86
87   const sg_host_t *hosts;
88   const SD_link_t *links;
89   xbt_os_timer_t parse_time = xbt_os_timer_new();
90
91   SD_init(&argc, argv);
92
93   if (parse_cmdline(&timings, &platformFile, argc, argv) || !platformFile) {
94     xbt_die("Invalid command line arguments: expected [--timings] platformFile");
95   }
96
97   XBT_DEBUG("%d,%s", timings, platformFile);
98
99   create_environment(parse_time, platformFile);
100
101   if (timings) {
102     XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time),
103              sg_host_count(), sg_link_count());
104   } else {
105     printf("<?xml version='1.0'?>\n");
106     printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\">\n");
107     printf("<platform version=\"%d\">\n", version);
108     printf("<AS id=\"AS0\" routing=\"Full\">\n");
109
110     // Hosts
111     totalHosts = sg_host_count();
112     hosts = sg_host_list();
113     qsort((void *) hosts, totalHosts, sizeof(sg_host_t), name_compare_hosts);
114
115     for (i = 0; i < totalHosts; i++) {
116       printf("  <host id=\"%s\" speed=\"%.0f\"", sg_host_get_name(hosts[i]), sg_host_speed(hosts[i]));
117       props = sg_host_get_properties(hosts[i]);
118       if (sg_host_core_count(hosts[i])>1) {
119         printf(" core=\"%d\"", sg_host_core_count(hosts[i]));
120       }
121       if (props && !xbt_dict_is_empty(props)) {
122         printf(">\n");
123         xbt_dict_foreach(props, cursor, key, data) {
124           printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
125         }
126         printf("  </host>\n");
127       } else {
128         printf("/>\n");
129       }
130     }
131
132     // Routers
133     xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
134       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, key, ROUTING_ASR_LEVEL);
135       if(value1->isRouter()) {
136         printf("  <router id=\"%s\"/>\n",key);
137       }
138     }
139
140     // Links
141     totalLinks = sg_link_count();
142     links = sg_link_list();
143
144     qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
145
146     for (i = 0; i < totalLinks; i++) {
147       printf("  <link id=\"");
148
149       printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", sg_link_name(links[i]),
150              sg_link_bandwidth(links[i]), sg_link_latency(links[i]));
151       if (sg_link_is_shared(links[i])) {
152         printf("/>\n");
153       } else {
154         printf(" sharing_policy=\"FATPIPE\"/>\n");
155       }
156     }
157
158     sg_host_t host1, host2;
159     xbt_dict_foreach(host_list, cursor_src, src, host1){ // Routes from host
160       value1 = sg_host_by_name(src)->pimpl_netcard;
161       xbt_dict_foreach(host_list, cursor_dst, dst, host2){ //to host
162         std::vector<Link*> *route = new std::vector<Link*>();
163         value2 = sg_host_by_name(dst)->pimpl_netcard;
164         routing_platf->getRouteAndLatency(value1, value2, route,NULL);
165         if (! route->empty()){
166           printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
167           for (auto link: *route)
168             printf("<%s id=\"%s\"/>",link_ctn,link->getName());
169           printf("\n  </route>\n");
170         }
171         delete route;
172       }
173       xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2){ //to router
174         value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
175         if(value2->isRouter()){
176           printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
177           std::vector<Link*> *route = new std::vector<Link*>();
178           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route,NULL);
179           for (auto link : *route)
180             printf("<%s id=\"%s\"/>",link_ctn,link->getName());
181           delete route;
182           printf("\n  </route>\n");
183         }
184       }
185     }
186
187     xbt_lib_foreach(as_router_lib, cursor_src, src, value1){ // Routes from router
188       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
189       if (value1->isRouter()){
190         xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2){ //to router
191           value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
192           if(value2->isRouter()){
193             printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
194             std::vector<Link*> *route = new std::vector<Link*>();
195             routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route,NULL);
196             for(auto link :*route)
197               printf("<%s id=\"%s\"/>",link_ctn,link->getName());
198             delete route;
199             printf("\n  </route>\n");
200           }
201         }
202         xbt_dict_foreach(host_list, cursor_dst, dst, value2){ //to host
203           printf("  <route src=\"%s\" dst=\"%s\">\n  ",src, dst);
204           std::vector<Link*> *route = new std::vector<Link*>();
205           value2 = sg_host_by_name(dst)->pimpl_netcard;
206           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route, NULL);
207           for(auto link : *route)
208             printf("<%s id=\"%s\"/>",link_ctn,link->getName());
209           delete route;
210           printf("\n  </route>\n");
211         }
212       }
213     }
214
215     printf("</AS>\n");
216     printf("</platform>\n");
217   }
218   SD_exit();
219   xbt_os_timer_free(parse_time);
220
221   return 0;
222 }