Logo AND Algorithmique Numérique Distribuée

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