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"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
19 static int name_compare_hosts(const void *n1, const void *n2)
21 return std::strcmp(sg_host_get_name(*(sg_host_t *) n1), sg_host_get_name(*(sg_host_t *) n2));
24 static int name_compare_links(const void *n1, const void *n2)
26 return std::strcmp(sg_link_name(*(SD_link_t *) n1),sg_link_name(*(SD_link_t *) n2));
29 static bool parse_cmdline(int* timings, char** platformFile, int argc, char** argv)
32 for (int i = 1; i < argc; i++) {
33 if (std::strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
34 if (not std::strcmp(argv[i], "--timings")) {
41 *platformFile = argv[i];
47 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
50 xbt_os_cputimer_start(parse_time);
51 SD_create_environment(platformFile);
52 xbt_os_cputimer_stop(parse_time);
54 catch (std::exception& e) {
55 xbt_die("Error while loading %s: %s", platformFile, e.what());
59 static void dump_platform()
62 xbt_dict_t props = nullptr;
63 xbt_dict_cursor_t cursor = nullptr;
67 std::printf("<?xml version='1.0'?>\n");
68 std::printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\">\n");
69 std::printf("<platform version=\"%d\">\n", version);
70 std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
73 unsigned int totalHosts = sg_host_count();
74 sg_host_t* hosts = sg_host_list();
75 std::qsort((void*)hosts, totalHosts, sizeof(sg_host_t), name_compare_hosts);
77 for (unsigned int i = 0; i < totalHosts; i++) {
78 std::printf(" <host id=\"%s\" speed=\"%.0f\"", hosts[i]->cname(), sg_host_speed(hosts[i]));
79 props = sg_host_get_properties(hosts[i]);
80 if (hosts[i]->coreCount() > 1) {
81 std::printf(" core=\"%d\"", hosts[i]->coreCount());
83 if (props && not xbt_dict_is_empty(props)) {
85 xbt_dict_foreach (props, cursor, key, data) {
86 std::printf(" <prop id=\"%s\" value=\"%s\"/>\n", key, data);
88 std::printf(" </host>\n");
95 std::vector<simgrid::kernel::routing::NetPoint*> netcardList;
96 simgrid::s4u::Engine::instance()->netpointList(&netcardList);
97 std::sort(netcardList.begin(), netcardList.end(),
98 [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
99 return a->name() < b->name();
102 for (auto srcCard : netcardList)
103 if (srcCard->isRouter())
104 std::printf(" <router id=\"%s\"/>\n", srcCard->cname());
107 unsigned int totalLinks = sg_link_count();
108 simgrid::s4u::Link** links = sg_link_list();
110 std::qsort((void*)links, totalLinks, sizeof(SD_link_t), name_compare_links);
112 for (unsigned int i = 0; i < totalLinks; i++) {
113 simgrid::s4u::Link* link = links[i];
114 std::printf(" <link id=\"");
116 std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->name(), link->bandwidth(), link->latency());
117 if (sg_link_is_shared(link)) {
120 std::printf(" sharing_policy=\"FATPIPE\"/>\n");
124 for (unsigned int it_src = 0; it_src < totalHosts; it_src++) { // Routes from host
125 simgrid::s4u::Host* host1 = hosts[it_src];
126 simgrid::kernel::routing::NetPoint* netcardSrc = host1->pimpl_netpoint;
127 for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
128 simgrid::s4u::Host* host2 = hosts[it_dst];
129 std::vector<simgrid::surf::LinkImpl*> route;
130 simgrid::kernel::routing::NetPoint* netcardDst = host2->pimpl_netpoint;
131 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(netcardSrc, netcardDst, &route, nullptr);
132 if (not route.empty()) {
133 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", host1->cname(), host2->cname());
134 for (auto link : route)
135 std::printf("<link_ctn id=\"%s\"/>", link->cname());
136 std::printf("\n </route>\n");
139 for (auto netcardDst : netcardList) { // to router
140 if (netcardDst->isRouter()) {
141 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", host1->cname(), netcardDst->cname());
142 std::vector<simgrid::surf::LinkImpl*> route;
143 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(netcardSrc, netcardDst, &route, nullptr);
144 for (auto link : route)
145 std::printf("<link_ctn id=\"%s\"/>", link->cname());
146 std::printf("\n </route>\n");
151 for (auto value1 : netcardList) { // Routes from router
152 if (value1->isRouter()) {
153 for (auto value2 : netcardList) { // to router
154 if (value2->isRouter()) {
155 std::printf(" <route src=\"%s\" dst=\"%s\">\n ", value1->cname(), value2->cname());
156 std::vector<simgrid::surf::LinkImpl*> route;
157 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, &route, nullptr);
158 for (auto link : route)
159 std::printf("<link_ctn id=\"%s\"/>", link->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->cname(), host2->cname());
166 std::vector<simgrid::surf::LinkImpl*> route;
167 simgrid::kernel::routing::NetPoint* netcardDst = host2->pimpl_netpoint;
168 simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, netcardDst, &route, nullptr);
169 for (auto link : route)
170 std::printf("<link_ctn id=\"%s\"/>", link->cname());
171 std::printf("\n </route>\n");
176 std::printf("</AS>\n");
177 std::printf("</platform>\n");
182 int main(int argc, char** argv)
184 char* platformFile = nullptr;
187 xbt_os_timer_t parse_time = xbt_os_timer_new();
189 SD_init(&argc, argv);
191 xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv) && platformFile,
192 "Invalid command line arguments: expected [--timings] platformFile");
194 XBT_DEBUG("%d,%s", timings, platformFile);
196 create_environment(parse_time, platformFile);
199 XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time), sg_host_count(),
206 xbt_os_timer_free(parse_time);