1 /* Copyright (c) 2008-2022. 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"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
19 namespace sg4 = simgrid::s4u;
21 static bool parse_cmdline(int* timings, char** platformFile, int argc, char** argv)
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")) {
33 *platformFile = argv[i];
39 static void create_environment(xbt_os_timer_t parse_time, const std::string& platformFile)
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);
47 static void dump_hosts()
49 std::vector<sg4::Host*> hosts = sg4::Engine::get_instance()->get_all_hosts();
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());
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)
61 if (not keys.empty()) {
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");
73 static void dump_links()
75 std::vector<sg4::Link*> links = sg4::Engine::get_instance()->get_all_links();
77 std::sort(links.begin(), links.end(),
78 [](const sg4::Link* a, const sg4::Link* b) { return a->get_name() < b->get_name(); });
80 for (auto const* link : links) {
81 std::printf(" <link id=\"");
83 std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->get_cname(), link->get_bandwidth(),
85 if (link->is_shared()) {
88 std::printf(" sharing_policy=\"FATPIPE\"/>\n");
93 static void dump_routers()
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();
101 for (auto const& src : netpoints)
102 if (src->is_router())
103 std::printf(" <router id=\"%s\"/>\n", src->get_cname());
106 static void dump_routes()
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();
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);
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");
129 for (auto const& dst : netpoints) { // to router
130 if (not dst->is_router())
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");
141 for (auto const& value1 : netpoints) { // Routes from router
142 if (not value1->is_router())
144 for (auto const& value2 : netpoints) { // to router
145 if (not value2->is_router())
147 std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
148 simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
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");
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");
168 static void dump_platform()
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");
189 std::printf("</AS>\n");
190 std::printf("</platform>\n");
193 int main(int argc, char** argv)
195 char* platformFile = nullptr;
198 xbt_os_timer_t parse_time = xbt_os_timer_new();
200 sg4::Engine e(&argc, argv);
202 xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv) && platformFile,
203 "Invalid command line arguments: expected [--timings] platformFile");
205 XBT_DEBUG("%d,%s", timings, platformFile);
207 create_environment(parse_time, platformFile);
210 XBT_INFO("Parsing time: %fs (%zu hosts, %zu links)", xbt_os_timer_elapsed(parse_time), e.get_host_count(),
216 xbt_os_timer_free(parse_time);