Logo AND Algorithmique Numérique Distribuée

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