Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'actor-yield' of github.com:Takishipp/simgrid into actor-yield
[simgrid.git] / src / kernel / routing / NetZoneImpl.cpp
1 /* Copyright (c) 2006-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 "src/kernel/routing/NetZoneImpl.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "src/kernel/routing/NetPoint.hpp"
10 #include "src/surf/cpu_interface.hpp"
11 #include "src/surf/network_interface.hpp"
12
13 #include "xbt/log.h"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_route);
16
17 namespace simgrid {
18 namespace kernel {
19 namespace routing {
20
21 class BypassRoute {
22 public:
23   explicit BypassRoute(NetPoint* gwSrc, NetPoint* gwDst) : gw_src(gwSrc), gw_dst(gwDst) {}
24   NetPoint* gw_src;
25   NetPoint* gw_dst;
26   std::vector<surf::LinkImpl*> links;
27 };
28
29 NetZoneImpl::NetZoneImpl(NetZone* father, std::string name) : NetZone(father, name)
30 {
31   xbt_assert(nullptr == simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name.c_str()),
32              "Refusing to create a second NetZone called '%s'.", name.c_str());
33
34   netpoint_ = new NetPoint(name, NetPoint::Type::NetZone, static_cast<NetZoneImpl*>(father));
35   XBT_DEBUG("NetZone '%s' created with the id '%u'", name.c_str(), netpoint_->id());
36 }
37
38 NetZoneImpl::~NetZoneImpl()
39 {
40   for (auto const& kv : bypassRoutes_)
41     delete kv.second;
42
43   simgrid::s4u::Engine::getInstance()->netpointUnregister(netpoint_);
44 }
45
46 simgrid::s4u::Host* NetZoneImpl::createHost(const char* name, std::vector<double>* speedPerPstate, int coreAmount,
47                                             std::map<std::string, std::string>* props)
48 {
49   simgrid::s4u::Host* res = new simgrid::s4u::Host(name);
50
51   if (hierarchy_ == RoutingMode::unset)
52     hierarchy_ = RoutingMode::base;
53
54   res->pimpl_netpoint = new NetPoint(name, NetPoint::Type::Host, this);
55
56   surf_cpu_model_pm->createCpu(res, speedPerPstate, coreAmount);
57
58   if (props != nullptr)
59     for (auto const& kv : *props)
60       res->setProperty(kv.first, kv.second);
61
62   simgrid::s4u::Host::onCreation(*res); // notify the signal
63
64   return res;
65 }
66
67 void NetZoneImpl::addBypassRoute(sg_platf_route_cbarg_t e_route)
68 {
69   /* Argument validity checks */
70   if (e_route->gw_dst) {
71     XBT_DEBUG("Load bypassNetzoneRoute from %s@%s to %s@%s", e_route->src->getCname(), e_route->gw_src->getCname(),
72               e_route->dst->getCname(), e_route->gw_dst->getCname());
73     xbt_assert(not e_route->link_list.empty(), "Bypass route between %s@%s and %s@%s cannot be empty.",
74                e_route->src->getCname(), e_route->gw_src->getCname(), e_route->dst->getCname(),
75                e_route->gw_dst->getCname());
76     xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
77                "The bypass route between %s@%s and %s@%s already exists.", e_route->src->getCname(),
78                e_route->gw_src->getCname(), e_route->dst->getCname(), e_route->gw_dst->getCname());
79   } else {
80     XBT_DEBUG("Load bypassRoute from %s to %s", e_route->src->getCname(), e_route->dst->getCname());
81     xbt_assert(not e_route->link_list.empty(), "Bypass route between %s and %s cannot be empty.",
82                e_route->src->getCname(), e_route->dst->getCname());
83     xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
84                "The bypass route between %s and %s already exists.", e_route->src->getCname(),
85                e_route->dst->getCname());
86   }
87
88   /* Build a copy that will be stored in the dict */
89   kernel::routing::BypassRoute* newRoute = new kernel::routing::BypassRoute(e_route->gw_src, e_route->gw_dst);
90   for (auto const& link : e_route->link_list)
91     newRoute->links.push_back(link);
92
93   /* Store it */
94   bypassRoutes_.insert({{e_route->src, e_route->dst}, newRoute});
95 }
96
97 /** @brief Get the common ancestor and its first children in each line leading to src and dst
98  *
99  * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
100  * @verbatim
101  *         platform root
102  *               |
103  *              ...                <- possibly long path
104  *               |
105  *         common_ancestor
106  *           /        \
107  *          /          \
108  *         /            \          <- direct links
109  *        /              \
110  *       /                \
111  *  src_ancestor     dst_ancestor  <- must be different in the recursive case
112  *      |                   |
113  *     ...                 ...     <-- possibly long paths (one hop or more)
114  *      |                   |
115  *     src                 dst
116  *  @endverbatim
117  *
118  *  In the base case (when src and dst are in the same netzone), things are as follows:
119  *  @verbatim
120  *                  platform root
121  *                        |
122  *                       ...                      <-- possibly long path
123  *                        |
124  * common_ancestor==src_ancestor==dst_ancestor    <-- all the same value
125  *                   /        \
126  *                  /          \                  <-- direct links (exactly one hop)
127  *                 /            \
128  *              src              dst
129  *  @endverbatim
130  *
131  * A specific recursive case occurs when src is the ancestor of dst. In this case,
132  * the base case routing should be used so the common_ancestor is specifically set
133  * to src_ancestor==dst_ancestor.
134  * Naturally, things are completely symmetrical if dst is the ancestor of src.
135  * @verbatim
136  *            platform root
137  *                  |
138  *                 ...                <-- possibly long path
139  *                  |
140  *  src == src_ancestor==dst_ancestor==common_ancestor <-- same value
141  *                  |
142  *                 ...                <-- possibly long path (one hop or more)
143  *                  |
144  *                 dst
145  *  @endverbatim
146  */
147 static void find_common_ancestors(NetPoint* src, NetPoint* dst,
148                                   /* OUT */ NetZoneImpl** common_ancestor, NetZoneImpl** src_ancestor,
149                                   NetZoneImpl** dst_ancestor)
150 {
151   /* Deal with the easy base case */
152   if (src->netzone() == dst->netzone()) {
153     *common_ancestor = src->netzone();
154     *src_ancestor    = *common_ancestor;
155     *dst_ancestor    = *common_ancestor;
156     return;
157   }
158
159   /* engage the full recursive search */
160
161   /* (1) find the path to root of src and dst*/
162   NetZoneImpl* src_as = src->netzone();
163   NetZoneImpl* dst_as = dst->netzone();
164
165   xbt_assert(src_as, "Host %s must be in a netzone", src->getCname());
166   xbt_assert(dst_as, "Host %s must be in a netzone", dst->getCname());
167
168   /* (2) find the path to the root routing component */
169   std::vector<NetZoneImpl*> path_src;
170   NetZoneImpl* current = src->netzone();
171   while (current != nullptr) {
172     path_src.push_back(current);
173     current = static_cast<NetZoneImpl*>(current->getFather());
174   }
175   std::vector<NetZoneImpl*> path_dst;
176   current = dst->netzone();
177   while (current != nullptr) {
178     path_dst.push_back(current);
179     current = static_cast<NetZoneImpl*>(current->getFather());
180   }
181
182   /* (3) find the common father.
183    * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
184    * So we move them down simultaneously as long as they point to the same content.
185    *
186    * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
187    */
188   NetZoneImpl* father = nullptr; // the netzone we dropped on the previous loop iteration
189   while (path_src.size() > 1 && path_dst.size() > 1 &&
190          path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
191     father = path_src.at(path_src.size() - 1);
192     path_src.pop_back();
193     path_dst.pop_back();
194   }
195
196   /* (4) we found the difference at least. Finalize the returned values */
197   *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
198   *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
199   if (*src_ancestor == *dst_ancestor) {             // src is the ancestor of dst, or the contrary
200     *common_ancestor = *src_ancestor;
201   } else {
202     *common_ancestor = father;
203   }
204 }
205
206 /* PRECONDITION: this is the common ancestor of src and dst */
207 bool NetZoneImpl::getBypassRoute(routing::NetPoint* src, routing::NetPoint* dst,
208                                  /* OUT */ std::vector<surf::LinkImpl*>& links, double* latency)
209 {
210   // If never set a bypass route return nullptr without any further computations
211   if (bypassRoutes_.empty())
212     return false;
213
214   /* Base case, no recursion is needed */
215   if (dst->netzone() == this && src->netzone() == this) {
216     if (bypassRoutes_.find({src, dst}) != bypassRoutes_.end()) {
217       BypassRoute* bypassedRoute = bypassRoutes_.at({src, dst});
218       for (surf::LinkImpl* const& link : bypassedRoute->links) {
219         links.push_back(link);
220         if (latency)
221           *latency += link->latency();
222       }
223       XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->getCname(), dst->getCname(),
224                 bypassedRoute->links.size());
225       return true;
226     }
227     return false;
228   }
229
230   /* Engage recursive search */
231
232   /* (1) find the path to the root routing component */
233   std::vector<NetZoneImpl*> path_src;
234   NetZone* current = src->netzone();
235   while (current != nullptr) {
236     path_src.push_back(static_cast<NetZoneImpl*>(current));
237     current = current->father_;
238   }
239
240   std::vector<NetZoneImpl*> path_dst;
241   current = dst->netzone();
242   while (current != nullptr) {
243     path_dst.push_back(static_cast<NetZoneImpl*>(current));
244     current = current->father_;
245   }
246
247   /* (2) find the common father */
248   while (path_src.size() > 1 && path_dst.size() > 1 &&
249          path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
250     path_src.pop_back();
251     path_dst.pop_back();
252   }
253
254   int max_index_src = path_src.size() - 1;
255   int max_index_dst = path_dst.size() - 1;
256
257   int max_index = std::max(max_index_src, max_index_dst);
258
259   /* (3) Search for a bypass making the path up to the ancestor useless */
260   BypassRoute* bypassedRoute = nullptr;
261   std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*> key;
262   for (int max = 0; max <= max_index; max++) {
263     for (int i = 0; i < max; i++) {
264       if (i <= max_index_src && max <= max_index_dst) {
265         key = {path_src.at(i)->netpoint_, path_dst.at(max)->netpoint_};
266         auto bpr = bypassRoutes_.find(key);
267         if (bpr != bypassRoutes_.end()) {
268           bypassedRoute = bpr->second;
269           break;
270         }
271       }
272       if (max <= max_index_src && i <= max_index_dst) {
273         key = {path_src.at(max)->netpoint_, path_dst.at(i)->netpoint_};
274         auto bpr = bypassRoutes_.find(key);
275         if (bpr != bypassRoutes_.end()) {
276           bypassedRoute = bpr->second;
277           break;
278         }
279       }
280     }
281
282     if (bypassedRoute)
283       break;
284
285     if (max <= max_index_src && max <= max_index_dst) {
286       key = {path_src.at(max)->netpoint_, path_dst.at(max)->netpoint_};
287       auto bpr = bypassRoutes_.find(key);
288       if (bpr != bypassRoutes_.end()) {
289         bypassedRoute = bpr->second;
290         break;
291       }
292     }
293   }
294
295   /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
296   if (bypassedRoute) {
297     XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
298               "calls to getRoute",
299               src->getCname(), dst->getCname(), bypassedRoute->links.size());
300     if (src != key.first)
301       getGlobalRoute(src, bypassedRoute->gw_src, links, latency);
302     for (surf::LinkImpl* const& link : bypassedRoute->links) {
303       links.push_back(link);
304       if (latency)
305         *latency += link->latency();
306     }
307     if (dst != key.second)
308       getGlobalRoute(bypassedRoute->gw_dst, dst, links, latency);
309     return true;
310   }
311   XBT_DEBUG("No bypass route from '%s' to '%s'.", src->getCname(), dst->getCname());
312   return false;
313 }
314
315 void NetZoneImpl::getGlobalRoute(routing::NetPoint* src, routing::NetPoint* dst,
316                                  /* OUT */ std::vector<surf::LinkImpl*>& links, double* latency)
317 {
318   s_sg_platf_route_cbarg_t route;
319
320   XBT_DEBUG("Resolve route from '%s' to '%s'", src->getCname(), dst->getCname());
321
322   /* Find how src and dst are interconnected */
323   NetZoneImpl *common_ancestor;
324   NetZoneImpl *src_ancestor;
325   NetZoneImpl *dst_ancestor;
326   find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
327   XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'", common_ancestor->getCname(),
328             src_ancestor->getCname(), dst_ancestor->getCname());
329
330   /* Check whether a direct bypass is defined. If so, use it and bail out */
331   if (common_ancestor->getBypassRoute(src, dst, links, latency))
332     return;
333
334   /* If src and dst are in the same netzone, life is good */
335   if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
336     route.link_list = std::move(links);
337     common_ancestor->getLocalRoute(src, dst, &route, latency);
338     links = std::move(route.link_list);
339     return;
340   }
341
342   /* Not in the same netzone, no bypass. We'll have to find our path between the netzones recursively */
343
344   common_ancestor->getLocalRoute(src_ancestor->netpoint_, dst_ancestor->netpoint_, &route, latency);
345   xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr), "bad gateways for route from \"%s\" to \"%s\"",
346              src->getCname(), dst->getCname());
347
348   /* If source gateway is not our source, we have to recursively find our way up to this point */
349   if (src != route.gw_src)
350     getGlobalRoute(src, route.gw_src, links, latency);
351   for (auto const& link : route.link_list)
352     links.push_back(link);
353
354   /* If dest gateway is not our destination, we have to recursively find our way from this point */
355   if (route.gw_dst != dst)
356     getGlobalRoute(route.gw_dst, dst, links, latency);
357 }
358 }
359 }
360 } // namespace