1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved. */
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. */
6 #include <xbt/xbt_os_time.h>
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"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
20 namespace sg4 = simgrid::s4u;
22 static bool parse_cmdline(bool* timings, char** platformFile, int argc, char** argv)
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")) {
34 *platformFile = argv[i];
37 return parse_ok && platformFile != nullptr;
40 static void dump_hosts(sg4::Engine& engine, std::stringstream& ss)
42 std::vector<sg4::Host*> hosts = engine.get_all_hosts();
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() << "\"";
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)
54 if (not keys.empty()) {
56 std::sort(keys.begin(), keys.end());
57 for (const std::string& key : keys)
58 ss << " <prop id=\"" << key << "\" value=\"" << props->at(key) << "\"/>\n";
66 static void dump_links(sg4::Engine& engine, std::stringstream& ss)
68 std::vector<sg4::Link*> links = engine.get_all_links();
70 std::sort(links.begin(), links.end(),
71 [](const sg4::Link* a, const sg4::Link* b) { return a->get_name() < b->get_name(); });
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()) {
82 ss << " sharing_policy=\"FATPIPE\"/>\n";
87 static void dump_routers(sg4::Engine& engine, std::stringstream& ss)
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();
95 for (auto const& src : netpoints)
97 ss << " <router id=\"" << src->get_name() << "\"/>\n";
100 static void dump_routes(sg4::Engine& engine, std::stringstream& ss)
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();
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);
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";
123 for (auto const& dst : netpoints) { // to router
124 if (not dst->is_router())
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";
135 for (auto const& value1 : netpoints) { // Routes from router
136 if (not value1->is_router())
138 for (auto const& value2 : netpoints) { // to router
139 if (not value2->is_router())
141 std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
142 simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
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";
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";
162 static std::string dump_platform(sg4::Engine& engine)
164 std::string version = "4.1";
165 std::stringstream ss;
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";
172 dump_hosts(engine, ss);
173 dump_routers(engine, ss);
174 dump_links(engine, ss);
175 dump_routes(engine, ss);
178 ss << "</platform>\n";
182 int main(int argc, char** argv)
184 char* platformFile = nullptr;
185 bool timings = false;
187 xbt_os_timer_t parse_time = xbt_os_timer_new();
189 sg4::Engine e(&argc, argv);
191 xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv),
192 "Invalid command line arguments: expected [--timings] platformFile");
194 xbt_os_cputimer_start(parse_time);
195 e.load_platform(platformFile);
197 xbt_os_cputimer_stop(parse_time);
200 XBT_INFO("Parsing time: %fs (%zu hosts, %zu links)", xbt_os_timer_elapsed(parse_time), e.get_host_count(),
203 std::printf("%s", dump_platform(e).c_str());
206 xbt_os_timer_free(parse_time);