Logo AND Algorithmique Numérique Distribuée

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