Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7d1b4999c384c3243e3780b4e291bb213e32dd64
[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/engine.hpp"
18 #include "simgrid/s4u/host.hpp"
19
20 #include <simgrid/simdag.h>
21
22 #include "src/kernel/routing/NetCard.hpp"
23 #include "src/surf/network_interface.hpp"
24
25
26 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
27
28 static int name_compare_hosts(const void *n1, const void *n2)
29 {
30   return std::strcmp(sg_host_get_name(*(sg_host_t *) n1), sg_host_get_name(*(sg_host_t *) n2));
31 }
32
33 static int name_compare_links(const void *n1, const void *n2)
34 {
35   return std::strcmp(sg_link_name(*(SD_link_t *) n1),sg_link_name(*(SD_link_t *) n2));
36 }
37
38 static int parse_cmdline(int *timings, char **platformFile, int argc, char **argv)
39 {
40   int wrong_option = 0;
41   for (int i = 1; i < argc; i++) {
42     if (std::strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
43       if (!std::strcmp(argv[i], "--timings")) {
44         *timings = 1;
45       } else {
46           wrong_option = 1;
47           break;
48       }
49     } else {
50       *platformFile = argv[i];
51     }
52   }
53   return wrong_option;
54 }
55
56 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
57 {
58   try {
59     xbt_os_cputimer_start(parse_time);
60     SD_create_environment(platformFile);
61     xbt_os_cputimer_stop(parse_time);
62   }
63   catch (std::exception& e) {
64     xbt_die("Error while loading %s: %s", platformFile, e.what());
65   }
66 }
67
68 int main(int argc, char **argv)
69 {
70   char *platformFile = nullptr;
71   int timings=0;
72   int version = 4;
73   unsigned int i;
74   xbt_dict_t props = nullptr;
75   xbt_dict_cursor_t cursor = nullptr;
76   char *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   std::vector<simgrid::kernel::routing::NetCard*> netcardList;
93   simgrid::s4u::Engine::instance()->netcardList(&netcardList);
94
95   if (timings) {
96     XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time),
97              sg_host_count(), sg_link_count());
98   } else {
99     std::printf("<?xml version='1.0'?>\n");
100     std::printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\">\n");
101     std::printf("<platform version=\"%d\">\n", version);
102     std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
103
104     // Hosts
105     unsigned int totalHosts = sg_host_count();
106     sg_host_t *hosts = sg_host_list();
107     std::qsort((void *) hosts, totalHosts, sizeof(sg_host_t), name_compare_hosts);
108
109     for (i = 0; i < totalHosts; i++) {
110       std::printf("  <host id=\"%s\" speed=\"%.0f\"", hosts[i]->cname(), sg_host_speed(hosts[i]));
111       props = sg_host_get_properties(hosts[i]);
112       if (hosts[i]->coreCount()>1) {
113         std::printf(" core=\"%d\"", hosts[i]->coreCount());
114       }
115       if (props && !xbt_dict_is_empty(props)) {
116         std::printf(">\n");
117         xbt_dict_foreach(props, cursor, key, data) {
118           std::printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
119         }
120         std::printf("  </host>\n");
121       } else {
122         std::printf("/>\n");
123       }
124     }
125
126     // Routers
127     for (auto srcCard : netcardList)
128       if (srcCard->isRouter())
129         std::printf("  <router id=\"%s\"/>\n", srcCard->cname());
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     for (unsigned int it_src = 0; it_src < totalHosts; it_src++) { // Routes from host
150       simgrid::s4u::Host* host1 = hosts[it_src];
151       value1 = host1->pimpl_netcard;
152       for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
153         simgrid::s4u::Host* host2 = hosts[it_dst];
154         std::vector<Link*> *route = new std::vector<Link*>();
155         value2 = host2->pimpl_netcard;
156         simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, route, nullptr);
157         if (! route->empty()){
158           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", host1->cname(), host2->cname());
159           for (auto link: *route)
160             std::printf("<link_ctn id=\"%s\"/>",link->getName());
161           std::printf("\n  </route>\n");
162         }
163         delete route;
164       }
165       for (auto value2 : netcardList) { // to router
166         if(value2->isRouter()){
167           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", host1->cname(), value2->cname());
168           std::vector<Link*> *route = new std::vector<Link*>();
169           simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, route, nullptr);
170           for (auto link : *route)
171             std::printf("<link_ctn id=\"%s\"/>",link->getName());
172           delete route;
173           std::printf("\n  </route>\n");
174         }
175       }
176     }
177
178     for (auto value1 : netcardList) { // Routes from router
179       if (value1->isRouter()){
180         for (auto value2 : netcardList) { // to router
181           if (value2->isRouter()) {
182             std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->cname(), value2->cname());
183             std::vector<Link*>* route = new std::vector<Link*>();
184             simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, route, nullptr);
185             for(auto link :*route)
186               std::printf("<link_ctn id=\"%s\"/>",link->getName());
187             delete route;
188             std::printf("\n  </route>\n");
189           }
190         }
191         for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
192           simgrid::s4u::Host* host2 = hosts[it_dst];
193           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->cname(), host2->cname());
194           std::vector<Link*> *route = new std::vector<Link*>();
195           value2 = host2->pimpl_netcard;
196           simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, route, nullptr);
197           for(auto link : *route)
198             std::printf("<link_ctn id=\"%s\"/>",link->getName());
199           delete route;
200           std::printf("\n  </route>\n");
201         }
202       }
203     }
204
205     std::printf("</AS>\n");
206     std::printf("</platform>\n");
207     std::free(hosts);
208     std::free(links);
209   }
210
211   SD_exit();
212   xbt_os_timer_free(parse_time);
213
214   return 0;
215 }