Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
various small cosmetics in the routing
[simgrid.git] / src / kernel / routing / AsRoutedGraph.cpp
1 /* Copyright (c) 2009-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "xbt/dict.h"
7 #include "xbt/log.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/dynar.h"
10 #include "xbt/graph.h"
11
12 #include "src/kernel/routing/AsRoutedGraph.hpp"
13 #include "src/surf/network_interface.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
16
17 void routing_route_free(sg_platf_route_cbarg_t route)
18 {
19   if (route) {
20     delete route->link_list;
21     xbt_free(route);
22   }
23 }
24
25 namespace simgrid {
26 namespace kernel {
27 namespace routing {
28
29 AsRoutedGraph::AsRoutedGraph(As* father, const char* name) : AsImpl(father, name)
30 {
31 }
32
33 }}} // namespace simgrid::kernel::routing
34
35 /* ************************************************************************** */
36 /* *********************** GENERIC BUSINESS METHODS ************************* */
37
38 static const char *instr_node_name(xbt_node_t node)
39 {
40   void *data = xbt_graph_node_get_data(node);
41   char *str = (char *) data;
42   return str;
43 }
44
45 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name, xbt_dict_t nodes)
46 {
47   xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
48   if (ret)
49     return ret;
50
51   ret = xbt_graph_new_node(graph, xbt_strdup(name));
52   xbt_dict_set(nodes, name, ret, nullptr);
53   return ret;
54 }
55
56 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d, xbt_dict_t edges)
57 {
58   const char *sn = instr_node_name(s);
59   const char *dn = instr_node_name(d);
60   int len = strlen(sn) + strlen(dn) + 1;
61   char *name = (char *) xbt_malloc(len * sizeof(char));
62
63   snprintf(name, len, "%s%s", sn, dn);
64   xbt_edge_t ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
65   if (ret == nullptr) {
66     snprintf(name, len, "%s%s", dn, sn);
67     ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
68   }
69
70   if (ret == nullptr) {
71     ret = xbt_graph_new_edge(graph, s, d, nullptr);
72     xbt_dict_set(edges, name, ret, nullptr);
73   }
74   free(name);
75   return ret;
76 }
77
78 namespace simgrid {
79 namespace kernel {
80 namespace routing {
81
82 void AsRoutedGraph::getOneLinkRoutes(std::vector<Onelink*>* accumulator)
83 {
84   sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
85   route->link_list             = new std::vector<Link*>();
86
87   int table_size = static_cast<int>(vertices_.size());
88   for (int src = 0; src < table_size; src++) {
89     for (int dst = 0; dst < table_size; dst++) {
90       route->link_list->clear();
91       NetCard* src_elm = vertices_.at(src);
92       NetCard* dst_elm = vertices_.at(dst);
93       this->getRouteAndLatency(src_elm, dst_elm, route, nullptr);
94
95       if (route->link_list->size() == 1) {
96         Link* link = route->link_list->at(0);
97         Onelink* onelink;
98         if (hierarchy_ == RoutingMode::base)
99           onelink = new Onelink(link, src_elm, dst_elm);
100         else if (hierarchy_ == RoutingMode::recursive)
101           onelink = new Onelink(link, route->gw_src, route->gw_dst);
102         else
103           onelink = new Onelink(link, nullptr, nullptr);
104         accumulator->push_back(onelink);
105       }
106     }
107   }
108   AsImpl::getOneLinkRoutes(accumulator); // Recursivly call this function on all my childs too
109 }
110
111 void AsRoutedGraph::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
112 {
113   for (auto my_src: vertices_){
114     for (auto my_dst: vertices_){
115       if (my_src == my_dst)
116         continue;
117
118       sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
119       route->link_list = new std::vector<Link*>();
120
121       getRouteAndLatency(my_src, my_dst, route, nullptr);
122
123       XBT_DEBUG("get_route_and_latency %s -> %s", my_src->name().c_str(), my_dst->name().c_str());
124
125       xbt_node_t current, previous;
126       const char *previous_name, *current_name;
127
128       if (route->gw_src) {
129         previous      = new_xbt_graph_node(graph, route->gw_src->name().c_str(), nodes);
130         previous_name = route->gw_src->name().c_str();
131       } else {
132         previous      = new_xbt_graph_node(graph, my_src->name().c_str(), nodes);
133         previous_name = my_src->name().c_str();
134       }
135
136       for (auto link: *route->link_list) {
137         const char *link_name = 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().c_str(), nodes);
148         current_name = route->gw_dst->name().c_str();
149       } else {
150         current      = new_xbt_graph_node(graph, my_dst->name().c_str(), nodes);
151         current_name = my_dst->name().c_str();
152       }
153       new_xbt_graph_edge(graph, previous, current, edges);
154       XBT_DEBUG ("  %s -> %s", previous_name, current_name);
155
156       delete route->link_list;
157       xbt_free (route);
158     }
159   }
160 }
161
162 /* ************************************************************************** */
163 /* ************************* GENERIC AUX FUNCTIONS ************************** */
164 /* change a route containing link names into a route containing link entities */
165 sg_platf_route_cbarg_t AsRoutedGraph::newExtendedRoute(RoutingMode hierarchy, sg_platf_route_cbarg_t routearg, int change_order)
166 {
167   sg_platf_route_cbarg_t result;
168
169   result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
170   result->link_list = new std::vector<Link*>();
171
172   xbt_assert(hierarchy == RoutingMode::base || hierarchy == RoutingMode::recursive,
173       "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
174
175   if (hierarchy == RoutingMode::recursive) {
176     xbt_assert(routearg->gw_src && routearg->gw_dst, "nullptr is obviously a deficient gateway");
177
178     result->gw_src = routearg->gw_src;
179     result->gw_dst = routearg->gw_dst;
180   }
181
182   for (auto link : *routearg->link_list) {
183     if (change_order)
184       result->link_list->push_back(link);
185     else
186       result->link_list->insert(result->link_list->begin(), link);
187   }
188
189   return result;
190 }
191
192 void AsRoutedGraph::getRouteCheckParams(NetCard *src, NetCard *dst)
193 {
194   xbt_assert(src, "Cannot find a route from nullptr to %s", dst->name().c_str());
195   xbt_assert(dst, "Cannot find a route from %s to nullptr", src->name().c_str());
196
197   As *src_as = src->containingAS();
198   As *dst_as = dst->containingAS();
199
200   xbt_assert(src_as == dst_as,
201              "Internal error: %s@%s and %s@%s are not in the same AS as expected. Please report that bug.",
202              src->name().c_str(), src_as->name(), dst->name().c_str(), dst_as->name());
203
204   xbt_assert(this == dst_as, "Internal error: route destination %s@%s is not in AS %s as expected (route source: "
205                              "%s@%s). Please report that bug.",
206              src->name().c_str(), dst->name().c_str(), src_as->name(), dst_as->name(), name());
207 }
208 void AsRoutedGraph::addRouteCheckParams(sg_platf_route_cbarg_t route) {
209   NetCard *src = route->src;
210   NetCard *dst = route->dst;
211   const char* srcName = src->name().c_str();
212   const char* dstName = dst->name().c_str();
213
214   if(!route->gw_dst && !route->gw_src) {
215     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
216     xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
217     xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
218     xbt_assert(! route->link_list->empty(), "Empty route (between %s and %s) forbidden.", srcName, dstName);
219     xbt_assert(! src->isAS(), "When defining a route, src cannot be an AS such as '%s'. Did you meant to have an ASroute?", srcName);
220     xbt_assert(! dst->isAS(), "When defining a route, dst cannot be an AS such as '%s'. Did you meant to have an ASroute?", dstName);
221   } else {
222     XBT_DEBUG("Load ASroute from %s@%s to %s@%s", srcName, route->gw_src->name().c_str(), dstName,
223               route->gw_dst->name().c_str());
224     xbt_assert(src->isAS(), "When defining an ASroute, src must be an AS but '%s' is not", srcName);
225     xbt_assert(dst->isAS(), "When defining an ASroute, dst must be an AS but '%s' is not", dstName);
226
227     xbt_assert(route->gw_src->isHost() || route->gw_src->isRouter(),
228         "When defining an ASroute, gw_src must be an host or a router but '%s' is not.", srcName);
229     xbt_assert(route->gw_dst->isHost() || route->gw_dst->isRouter(),
230         "When defining an ASroute, gw_dst must be an host or a router but '%s' is not.", dstName);
231
232     xbt_assert(route->gw_src != route->gw_dst, "Cannot define an ASroute from '%s' to itself",
233                route->gw_src->name().c_str());
234
235     xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.", srcName,
236                route->gw_src->name().c_str(), dstName, route->gw_dst->name().c_str(), srcName);
237     xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.", srcName,
238                route->gw_src->name().c_str(), dstName, route->gw_dst->name().c_str(), dstName);
239     xbt_assert(!route->link_list->empty(), "Empty route (between %s@%s and %s@%s) forbidden.", srcName,
240                route->gw_src->name().c_str(), dstName, route->gw_dst->name().c_str());
241   }
242 }
243
244 }}}