Logo AND Algorithmique Numérique Distribuée

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