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/simdag.h"
12 #include "src/surf/network_interface.hpp"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
19 static bool parse_cmdline(int* timings, char** platformFile, int argc, char** argv)
22 for (int i = 1; i < argc; i++) {
23 if (std::strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
24 if (not std::strcmp(argv[i], "--timings")) {
31 *platformFile = argv[i];
37 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
39 xbt_os_cputimer_start(parse_time);
40 SD_create_environment(platformFile);
41 xbt_os_cputimer_stop(parse_time);
44 static void dump_hosts()
46 size_t totalHosts = sg_host_count();
47 sg_host_t* hosts = sg_host_list();
48 std::sort(hosts, hosts + totalHosts,
49 [](const_sg_host_t a, const_sg_host_t b) { return strcmp(sg_host_get_name(a), sg_host_get_name(b)) < 0; });
51 for (size_t i = 0; i < totalHosts; i++) {
52 std::printf(" <host id=\"%s\" speed=\"%.0f\"", hosts[i]->get_cname(), sg_host_get_speed(hosts[i]));
53 const std::unordered_map<std::string, std::string>* props = hosts[i]->get_properties();
54 if (hosts[i]->get_core_count() > 1) {
55 std::printf(" core=\"%d\"", hosts[i]->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& kv : *props)
60 keys.push_back(kv.first);
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");
74 static void dump_links()
76 size_t totalLinks = sg_link_count();
77 simgrid::s4u::Link** links = sg_link_list();
79 std::sort(links, links + totalLinks, [](const simgrid::s4u::Link* a, const simgrid::s4u::Link* b) {
80 return strcmp(sg_link_get_name(a), sg_link_get_name(b)) < 0;
83 for (size_t i = 0; i < totalLinks; i++) {
84 const simgrid::s4u::Link* link = links[i];
85 std::printf(" <link id=\"");
87 std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->get_cname(), link->get_bandwidth(), link->get_latency());
88 if (sg_link_is_shared(link)) {
91 std::printf(" sharing_policy=\"FATPIPE\"/>\n");
98 static void dump_routers()
100 std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
101 simgrid::s4u::Engine::get_instance()->get_all_netpoints();
102 std::sort(netpoints.begin(), netpoints.end(),
103 [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
104 return a->get_name() < b->get_name();
107 for (auto const& src : netpoints)
108 if (src->is_router())
109 std::printf(" <router id=\"%s\"/>\n", src->get_cname());
112 static void dump_routes()
114 size_t totalHosts = sg_host_count();
115 sg_host_t* hosts = sg_host_list();
116 std::sort(hosts, hosts + totalHosts,
117 [](const_sg_host_t a, const_sg_host_t b) { return strcmp(sg_host_get_name(a), sg_host_get_name(b)) < 0; });
118 std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
119 simgrid::s4u::Engine::get_instance()->get_all_netpoints();
120 std::sort(netpoints.begin(), netpoints.end(),
121 [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
122 return a->get_name() < b->get_name();
125 for (size_t it_src = 0; it_src < totalHosts; it_src++) { // Routes from host
126 const simgrid::s4u::Host* host1 = hosts[it_src];
127 simgrid::kernel::routing::NetPoint* src = host1->get_netpoint();
128 for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
129 const simgrid::s4u::Host* host2 = hosts[it_dst];
130 std::vector<simgrid::kernel::resource::LinkImpl*> route;
131 simgrid::kernel::routing::NetPoint* dst = host2->get_netpoint();
132 simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
135 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", host1->get_cname(), host2->get_cname());
136 for (auto const& link : route)
137 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
138 std::printf("\n </route>\n");
141 for (auto const& dst : netpoints) { // to router
142 if (not dst->is_router())
144 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", host1->get_cname(), dst->get_cname());
145 std::vector<simgrid::kernel::resource::LinkImpl*> route;
146 simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
147 for (auto const& link : route)
148 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
149 std::printf("\n </route>\n");
153 for (auto const& value1 : netpoints) { // Routes from router
154 if (not value1->is_router())
156 for (auto const& value2 : netpoints) { // to router
157 if (not value2->is_router())
159 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", value1->get_cname(), value2->get_cname());
160 std::vector<simgrid::kernel::resource::LinkImpl*> route;
161 simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
162 for (auto const& link : route)
163 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
164 std::printf("\n </route>\n");
166 for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
167 const simgrid::s4u::Host* host2 = hosts[it_dst];
168 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", value1->get_cname(), host2->get_cname());
169 std::vector<simgrid::kernel::resource::LinkImpl*> route;
170 simgrid::kernel::routing::NetPoint* netcardDst = host2->get_netpoint();
171 simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
172 for (auto const& link : route)
173 std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
174 std::printf("\n </route>\n");
180 static void dump_platform()
184 std::printf("<?xml version='1.0'?>\n");
185 std::printf("<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n");
186 std::printf("<platform version=\"%d\">\n", version);
187 std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
201 std::printf("</AS>\n");
202 std::printf("</platform>\n");
205 int main(int argc, char** argv)
207 char* platformFile = nullptr;
210 xbt_os_timer_t parse_time = xbt_os_timer_new();
212 SD_init(&argc, argv);
214 if (not parse_cmdline(&timings, &platformFile, argc, argv) || not platformFile)
215 xbt_die("Invalid command line arguments: expected [--timings] platformFile");
217 XBT_DEBUG("%d,%s", timings, platformFile);
219 create_environment(parse_time, platformFile);
222 XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time), sg_host_count(),
228 xbt_os_timer_free(parse_time);