Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename AsImpl::getRouteRecursive to AsImpl::getGlobalRoute (+doc improvment)
[simgrid.git] / src / kernel / routing / AsImpl.cpp
1 /* Copyright (c) 2006-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/log.h"
7
8 #include "simgrid/s4u/host.hpp"
9 #include "src/kernel/routing/AsImpl.hpp"
10 #include "src/surf/cpu_interface.hpp"
11 #include "src/surf/network_interface.hpp" // Link FIXME: move to proper header
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(AsImpl,surf, "Implementation of S4U autonomous systems");
14
15 namespace simgrid {
16   namespace kernel {
17   namespace routing {
18
19   AsImpl::AsImpl(As* father, const char* name) : As(father, name)
20   {
21     xbt_assert(nullptr == xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL),
22                "Refusing to create a second AS called '%s'.", name);
23
24     netcard_ = new NetCardImpl(name, NetCard::Type::As, static_cast<AsImpl*>(father));
25     xbt_lib_set(as_router_lib, name, ROUTING_ASR_LEVEL, static_cast<void*>(netcard_));
26     XBT_DEBUG("AS '%s' created with the id '%d'", name, netcard_->id());
27   }
28
29   simgrid::s4u::Host* AsImpl::createHost(const char* name, std::vector<double>* speedPerPstate, int coreAmount)
30   {
31     simgrid::s4u::Host* res = new simgrid::s4u::Host(name);
32
33     if (hierarchy_ == RoutingMode::unset)
34       hierarchy_ = RoutingMode::base;
35
36     res->pimpl_netcard = new NetCardImpl(name, NetCard::Type::Host, this);
37
38     surf_cpu_model_pm->createCpu(res, speedPerPstate, coreAmount);
39
40     return res;
41   }
42
43   void AsImpl::getOneLinkRoutes(std::vector<Onelink*>* accumulator)
44   {
45     // recursing only. I have no route myself :)
46     char* key;
47     xbt_dict_cursor_t cursor = nullptr;
48     AsImpl* rc_child;
49     xbt_dict_foreach (children(), cursor, key, rc_child) {
50       rc_child->getOneLinkRoutes(accumulator);
51     }
52   }
53
54   /** @brief Get the common ancestor and its first children in each line leading to src and dst
55    *
56    * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
57    * @verbatim
58    *         platform root
59    *               |
60    *              ...                <- possibly long path
61    *               |
62    *         common_ancestor
63    *           /        \
64    *          /          \
65    *         /            \          <- direct links
66    *        /              \
67    *       /                \
68    *  src_ancestor     dst_ancestor  <- must be different in the recursive case
69    *      |                   |
70    *     ...                 ...     <-- possibly long pathes (one hop or more)
71    *      |                   |
72    *     src                 dst
73    *  @endverbatim
74    *
75    *  In the base case (when src and dst are in the same AS), things are as follows:
76    *  @verbatim
77    *                  platform root
78    *                        |
79    *                       ...                      <-- possibly long path
80    *                        |
81    * common_ancestor==src_ancestor==dst_ancestor    <-- all the same value
82    *                   /        \
83    *                  /          \                  <-- direct links (exactly one hop)
84    *                 /            \
85    *              src              dst
86    *  @endverbatim
87    *
88    * A specific recursive case occurs when src is the ancestor of dst. In this case,
89    * the base case routing should be used so the common_ancestor is specifically set
90    * to src_ancestor==dst_ancestor.
91    * Naturally, things are completely symmetrical if dst is the ancestor of src.
92    * @verbatim
93    *            platform root
94    *                  |
95    *                 ...                <-- possibly long path
96    *                  |
97    *  src == src_ancestor==dst_ancestor==common_ancestor <-- same value
98    *                  |
99    *                 ...                <-- possibly long path (one hop or more)
100    *                  |
101    *                 dst
102    *  @endverbatim
103    */
104   static void find_common_ancestors(NetCard* src, NetCard* dst,
105                                     /* OUT */ AsImpl** common_ancestor, AsImpl** src_ancestor, AsImpl** dst_ancestor)
106   {
107     /* Deal with the easy base case */
108     if (src->containingAS() == dst->containingAS()) {
109       *common_ancestor = src->containingAS();
110       *src_ancestor    = *common_ancestor;
111       *dst_ancestor    = *common_ancestor;
112       return;
113     }
114
115     /* engage the full recursive search */
116
117     /* (1) find the path to root of src and dst*/
118     AsImpl* src_as = src->containingAS();
119     AsImpl* dst_as = dst->containingAS();
120
121     xbt_assert(src_as, "Host %s must be in an AS", src->name().c_str());
122     xbt_assert(dst_as, "Host %s must be in an AS", dst->name().c_str());
123
124     /* (2) find the path to the root routing component */
125     std::vector<AsImpl*> path_src;
126     AsImpl* current = src->containingAS();
127     while (current != nullptr) {
128       path_src.push_back(current);
129       current = static_cast<AsImpl*>(current->father());
130     }
131     std::vector<AsImpl*> path_dst;
132     current = dst->containingAS();
133     while (current != nullptr) {
134       path_dst.push_back(current);
135       current = static_cast<AsImpl*>(current->father());
136     }
137
138     /* (3) find the common father.
139      * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
140      * So we move them down simultaneously as long as they point to the same content.
141      *
142      * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
143      */
144     AsImpl* father = nullptr; // the AS we dropped on the previous loop iteration
145     while (path_src.size() > 1 && path_dst.size() > 1 &&
146            path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
147       father = path_src.at(path_src.size() - 1);
148       path_src.pop_back();
149       path_dst.pop_back();
150     }
151
152     /* (4) we found the difference at least. Finalize the returned values */
153     *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
154     *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
155     if (*src_ancestor == *dst_ancestor) {             // src is the ancestor of dst, or the contrary
156       *common_ancestor = *src_ancestor;
157     } else {
158       *common_ancestor = father;
159     }
160   }
161
162   /* PRECONDITION: this is the common ancestor of src and dst */
163   bool AsImpl::getBypassRoute(routing::NetCard* src, routing::NetCard* dst,
164                               /* OUT */ std::vector<surf::Link*>* links, double* latency)
165   {
166     // If never set a bypass route return nullptr without any further computations
167     XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name().c_str(), dst->name().c_str());
168     if (bypassRoutes_.empty())
169       return false;
170
171     /* Base case, no recursion is needed */
172     if (dst->containingAS() == this && src->containingAS() == this) {
173       if (bypassRoutes_.find({src, dst}) != bypassRoutes_.end()) {
174         AsRoute* bypassedRoute = bypassRoutes_.at({src, dst});
175         for (surf::Link* link : bypassedRoute->links) {
176           links->push_back(link);
177           if (latency)
178             *latency += link->latency();
179         }
180         XBT_DEBUG("Found a bypass route with %zu links", bypassedRoute->links.size());
181         return true;
182       }
183       return false;
184     }
185
186     /* Engage recursive search */
187
188
189     /* (1) find the path to the root routing component */
190     std::vector<AsImpl*> path_src;
191     As* current = src->containingAS();
192     while (current != nullptr) {
193       path_src.push_back(static_cast<AsImpl*>(current));
194       current = current->father_;
195     }
196
197     std::vector<AsImpl*> path_dst;
198     current = dst->containingAS();
199     while (current != nullptr) {
200       path_dst.push_back(static_cast<AsImpl*>(current));
201       current = current->father_;
202     }
203
204     /* (2) find the common father */
205     while (path_src.size() > 1 && path_dst.size() > 1 &&
206            path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
207       path_src.pop_back();
208       path_dst.pop_back();
209     }
210
211     int max_index_src = path_src.size() - 1;
212     int max_index_dst = path_dst.size() - 1;
213
214     int max_index = std::max(max_index_src, max_index_dst);
215
216     /* (3) Search for a bypass making the path up to the ancestor useless */
217     AsRoute* bypassedRoute = nullptr;
218     std::pair<kernel::routing::NetCard*, kernel::routing::NetCard*> key;
219     for (int max = 0; max <= max_index; max++) {
220       for (int i = 0; i < max; i++) {
221         if (i <= max_index_src && max <= max_index_dst) {
222           key = {path_src.at(i)->netcard_, path_dst.at(max)->netcard_};
223           if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
224             bypassedRoute = bypassRoutes_.at(key);
225             break;
226           }
227         }
228         if (max <= max_index_src && i <= max_index_dst) {
229           key = {path_src.at(max)->netcard_, path_dst.at(i)->netcard_};
230           if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
231             bypassedRoute = bypassRoutes_.at(key);
232             break;
233           }
234         }
235       }
236
237       if (bypassedRoute)
238         break;
239
240       if (max <= max_index_src && max <= max_index_dst) {
241         key = {path_src.at(max)->netcard_, path_dst.at(max)->netcard_};
242         if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
243           bypassedRoute = bypassRoutes_.at(key);
244           break;
245         }
246       }
247     }
248
249     /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
250     if (bypassedRoute) {
251       if (src != key.first)
252         getGlobalRoute(src, const_cast<NetCard*>(bypassedRoute->gw_src), links, latency);
253       for (surf::Link* link : bypassedRoute->links) {
254         links->push_back(link);
255         if (latency)
256           *latency += link->latency();
257       }
258       if (dst != key.second)
259         getGlobalRoute(const_cast<NetCard*>(bypassedRoute->gw_dst), dst, links, latency);
260       return true;
261     }
262     return false;
263     }
264
265     void AsImpl::getGlobalRoute(routing::NetCard* src, routing::NetCard* dst,
266                                 /* OUT */ std::vector<surf::Link*>* links, double* latency)
267     {
268       s_sg_platf_route_cbarg_t route;
269       memset(&route,0,sizeof(route));
270
271       XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src->name().c_str(), dst->name().c_str());
272
273       /* Find how src and dst are interconnected */
274       AsImpl *common_ancestor, *src_ancestor, *dst_ancestor;
275       find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
276       XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
277           common_ancestor->name(), src_ancestor->name(), dst_ancestor->name());
278
279       /* Check whether a direct bypass is defined. If so, use it and bail out */
280       if (common_ancestor->getBypassRoute(src, dst, links, latency))
281         return;
282
283       /* If src and dst are in the same AS, life is good */
284       if (src_ancestor == dst_ancestor) {       /* SURF_ROUTING_BASE */
285         route.link_list = links;
286         common_ancestor->getLocalRoute(src, dst, &route, latency);
287         return;
288       }
289
290       /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
291
292       route.link_list = new std::vector<surf::Link*>();
293
294       common_ancestor->getLocalRoute(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
295       xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr), "bad gateways for route from \"%s\" to \"%s\"",
296                  src->name().c_str(), dst->name().c_str());
297
298       /* If source gateway is not our source, we have to recursively find our way up to this point */
299       if (src != route.gw_src)
300         getGlobalRoute(src, route.gw_src, links, latency);
301       for (auto link: *route.link_list)
302         links->push_back(link);
303       delete route.link_list;
304
305       /* If dest gateway is not our destination, we have to recursively find our way from this point */
306       if (route.gw_dst != dst)
307         getGlobalRoute(route.gw_dst, dst, links, latency);
308     }
309
310 }}} // namespace