Logo AND Algorithmique Numérique Distribuée

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