Logo AND Algorithmique Numérique Distribuée

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