Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
AS->Zone + stringification
[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& 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 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->cname(), e_route->gw_src->cname(),
72               e_route->dst->cname(), e_route->gw_dst->cname());
73     xbt_assert(not e_route->link_list->empty(), "Bypass route between %s@%s and %s@%s cannot be empty.",
74                e_route->src->cname(), e_route->gw_src->cname(), e_route->dst->cname(), e_route->gw_dst->cname());
75     xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
76                "The bypass route between %s@%s and %s@%s already exists.", e_route->src->cname(),
77                e_route->gw_src->cname(), e_route->dst->cname(), e_route->gw_dst->cname());
78   } else {
79     XBT_DEBUG("Load bypassRoute from %s to %s", e_route->src->cname(), e_route->dst->cname());
80     xbt_assert(not e_route->link_list->empty(), "Bypass route between %s and %s cannot be empty.",
81                e_route->src->cname(), e_route->dst->cname());
82     xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
83                "The bypass route between %s and %s already exists.", e_route->src->cname(), e_route->dst->cname());
84   }
85
86   /* Build a copy that will be stored in the dict */
87   kernel::routing::BypassRoute* newRoute = new kernel::routing::BypassRoute(e_route->gw_src, e_route->gw_dst);
88   for (auto link : *e_route->link_list)
89     newRoute->links.push_back(link);
90
91   /* Store it */
92   bypassRoutes_.insert({{e_route->src, e_route->dst}, newRoute});
93 }
94
95 /** @brief Get the common ancestor and its first children in each line leading to src and dst
96  *
97  * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
98  * @verbatim
99  *         platform root
100  *               |
101  *              ...                <- possibly long path
102  *               |
103  *         common_ancestor
104  *           /        \
105  *          /          \
106  *         /            \          <- direct links
107  *        /              \
108  *       /                \
109  *  src_ancestor     dst_ancestor  <- must be different in the recursive case
110  *      |                   |
111  *     ...                 ...     <-- possibly long paths (one hop or more)
112  *      |                   |
113  *     src                 dst
114  *  @endverbatim
115  *
116  *  In the base case (when src and dst are in the same netzone), things are as follows:
117  *  @verbatim
118  *                  platform root
119  *                        |
120  *                       ...                      <-- possibly long path
121  *                        |
122  * common_ancestor==src_ancestor==dst_ancestor    <-- all the same value
123  *                   /        \
124  *                  /          \                  <-- direct links (exactly one hop)
125  *                 /            \
126  *              src              dst
127  *  @endverbatim
128  *
129  * A specific recursive case occurs when src is the ancestor of dst. In this case,
130  * the base case routing should be used so the common_ancestor is specifically set
131  * to src_ancestor==dst_ancestor.
132  * Naturally, things are completely symmetrical if dst is the ancestor of src.
133  * @verbatim
134  *            platform root
135  *                  |
136  *                 ...                <-- possibly long path
137  *                  |
138  *  src == src_ancestor==dst_ancestor==common_ancestor <-- same value
139  *                  |
140  *                 ...                <-- possibly long path (one hop or more)
141  *                  |
142  *                 dst
143  *  @endverbatim
144  */
145 static void find_common_ancestors(NetPoint* src, NetPoint* dst,
146                                   /* OUT */ NetZoneImpl** common_ancestor, NetZoneImpl** src_ancestor,
147                                   NetZoneImpl** dst_ancestor)
148 {
149   /* Deal with the easy base case */
150   if (src->netzone() == dst->netzone()) {
151     *common_ancestor = src->netzone();
152     *src_ancestor    = *common_ancestor;
153     *dst_ancestor    = *common_ancestor;
154     return;
155   }
156
157   /* engage the full recursive search */
158
159   /* (1) find the path to root of src and dst*/
160   NetZoneImpl* src_as = src->netzone();
161   NetZoneImpl* dst_as = dst->netzone();
162
163   xbt_assert(src_as, "Host %s must be in a netzone", src->cname());
164   xbt_assert(dst_as, "Host %s must be in a netzone", dst->cname());
165
166   /* (2) find the path to the root routing component */
167   std::vector<NetZoneImpl*> path_src;
168   NetZoneImpl* current = src->netzone();
169   while (current != nullptr) {
170     path_src.push_back(current);
171     current = static_cast<NetZoneImpl*>(current->getFather());
172   }
173   std::vector<NetZoneImpl*> path_dst;
174   current = dst->netzone();
175   while (current != nullptr) {
176     path_dst.push_back(current);
177     current = static_cast<NetZoneImpl*>(current->getFather());
178   }
179
180   /* (3) find the common father.
181    * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
182    * So we move them down simultaneously as long as they point to the same content.
183    *
184    * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
185    */
186   NetZoneImpl* father = nullptr; // the netzone we dropped on the previous loop iteration
187   while (path_src.size() > 1 && path_dst.size() > 1 &&
188          path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
189     father = path_src.at(path_src.size() - 1);
190     path_src.pop_back();
191     path_dst.pop_back();
192   }
193
194   /* (4) we found the difference at least. Finalize the returned values */
195   *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
196   *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
197   if (*src_ancestor == *dst_ancestor) {             // src is the ancestor of dst, or the contrary
198     *common_ancestor = *src_ancestor;
199   } else {
200     *common_ancestor = father;
201   }
202 }
203
204 /* PRECONDITION: this is the common ancestor of src and dst */
205 bool NetZoneImpl::getBypassRoute(routing::NetPoint* src, routing::NetPoint* dst,
206                                  /* OUT */ std::vector<surf::LinkImpl*>* links, double* latency)
207 {
208   // If never set a bypass route return nullptr without any further computations
209   if (bypassRoutes_.empty())
210     return false;
211
212   /* Base case, no recursion is needed */
213   if (dst->netzone() == this && src->netzone() == this) {
214     if (bypassRoutes_.find({src, dst}) != bypassRoutes_.end()) {
215       BypassRoute* bypassedRoute = bypassRoutes_.at({src, dst});
216       for (surf::LinkImpl* link : bypassedRoute->links) {
217         links->push_back(link);
218         if (latency)
219           *latency += link->latency();
220       }
221       XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->cname(), dst->cname(),
222                 bypassedRoute->links.size());
223       return true;
224     }
225     return false;
226   }
227
228   /* Engage recursive search */
229
230   /* (1) find the path to the root routing component */
231   std::vector<NetZoneImpl*> path_src;
232   NetZone* current = src->netzone();
233   while (current != nullptr) {
234     path_src.push_back(static_cast<NetZoneImpl*>(current));
235     current = current->father_;
236   }
237
238   std::vector<NetZoneImpl*> path_dst;
239   current = dst->netzone();
240   while (current != nullptr) {
241     path_dst.push_back(static_cast<NetZoneImpl*>(current));
242     current = current->father_;
243   }
244
245   /* (2) find the common father */
246   while (path_src.size() > 1 && path_dst.size() > 1 &&
247          path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
248     path_src.pop_back();
249     path_dst.pop_back();
250   }
251
252   int max_index_src = path_src.size() - 1;
253   int max_index_dst = path_dst.size() - 1;
254
255   int max_index = std::max(max_index_src, max_index_dst);
256
257   /* (3) Search for a bypass making the path up to the ancestor useless */
258   BypassRoute* bypassedRoute = nullptr;
259   std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*> key;
260   for (int max = 0; max <= max_index; max++) {
261     for (int i = 0; i < max; i++) {
262       if (i <= max_index_src && max <= max_index_dst) {
263         key = {path_src.at(i)->netpoint_, path_dst.at(max)->netpoint_};
264         auto bpr = bypassRoutes_.find(key);
265         if (bpr != bypassRoutes_.end()) {
266           bypassedRoute = bpr->second;
267           break;
268         }
269       }
270       if (max <= max_index_src && i <= max_index_dst) {
271         key = {path_src.at(max)->netpoint_, path_dst.at(i)->netpoint_};
272         auto bpr = bypassRoutes_.find(key);
273         if (bpr != bypassRoutes_.end()) {
274           bypassedRoute = bpr->second;
275           break;
276         }
277       }
278     }
279
280     if (bypassedRoute)
281       break;
282
283     if (max <= max_index_src && max <= max_index_dst) {
284       key = {path_src.at(max)->netpoint_, path_dst.at(max)->netpoint_};
285       auto bpr = bypassRoutes_.find(key);
286       if (bpr != bypassRoutes_.end()) {
287         bypassedRoute = bpr->second;
288         break;
289       }
290     }
291   }
292
293   /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
294   if (bypassedRoute) {
295     XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
296               "calls to getRoute",
297               src->cname(), dst->cname(), bypassedRoute->links.size());
298     if (src != key.first)
299       getGlobalRoute(src, bypassedRoute->gw_src, links, latency);
300     for (surf::LinkImpl* link : bypassedRoute->links) {
301       links->push_back(link);
302       if (latency)
303         *latency += link->latency();
304     }
305     if (dst != key.second)
306       getGlobalRoute(bypassedRoute->gw_dst, dst, links, latency);
307     return true;
308   }
309   XBT_DEBUG("No bypass route from '%s' to '%s'.", src->cname(), dst->cname());
310   return false;
311 }
312
313 void NetZoneImpl::getGlobalRoute(routing::NetPoint* src, routing::NetPoint* dst,
314                                  /* OUT */ std::vector<surf::LinkImpl*>* links, double* latency)
315 {
316   s_sg_platf_route_cbarg_t route;
317   memset(&route, 0, sizeof(route));
318
319   XBT_DEBUG("Resolve route from '%s' to '%s'", src->cname(), dst->cname());
320
321   /* Find how src and dst are interconnected */
322   NetZoneImpl *common_ancestor;
323   NetZoneImpl *src_ancestor;
324   NetZoneImpl *dst_ancestor;
325   find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
326   XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'", common_ancestor->getCname(),
327             src_ancestor->getCname(), dst_ancestor->getCname());
328
329   /* Check whether a direct bypass is defined. If so, use it and bail out */
330   if (common_ancestor->getBypassRoute(src, dst, links, latency))
331     return;
332
333   /* If src and dst are in the same netzone, life is good */
334   if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
335     route.link_list = links;
336     common_ancestor->getLocalRoute(src, dst, &route, latency);
337     return;
338   }
339
340   /* Not in the same netzone, no bypass. We'll have to find our path between the netzones recursively */
341
342   route.link_list = new std::vector<surf::LinkImpl*>();
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->cname(), dst->cname());
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 link : *route.link_list)
352     links->push_back(link);
353   delete route.link_list;
354
355   /* If dest gateway is not our destination, we have to recursively find our way from this point */
356   if (route.gw_dst != dst)
357     getGlobalRoute(route.gw_dst, dst, links, latency);
358 }
359 }
360 }
361 } // namespace