Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cd94fdc94b3e29e753b92194a86ccee86dd4d16f
[simgrid.git] / teshsuite / simdag / flatifier / flatifier.cpp
1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <xbt/xbt_os_time.h>
7
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
10
11 #include "simgrid/simdag.h"
12
13 #include "src/kernel/routing/NetPoint.hpp"
14 #include "src/surf/network_interface.hpp"
15
16 #include <algorithm>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
19
20 static bool parse_cmdline(int* timings, char** platformFile, int argc, char** argv)
21 {
22   bool parse_ok = true;
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")) {
26         *timings = 1;
27       } else {
28         parse_ok = false;
29         break;
30       }
31     } else {
32       *platformFile = argv[i];
33     }
34   }
35   return parse_ok;
36 }
37
38 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
39 {
40   try {
41     xbt_os_cputimer_start(parse_time);
42     SD_create_environment(platformFile);
43     xbt_os_cputimer_stop(parse_time);
44   }
45   catch (std::exception& e) {
46     xbt_die("Error while loading %s: %s", platformFile, e.what());
47   }
48 }
49
50 static void dump_hosts()
51 {
52   std::map<std::string, std::string>* props = nullptr;
53   unsigned int totalHosts = sg_host_count();
54   sg_host_t* hosts        = sg_host_list();
55   std::sort(hosts, hosts + totalHosts,
56             [](sg_host_t a, sg_host_t b) { return strcmp(sg_host_get_name(a), sg_host_get_name(b)) < 0; });
57
58   for (unsigned int i = 0; i < totalHosts; i++) {
59     std::printf("  <host id=\"%s\" speed=\"%.0f\"", hosts[i]->getCname(), sg_host_speed(hosts[i]));
60     props = hosts[i]->getProperties();
61     if (hosts[i]->getCoreCount() > 1) {
62       std::printf(" core=\"%d\"", hosts[i]->getCoreCount());
63     }
64     if (props && not props->empty()) {
65       std::printf(">\n");
66       for (auto const& kv : *props) {
67         std::printf("    <prop id=\"%s\" value=\"%s\"/>\n", kv.first.c_str(), kv.second.c_str());
68       }
69       std::printf("  </host>\n");
70     } else {
71       std::printf("/>\n");
72     }
73   }
74   std::free(hosts);
75 }
76
77 static void dump_links()
78 {
79   unsigned int totalLinks    = sg_link_count();
80   simgrid::s4u::Link** links = sg_link_list();
81
82   std::sort(links, links + totalLinks,
83             [](simgrid::s4u::Link* a, simgrid::s4u::Link* b) { return strcmp(sg_link_name(a), sg_link_name(b)) < 0; });
84
85   for (unsigned int i = 0; i < totalLinks; i++) {
86     simgrid::s4u::Link* link = links[i];
87     std::printf("  <link id=\"");
88
89     std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->getCname(), link->bandwidth(), link->latency());
90     if (sg_link_is_shared(link)) {
91       std::printf("/>\n");
92     } else {
93       std::printf(" sharing_policy=\"FATPIPE\"/>\n");
94     }
95   }
96
97   std::free(links);
98 }
99
100 static void dump_routers()
101 {
102   std::vector<simgrid::kernel::routing::NetPoint*> netcardList;
103   simgrid::s4u::Engine::getInstance()->getNetpointList(&netcardList);
104   std::sort(netcardList.begin(), netcardList.end(),
105             [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
106               return a->getName() < b->getName();
107             });
108
109   for (auto const& srcCard : netcardList)
110     if (srcCard->isRouter())
111       std::printf("  <router id=\"%s\"/>\n", srcCard->getCname());
112 }
113
114 static void dump_routes()
115 {
116   unsigned int totalHosts = sg_host_count();
117   sg_host_t* hosts        = sg_host_list();
118   std::sort(hosts, hosts + totalHosts,
119             [](sg_host_t a, sg_host_t b) { return strcmp(sg_host_get_name(a), sg_host_get_name(b)) < 0; });
120   std::vector<simgrid::kernel::routing::NetPoint*> netcardList;
121   simgrid::s4u::Engine::getInstance()->getNetpointList(&netcardList);
122   std::sort(netcardList.begin(), netcardList.end(),
123             [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
124               return a->getName() < b->getName();
125             });
126
127   for (unsigned int it_src = 0; it_src < totalHosts; it_src++) { // Routes from host
128     simgrid::s4u::Host* host1                      = hosts[it_src];
129     simgrid::kernel::routing::NetPoint* netcardSrc = host1->pimpl_netpoint;
130     for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
131       simgrid::s4u::Host* host2 = hosts[it_dst];
132       std::vector<simgrid::surf::LinkImpl*> route;
133       simgrid::kernel::routing::NetPoint* netcardDst = host2->pimpl_netpoint;
134       simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(netcardSrc, netcardDst, route, nullptr);
135       if (not route.empty()) {
136         std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", host1->getCname(), host2->getCname());
137         for (auto const& link : route)
138           std::printf("<link_ctn id=\"%s\"/>", link->getCname());
139         std::printf("\n  </route>\n");
140       }
141     }
142
143     for (auto const& netcardDst : netcardList) { // to router
144       if (netcardDst->isRouter()) {
145         std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", host1->getCname(), netcardDst->getCname());
146         std::vector<simgrid::surf::LinkImpl*> route;
147         simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(netcardSrc, netcardDst, route, nullptr);
148         for (auto const& link : route)
149           std::printf("<link_ctn id=\"%s\"/>", link->getCname());
150         std::printf("\n  </route>\n");
151       }
152     }
153   }
154
155   for (auto const& value1 : netcardList) { // Routes from router
156     if (value1->isRouter()) {
157       for (auto const& value2 : netcardList) { // to router
158         if (value2->isRouter()) {
159           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->getCname(), value2->getCname());
160           std::vector<simgrid::surf::LinkImpl*> route;
161           simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, route, nullptr);
162           for (auto const& link : route)
163             std::printf("<link_ctn id=\"%s\"/>", link->getCname());
164           std::printf("\n  </route>\n");
165         }
166       }
167       for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
168         simgrid::s4u::Host* host2 = hosts[it_dst];
169         std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->getCname(), host2->getCname());
170         std::vector<simgrid::surf::LinkImpl*> route;
171         simgrid::kernel::routing::NetPoint* netcardDst = host2->pimpl_netpoint;
172         simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, netcardDst, route, nullptr);
173         for (auto const& link : route)
174           std::printf("<link_ctn id=\"%s\"/>", link->getCname());
175         std::printf("\n  </route>\n");
176       }
177     }
178   }
179   std::free(hosts);
180 }
181
182 static void dump_platform()
183 {
184   int version = 4;
185
186   std::printf("<?xml version='1.0'?>\n");
187   std::printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\">\n");
188   std::printf("<platform version=\"%d\">\n", version);
189   std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
190
191   // Hosts
192   dump_hosts();
193
194   // Routers
195   dump_routers();
196
197   // Links
198   dump_links();
199
200   // Routes
201   dump_routes();
202
203   std::printf("</AS>\n");
204   std::printf("</platform>\n");
205 }
206
207 int main(int argc, char** argv)
208 {
209   char* platformFile = nullptr;
210   int timings        = 0;
211
212   xbt_os_timer_t parse_time = xbt_os_timer_new();
213
214   SD_init(&argc, argv);
215
216   xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv) && platformFile,
217              "Invalid command line arguments: expected [--timings] platformFile");
218
219   XBT_DEBUG("%d,%s", timings, platformFile);
220
221   create_environment(parse_time, platformFile);
222
223   if (timings) {
224     XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time), sg_host_count(),
225              sg_link_count());
226   } else {
227     dump_platform();
228   }
229
230   xbt_os_timer_free(parse_time);
231
232   return 0;
233 }