Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[TESHSUITE] NULL -> nullptr substitution
[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 "src/surf/network_interface.hpp"
8 #include "simgrid/simdag.h"
9 #include "xbt/xbt_os_time.h"
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier, "Logging specific to this platform parsing tool");
12
13 static int name_compare_hosts(const void *n1, const void *n2)
14 {
15   return strcmp(sg_host_get_name(*(sg_host_t *) n1), sg_host_get_name(*(sg_host_t *) n2));
16 }
17
18 static int name_compare_links(const void *n1, const void *n2)
19 {
20   return strcmp(sg_link_name(*(SD_link_t *) n1),sg_link_name(*(SD_link_t *) n2));
21 }
22
23 static int parse_cmdline(int *timings, char **platformFile, int argc, char **argv)
24 {
25   int wrong_option = 0;
26   for (int i = 1; i < argc; i++) {
27     if (strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
28       if (!strcmp(argv[i], "--timings")) {
29         *timings = 1;
30       } else {
31           wrong_option = 1;
32           break;
33       }
34     } else {
35       *platformFile = argv[i];
36     }
37   }
38   return wrong_option;
39 }
40
41 static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
42 {
43   xbt_ex_t e;
44   TRY {
45     xbt_os_cputimer_start(parse_time);
46     SD_create_environment(platformFile);
47     xbt_os_cputimer_stop(parse_time);
48   }
49   CATCH(e) {
50     xbt_die("Error while loading %s: %s", platformFile, e.msg);
51   }
52 }
53
54 int main(int argc, char **argv)
55 {
56   char *platformFile = nullptr;
57   int timings=0;
58   int version = 4;
59   unsigned int i;
60   xbt_dict_t props = nullptr;
61   xbt_dict_cursor_t cursor = nullptr;
62   xbt_lib_cursor_t cursor_src = nullptr;
63   xbt_lib_cursor_t cursor_dst = nullptr;
64   char *src,*dst,*key,*data;
65   sg_netcard_t value1;
66   sg_netcard_t value2;
67
68   xbt_os_timer_t parse_time = xbt_os_timer_new();
69
70   SD_init(&argc, argv);
71
72   if (parse_cmdline(&timings, &platformFile, argc, argv) || !platformFile) {
73     xbt_die("Invalid command line arguments: expected [--timings] platformFile");
74   }
75
76   XBT_DEBUG("%d,%s", timings, platformFile);
77
78   create_environment(parse_time, platformFile);
79
80   if (timings) {
81     XBT_INFO("Parsing time: %fs (%zu hosts, %d links)", xbt_os_timer_elapsed(parse_time),
82              sg_host_count(), sg_link_count());
83   } else {
84     printf("<?xml version='1.0'?>\n");
85     printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\">\n");
86     printf("<platform version=\"%d\">\n", version);
87     printf("<AS id=\"AS0\" routing=\"Full\">\n");
88
89     // Hosts
90     unsigned int totalHosts = sg_host_count();
91     sg_host_t *hosts = sg_host_list();
92     qsort((void *) hosts, totalHosts, sizeof(sg_host_t), name_compare_hosts);
93
94     for (i = 0; i < totalHosts; i++) {
95       printf("  <host id=\"%s\" speed=\"%.0f\"", sg_host_get_name(hosts[i]), sg_host_speed(hosts[i]));
96       props = sg_host_get_properties(hosts[i]);
97       if (hosts[i]->core_count()>1) {
98         printf(" core=\"%d\"", hosts[i]->core_count());
99       }
100       if (props && !xbt_dict_is_empty(props)) {
101         printf(">\n");
102         xbt_dict_foreach(props, cursor, key, data) {
103           printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
104         }
105         printf("  </host>\n");
106       } else {
107         printf("/>\n");
108       }
109     }
110
111     // Routers
112     xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
113       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, key, ROUTING_ASR_LEVEL);
114       if(value1->isRouter()) {
115         printf("  <router id=\"%s\"/>\n",key);
116       }
117     }
118
119     // Links
120     unsigned int totalLinks = sg_link_count();
121     SD_link_t *links = sg_link_list();
122
123     qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
124
125     for (i = 0; i < totalLinks; i++) {
126       printf("  <link id=\"");
127
128       printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"", sg_link_name(links[i]),
129              sg_link_bandwidth(links[i]), sg_link_latency(links[i]));
130       if (sg_link_is_shared(links[i])) {
131         printf("/>\n");
132       } else {
133         printf(" sharing_policy=\"FATPIPE\"/>\n");
134       }
135     }
136
137     sg_host_t host1, host2;
138     xbt_dict_foreach(host_list, cursor_src, src, host1){ // Routes from host
139       value1 = sg_host_by_name(src)->pimpl_netcard;
140       xbt_dict_foreach(host_list, cursor_dst, dst, host2){ //to host
141         std::vector<Link*> *route = new std::vector<Link*>();
142         value2 = sg_host_by_name(dst)->pimpl_netcard;
143         routing_platf->getRouteAndLatency(value1, value2, route,nullptr);
144         if (! route->empty()){
145           printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
146           for (auto link: *route)
147             printf("<link_ctn id=\"%s\"/>",link->getName());
148           printf("\n  </route>\n");
149         }
150         delete route;
151       }
152       xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2){ //to router
153         value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
154         if(value2->isRouter()){
155           printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
156           std::vector<Link*> *route = new std::vector<Link*>();
157           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route,nullptr);
158           for (auto link : *route)
159             printf("<link_ctn id=\"%s\"/>",link->getName());
160           delete route;
161           printf("\n  </route>\n");
162         }
163       }
164     }
165
166     xbt_lib_foreach(as_router_lib, cursor_src, src, value1){ // Routes from router
167       value1 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
168       if (value1->isRouter()){
169         xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2){ //to router
170           value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
171           if(value2->isRouter()){
172             printf("  <route src=\"%s\" dst=\"%s\">\n  ", src, dst);
173             std::vector<Link*> *route = new std::vector<Link*>();
174             routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route,nullptr);
175             for(auto link :*route)
176               printf("<link_ctn id=\"%s\"/>",link->getName());
177             delete route;
178             printf("\n  </route>\n");
179           }
180         }
181         xbt_dict_foreach(host_list, cursor_dst, dst, value2){ //to host
182           printf("  <route src=\"%s\" dst=\"%s\">\n  ",src, dst);
183           std::vector<Link*> *route = new std::vector<Link*>();
184           value2 = sg_host_by_name(dst)->pimpl_netcard;
185           routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route, nullptr);
186           for(auto link : *route)
187             printf("<link_ctn id=\"%s\"/>",link->getName());
188           delete route;
189           printf("\n  </route>\n");
190         }
191       }
192     }
193
194     printf("</AS>\n");
195     printf("</platform>\n");
196     free(hosts);
197     free(links);
198   }
199
200   SD_exit();
201   xbt_os_timer_free(parse_time);
202
203   return 0;
204 }