Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0263235c4e03940e77c32e0faab132aeb6f1bdff
[simgrid.git] / src / kernel / routing / RoutedZone.cpp
1 /* Copyright (c) 2009-2017. 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/graph.h"
8 #include "xbt/log.h"
9 #include "xbt/sysdep.h"
10
11 #include "src/kernel/routing/NetPoint.hpp"
12 #include "src/kernel/routing/RoutedZone.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     delete route;
22   }
23 }
24
25 /* ***************************************************************** */
26 /* *********************** GENERIC METHODS ************************* */
27
28 static const char* instr_node_name(xbt_node_t node)
29 {
30   void* data = xbt_graph_node_get_data(node);
31   return static_cast<const char*>(data);
32 }
33
34 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char* name, std::map<std::string, xbt_node_t>* nodes)
35 {
36
37   auto elm = nodes->find(name);
38   if (elm == nodes->end()) {
39     xbt_node_t ret = xbt_graph_new_node(graph, xbt_strdup(name));
40     nodes->insert({name, ret});
41     return ret;
42   } else
43     return elm->second;
44 }
45
46 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d, std::map<std::string, xbt_edge_t>* edges)
47 {
48   const char* sn = instr_node_name(s);
49   const char* dn = instr_node_name(d);
50   std::string name = std::string(sn) + dn;
51
52   auto elm = edges->find(name);
53   if (elm == edges->end()) {
54     name = std::string(dn) + sn;
55     elm  = edges->find(name);
56   }
57
58   if (elm == edges->end()) {
59     xbt_edge_t ret = xbt_graph_new_edge(graph, s, d, nullptr);
60     edges->insert({name, ret});
61     return ret;
62   } else
63     return elm->second;
64 }
65
66 namespace simgrid {
67 namespace kernel {
68 namespace routing {
69
70 RoutedZone::RoutedZone(NetZone* father, std::string name) : NetZoneImpl(father, name)
71 {
72 }
73
74 void RoutedZone::getGraph(xbt_graph_t graph, std::map<std::string, xbt_node_t>* nodes,
75                           std::map<std::string, xbt_edge_t>* edges)
76 {
77   std::vector<kernel::routing::NetPoint*> vertices = getVertices();
78
79   for (auto const& my_src : vertices) {
80     for (auto const& my_dst : vertices) {
81       if (my_src == my_dst)
82         continue;
83
84       sg_platf_route_cbarg_t route = new s_sg_platf_route_cbarg_t;
85       route->link_list             = new std::vector<surf::LinkImpl*>();
86
87       getLocalRoute(my_src, my_dst, route, nullptr);
88
89       XBT_DEBUG("get_route_and_latency %s -> %s", my_src->getCname(), my_dst->getCname());
90
91       xbt_node_t current;
92       xbt_node_t previous;
93       const char *previous_name;
94       const char *current_name;
95
96       if (route->gw_src) {
97         previous      = new_xbt_graph_node(graph, route->gw_src->getCname(), nodes);
98         previous_name = route->gw_src->getCname();
99       } else {
100         previous      = new_xbt_graph_node(graph, my_src->getCname(), nodes);
101         previous_name = my_src->getCname();
102       }
103
104       for (auto const& link : *route->link_list) {
105         const char* link_name = link->getCname();
106         current               = new_xbt_graph_node(graph, link_name, nodes);
107         current_name          = link_name;
108         new_xbt_graph_edge(graph, previous, current, edges);
109         XBT_DEBUG("  %s -> %s", previous_name, current_name);
110         previous      = current;
111         previous_name = current_name;
112       }
113
114       if (route->gw_dst) {
115         current      = new_xbt_graph_node(graph, route->gw_dst->getCname(), nodes);
116         current_name = route->gw_dst->getCname();
117       } else {
118         current      = new_xbt_graph_node(graph, my_dst->getCname(), nodes);
119         current_name = my_dst->getCname();
120       }
121       new_xbt_graph_edge(graph, previous, current, edges);
122       XBT_DEBUG("  %s -> %s", previous_name, current_name);
123
124       delete route->link_list;
125       delete route;
126     }
127   }
128 }
129
130 /* ************************************************************************** */
131 /* ************************* GENERIC AUX FUNCTIONS ************************** */
132 /* change a route containing link names into a route containing link entities */
133 sg_platf_route_cbarg_t RoutedZone::newExtendedRoute(RoutingMode hierarchy, sg_platf_route_cbarg_t routearg,
134                                                     bool change_order)
135 {
136   sg_platf_route_cbarg_t result;
137
138   result            = new s_sg_platf_route_cbarg_t;
139   result->link_list = new std::vector<surf::LinkImpl*>();
140
141   xbt_assert(hierarchy == RoutingMode::base || hierarchy == RoutingMode::recursive,
142              "The hierarchy of this netzone is neither BASIC nor RECURSIVE, I'm lost here.");
143
144   if (hierarchy == RoutingMode::recursive) {
145     xbt_assert(routearg->gw_src && routearg->gw_dst, "nullptr is obviously a deficient gateway");
146
147     result->gw_src = routearg->gw_src;
148     result->gw_dst = routearg->gw_dst;
149   }
150
151   for (auto const& link : *routearg->link_list) {
152     if (change_order)
153       result->link_list->push_back(link);
154     else
155       result->link_list->insert(result->link_list->begin(), link);
156   }
157   result->link_list->shrink_to_fit();
158
159   return result;
160 }
161
162 void RoutedZone::getRouteCheckParams(NetPoint* src, NetPoint* dst)
163 {
164   xbt_assert(src, "Cannot find a route from nullptr to %s", dst->getCname());
165   xbt_assert(dst, "Cannot find a route from %s to nullptr", src->getCname());
166
167   NetZone* src_as = src->netzone();
168   NetZone* dst_as = dst->netzone();
169
170   xbt_assert(src_as == dst_as,
171              "Internal error: %s@%s and %s@%s are not in the same netzone as expected. Please report that bug.",
172              src->getCname(), src_as->getCname(), dst->getCname(), dst_as->getCname());
173
174   xbt_assert(this == dst_as, "Internal error: route destination %s@%s is not in netzone %s as expected (route source: "
175                              "%s@%s). Please report that bug.",
176              src->getCname(), dst->getCname(), src_as->getCname(), dst_as->getCname(), getCname());
177 }
178 void RoutedZone::addRouteCheckParams(sg_platf_route_cbarg_t route)
179 {
180   NetPoint* src       = route->src;
181   NetPoint* dst       = route->dst;
182   const char* srcName = src->getCname();
183   const char* dstName = dst->getCname();
184
185   if (not route->gw_dst || not route->gw_src) {
186     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
187     xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
188     xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
189     xbt_assert(not route->link_list->empty(), "Empty route (between %s and %s) forbidden.", srcName, dstName);
190     xbt_assert(not src->isNetZone(),
191                "When defining a route, src cannot be a netzone such as '%s'. Did you meant to have an NetzoneRoute?",
192                srcName);
193     xbt_assert(not dst->isNetZone(),
194                "When defining a route, dst cannot be a netzone such as '%s'. Did you meant to have an NetzoneRoute?",
195                dstName);
196   } else {
197     XBT_DEBUG("Load NetzoneRoute from %s@%s to %s@%s", srcName, route->gw_src->getCname(), dstName,
198               route->gw_dst->getCname());
199     xbt_assert(src->isNetZone(), "When defining a NetzoneRoute, src must be a netzone but '%s' is not", srcName);
200     xbt_assert(dst->isNetZone(), "When defining a NetzoneRoute, dst must be a netzone but '%s' is not", dstName);
201
202     xbt_assert(route->gw_src->isHost() || route->gw_src->isRouter(),
203                "When defining a NetzoneRoute, gw_src must be an host or a router but '%s' is not.", srcName);
204     xbt_assert(route->gw_dst->isHost() || route->gw_dst->isRouter(),
205                "When defining a NetzoneRoute, gw_dst must be an host or a router but '%s' is not.", dstName);
206
207     xbt_assert(route->gw_src != route->gw_dst, "Cannot define an NetzoneRoute from '%s' to itself",
208                route->gw_src->getCname());
209
210     xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.", srcName, route->gw_src->getCname(),
211                dstName, route->gw_dst->getCname(), srcName);
212     xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.", srcName, route->gw_src->getCname(),
213                dstName, route->gw_dst->getCname(), dstName);
214     xbt_assert(not route->link_list->empty(), "Empty route (between %s@%s and %s@%s) forbidden.", srcName,
215                route->gw_src->getCname(), dstName, route->gw_dst->getCname());
216   }
217
218   onRouteCreation(route->symmetrical, route->src, route->dst, route->gw_src, route->gw_dst, route->link_list);
219 }
220 }
221 }
222 }