Logo AND Algorithmique Numérique Distribuée

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