Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d9496b962904aed0edb0f6cb86e0367fd495c74d
[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 "src/surf/network_interface.hpp"
12
13 #include <algorithm>
14 #include <cstring>
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
17
18 namespace sg4 = simgrid::s4u;
19
20 static bool parse_cmdline(int* timings, char** platformFile, int argc, char** argv)
21 {
22   bool parse_ok = true;
23   for (int i = 1; i < argc; i++) {
24     if (std::strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
25       if (not std::strcmp(argv[i], "--timings")) {
26         *timings = 1;
27       } else {
28         parse_ok = false;
29         break;
30       }
31     } else {
32       *platformFile = argv[i];
33     }
34   }
35   return parse_ok;
36 }
37
38 static void create_environment(xbt_os_timer_t parse_time, const std::string platformFile)
39 {
40   xbt_os_cputimer_start(parse_time);
41   sg4::Engine::get_instance()->load_platform(platformFile);
42   xbt_os_cputimer_stop(parse_time);
43 }
44
45 static void dump_hosts()
46 {
47   std::vector<sg4::Host*> hosts  = sg4::Engine::get_instance()->get_all_hosts();
48   std::sort(hosts.begin(), hosts.end(),
49             [](const sg4::Host* a, const sg4::Host* b) { return a->get_name() < b->get_name(); });
50
51   for (auto 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& kv : *props)
60       keys.push_back(kv.first);
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(), [](const sg4::Link* a, const sg4::Link* b) {
78     return a->get_name() < b->get_name();
79   });
80
81   for (auto link : links) {
82     std::printf("  <link id=\"");
83
84     std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->get_cname(), link->get_bandwidth(), link->get_latency());
85     if (sg_link_is_shared(link)) {
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 =
96       sg4::Engine::get_instance()->get_all_netpoints();
97   std::sort(netpoints.begin(), netpoints.end(),
98             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
99               return a->get_name() < b->get_name();
100             });
101
102   for (auto const& src : netpoints)
103     if (src->is_router())
104       std::printf("  <router id=\"%s\"/>\n", src->get_cname());
105 }
106
107 static void dump_routes()
108 {
109   std::vector<sg4::Host*> hosts  = sg4::Engine::get_instance()->get_all_hosts();
110   std::sort(hosts.begin(), hosts.end(),
111             [](const sg4::Host* a, const sg4::Host* b) { return a->get_name() < b->get_name(); });
112   std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
113       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     simgrid::kernel::routing::NetPoint* src = src_host->get_netpoint();
121     for (auto dst_host : hosts) { // Routes to host
122       std::vector<simgrid::kernel::resource::LinkImpl*> route;
123       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::LinkImpl*> 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::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->get_cname(), value2->get_cname());
152       std::vector<simgrid::kernel::resource::LinkImpl*> route;
153       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
154       for (auto const& link : route)
155         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
156       std::printf("\n  </route>\n");
157     }
158     for (auto dst_host : hosts) { // Routes to host
159       std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->get_cname(), dst_host->get_cname());
160       std::vector<simgrid::kernel::resource::LinkImpl*> route;
161       simgrid::kernel::routing::NetPoint* netcardDst = dst_host->get_netpoint();
162       simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
163       for (auto const& link : route)
164         std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
165       std::printf("\n  </route>\n");
166     }
167   }
168 }
169
170 static void dump_platform()
171 {
172   int version = 4;
173
174   std::printf("<?xml version='1.0'?>\n");
175   std::printf("<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n");
176   std::printf("<platform version=\"%d\">\n", version);
177   std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
178
179   // Hosts
180   dump_hosts();
181
182   // Routers
183   dump_routers();
184
185   // Links
186   dump_links();
187
188   // Routes
189   dump_routes();
190
191   std::printf("</AS>\n");
192   std::printf("</platform>\n");
193 }
194
195 int main(int argc, char** argv)
196 {
197   char* platformFile = nullptr;
198   int timings        = 0;
199
200   xbt_os_timer_t parse_time = xbt_os_timer_new();
201
202   sg4::Engine e(&argc, argv);
203
204   if (not parse_cmdline(&timings, &platformFile, argc, argv) || not platformFile)
205     xbt_die("Invalid command line arguments: expected [--timings] platformFile");
206
207   XBT_DEBUG("%d,%s", timings, platformFile);
208
209   create_environment(parse_time, platformFile);
210
211   if (timings) {
212     XBT_INFO("Parsing time: %fs (%zu hosts, %zu links)", xbt_os_timer_elapsed(parse_time), e.get_host_count(),
213              e.get_link_count());
214   } else {
215     dump_platform();
216   }
217
218   xbt_os_timer_free(parse_time);
219
220   return 0;
221 }