Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a Link::get_concurrency_limit and use it in the flatifier
[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->get_concurrency_limit() != -1)
78       ss << " concurrency=\"" << link->get_concurrency_limit() << "\"";
79     if (link->is_shared()) {
80       ss << "/>\n";
81     } else {
82       ss << " sharing_policy=\"FATPIPE\"/>\n";
83     }
84   }
85 }
86
87 static void dump_routers(sg4::Engine& engine, std::stringstream& ss)
88 {
89   std::vector<simgrid::kernel::routing::NetPoint*> netpoints = engine.get_all_netpoints();
90   std::sort(netpoints.begin(), netpoints.end(),
91             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
92               return a->get_name() < b->get_name();
93             });
94
95   for (auto const& src : netpoints)
96     if (src->is_router())
97       ss << "  <router id=\"" << src->get_name() << "\"/>\n";
98 }
99
100 static void dump_routes(sg4::Engine& engine, std::stringstream& ss)
101 {
102   auto hosts     = engine.get_all_hosts();
103   auto netpoints = engine.get_all_netpoints();
104   std::sort(netpoints.begin(), netpoints.end(),
105             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
106               return a->get_name() < b->get_name();
107             });
108
109   for (auto const* src_host : hosts) { // Routes from host
110     const simgrid::kernel::routing::NetPoint* src = src_host->get_netpoint();
111     for (auto const* dst_host : hosts) { // Routes to host
112       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
113       const simgrid::kernel::routing::NetPoint* dst = dst_host->get_netpoint();
114       simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
115       if (route.empty())
116         continue;
117       ss << "  <route src=\"" << src_host->get_name() << "\" dst=\"" << dst_host->get_name() << "\">\n  ";
118       for (auto const& link : route)
119         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
120       ss << "\n  </route>\n";
121     }
122
123     for (auto const& dst : netpoints) { // to router
124       if (not dst->is_router())
125         continue;
126       ss << "  <route src=\"" << src_host->get_name() << "\" dst=\"" << dst->get_name() << "\">\n  ";
127       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
128       simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
129       for (auto const& link : route)
130         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
131       ss << "\n  </route>\n";
132     }
133   }
134
135   for (auto const& value1 : netpoints) { // Routes from router
136     if (not value1->is_router())
137       continue;
138     for (auto const& value2 : netpoints) { // to router
139       if (not value2->is_router())
140         continue;
141       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
142       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
143       if (route.empty())
144         continue;
145       ss << "  <route src=\"" << value1->get_name() << "\" dst=\"" << value2->get_name() << "\">\n  ";
146       for (auto const& link : route)
147         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
148       ss << "\n  </route>\n";
149     }
150     for (auto const* dst_host : hosts) { // Routes to host
151       ss << "  <route src=\"" << value1->get_name() << "\" dst=\"" << dst_host->get_name() << "\">\n  ";
152       std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
153       const simgrid::kernel::routing::NetPoint* netcardDst = dst_host->get_netpoint();
154       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
155       for (auto const& link : route)
156         ss << "<link_ctn id=\"" << link->get_name() << "\"/>";
157       ss << "\n  </route>\n";
158     }
159   }
160 }
161
162 static std::string dump_platform(sg4::Engine& engine)
163 {
164   std::string version = "4.1";
165   std::stringstream ss;
166
167   ss << "<?xml version='1.0'?>\n";
168   ss << "<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n";
169   ss << "<platform version=\"" << version << "\">\n";
170   ss << "<AS id=\"" << engine.get_netzone_root()->get_name() << "\" routing=\"Full\">\n";
171
172   dump_hosts(engine, ss);
173   dump_routers(engine, ss);
174   dump_links(engine, ss);
175   dump_routes(engine, ss);
176
177   ss << "</AS>\n";
178   ss << "</platform>\n";
179   return ss.str();
180 }
181
182 int main(int argc, char** argv)
183 {
184   char* platformFile = nullptr;
185   bool timings       = false;
186
187   xbt_os_timer_t parse_time = xbt_os_timer_new();
188
189   sg4::Engine e(&argc, argv);
190
191   xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv),
192              "Invalid command line arguments: expected [--timings] platformFile");
193
194   xbt_os_cputimer_start(parse_time);
195   e.load_platform(platformFile);
196   e.seal_platform();
197   xbt_os_cputimer_stop(parse_time);
198
199   if (timings) {
200     XBT_INFO("Parsing time: %fs (%zu hosts, %zu links)", xbt_os_timer_elapsed(parse_time), e.get_host_count(),
201              e.get_link_count());
202   } else {
203     std::printf("%s", dump_platform(e).c_str());
204   }
205
206   xbt_os_timer_free(parse_time);
207
208   return 0;
209 }