Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
89ebb227761bfbbd564731131daea76fad1ca5aa
[simgrid.git] / teshsuite / platforms / flatifier.cpp
1 /* Copyright (c) 2008-2023. 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 #include <sstream>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
19
20 namespace sg4 = simgrid::s4u;
21
22 static bool parse_cmdline(bool* timings, char** platformFile, int argc, char** argv)
23 {
24   bool parse_ok = true;
25   for (int i = 1; i < argc; i++) {
26     if (std::strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
27       if (not std::strcmp(argv[i], "--timings")) {
28         *timings = true;
29       } else {
30         parse_ok = false;
31         break;
32       }
33     } else {
34       *platformFile = argv[i];
35     }
36   }
37   return parse_ok && platformFile != nullptr;
38 }
39
40 static void dump_hosts(sg4::Engine& engine, std::stringstream& ss)
41 {
42   std::vector<sg4::Host*> hosts = engine.get_all_hosts();
43
44   for (auto const* h : hosts) {
45     ss << "  <host id=\"" << h->get_name() << "\" speed=\"" << h->get_speed() << "\"";
46     const std::unordered_map<std::string, std::string>* props = h->get_properties();
47     if (h->get_core_count() > 1)
48       ss << " core=\"" << h->get_core_count() << "\"";
49
50     // Sort the properties before displaying them, so that the tests are perfectly reproducible
51     std::vector<std::string> keys;
52     for (auto const& [key, _] : *props)
53       keys.push_back(key);
54     if (not keys.empty()) {
55       ss << ">\n";
56       std::sort(keys.begin(), keys.end());
57       for (const std::string& key : keys)
58         ss << "    <prop id=\"" << key << "\" value=\"" << props->at(key) << "\"/>\n";
59       ss << "  </host>\n";
60     } else {
61       ss << "/>\n";
62     }
63   }
64 }
65
66 static void dump_links(sg4::Engine& engine, std::stringstream& ss)
67 {
68   std::vector<sg4::Link*> links = engine.get_all_links();
69
70   std::sort(links.begin(), links.end(),
71             [](const sg4::Link* a, const sg4::Link* b) { return a->get_name() < b->get_name(); });
72
73   for (auto const* link : links) {
74     ss << "  <link id=\"" << link->get_name() << "\" ";
75     ss << "bandwidth=\"" << link->get_bandwidth() << "\" ";
76     ss << "latency=\"" << link->get_latency() << "\"";
77     if (link->is_shared()) {
78       ss << "/>\n";
79     } else {
80       ss << " sharing_policy=\"FATPIPE\"/>\n";
81     }
82   }
83 }
84
85 static void dump_routers(sg4::Engine& engine, std::stringstream& ss)
86 {
87   std::vector<simgrid::kernel::routing::NetPoint*> netpoints = engine.get_all_netpoints();
88   std::sort(netpoints.begin(), netpoints.end(),
89             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
90               return a->get_name() < b->get_name();
91             });
92
93   for (auto const& src : netpoints)
94     if (src->is_router())
95       ss << "  <router id=\"" << src->get_name() << "\"/>\n";
96 }
97
98 static void dump_routes(sg4::Engine& engine, std::stringstream& ss)
99 {
100   auto hosts     = engine.get_all_hosts();
101   auto netpoints = engine.get_all_netpoints();
102   std::sort(netpoints.begin(), netpoints.end(),
103             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
104               return a->get_name() < b->get_name();
105             });
106
107   for (auto const* src_host : hosts) { // Routes from host
108     const simgrid::kernel::routing::NetPoint* src = src_host->get_netpoint();
109     for (auto const* dst_host : hosts) { // Routes to host
110       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
111       const simgrid::kernel::routing::NetPoint* dst = dst_host->get_netpoint();
112       simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
113       if (route.empty())
114         continue;
115       ss << "  <route src=\"" << src_host->get_name() << "\" dst=\"" << dst_host->get_name() << "\">\n  ";
116       for (auto const& link : route)
117         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
118       ss << "\n  </route>\n";
119     }
120
121     for (auto const& dst : netpoints) { // to router
122       if (not dst->is_router())
123         continue;
124       ss << "  <route src=\"" << src_host->get_name() << "\" dst=\"" << dst->get_name() << "\">\n  ";
125       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
126       simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
127       for (auto const& link : route)
128         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
129       ss << "\n  </route>\n";
130     }
131   }
132
133   for (auto const& value1 : netpoints) { // Routes from router
134     if (not value1->is_router())
135       continue;
136     for (auto const& value2 : netpoints) { // to router
137       if (not value2->is_router())
138         continue;
139       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
140       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
141       if (route.empty())
142         continue;
143       ss << "  <route src=\"" << value1->get_name() << "\" dst=\"" << value2->get_name() << "\">\n  ";
144       for (auto const& link : route)
145         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
146       ss << "\n  </route>\n";
147     }
148     for (auto const* dst_host : hosts) { // Routes to host
149       ss << "  <route src=\"" << value1->get_name() << "\" dst=\"" << dst_host->get_name() << "\">\n  ";
150       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
151       const simgrid::kernel::routing::NetPoint* netcardDst = dst_host->get_netpoint();
152       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
153       for (auto const& link : route)
154         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
155       ss << "\n  </route>\n";
156     }
157   }
158 }
159
160 static std::string dump_platform(sg4::Engine& engine)
161 {
162   std::string version = "4.1";
163   std::stringstream ss;
164
165   ss << "<?xml version='1.0'?>\n";
166   ss << "<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n";
167   ss << "<platform version=\"" << version << "\">\n";
168   ss << "<AS id=\"" << engine.get_netzone_root()->get_name() << "\" routing=\"Full\">\n";
169
170   dump_hosts(engine, ss);
171   dump_routers(engine, ss);
172   dump_links(engine, ss);
173   dump_routes(engine, ss);
174
175   ss << "</AS>\n";
176   ss << "</platform>\n";
177   return ss.str();
178 }
179
180 int main(int argc, char** argv)
181 {
182   char* platformFile = nullptr;
183   bool timings       = false;
184
185   xbt_os_timer_t parse_time = xbt_os_timer_new();
186
187   sg4::Engine e(&argc, argv);
188
189   xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv),
190              "Invalid command line arguments: expected [--timings] platformFile");
191
192   xbt_os_cputimer_start(parse_time);
193   e.load_platform(platformFile);
194   e.seal_platform();
195   xbt_os_cputimer_stop(parse_time);
196
197   if (timings) {
198     XBT_INFO("Parsing time: %fs (%zu hosts, %zu links)", xbt_os_timer_elapsed(parse_time), e.get_host_count(),
199              e.get_link_count());
200   } else {
201     std::printf("%s", dump_platform(e).c_str());
202   }
203
204   xbt_os_timer_free(parse_time);
205
206   return 0;
207 }