Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tend to the google coding standards in all S4U API
[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 int name_compare_hosts(const void *n1, const void *n2)
21 {
22   return std::strcmp(sg_host_get_name(*(sg_host_t *) n1), sg_host_get_name(*(sg_host_t *) n2));
23 }
24
25 static int name_compare_links(const void *n1, const void *n2)
26 {
27   return std::strcmp(sg_link_name(*(SD_link_t *) n1),sg_link_name(*(SD_link_t *) n2));
28 }
29
30 static bool parse_cmdline(int* timings, char** platformFile, int argc, char** argv)
31 {
32   bool parse_ok = true;
33   for (int i = 1; i < argc; i++) {
34     if (std::strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
35       if (not std::strcmp(argv[i], "--timings")) {
36         *timings = 1;
37       } else {
38         parse_ok = false;
39         break;
40       }
41     } else {
42       *platformFile = argv[i];
43     }
44   }
45   return parse_ok;
46 }
47
48 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
49 {
50   try {
51     xbt_os_cputimer_start(parse_time);
52     SD_create_environment(platformFile);
53     xbt_os_cputimer_stop(parse_time);
54   }
55   catch (std::exception& e) {
56     xbt_die("Error while loading %s: %s", platformFile, e.what());
57   }
58 }
59
60 static void dump_platform()
61 {
62   int version = 4;
63   xbt_dict_t props = nullptr;
64   xbt_dict_cursor_t cursor = nullptr;
65   char* key;
66   char* data;
67
68   std::printf("<?xml version='1.0'?>\n");
69   std::printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\">\n");
70   std::printf("<platform version=\"%d\">\n", version);
71   std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
72
73   // Hosts
74   unsigned int totalHosts = sg_host_count();
75   sg_host_t* hosts        = sg_host_list();
76   std::qsort((void*)hosts, totalHosts, sizeof(sg_host_t), name_compare_hosts);
77
78   for (unsigned int i = 0; i < totalHosts; i++) {
79     std::printf("  <host id=\"%s\" speed=\"%.0f\"", hosts[i]->getCname(), sg_host_speed(hosts[i]));
80     props = sg_host_get_properties(hosts[i]);
81     if (hosts[i]->getCoreCount() > 1) {
82       std::printf(" core=\"%d\"", hosts[i]->getCoreCount());
83     }
84     if (props && not xbt_dict_is_empty(props)) {
85       std::printf(">\n");
86       xbt_dict_foreach (props, cursor, key, data) {
87         std::printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
88       }
89       std::printf("  </host>\n");
90     } else {
91       std::printf("/>\n");
92     }
93   }
94
95   // Routers
96   std::vector<simgrid::kernel::routing::NetPoint*> netcardList;
97   simgrid::s4u::Engine::getInstance()->getNetpointList(&netcardList);
98   std::sort(netcardList.begin(), netcardList.end(),
99             [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
100               return a->name() < b->name();
101             });
102
103   for (auto srcCard : netcardList)
104     if (srcCard->isRouter())
105       std::printf("  <router id=\"%s\"/>\n", srcCard->cname());
106
107   // Links
108   unsigned int totalLinks    = sg_link_count();
109   simgrid::s4u::Link** links = sg_link_list();
110
111   std::qsort((void*)links, totalLinks, sizeof(SD_link_t), name_compare_links);
112
113   for (unsigned int i = 0; i < totalLinks; i++) {
114     simgrid::s4u::Link* link = links[i];
115     std::printf("  <link id=\"");
116
117     std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", link->name(), link->bandwidth(), link->latency());
118     if (sg_link_is_shared(link)) {
119       std::printf("/>\n");
120     } else {
121       std::printf(" sharing_policy=\"FATPIPE\"/>\n");
122     }
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->getCname(), host2->getCname());
135         for (auto link : route)
136           std::printf("<link_ctn id=\"%s\"/>", link->cname());
137         std::printf("\n  </route>\n");
138       }
139     }
140     for (auto netcardDst : netcardList) { // to router
141       if (netcardDst->isRouter()) {
142         std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", host1->getCname(), netcardDst->cname());
143         std::vector<simgrid::surf::LinkImpl*> route;
144         simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(netcardSrc, netcardDst, &route, nullptr);
145         for (auto link : route)
146           std::printf("<link_ctn id=\"%s\"/>", link->cname());
147         std::printf("\n  </route>\n");
148       }
149     }
150   }
151
152   for (auto value1 : netcardList) { // Routes from router
153     if (value1->isRouter()) {
154       for (auto value2 : netcardList) { // to router
155         if (value2->isRouter()) {
156           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->cname(), value2->cname());
157           std::vector<simgrid::surf::LinkImpl*> route;
158           simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, &route, nullptr);
159           for (auto link : route)
160             std::printf("<link_ctn id=\"%s\"/>", link->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->cname(), host2->getCname());
167         std::vector<simgrid::surf::LinkImpl*> route;
168         simgrid::kernel::routing::NetPoint* netcardDst = host2->pimpl_netpoint;
169         simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, netcardDst, &route, nullptr);
170         for (auto link : route)
171           std::printf("<link_ctn id=\"%s\"/>", link->cname());
172         std::printf("\n  </route>\n");
173       }
174     }
175   }
176
177   std::printf("</AS>\n");
178   std::printf("</platform>\n");
179   std::free(hosts);
180   std::free(links);
181 }
182
183 int main(int argc, char** argv)
184 {
185   char* platformFile = nullptr;
186   int timings        = 0;
187
188   xbt_os_timer_t parse_time = xbt_os_timer_new();
189
190   SD_init(&argc, argv);
191
192   xbt_assert(parse_cmdline(&timings, &platformFile, argc, argv) && platformFile,
193              "Invalid command line arguments: expected [--timings] platformFile");
194
195   XBT_DEBUG("%d,%s", timings, platformFile);
196
197   create_environment(parse_time, platformFile);
198
199   if (timings) {
200     XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time), sg_host_count(),
201              sg_link_count());
202   } else {
203     dump_platform();
204   }
205
206   xbt_os_timer_free(parse_time);
207
208   return 0;
209 }