Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use structured binding declarations (sonar, c++17).
[simgrid.git] / teshsuite / platforms / flatifier.cpp
1 /* Copyright (c) 2008-2022. 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/kernel/resource/StandardLinkImpl.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   sg4::Engine::get_instance()->seal_platform();
44   xbt_os_cputimer_stop(parse_time);
45 }
46
47 static void dump_hosts()
48 {
49   std::vector<sg4::Host*> hosts = sg4::Engine::get_instance()->get_all_hosts();
50   std::sort(hosts.begin(), hosts.end(),
51             [](const sg4::Host* a, const sg4::Host* b) { return a->get_name() < b->get_name(); });
52
53   for (auto h : hosts) {
54     std::printf("  <host id=\"%s\" speed=\"%.0f\"", h->get_cname(), h->get_speed());
55     const std::unordered_map<std::string, std::string>* props = h->get_properties();
56     if (h->get_core_count() > 1) {
57       std::printf(" core=\"%d\"", h->get_core_count());
58     }
59     // Sort the properties before displaying them, so that the tests are perfectly reproducible
60     std::vector<std::string> keys;
61     for (auto const& [key, _] : *props)
62       keys.push_back(key);
63     if (not keys.empty()) {
64       std::printf(">\n");
65       std::sort(keys.begin(), keys.end());
66       for (const std::string& key : keys)
67         std::printf("    <prop id=\"%s\" value=\"%s\"/>\n", key.c_str(), props->at(key).c_str());
68       std::printf("  </host>\n");
69     } else {
70       std::printf("/>\n");
71     }
72   }
73 }
74
75 static void dump_links()
76 {
77   std::vector<sg4::Link*> links = sg4::Engine::get_instance()->get_all_links();
78
79   std::sort(links.begin(), links.end(),
80             [](const sg4::Link* a, const sg4::Link* b) { return a->get_name() < b->get_name(); });
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(),
86                 link->get_latency());
87     if (link->is_shared()) {
88       std::printf("/>\n");
89     } else {
90       std::printf(" sharing_policy=\"FATPIPE\"/>\n");
91     }
92   }
93 }
94
95 static void dump_routers()
96 {
97   std::vector<simgrid::kernel::routing::NetPoint*> netpoints = 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 = sg4::Engine::get_instance()->get_all_netpoints();
114   std::sort(netpoints.begin(), netpoints.end(),
115             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
116               return a->get_name() < b->get_name();
117             });
118
119   for (auto src_host : hosts) { // Routes from host
120     const simgrid::kernel::routing::NetPoint* src = src_host->get_netpoint();
121     for (auto dst_host : hosts) { // Routes to host
122       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
123       const simgrid::kernel::routing::NetPoint* dst = dst_host->get_netpoint();
124       simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
125       if (route.empty())
126         continue;
127       std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", src_host->get_cname(), dst_host->get_cname());
128       for (auto const& link : route)
129         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
130       std::printf("\n  </route>\n");
131     }
132
133     for (auto const& dst : netpoints) { // to router
134       if (not dst->is_router())
135         continue;
136       std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", src_host->get_cname(), dst->get_cname());
137       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
138       simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
139       for (auto const& link : route)
140         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
141       std::printf("\n  </route>\n");
142     }
143   }
144
145   for (auto const& value1 : netpoints) { // Routes from router
146     if (not value1->is_router())
147       continue;
148     for (auto const& value2 : netpoints) { // to router
149       if (not value2->is_router())
150         continue;
151       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
152       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
153       if (route.empty())
154         continue;
155       std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->get_cname(), value2->get_cname());
156       for (auto const& link : route)
157         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
158       std::printf("\n  </route>\n");
159     }
160     for (auto dst_host : hosts) { // Routes to host
161       std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->get_cname(), dst_host->get_cname());
162       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
163       const simgrid::kernel::routing::NetPoint* netcardDst = dst_host->get_netpoint();
164       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
165       for (auto const& link : route)
166         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
167       std::printf("\n  </route>\n");
168     }
169   }
170 }
171
172 static void dump_platform()
173 {
174   int version = 4;
175
176   std::printf("<?xml version='1.0'?>\n");
177   std::printf("<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n");
178   std::printf("<platform version=\"%d\">\n", version);
179   std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
180
181   // Hosts
182   dump_hosts();
183
184   // Routers
185   dump_routers();
186
187   // Links
188   dump_links();
189
190   // Routes
191   dump_routes();
192
193   std::printf("</AS>\n");
194   std::printf("</platform>\n");
195 }
196
197 int main(int argc, char** argv)
198 {
199   char* platformFile = nullptr;
200   int timings        = 0;
201
202   xbt_os_timer_t parse_time = xbt_os_timer_new();
203
204   sg4::Engine e(&argc, argv);
205
206   xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv) && platformFile,
207              "Invalid command line arguments: expected [--timings] platformFile");
208
209   XBT_DEBUG("%d,%s", timings, platformFile);
210
211   create_environment(parse_time, platformFile);
212
213   if (timings) {
214     XBT_INFO("Parsing time: %fs (%zu hosts, %zu links)", xbt_os_timer_elapsed(parse_time), e.get_host_count(),
215              e.get_link_count());
216   } else {
217     dump_platform();
218   }
219
220   xbt_os_timer_free(parse_time);
221
222   return 0;
223 }