Logo AND Algorithmique Numérique Distribuée

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