1 /* Copyright (c) 2008-2017. 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/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
11 #include "simgrid/simdag.h"
13 #include "src/kernel/routing/NetPoint.hpp"
14 #include "src/surf/network_interface.hpp"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
20 static bool parse_cmdline(int* timings, char** platformFile, int argc, char** argv)
23 for (int i = 1; i < argc; i++) {
24 if (std::strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
25 if (not std::strcmp(argv[i], "--timings")) {
32 *platformFile = argv[i];
38 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
41 xbt_os_cputimer_start(parse_time);
42 SD_create_environment(platformFile);
43 xbt_os_cputimer_stop(parse_time);
45 catch (std::exception& e) {
46 xbt_die("Error while loading %s: %s", platformFile, e.what());
50 static void dump_platform()
53 std::map<std::string, std::string>* props = nullptr;
55 std::printf("<?xml version='1.0'?>\n");
56 std::printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\">\n");
57 std::printf("<platform version=\"%d\">\n", version);
58 std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
61 unsigned int totalHosts = sg_host_count();
62 sg_host_t* hosts = sg_host_list();
63 std::sort(hosts, hosts + totalHosts,
64 [](sg_host_t a, sg_host_t b) { return strcmp(sg_host_get_name(a), sg_host_get_name(b)) < 0; });
66 for (unsigned int i = 0; i < totalHosts; i++) {
67 std::printf(" <host id=\"%s\" speed=\"%.0f\"", hosts[i]->getCname(), sg_host_speed(hosts[i]));
68 props = hosts[i]->getProperties();
69 if (hosts[i]->getCoreCount() > 1) {
70 std::printf(" core=\"%d\"", hosts[i]->getCoreCount());
72 if (props && not props->empty()) {
74 for (auto const& kv : *props) {
75 std::printf(" <prop id=\"%s\" value=\"%s\"/>\n", kv.first.c_str(), kv.second.c_str());
77 std::printf(" </host>\n");
84 std::vector<simgrid::kernel::routing::NetPoint*> netcardList;
85 simgrid::s4u::Engine::getInstance()->getNetpointList(&netcardList);
86 std::sort(netcardList.begin(), netcardList.end(),
87 [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
88 return a->name() < b->name();
91 for (auto const& srcCard : netcardList)
92 if (srcCard->isRouter())
93 std::printf(" <router id=\"%s\"/>\n", srcCard->cname());
96 unsigned int totalLinks = sg_link_count();
97 simgrid::s4u::Link** links = sg_link_list();
99 std::sort(links, links + totalLinks,
100 [](simgrid::s4u::Link* a, simgrid::s4u::Link* b) { return strcmp(sg_link_name(a), sg_link_name(b)) < 0; });
102 for (unsigned int i = 0; i < totalLinks; i++) {
103 simgrid::s4u::Link* link = links[i];
104 std::printf(" <link id=\"");
106 std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->name(), link->bandwidth(), link->latency());
107 if (sg_link_is_shared(link)) {
110 std::printf(" sharing_policy=\"FATPIPE\"/>\n");
114 for (unsigned int it_src = 0; it_src < totalHosts; it_src++) { // Routes from host
115 simgrid::s4u::Host* host1 = hosts[it_src];
116 simgrid::kernel::routing::NetPoint* netcardSrc = host1->pimpl_netpoint;
117 for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
118 simgrid::s4u::Host* host2 = hosts[it_dst];
119 std::vector<simgrid::surf::LinkImpl*> route;
120 simgrid::kernel::routing::NetPoint* netcardDst = host2->pimpl_netpoint;
121 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(netcardSrc, netcardDst, &route, nullptr);
122 if (not route.empty()) {
123 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", host1->getCname(), host2->getCname());
124 for (auto const& link : route)
125 std::printf("<link_ctn id=\"%s\"/>", link->cname());
126 std::printf("\n </route>\n");
129 for (auto const& netcardDst : netcardList) { // to router
130 if (netcardDst->isRouter()) {
131 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", host1->getCname(), netcardDst->cname());
132 std::vector<simgrid::surf::LinkImpl*> route;
133 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(netcardSrc, netcardDst, &route, nullptr);
134 for (auto const& link : route)
135 std::printf("<link_ctn id=\"%s\"/>", link->cname());
136 std::printf("\n </route>\n");
141 for (auto const& value1 : netcardList) { // Routes from router
142 if (value1->isRouter()) {
143 for (auto const& value2 : netcardList) { // to router
144 if (value2->isRouter()) {
145 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", value1->cname(), value2->cname());
146 std::vector<simgrid::surf::LinkImpl*> route;
147 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, &route, nullptr);
148 for (auto const& link : route)
149 std::printf("<link_ctn id=\"%s\"/>", link->cname());
150 std::printf("\n </route>\n");
153 for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
154 simgrid::s4u::Host* host2 = hosts[it_dst];
155 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", value1->cname(), host2->getCname());
156 std::vector<simgrid::surf::LinkImpl*> route;
157 simgrid::kernel::routing::NetPoint* netcardDst = host2->pimpl_netpoint;
158 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, netcardDst, &route, nullptr);
159 for (auto const& link : route)
160 std::printf("<link_ctn id=\"%s\"/>", link->cname());
161 std::printf("\n </route>\n");
166 std::printf("</AS>\n");
167 std::printf("</platform>\n");
172 int main(int argc, char** argv)
174 char* platformFile = nullptr;
177 xbt_os_timer_t parse_time = xbt_os_timer_new();
179 SD_init(&argc, argv);
181 xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv) && platformFile,
182 "Invalid command line arguments: expected [--timings] platformFile");
184 XBT_DEBUG("%d,%s", timings, platformFile);
186 create_environment(parse_time, platformFile);
189 XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time), sg_host_count(),
195 xbt_os_timer_free(parse_time);