Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[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/NetCard.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 int parse_cmdline(int *timings, char **platformFile, int argc, char **argv)
39 {
40   int wrong_option = 0;
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           wrong_option = 1;
47           break;
48       }
49     } else {
50       *platformFile = argv[i];
51     }
52   }
53   return wrong_option;
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 int main(int argc, char **argv)
69 {
70   char *platformFile = nullptr;
71   int timings=0;
72   int version = 4;
73   unsigned int i;
74   xbt_dict_t props = nullptr;
75   xbt_dict_cursor_t cursor = nullptr;
76   char *key, *data;
77
78   xbt_os_timer_t parse_time = xbt_os_timer_new();
79
80   SD_init(&argc, argv);
81
82   if (parse_cmdline(&timings, &platformFile, argc, argv) || !platformFile) {
83     xbt_die("Invalid command line arguments: expected [--timings] platformFile");
84   }
85
86   XBT_DEBUG("%d,%s", timings, platformFile);
87
88   create_environment(parse_time, platformFile);
89
90   std::vector<simgrid::kernel::routing::NetCard*> netcardList;
91   simgrid::s4u::Engine::instance()->netcardList(&netcardList);
92
93   if (timings) {
94     XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time),
95              sg_host_count(), sg_link_count());
96   } else {
97     std::printf("<?xml version='1.0'?>\n");
98     std::printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\">\n");
99     std::printf("<platform version=\"%d\">\n", version);
100     std::printf("<AS id=\"AS0\" routing=\"Full\">\n");
101
102     // Hosts
103     unsigned int totalHosts = sg_host_count();
104     sg_host_t *hosts = sg_host_list();
105     std::qsort((void *) hosts, totalHosts, sizeof(sg_host_t), name_compare_hosts);
106
107     for (i = 0; i < totalHosts; i++) {
108       std::printf("  <host id=\"%s\" speed=\"%.0f\"", hosts[i]->cname(), sg_host_speed(hosts[i]));
109       props = sg_host_get_properties(hosts[i]);
110       if (hosts[i]->coreCount()>1) {
111         std::printf(" core=\"%d\"", hosts[i]->coreCount());
112       }
113       if (props && !xbt_dict_is_empty(props)) {
114         std::printf(">\n");
115         xbt_dict_foreach(props, cursor, key, data) {
116           std::printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
117         }
118         std::printf("  </host>\n");
119       } else {
120         std::printf("/>\n");
121       }
122     }
123
124     // Routers
125     for (auto srcCard : netcardList)
126       if (srcCard->isRouter())
127         std::printf("  <router id=\"%s\"/>\n", srcCard->cname());
128
129     // Links
130     unsigned int totalLinks = sg_link_count();
131     SD_link_t *links = sg_link_list();
132
133     std::qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
134
135     for (i = 0; i < totalLinks; i++) {
136       std::printf("  <link id=\"");
137
138       std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", sg_link_name(links[i]),
139              sg_link_bandwidth(links[i]), sg_link_latency(links[i]));
140       if (sg_link_is_shared(links[i])) {
141         std::printf("/>\n");
142       } else {
143         std::printf(" sharing_policy=\"FATPIPE\"/>\n");
144       }
145     }
146
147     for (unsigned int it_src = 0; it_src < totalHosts; it_src++) { // Routes from host
148       simgrid::s4u::Host* host1 = hosts[it_src];
149       simgrid::kernel::routing::NetCard* netcardSrc = host1->pimpl_netcard;
150       for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
151         simgrid::s4u::Host* host2 = hosts[it_dst];
152         std::vector<Link*> route;
153         simgrid::kernel::routing::NetCard* netcardDst = host2->pimpl_netcard;
154         simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(netcardSrc, netcardDst, &route, nullptr);
155         if (!route.empty()) {
156           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", host1->cname(), host2->cname());
157           for (auto link : route)
158             std::printf("<link_ctn id=\"%s\"/>",link->getName());
159           std::printf("\n  </route>\n");
160         }
161       }
162       for (auto netcardDst : netcardList) { // to router
163         if (netcardDst->isRouter()) {
164           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", host1->cname(), netcardDst->cname());
165           std::vector<Link*> route;
166           simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(netcardSrc, netcardDst, &route, nullptr);
167           for (auto link : route)
168             std::printf("<link_ctn id=\"%s\"/>",link->getName());
169           std::printf("\n  </route>\n");
170         }
171       }
172     }
173
174     for (auto value1 : netcardList) { // Routes from router
175       if (value1->isRouter()){
176         for (auto value2 : netcardList) { // to router
177           if (value2->isRouter()) {
178             std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->cname(), value2->cname());
179             std::vector<Link*> route;
180             simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, &route, nullptr);
181             for (auto link : route)
182               std::printf("<link_ctn id=\"%s\"/>",link->getName());
183             std::printf("\n  </route>\n");
184           }
185         }
186         for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
187           simgrid::s4u::Host* host2 = hosts[it_dst];
188           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", value1->cname(), host2->cname());
189           std::vector<Link*> route;
190           simgrid::kernel::routing::NetCard* netcardDst = host2->pimpl_netcard;
191           simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, netcardDst, &route, nullptr);
192           for (auto link : route)
193             std::printf("<link_ctn id=\"%s\"/>",link->getName());
194           std::printf("\n  </route>\n");
195         }
196       }
197     }
198
199     std::printf("</AS>\n");
200     std::printf("</platform>\n");
201     std::free(hosts);
202     std::free(links);
203   }
204
205   SD_exit();
206   xbt_os_timer_free(parse_time);
207
208   return 0;
209 }