Logo AND Algorithmique Numérique Distribuée

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