Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Hide std::free to Sonar.
[simgrid.git] / teshsuite / simdag / flatifier / flatifier.cpp
1 /* Copyright (c) 2008-2019. 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   xbt_os_cputimer_start(parse_time);
39   SD_create_environment(platformFile);
40   xbt_os_cputimer_stop(parse_time);
41 }
42
43 static void dump_hosts()
44 {
45   std::unordered_map<std::string, std::string>* props = nullptr;
46   unsigned int totalHosts = sg_host_count();
47   sg_host_t* hosts        = sg_host_list();
48   std::sort(hosts, hosts + totalHosts,
49             [](sg_host_t a, sg_host_t b) { return strcmp(sg_host_get_name(a), sg_host_get_name(b)) < 0; });
50
51   for (unsigned int i = 0; i < totalHosts; i++) {
52     std::printf("  <host id=\"%s\" speed=\"%.0f\"", hosts[i]->get_cname(), sg_host_speed(hosts[i]));
53     props = hosts[i]->get_properties();
54     if (hosts[i]->get_core_count() > 1) {
55       std::printf(" core=\"%d\"", hosts[i]->get_core_count());
56     }
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()) {
62       std::printf(">\n");
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");
67     } else {
68       std::printf("/>\n");
69     }
70   }
71   xbt_free(hosts);
72 }
73
74 static void dump_links()
75 {
76   unsigned int totalLinks    = sg_link_count();
77   simgrid::s4u::Link** links = sg_link_list();
78
79   std::sort(links, links + totalLinks,
80             [](simgrid::s4u::Link* a, simgrid::s4u::Link* b) { return strcmp(sg_link_name(a), sg_link_name(b)) < 0; });
81
82   for (unsigned int i = 0; i < totalLinks; i++) {
83     simgrid::s4u::Link* link = links[i];
84     std::printf("  <link id=\"");
85
86     std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->get_cname(), link->get_bandwidth(), link->get_latency());
87     if (sg_link_is_shared(link)) {
88       std::printf("/>\n");
89     } else {
90       std::printf(" sharing_policy=\"FATPIPE\"/>\n");
91     }
92   }
93
94   xbt_free(links);
95 }
96
97 static void dump_routers()
98 {
99   std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
100       simgrid::s4u::Engine::get_instance()->get_all_netpoints();
101   std::sort(netpoints.begin(), netpoints.end(),
102             [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
103               return a->get_name() < b->get_name();
104             });
105
106   for (auto const& src : netpoints)
107     if (src->is_router())
108       std::printf("  <router id=\"%s\"/>\n", src->get_cname());
109 }
110
111 static void dump_routes()
112 {
113   unsigned int totalHosts = sg_host_count();
114   sg_host_t* hosts        = sg_host_list();
115   std::sort(hosts, hosts + totalHosts,
116             [](sg_host_t a, sg_host_t b) { return strcmp(sg_host_get_name(a), sg_host_get_name(b)) < 0; });
117   std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
118       simgrid::s4u::Engine::get_instance()->get_all_netpoints();
119   std::sort(netpoints.begin(), netpoints.end(),
120             [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
121               return a->get_name() < b->get_name();
122             });
123
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* src = 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::kernel::resource::LinkImpl*> route;
130       simgrid::kernel::routing::NetPoint* dst = host2->pimpl_netpoint;
131       simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
132       if (not route.empty()) {
133         std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", host1->get_cname(), host2->get_cname());
134         for (auto const& link : route)
135           std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
136         std::printf("\n  </route>\n");
137       }
138     }
139
140     for (auto const& dst : netpoints) { // to router
141       if (dst->is_router()) {
142         std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", host1->get_cname(), dst->get_cname());
143         std::vector<simgrid::kernel::resource::LinkImpl*> route;
144         simgrid::kernel::routing::NetZoneImpl::get_global_route(src, dst, route, nullptr);
145         for (auto const& link : route)
146           std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
147         std::printf("\n  </route>\n");
148       }
149     }
150   }
151
152   for (auto const& value1 : netpoints) { // Routes from router
153     if (value1->is_router()) {
154       for (auto const& value2 : netpoints) { // to router
155         if (value2->is_router()) {
156           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->get_cname(), value2->get_cname());
157           std::vector<simgrid::kernel::resource::LinkImpl*> route;
158           simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, value2, route, nullptr);
159           for (auto const& link : route)
160             std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
161           std::printf("\n  </route>\n");
162         }
163       }
164       for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
165         simgrid::s4u::Host* host2 = hosts[it_dst];
166         std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->get_cname(), host2->get_cname());
167         std::vector<simgrid::kernel::resource::LinkImpl*> route;
168         simgrid::kernel::routing::NetPoint* netcardDst = host2->pimpl_netpoint;
169         simgrid::kernel::routing::NetZoneImpl::get_global_route(value1, netcardDst, route, nullptr);
170         for (auto const& link : route)
171           std::printf("<link_ctn id=\"%s\"/>", link->get_cname());
172         std::printf("\n  </route>\n");
173       }
174     }
175   }
176   xbt_free(hosts);
177 }
178
179 static void dump_platform()
180 {
181   int version = 4;
182
183   std::printf("<?xml version='1.0'?>\n");
184   std::printf("<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">\n");
185   std::printf("<platform version=\"%d\">\n", version);
186   std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
187
188   // Hosts
189   dump_hosts();
190
191   // Routers
192   dump_routers();
193
194   // Links
195   dump_links();
196
197   // Routes
198   dump_routes();
199
200   std::printf("</AS>\n");
201   std::printf("</platform>\n");
202 }
203
204 int main(int argc, char** argv)
205 {
206   char* platformFile = nullptr;
207   int timings        = 0;
208
209   xbt_os_timer_t parse_time = xbt_os_timer_new();
210
211   SD_init(&argc, argv);
212
213   xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv) && platformFile,
214              "Invalid command line arguments: expected [--timings] platformFile");
215
216   XBT_DEBUG("%d,%s", timings, platformFile);
217
218   create_environment(parse_time, platformFile);
219
220   if (timings) {
221     XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time), sg_host_count(),
222              sg_link_count());
223   } else {
224     dump_platform();
225   }
226
227   xbt_os_timer_free(parse_time);
228
229   return 0;
230 }