Logo AND Algorithmique Numérique Distribuée

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