Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
flatifier.cpp: revert change from commit ba1c6dce93, and omit empty routes from output.
[simgrid.git] / teshsuite / simdag / flatifier / flatifier.cpp
1 /* Copyright (c) 2008-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <xbt/xbt_os_time.h>
7
8 #include "simgrid/kernel/routing/NetPoint.hpp"
9 #include "simgrid/s4u/Engine.hpp"
10 #include "simgrid/s4u/Host.hpp"
11 #include "simgrid/s4u/Link.hpp"
12 #include "src/surf/network_interface.hpp"
13
14 #include <algorithm>
15 #include <cstring>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
18
19 namespace sg4 = simgrid::s4u;
20
21 static bool parse_cmdline(int* timings, char** platformFile, int argc, char** argv)
22 {
23   bool parse_ok = true;
24   for (int i = 1; i < argc; i++) {
25     if (std::strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
26       if (not std::strcmp(argv[i], "--timings")) {
27         *timings = 1;
28       } else {
29         parse_ok = false;
30         break;
31       }
32     } else {
33       *platformFile = argv[i];
34     }
35   }
36   return parse_ok;
37 }
38
39 static void create_environment(xbt_os_timer_t parse_time, const std::string& platformFile)
40 {
41   xbt_os_cputimer_start(parse_time);
42   sg4::Engine::get_instance()->load_platform(platformFile);
43   xbt_os_cputimer_stop(parse_time);
44 }
45
46 static void dump_hosts()
47 {
48   std::vector<sg4::Host*> hosts  = sg4::Engine::get_instance()->get_all_hosts();
49   std::sort(hosts.begin(), hosts.end(),
50             [](const sg4::Host* a, const sg4::Host* b) { return a->get_name() < b->get_name(); });
51
52   for (auto h : hosts) {
53     std::printf("  <host id=\"%s\" speed=\"%.0f\"", h->get_cname(), h->get_speed());
54     const std::unordered_map<std::string, std::string>* props = h->get_properties();
55     if (h->get_core_count() > 1) {
56       std::printf(" core=\"%d\"", h->get_core_count());
57     }
58     // Sort the properties before displaying them, so that the tests are perfectly reproducible
59     std::vector<std::string> keys;
60     for (auto const& kv : *props)
61       keys.push_back(kv.first);
62     if (not keys.empty()) {
63       std::printf(">\n");
64       std::sort(keys.begin(), keys.end());
65       for (const std::string& key : keys)
66         std::printf("    <prop id=\"%s\" value=\"%s\"/>\n", key.c_str(), props->at(key).c_str());
67       std::printf("  </host>\n");
68     } else {
69       std::printf("/>\n");
70     }
71   }
72 }
73
74 static void dump_links()
75 {
76   std::vector<sg4::Link*> links = sg4::Engine::get_instance()->get_all_links();
77
78   std::sort(links.begin(), links.end(), [](const sg4::Link* a, const sg4::Link* b) {
79     return a->get_name() < b->get_name();
80   });
81
82   for (auto link : links) {
83     std::printf("  <link id=\"");
84
85     std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->get_cname(), link->get_bandwidth(), link->get_latency());
86     if (link->is_shared()) {
87       std::printf("/>\n");
88     } else {
89       std::printf(" sharing_policy=\"FATPIPE\"/>\n");
90     }
91   }
92 }
93
94 static void dump_routers()
95 {
96   std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
97       sg4::Engine::get_instance()->get_all_netpoints();
98   std::sort(netpoints.begin(), netpoints.end(),
99             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
100               return a->get_name() < b->get_name();
101             });
102
103   for (auto const& src : netpoints)
104     if (src->is_router())
105       std::printf("  <router id=\"%s\"/>\n", src->get_cname());
106 }
107
108 static void dump_routes()
109 {
110   std::vector<sg4::Host*> hosts  = sg4::Engine::get_instance()->get_all_hosts();
111   std::sort(hosts.begin(), hosts.end(),
112             [](const sg4::Host* a, const sg4::Host* b) { return a->get_name() < b->get_name(); });
113   std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
114       sg4::Engine::get_instance()->get_all_netpoints();
115   std::sort(netpoints.begin(), netpoints.end(),
116             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
117               return a->get_name() < b->get_name();
118             });
119
120   for (auto src_host : hosts) { // Routes from host
121     const simgrid::kernel::routing::NetPoint* src = src_host->get_netpoint();
122     for (auto dst_host : hosts) { // Routes to host
123       std::vector<simgrid::kernel::resource::LinkImpl*> route;
124       const simgrid::kernel::routing::NetPoint* dst = dst_host->get_netpoint();
125       simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
126       if (route.empty())
127         continue;
128       std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", src_host->get_cname(), dst_host->get_cname());
129       for (auto const& link : route)
130         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
131       std::printf("\n  </route>\n");
132     }
133
134     for (auto const& dst : netpoints) { // to router
135       if (not dst->is_router())
136         continue;
137       std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", src_host->get_cname(), dst->get_cname());
138       std::vector<simgrid::kernel::resource::LinkImpl*> route;
139       simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
140       for (auto const& link : route)
141         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
142       std::printf("\n  </route>\n");
143     }
144   }
145
146   for (auto const& value1 : netpoints) { // Routes from router
147     if (not value1->is_router())
148       continue;
149     for (auto const& value2 : netpoints) { // to router
150       if (not value2->is_router())
151         continue;
152       std::vector<simgrid::kernel::resource::LinkImpl*> route;
153       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
154       if (route.empty())
155         continue;
156       std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->get_cname(), value2->get_cname());
157       for (auto const& link : route)
158         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
159       std::printf("\n  </route>\n");
160     }
161     for (auto dst_host : hosts) { // Routes to host
162       std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->get_cname(), dst_host->get_cname());
163       std::vector<simgrid::kernel::resource::LinkImpl*> route;
164       const simgrid::kernel::routing::NetPoint* netcardDst = dst_host->get_netpoint();
165       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
166       for (auto const& link : route)
167         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
168       std::printf("\n  </route>\n");
169     }
170   }
171 }
172
173 static void dump_platform()
174 {
175   int version = 4;
176
177   std::printf("<?xml version='1.0'?>\n");
178   std::printf("<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n");
179   std::printf("<platform version=\"%d\">\n", version);
180   std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
181
182   // Hosts
183   dump_hosts();
184
185   // Routers
186   dump_routers();
187
188   // Links
189   dump_links();
190
191   // Routes
192   dump_routes();
193
194   std::printf("</AS>\n");
195   std::printf("</platform>\n");
196 }
197
198 int main(int argc, char** argv)
199 {
200   char* platformFile = nullptr;
201   int timings        = 0;
202
203   xbt_os_timer_t parse_time = xbt_os_timer_new();
204
205   sg4::Engine e(&argc, argv);
206
207   xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv) && platformFile,
208              "Invalid command line arguments: expected [--timings] platformFile");
209
210   XBT_DEBUG("%d,%s", timings, platformFile);
211
212   create_environment(parse_time, platformFile);
213
214   if (timings) {
215     XBT_INFO("Parsing time: %fs (%zu hosts, %zu links)", xbt_os_timer_elapsed(parse_time), e.get_host_count(),
216              e.get_link_count());
217   } else {
218     dump_platform();
219   }
220
221   xbt_os_timer_free(parse_time);
222
223   return 0;
224 }