1 /* Copyright (c) 2008-2021. 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/surf/network_interface.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 xbt_os_cputimer_stop(parse_time);
46 static void dump_hosts()
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(); });
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());
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()) {
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");
74 static void dump_links()
76 std::vector<sg4::Link*> links = sg4::Engine::get_instance()->get_all_links();
78 std::sort(links.begin(), links.end(),
79 [](const sg4::Link* a, const sg4::Link* b) { return a->get_name() < b->get_name(); });
81 for (auto link : links) {
82 std::printf(" <link id=\"");
84 std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->get_cname(), link->get_bandwidth(),
86 if (link->is_shared()) {
89 std::printf(" sharing_policy=\"FATPIPE\"/>\n");
94 static void dump_routers()
96 std::vector<simgrid::kernel::routing::NetPoint*> netpoints = 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();
102 for (auto const& src : netpoints)
103 if (src->is_router())
104 std::printf(" <router id=\"%s\"/>\n", src->get_cname());
107 static void dump_routes()
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 = sg4::Engine::get_instance()->get_all_netpoints();
113 std::sort(netpoints.begin(), netpoints.end(),
114 [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
115 return a->get_name() < b->get_name();
118 for (auto src_host : hosts) { // Routes from host
119 const simgrid::kernel::routing::NetPoint* src = src_host->get_netpoint();
120 for (auto dst_host : hosts) { // Routes to host
121 std::vector<simgrid::kernel::resource::LinkImpl*> route;
122 const simgrid::kernel::routing::NetPoint* dst = dst_host->get_netpoint();
123 simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
126 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", src_host->get_cname(), dst_host->get_cname());
127 for (auto const& link : route)
128 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
129 std::printf("\n </route>\n");
132 for (auto const& dst : netpoints) { // to router
133 if (not dst->is_router())
135 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", src_host->get_cname(), dst->get_cname());
136 std::vector<simgrid::kernel::resource::LinkImpl*> route;
137 simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
138 for (auto const& link : route)
139 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
140 std::printf("\n </route>\n");
144 for (auto const& value1 : netpoints) { // Routes from router
145 if (not value1->is_router())
147 for (auto const& value2 : netpoints) { // to router
148 if (not value2->is_router())
150 std::vector<simgrid::kernel::resource::LinkImpl*> route;
151 simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
154 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", value1->get_cname(), value2->get_cname());
155 for (auto const& link : route)
156 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
157 std::printf("\n </route>\n");
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 const 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");
171 static void dump_platform()
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");
192 std::printf("</AS>\n");
193 std::printf("</platform>\n");
196 int main(int argc, char** argv)
198 char* platformFile = nullptr;
201 xbt_os_timer_t parse_time = xbt_os_timer_new();
203 sg4::Engine e(&argc, argv);
205 xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv) && platformFile,
206 "Invalid command line arguments: expected [--timings] platformFile");
208 XBT_DEBUG("%d,%s", timings, platformFile);
210 create_environment(parse_time, platformFile);
213 XBT_INFO("Parsing time: %fs (%zu hosts, %zu links)", xbt_os_timer_elapsed(parse_time), e.get_host_count(),
219 xbt_os_timer_free(parse_time);