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();
50 std::sort(hosts.begin(), hosts.end(),
51 [](const sg4::Host* a, const sg4::Host* b) { return a->get_name() < b->get_name(); });
53 for (auto h : hosts) {
54 std::printf(" <host id=\"%s\" speed=\"%.0f\"", h->get_cname(), h->get_speed());
55 const std::unordered_map<std::string, std::string>* props = h->get_properties();
56 if (h->get_core_count() > 1) {
57 std::printf(" core=\"%d\"", h->get_core_count());
59 // Sort the properties before displaying them, so that the tests are perfectly reproducible
60 std::vector<std::string> keys;
61 for (auto const& kv : *props)
62 keys.push_back(kv.first);
63 if (not keys.empty()) {
65 std::sort(keys.begin(), keys.end());
66 for (const std::string& key : keys)
67 std::printf(" <prop id=\"%s\" value=\"%s\"/>\n", key.c_str(), props->at(key).c_str());
68 std::printf(" </host>\n");
75 static void dump_links()
77 std::vector<sg4::Link*> links = sg4::Engine::get_instance()->get_all_links();
79 std::sort(links.begin(), links.end(),
80 [](const sg4::Link* a, const sg4::Link* b) { return a->get_name() < b->get_name(); });
82 for (auto link : links) {
83 std::printf(" <link id=\"");
85 std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->get_cname(), link->get_bandwidth(),
87 if (link->is_shared()) {
90 std::printf(" sharing_policy=\"FATPIPE\"/>\n");
95 static void dump_routers()
97 std::vector<simgrid::kernel::routing::NetPoint*> netpoints = 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();
103 for (auto const& src : netpoints)
104 if (src->is_router())
105 std::printf(" <router id=\"%s\"/>\n", src->get_cname());
108 static void dump_routes()
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 = 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();
119 for (auto src_host : hosts) { // Routes from host
120 const simgrid::kernel::routing::NetPoint* src = src_host->get_netpoint();
121 for (auto dst_host : hosts) { // Routes to host
122 std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
123 const simgrid::kernel::routing::NetPoint* dst = dst_host->get_netpoint();
124 simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
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");
133 for (auto const& dst : netpoints) { // to router
134 if (not dst->is_router())
136 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", src_host->get_cname(), dst->get_cname());
137 std::vector<simgrid::kernel::resource::StandardLinkImpl*> 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");
145 for (auto const& value1 : netpoints) { // Routes from router
146 if (not value1->is_router())
148 for (auto const& value2 : netpoints) { // to router
149 if (not value2->is_router())
151 std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
152 simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
155 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", value1->get_cname(), value2->get_cname());
156 for (auto const& link : route)
157 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
158 std::printf("\n </route>\n");
160 for (auto dst_host : hosts) { // Routes to host
161 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", value1->get_cname(), dst_host->get_cname());
162 std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
163 const simgrid::kernel::routing::NetPoint* netcardDst = dst_host->get_netpoint();
164 simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
165 for (auto const& link : route)
166 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
167 std::printf("\n </route>\n");
172 static void dump_platform()
176 std::printf("<?xml version='1.0'?>\n");
177 std::printf("<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n");
178 std::printf("<platform version=\"%d\">\n", version);
179 std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
193 std::printf("</AS>\n");
194 std::printf("</platform>\n");
197 int main(int argc, char** argv)
199 char* platformFile = nullptr;
202 xbt_os_timer_t parse_time = xbt_os_timer_new();
204 sg4::Engine e(&argc, argv);
206 xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv) && platformFile,
207 "Invalid command line arguments: expected [--timings] platformFile");
209 XBT_DEBUG("%d,%s", timings, platformFile);
211 create_environment(parse_time, platformFile);
214 XBT_INFO("Parsing time: %fs (%zu hosts, %zu links)", xbt_os_timer_elapsed(parse_time), e.get_host_count(),
220 xbt_os_timer_free(parse_time);