Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Say goodbye to last global: surf_host_model
[simgrid.git] / src / kernel / routing / NetZoneImpl.cpp
1 /* Copyright (c) 2006-2021. 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/kernel/resource/DiskImpl.hpp"
11 #include "src/surf/HostImpl.hpp"
12 #include "src/surf/cpu_interface.hpp"
13 #include "src/surf/network_interface.hpp"
14 #include "src/surf/xml/platf_private.hpp"
15 #include "surf/surf.hpp"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_route);
18
19 namespace simgrid {
20 namespace kernel {
21 namespace routing {
22
23 NetZoneImpl::NetZoneImpl(NetZoneImpl* father, const std::string& name, resource::NetworkModel* network_model)
24     : piface_(this), father_(father), name_(name), network_model_(network_model)
25 {
26   xbt_assert(nullptr == s4u::Engine::get_instance()->netpoint_by_name_or_null(get_name()),
27              "Refusing to create a second NetZone called '%s'.", get_cname());
28
29   netpoint_ = new NetPoint(name_, NetPoint::Type::NetZone, father_);
30   if (models_by_type[simgrid::kernel::resource::Model::Type::CPU_VM].size() > 0) {
31     cpu_model_vm_ = static_cast<simgrid::kernel::resource::CpuModel*>(
32         models_by_type[simgrid::kernel::resource::Model::Type::CPU_VM][0]);
33   }
34   cpu_model_pm_ = static_cast<simgrid::kernel::resource::CpuModel*>(
35       models_by_type[simgrid::kernel::resource::Model::Type::CPU_PM][0]);
36   disk_model_ = static_cast<simgrid::kernel::resource::DiskModel*>(
37       models_by_type[simgrid::kernel::resource::Model::Type::DISK][0]);
38   // FIXME[donassolo]: we probably need some validation of the coherence among
39   // the different models in each netZone
40   host_model_ = static_cast<simgrid::surf::HostModel*>(models_by_type[simgrid::kernel::resource::Model::Type::HOST][0]);
41   XBT_DEBUG("NetZone '%s' created with the id '%u'", get_cname(), netpoint_->id());
42 }
43
44 NetZoneImpl::~NetZoneImpl()
45 {
46   for (auto const& nz : children_)
47     delete nz;
48
49   for (auto const& kv : bypass_routes_)
50     delete kv.second;
51
52   s4u::Engine::get_instance()->netpoint_unregister(netpoint_);
53 }
54
55 /** @brief Returns the list of the hosts found in this NetZone (not recursively)
56  *
57  * Only the hosts that are directly contained in this NetZone are retrieved,
58  * not the ones contained in sub-netzones.
59  */
60 std::vector<s4u::Host*> NetZoneImpl::get_all_hosts() const
61 {
62   std::vector<s4u::Host*> res;
63   for (auto const& card : get_vertices()) {
64     s4u::Host* host = s4u::Host::by_name_or_null(card->get_name());
65     if (host != nullptr)
66       res.push_back(host);
67   }
68   return res;
69 }
70 int NetZoneImpl::get_host_count() const
71 {
72   int count = 0;
73   for (auto const& card : get_vertices()) {
74     const s4u::Host* host = s4u::Host::by_name_or_null(card->get_name());
75     if (host != nullptr)
76       count++;
77   }
78   return count;
79 }
80
81 s4u::Disk* NetZoneImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
82 {
83   auto* l = disk_model_->create_disk(name, read_bandwidth, write_bandwidth);
84
85   return l->get_iface();
86 }
87
88 s4u::Link* NetZoneImpl::create_link(const std::string& name, const std::vector<double>& bandwidths,
89                                     s4u::Link::SharingPolicy policy)
90 {
91   auto* l = network_model_->create_link(name, bandwidths, policy);
92
93   return l->get_iface();
94 }
95
96 s4u::Host* NetZoneImpl::create_host(const std::string& name, const std::vector<double>& speed_per_pstate,
97                                     int core_amount)
98 {
99   if (hierarchy_ == RoutingMode::unset)
100     hierarchy_ = RoutingMode::base;
101
102   auto* res = new s4u::Host(name);
103   res->set_netpoint(new NetPoint(name, NetPoint::Type::Host, this));
104
105   cpu_model_pm_->create_cpu(res, speed_per_pstate)->set_core_count(core_amount)->seal();
106
107   return res;
108 }
109
110 int NetZoneImpl::add_component(NetPoint* elm)
111 {
112   vertices_.push_back(elm);
113   return vertices_.size() - 1; // The rank of the newly created object
114 }
115
116 void NetZoneImpl::add_route(NetPoint* /*src*/, NetPoint* /*dst*/, NetPoint* /*gw_src*/, NetPoint* /*gw_dst*/,
117                             std::vector<resource::LinkImpl*>& /*link_list*/, bool /*symmetrical*/)
118 {
119   xbt_die("NetZone '%s' does not accept new routes (wrong class).", get_cname());
120 }
121
122 void NetZoneImpl::add_bypass_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
123                                    std::vector<resource::LinkImpl*>& link_list, bool /* symmetrical */)
124 {
125   /* Argument validity checks */
126   if (gw_dst) {
127     XBT_DEBUG("Load bypassNetzoneRoute from %s@%s to %s@%s", src->get_cname(), gw_src->get_cname(), dst->get_cname(),
128               gw_dst->get_cname());
129     xbt_assert(not link_list.empty(), "Bypass route between %s@%s and %s@%s cannot be empty.", src->get_cname(),
130                gw_src->get_cname(), dst->get_cname(), gw_dst->get_cname());
131     xbt_assert(bypass_routes_.find({src, dst}) == bypass_routes_.end(),
132                "The bypass route between %s@%s and %s@%s already exists.", src->get_cname(), gw_src->get_cname(),
133                dst->get_cname(), gw_dst->get_cname());
134   } else {
135     XBT_DEBUG("Load bypassRoute from %s to %s", src->get_cname(), dst->get_cname());
136     xbt_assert(not link_list.empty(), "Bypass route between %s and %s cannot be empty.", src->get_cname(),
137                dst->get_cname());
138     xbt_assert(bypass_routes_.find({src, dst}) == bypass_routes_.end(),
139                "The bypass route between %s and %s already exists.", src->get_cname(), dst->get_cname());
140   }
141
142   /* Build a copy that will be stored in the dict */
143   auto* newRoute = new BypassRoute(gw_src, gw_dst);
144   for (auto const& link : link_list)
145     newRoute->links.push_back(link);
146
147   /* Store it */
148   bypass_routes_.insert({{src, dst}, newRoute});
149 }
150
151 /** @brief Get the common ancestor and its first children in each line leading to src and dst
152  *
153  * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
154  * @verbatim
155  *         platform root
156  *               |
157  *              ...                <- possibly long path
158  *               |
159  *         common_ancestor
160  *           /        \
161  *          /          \
162  *         /            \          <- direct links
163  *        /              \
164  *       /                \
165  *  src_ancestor     dst_ancestor  <- must be different in the recursive case
166  *      |                   |
167  *     ...                 ...     <-- possibly long paths (one hop or more)
168  *      |                   |
169  *     src                 dst
170  *  @endverbatim
171  *
172  *  In the base case (when src and dst are in the same netzone), things are as follows:
173  *  @verbatim
174  *                  platform root
175  *                        |
176  *                       ...                      <-- possibly long path
177  *                        |
178  * common_ancestor==src_ancestor==dst_ancestor    <-- all the same value
179  *                   /        \
180  *                  /          \                  <-- direct links (exactly one hop)
181  *                 /            \
182  *              src              dst
183  *  @endverbatim
184  *
185  * A specific recursive case occurs when src is the ancestor of dst. In this case,
186  * the base case routing should be used so the common_ancestor is specifically set
187  * to src_ancestor==dst_ancestor.
188  * Naturally, things are completely symmetrical if dst is the ancestor of src.
189  * @verbatim
190  *            platform root
191  *                  |
192  *                 ...                <-- possibly long path
193  *                  |
194  *  src == src_ancestor==dst_ancestor==common_ancestor <-- same value
195  *                  |
196  *                 ...                <-- possibly long path (one hop or more)
197  *                  |
198  *                 dst
199  *  @endverbatim
200  */
201 static void find_common_ancestors(NetPoint* src, NetPoint* dst,
202                                   /* OUT */ NetZoneImpl** common_ancestor, NetZoneImpl** src_ancestor,
203                                   NetZoneImpl** dst_ancestor)
204 {
205   /* Deal with the easy base case */
206   if (src->get_englobing_zone() == dst->get_englobing_zone()) {
207     *common_ancestor = src->get_englobing_zone();
208     *src_ancestor    = *common_ancestor;
209     *dst_ancestor    = *common_ancestor;
210     return;
211   }
212
213   /* engage the full recursive search */
214
215   /* (1) find the path to root of src and dst*/
216   const NetZoneImpl* src_as = src->get_englobing_zone();
217   const NetZoneImpl* dst_as = dst->get_englobing_zone();
218
219   xbt_assert(src_as, "Host %s must be in a netzone", src->get_cname());
220   xbt_assert(dst_as, "Host %s must be in a netzone", dst->get_cname());
221
222   /* (2) find the path to the root routing component */
223   std::vector<NetZoneImpl*> path_src;
224   NetZoneImpl* current = src->get_englobing_zone();
225   while (current != nullptr) {
226     path_src.push_back(current);
227     current = current->get_father();
228   }
229   std::vector<NetZoneImpl*> path_dst;
230   current = dst->get_englobing_zone();
231   while (current != nullptr) {
232     path_dst.push_back(current);
233     current = current->get_father();
234   }
235
236   /* (3) find the common father.
237    * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
238    * So we move them down simultaneously as long as they point to the same content.
239    *
240    * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
241    */
242   NetZoneImpl* father = nullptr; // the netzone we dropped on the previous loop iteration
243   while (path_src.size() > 1 && path_dst.size() > 1 &&
244          path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
245     father = path_src.at(path_src.size() - 1);
246     path_src.pop_back();
247     path_dst.pop_back();
248   }
249
250   /* (4) we found the difference at least. Finalize the returned values */
251   *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
252   *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
253   if (*src_ancestor == *dst_ancestor) {             // src is the ancestor of dst, or the contrary
254     *common_ancestor = *src_ancestor;
255   } else {
256     *common_ancestor = father;
257   }
258 }
259
260 /* PRECONDITION: this is the common ancestor of src and dst */
261 bool NetZoneImpl::get_bypass_route(NetPoint* src, NetPoint* dst,
262                                    /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency)
263 {
264   // If never set a bypass route return nullptr without any further computations
265   if (bypass_routes_.empty())
266     return false;
267
268   /* Base case, no recursion is needed */
269   if (dst->get_englobing_zone() == this && src->get_englobing_zone() == this) {
270     if (bypass_routes_.find({src, dst}) != bypass_routes_.end()) {
271       const BypassRoute* bypassedRoute = bypass_routes_.at({src, dst});
272       for (resource::LinkImpl* const& link : bypassedRoute->links) {
273         links.push_back(link);
274         if (latency)
275           *latency += link->get_latency();
276       }
277       XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->get_cname(), dst->get_cname(),
278                 bypassedRoute->links.size());
279       return true;
280     }
281     return false;
282   }
283
284   /* Engage recursive search */
285
286   /* (1) find the path to the root routing component */
287   std::vector<NetZoneImpl*> path_src;
288   NetZoneImpl* current = src->get_englobing_zone();
289   while (current != nullptr) {
290     path_src.push_back(current);
291     current = current->father_;
292   }
293
294   std::vector<NetZoneImpl*> path_dst;
295   current = dst->get_englobing_zone();
296   while (current != nullptr) {
297     path_dst.push_back(current);
298     current = current->father_;
299   }
300
301   /* (2) find the common father */
302   while (path_src.size() > 1 && path_dst.size() > 1 &&
303          path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
304     path_src.pop_back();
305     path_dst.pop_back();
306   }
307
308   int max_index_src = path_src.size() - 1;
309   int max_index_dst = path_dst.size() - 1;
310
311   int max_index = std::max(max_index_src, max_index_dst);
312
313   /* (3) Search for a bypass making the path up to the ancestor useless */
314   const BypassRoute* bypassedRoute = nullptr;
315   std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*> key;
316   for (int max = 0; max <= max_index && not bypassedRoute; max++) {
317     for (int i = 0; i < max && not bypassedRoute; i++) {
318       if (i <= max_index_src && max <= max_index_dst) {
319         key      = {path_src.at(i)->netpoint_, path_dst.at(max)->netpoint_};
320         auto bpr = bypass_routes_.find(key);
321         if (bpr != bypass_routes_.end()) {
322           bypassedRoute = bpr->second;
323         }
324       }
325       if (not bypassedRoute && max <= max_index_src && i <= max_index_dst) {
326         key      = {path_src.at(max)->netpoint_, path_dst.at(i)->netpoint_};
327         auto bpr = bypass_routes_.find(key);
328         if (bpr != bypass_routes_.end()) {
329           bypassedRoute = bpr->second;
330         }
331       }
332     }
333
334     if (not bypassedRoute && max <= max_index_src && max <= max_index_dst) {
335       key      = {path_src.at(max)->netpoint_, path_dst.at(max)->netpoint_};
336       auto bpr = bypass_routes_.find(key);
337       if (bpr != bypass_routes_.end()) {
338         bypassedRoute = bpr->second;
339       }
340     }
341   }
342
343   /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
344   if (bypassedRoute) {
345     XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
346               "calls to getRoute",
347               src->get_cname(), dst->get_cname(), bypassedRoute->links.size());
348     if (src != key.first)
349       get_global_route(src, bypassedRoute->gw_src, links, latency);
350     for (resource::LinkImpl* const& link : bypassedRoute->links) {
351       links.push_back(link);
352       if (latency)
353         *latency += link->get_latency();
354     }
355     if (dst != key.second)
356       get_global_route(bypassedRoute->gw_dst, dst, links, latency);
357     return true;
358   }
359   XBT_DEBUG("No bypass route from '%s' to '%s'.", src->get_cname(), dst->get_cname());
360   return false;
361 }
362
363 void NetZoneImpl::get_global_route(NetPoint* src, NetPoint* dst,
364                                    /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency)
365 {
366   RouteCreationArgs route;
367
368   XBT_DEBUG("Resolve route from '%s' to '%s'", src->get_cname(), dst->get_cname());
369
370   /* Find how src and dst are interconnected */
371   NetZoneImpl* common_ancestor;
372   NetZoneImpl* src_ancestor;
373   NetZoneImpl* dst_ancestor;
374   find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
375   XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'", common_ancestor->get_cname(),
376             src_ancestor->get_cname(), dst_ancestor->get_cname());
377
378   /* Check whether a direct bypass is defined. If so, use it and bail out */
379   if (common_ancestor->get_bypass_route(src, dst, links, latency))
380     return;
381
382   /* If src and dst are in the same netzone, life is good */
383   if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
384     route.link_list = std::move(links);
385     common_ancestor->get_local_route(src, dst, &route, latency);
386     links = std::move(route.link_list);
387     return;
388   }
389
390   /* Not in the same netzone, no bypass. We'll have to find our path between the netzones recursively */
391
392   common_ancestor->get_local_route(src_ancestor->netpoint_, dst_ancestor->netpoint_, &route, latency);
393   xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr), "Bad gateways for route from '%s' to '%s'.",
394              src->get_cname(), dst->get_cname());
395
396   /* If source gateway is not our source, we have to recursively find our way up to this point */
397   if (src != route.gw_src)
398     get_global_route(src, route.gw_src, links, latency);
399   for (auto const& link : route.link_list)
400     links.push_back(link);
401
402   /* If dest gateway is not our destination, we have to recursively find our way from this point */
403   if (route.gw_dst != dst)
404     get_global_route(route.gw_dst, dst, links, latency);
405 }
406 } // namespace routing
407 } // namespace kernel
408 } // namespace simgrid