Logo AND Algorithmique Numérique Distribuée

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