Logo AND Algorithmique Numérique Distribuée

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