Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename the class objects and C types for the As -> NetZone transition
[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/host.hpp>
18
19 #include <simgrid/simdag.h>
20
21 #include "src/kernel/routing/NetCard.hpp"
22 #include "src/surf/network_interface.hpp"
23
24
25 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
26
27 static int name_compare_hosts(const void *n1, const void *n2)
28 {
29   return std::strcmp(sg_host_get_name(*(sg_host_t *) n1), sg_host_get_name(*(sg_host_t *) n2));
30 }
31
32 static int name_compare_links(const void *n1, const void *n2)
33 {
34   return std::strcmp(sg_link_name(*(SD_link_t *) n1),sg_link_name(*(SD_link_t *) n2));
35 }
36
37 static int parse_cmdline(int *timings, char **platformFile, int argc, char **argv)
38 {
39   int wrong_option = 0;
40   for (int i = 1; i < argc; i++) {
41     if (std::strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
42       if (!std::strcmp(argv[i], "--timings")) {
43         *timings = 1;
44       } else {
45           wrong_option = 1;
46           break;
47       }
48     } else {
49       *platformFile = argv[i];
50     }
51   }
52   return wrong_option;
53 }
54
55 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
56 {
57   try {
58     xbt_os_cputimer_start(parse_time);
59     SD_create_environment(platformFile);
60     xbt_os_cputimer_stop(parse_time);
61   }
62   catch (std::exception& e) {
63     xbt_die("Error while loading %s: %s", platformFile, e.what());
64   }
65 }
66
67 int main(int argc, char **argv)
68 {
69   char *platformFile = nullptr;
70   int timings=0;
71   int version = 4;
72   unsigned int i;
73   xbt_dict_t props = nullptr;
74   xbt_dict_cursor_t cursor = nullptr;
75   xbt_lib_cursor_t cursor_src = nullptr;
76   xbt_lib_cursor_t cursor_dst = nullptr;
77   char *src,*dst,*key,*data;
78   sg_netcard_t value1;
79   sg_netcard_t value2;
80
81   xbt_os_timer_t parse_time = xbt_os_timer_new();
82
83   SD_init(&argc, argv);
84
85   if (parse_cmdline(&timings, &platformFile, argc, argv) || !platformFile) {
86     xbt_die("Invalid command line arguments: expected [--timings] platformFile");
87   }
88
89   XBT_DEBUG("%d,%s", timings, platformFile);
90
91   create_environment(parse_time, platformFile);
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     xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
126       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, key, ROUTING_ASR_LEVEL);
127       if(value1->isRouter()) {
128         std::printf("  <router id=\"%s\"/>\n",key);
129       }
130     }
131
132     // Links
133     unsigned int totalLinks = sg_link_count();
134     SD_link_t *links = sg_link_list();
135
136     std::qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
137
138     for (i = 0; i < totalLinks; i++) {
139       std::printf("  <link id=\"");
140
141       std::printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", sg_link_name(links[i]),
142              sg_link_bandwidth(links[i]), sg_link_latency(links[i]));
143       if (sg_link_is_shared(links[i])) {
144         std::printf("/>\n");
145       } else {
146         std::printf(" sharing_policy=\"FATPIPE\"/>\n");
147       }
148     }
149
150     sg_host_t host1, host2;
151     for (unsigned int it_src = 0; it_src < totalHosts; it_src++) { // Routes from host
152       host1 = hosts[it_src];
153       value1 = host1->pimpl_netcard;
154       for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
155         host2 = hosts[it_dst];
156         std::vector<Link*> *route = new std::vector<Link*>();
157         value2 = host2->pimpl_netcard;
158         simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, route, nullptr);
159         if (! route->empty()){
160           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", host1->cname(), host2->cname());
161           for (auto link: *route)
162             std::printf("<link_ctn id=\"%s\"/>",link->getName());
163           std::printf("\n  </route>\n");
164         }
165         delete route;
166       }
167       xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2){ //to router
168         value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
169         if(value2->isRouter()){
170           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", host1->cname(), dst);
171           std::vector<Link*> *route = new std::vector<Link*>();
172           simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, route, nullptr);
173           for (auto link : *route)
174             std::printf("<link_ctn id=\"%s\"/>",link->getName());
175           delete route;
176           std::printf("\n  </route>\n");
177         }
178       }
179     }
180
181     xbt_lib_foreach(as_router_lib, cursor_src, src, value1){ // Routes from router
182       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
183       if (value1->isRouter()){
184         xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2){ //to router
185           value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
186           if(value2->isRouter()){
187             std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
188             std::vector<Link*> *route = new std::vector<Link*>();
189             simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, route, nullptr);
190             for(auto link :*route)
191               std::printf("<link_ctn id=\"%s\"/>",link->getName());
192             delete route;
193             std::printf("\n  </route>\n");
194           }
195         }
196         for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host
197           host2 = hosts[it_dst];
198           std::printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, host2->cname());
199           std::vector<Link*> *route = new std::vector<Link*>();
200           value2 = host2->pimpl_netcard;
201           simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(value1, value2, route, nullptr);
202           for(auto link : *route)
203             std::printf("<link_ctn id=\"%s\"/>",link->getName());
204           delete route;
205           std::printf("\n  </route>\n");
206         }
207       }
208     }
209
210     std::printf("</AS>\n");
211     std::printf("</platform>\n");
212     std::free(hosts);
213     std::free(links);
214   }
215
216   SD_exit();
217   xbt_os_timer_free(parse_time);
218
219   return 0;
220 }