Logo AND Algorithmique Numérique Distribuée

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