Logo AND Algorithmique Numérique Distribuée

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