1 /* Copyright (c) 2008-2019. 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/simdag.h"
12 #include "src/surf/network_interface.hpp"
16 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
18 static bool parse_cmdline(int* timings, char** platformFile, int argc, char** argv)
21 for (int i = 1; i < argc; i++) {
22 if (std::strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
23 if (not std::strcmp(argv[i], "--timings")) {
30 *platformFile = argv[i];
36 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
38 xbt_os_cputimer_start(parse_time);
39 SD_create_environment(platformFile);
40 xbt_os_cputimer_stop(parse_time);
43 static void dump_hosts()
45 unsigned int totalHosts = sg_host_count();
46 sg_host_t* hosts = sg_host_list();
47 std::sort(hosts, hosts + totalHosts,
48 [](sg_host_t a, sg_host_t b) { return strcmp(sg_host_get_name(a), sg_host_get_name(b)) < 0; });
50 for (unsigned int i = 0; i < totalHosts; i++) {
51 std::printf(" <host id=\"%s\" speed=\"%.0f\"", hosts[i]->get_cname(), sg_host_speed(hosts[i]));
52 const std::unordered_map<std::string, std::string>* props = hosts[i]->get_properties();
53 if (hosts[i]->get_core_count() > 1) {
54 std::printf(" core=\"%d\"", hosts[i]->get_core_count());
56 // Sort the properties before displaying them, so that the tests are perfectly reproducible
57 std::vector<std::string> keys;
58 for (auto const& kv : *props)
59 keys.push_back(kv.first);
60 if (not keys.empty()) {
62 std::sort(keys.begin(), keys.end());
63 for (const std::string& key : keys)
64 std::printf(" <prop id=\"%s\" value=\"%s\"/>\n", key.c_str(), props->at(key).c_str());
65 std::printf(" </host>\n");
73 static void dump_links()
75 unsigned int totalLinks = sg_link_count();
76 simgrid::s4u::Link** links = sg_link_list();
78 std::sort(links, links + totalLinks,
79 [](simgrid::s4u::Link* a, simgrid::s4u::Link* b) { return strcmp(sg_link_name(a), sg_link_name(b)) < 0; });
81 for (unsigned int i = 0; i < totalLinks; i++) {
82 simgrid::s4u::Link* link = links[i];
83 std::printf(" <link id=\"");
85 std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->get_cname(), link->get_bandwidth(), link->get_latency());
86 if (sg_link_is_shared(link)) {
89 std::printf(" sharing_policy=\"FATPIPE\"/>\n");
96 static void dump_routers()
98 std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
99 simgrid::s4u::Engine::get_instance()->get_all_netpoints();
100 std::sort(netpoints.begin(), netpoints.end(),
101 [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
102 return a->get_name() < b->get_name();
105 for (auto const& src : netpoints)
106 if (src->is_router())
107 std::printf(" <router id=\"%s\"/>\n", src->get_cname());
110 static void dump_routes()
112 unsigned int totalHosts = sg_host_count();
113 sg_host_t* hosts = sg_host_list();
114 std::sort(hosts, hosts + totalHosts,
115 [](sg_host_t a, sg_host_t b) { return strcmp(sg_host_get_name(a), sg_host_get_name(b)) < 0; });
116 std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
117 simgrid::s4u::Engine::get_instance()->get_all_netpoints();
118 std::sort(netpoints.begin(), netpoints.end(),
119 [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
120 return a->get_name() < b->get_name();
123 for (unsigned int it_src = 0; it_src < totalHosts; it_src++) { // Routes from host
124 simgrid::s4u::Host* host1 = hosts[it_src];
125 simgrid::kernel::routing::NetPoint* src = host1->pimpl_netpoint;
126 for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
127 simgrid::s4u::Host* host2 = hosts[it_dst];
128 std::vector<simgrid::kernel::resource::LinkImpl*> route;
129 simgrid::kernel::routing::NetPoint* dst = host2->pimpl_netpoint;
130 simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
131 if (not route.empty()) {
132 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", host1->get_cname(), host2->get_cname());
133 for (auto const& link : route)
134 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
135 std::printf("\n </route>\n");
139 for (auto const& dst : netpoints) { // to router
140 if (dst->is_router()) {
141 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", host1->get_cname(), dst->get_cname());
142 std::vector<simgrid::kernel::resource::LinkImpl*> route;
143 simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
144 for (auto const& link : route)
145 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
146 std::printf("\n </route>\n");
151 for (auto const& value1 : netpoints) { // Routes from router
152 if (value1->is_router()) {
153 for (auto const& value2 : netpoints) { // to router
154 if (value2->is_router()) {
155 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", value1->get_cname(), value2->get_cname());
156 std::vector<simgrid::kernel::resource::LinkImpl*> route;
157 simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
158 for (auto const& link : route)
159 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
160 std::printf("\n </route>\n");
163 for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
164 simgrid::s4u::Host* host2 = hosts[it_dst];
165 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", value1->get_cname(), host2->get_cname());
166 std::vector<simgrid::kernel::resource::LinkImpl*> route;
167 simgrid::kernel::routing::NetPoint* netcardDst = host2->pimpl_netpoint;
168 simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
169 for (auto const& link : route)
170 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
171 std::printf("\n </route>\n");
178 static void dump_platform()
182 std::printf("<?xml version='1.0'?>\n");
183 std::printf("<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n");
184 std::printf("<platform version=\"%d\">\n", version);
185 std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
199 std::printf("</AS>\n");
200 std::printf("</platform>\n");
203 int main(int argc, char** argv)
205 char* platformFile = nullptr;
208 xbt_os_timer_t parse_time = xbt_os_timer_new();
210 SD_init(&argc, argv);
212 if (not parse_cmdline(&timings, &platformFile, argc, argv) || not platformFile)
213 xbt_die("Invalid command line arguments: expected [--timings] platformFile");
215 XBT_DEBUG("%d,%s", timings, platformFile);
217 create_environment(parse_time, platformFile);
220 XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time), sg_host_count(),
226 xbt_os_timer_free(parse_time);