Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
FatTreeZone: Add limiters for switches
[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     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       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::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->get_cname(), value2->get_cname());
153       std::vector<simgrid::kernel::resource::LinkImpl*> route;
154       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
155       for (auto const& link : route)
156         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
157       std::printf("\n  </route>\n");
158     }
159     for (auto dst_host : hosts) { // Routes to host
160       std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->get_cname(), dst_host->get_cname());
161       std::vector<simgrid::kernel::resource::LinkImpl*> route;
162       simgrid::kernel::routing::NetPoint* netcardDst = dst_host->get_netpoint();
163       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
164       for (auto const& link : route)
165         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
166       std::printf("\n  </route>\n");
167     }
168   }
169 }
170
171 static void dump_platform()
172 {
173   int version = 4;
174
175   std::printf("<?xml version='1.0'?>\n");
176   std::printf("<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n");
177   std::printf("<platform version=\"%d\">\n", version);
178   std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
179
180   // Hosts
181   dump_hosts();
182
183   // Routers
184   dump_routers();
185
186   // Links
187   dump_links();
188
189   // Routes
190   dump_routes();
191
192   std::printf("</AS>\n");
193   std::printf("</platform>\n");
194 }
195
196 int main(int argc, char** argv)
197 {
198   char* platformFile = nullptr;
199   int timings        = 0;
200
201   xbt_os_timer_t parse_time = xbt_os_timer_new();
202
203   sg4::Engine e(&argc, argv);
204
205   xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv) && platformFile,
206              "Invalid command line arguments: expected [--timings] platformFile");
207
208   XBT_DEBUG("%d,%s", timings, platformFile);
209
210   create_environment(parse_time, platformFile);
211
212   if (timings) {
213     XBT_INFO("Parsing time: %fs (%zu hosts, %zu links)", xbt_os_timer_elapsed(parse_time), e.get_host_count(),
214              e.get_link_count());
215   } else {
216     dump_platform();
217   }
218
219   xbt_os_timer_free(parse_time);
220
221   return 0;
222 }