Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c5e1d634d7a7ec0221895094c9b72ed2af810f6e
[simgrid.git] / src / surf / surf_routing_RoutedGraph.cpp
1 /* Copyright (c) 2009-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 "surf_routing_RoutedGraph.hpp"
8
9 #include <cstdlib>
10
11 #include <algorithm>
12
13 #include "xbt/dict.h"
14 #include "xbt/log.h"
15 #include "xbt/sysdep.h"
16 #include "xbt/dynar.h"
17 #include "xbt/graph.h"
18
19 #include "surf_routing_private.hpp"
20 #include "network_interface.hpp"
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
23
24 void routing_route_free(sg_platf_route_cbarg_t route)
25 {
26   if (route) {
27     xbt_dynar_free(&route->link_list);
28     xbt_free(route);
29   }
30 }
31
32 namespace simgrid {
33 namespace surf {
34   
35 AsRoutedGraph::AsRoutedGraph(const char*name)
36   : As(name)
37 {
38 }
39
40 AsRoutedGraph::~AsRoutedGraph()
41 {
42 }
43
44
45 }
46 }
47
48 /* ************************************************************************** */
49 /* *********************** GENERIC BUSINESS METHODS ************************* */
50
51 static const char *instr_node_name(xbt_node_t node)
52 {
53   void *data = xbt_graph_node_get_data(node);
54   char *str = (char *) data;
55   return str;
56 }
57
58 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
59                               xbt_dict_t nodes)
60 {
61   xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
62   if (ret)
63     return ret;
64
65   ret = xbt_graph_new_node(graph, xbt_strdup(name));
66   xbt_dict_set(nodes, name, ret, NULL);
67   return ret;
68 }
69
70 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
71                               xbt_dict_t edges)
72 {
73   xbt_edge_t ret;
74
75   const char *sn = instr_node_name(s);
76   const char *dn = instr_node_name(d);
77   int len = strlen(sn) + strlen(dn) + 1;
78   char *name = (char *) xbt_malloc(len * sizeof(char));
79
80
81   snprintf(name, len, "%s%s", sn, dn);
82   ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
83   if (ret == NULL) {
84     snprintf(name, len, "%s%s", dn, sn);
85     ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
86   }
87
88   if (ret == NULL) {
89     ret = xbt_graph_new_edge(graph, s, d, NULL);
90     xbt_dict_set(edges, name, ret, NULL);
91   }
92   free(name);
93   return ret;
94 }
95
96 namespace simgrid {
97 namespace surf {
98
99 void AsRoutedGraph::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
100 {
101   int src, dst;
102   int table_size = xbt_dynar_length(vertices_);
103
104
105   for (src = 0; src < table_size; src++) {
106     NetCard *my_src =
107         xbt_dynar_get_as(vertices_, src, NetCard*);
108     for (dst = 0; dst < table_size; dst++) {
109       if (src == dst)
110         continue;
111       NetCard *my_dst =
112           xbt_dynar_get_as(vertices_, dst, NetCard*);
113
114       sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
115       route->link_list = xbt_dynar_new(sizeof(Link*), NULL);
116
117       getRouteAndLatency(my_src, my_dst, route, NULL);
118
119       XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->name(), my_dst->name());
120
121       unsigned int cpt;
122       void *link;
123
124       xbt_node_t current, previous;
125       const char *previous_name, *current_name;
126
127       if (route->gw_src) {
128         previous = new_xbt_graph_node(graph, route->gw_src->name(), nodes);
129         previous_name = route->gw_src->name();
130       } else {
131         previous = new_xbt_graph_node(graph, my_src->name(), nodes);
132         previous_name = my_src->name();
133       }
134
135       xbt_dynar_foreach(route->link_list, cpt, link) {
136         const char *link_name = static_cast<simgrid::surf::Resource*>(
137           link)->getName();
138         current = new_xbt_graph_node(graph, link_name, nodes);
139         current_name = link_name;
140         new_xbt_graph_edge(graph, previous, current, edges);
141         XBT_DEBUG ("  %s -> %s", previous_name, current_name);
142         previous = current;
143         previous_name = current_name;
144       }
145
146       if (route->gw_dst) {
147         current = new_xbt_graph_node(graph, route->gw_dst->name(), nodes);
148         current_name = route->gw_dst->name();
149       } else {
150         current = new_xbt_graph_node(graph, my_dst->name(), nodes);
151         current_name = my_dst->name();
152       }
153       new_xbt_graph_edge(graph, previous, current, edges);
154       XBT_DEBUG ("  %s -> %s", previous_name, current_name);
155
156       xbt_dynar_free (&(route->link_list));
157       xbt_free (route);
158     }
159   }
160 }
161
162
163 /* ************************************************************************** */
164 /* ************************* GENERIC AUX FUNCTIONS ************************** */
165 /* change a route containing link names into a route containing link entities */
166 sg_platf_route_cbarg_t AsRoutedGraph::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
167       sg_platf_route_cbarg_t routearg, int change_order) {
168
169   sg_platf_route_cbarg_t result;
170   char *link_name;
171   unsigned int cpt;
172
173   result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
174   result->link_list = xbt_dynar_new(sizeof(Link*), NULL);
175
176   xbt_assert(hierarchy == SURF_ROUTING_BASE
177       || hierarchy == SURF_ROUTING_RECURSIVE,
178       "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
179
180   if (hierarchy == SURF_ROUTING_RECURSIVE) {
181
182     xbt_assert(routearg->gw_src && routearg->gw_dst, "NULL is obviously a deficient gateway");
183
184     /* remember not erase the gateway names */
185     result->gw_src = routearg->gw_src;
186     result->gw_dst = routearg->gw_dst;
187   }
188
189   xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
190
191     Link *link = Link::byName(link_name);
192     if (link) {
193       if (change_order)
194         xbt_dynar_push(result->link_list, &link);
195       else
196         xbt_dynar_unshift(result->link_list, &link);
197     } else
198       THROWF(mismatch_error, 0, "Link '%s' not found", link_name);
199   }
200
201   return result;
202 }
203
204 void AsRoutedGraph::getRouteCheckParams(NetCard *src, NetCard *dst)
205 {
206   xbt_assert(src,"Cannot find a route from NULL to %s", dst->name());
207   xbt_assert(dst,"Cannot find a route from %s to NULL", src->name());
208
209   As *src_as = src->containingAS();
210   As *dst_as = dst->containingAS();
211
212   xbt_assert(src_as == dst_as, "Internal error: %s@%s and %s@%s are not in the same AS as expected. Please report that bug.",
213         src->name(), src_as->name_, dst->name(), dst_as->name_);
214
215   xbt_assert(this == dst_as,
216       "Internal error: route destination %s@%s is not in AS %s as expected (route source: %s@%s). Please report that bug.",
217         src->name(), dst->name(),  src_as->name_, dst_as->name_,  name_);
218 }
219 void AsRoutedGraph::addRouteCheckParams(sg_platf_route_cbarg_t route) {
220   const char *srcName = route->src;
221   const char *dstName = route->dst;
222   NetCard *src = sg_netcard_by_name_or_null(srcName);
223   NetCard *dst = sg_netcard_by_name_or_null(dstName);
224
225   if(!route->gw_dst && !route->gw_src) {
226     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
227     xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
228     xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
229     xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s and %s) forbidden.", srcName, dstName);
230     xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_HOST || src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
231         "When defining a route, src must be an host or a router but '%s' is not. Did you meant to have an ASroute?", srcName);
232     xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
233         "When defining a route, dst must be an host or a router but '%s' is not. Did you meant to have an ASroute?", dstName);
234   } else {
235     XBT_DEBUG("Load ASroute from %s@%s to %s@%s", srcName, route->gw_src->name(), dstName, route->gw_dst->name());
236     xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_AS,
237         "When defining an ASroute, src must be an AS but '%s' is not", srcName);
238     xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_AS,
239         "When defining an ASroute, dst must be an AS but '%s' is not", dstName);
240
241     xbt_assert(route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
242         "When defining an ASroute, gw_src must be an host or a router but '%s' is not.", srcName);
243     xbt_assert(route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
244         "When defining an ASroute, gw_dst must be an host or a router but '%s' is not.", dstName);
245
246     xbt_assert(route->gw_src != route->gw_dst, "Cannot define an ASroute from '%s' to itself", route->gw_src->name());
247
248     xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
249         srcName,route->gw_src->name(), dstName,route->gw_dst->name(), srcName);
250     xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
251         srcName,route->gw_src->name(), dstName,route->gw_dst->name(), dstName);
252     xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s@%s and %s@%s) forbidden.",
253         srcName,route->gw_src->name(), dstName,route->gw_dst->name());
254   }
255 }
256
257 }
258 }