Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Moving the models initialization to NetZoneImpl.
[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/include/simgrid/sg_config.hpp"
11 #include "src/kernel/EngineImpl.hpp"
12 #include "src/kernel/resource/DiskImpl.hpp"
13 #include "src/surf/HostImpl.hpp"
14 #include "src/surf/cpu_interface.hpp"
15 #include "src/surf/network_interface.hpp"
16 #include "src/surf/xml/platf_private.hpp"
17 #include "surf/surf.hpp"
18
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_route);
20
21 namespace simgrid {
22 namespace kernel {
23 namespace routing {
24
25 /* Pick the right models for CPU, net and host, and call their model_init_preparse */
26 static void surf_config_models_setup()
27 {
28   std::string host_model_name    = simgrid::config::get_value<std::string>("host/model");
29   std::string network_model_name = simgrid::config::get_value<std::string>("network/model");
30   std::string cpu_model_name     = simgrid::config::get_value<std::string>("cpu/model");
31   std::string disk_model_name    = simgrid::config::get_value<std::string>("disk/model");
32
33   /* The compound host model is needed when using non-default net/cpu models */
34   if ((not simgrid::config::is_default("network/model") || not simgrid::config::is_default("cpu/model")) &&
35       simgrid::config::is_default("host/model")) {
36     host_model_name = "compound";
37     simgrid::config::set_value("host/model", host_model_name);
38   }
39
40   XBT_DEBUG("host model: %s", host_model_name.c_str());
41   if (host_model_name == "compound") {
42     xbt_assert(not cpu_model_name.empty(), "Set a cpu model to use with the 'compound' host model");
43     xbt_assert(not network_model_name.empty(), "Set a network model to use with the 'compound' host model");
44
45     int cpu_id = find_model_description(surf_cpu_model_description, cpu_model_name);
46     surf_cpu_model_description[cpu_id].model_init_preparse();
47
48     int network_id = find_model_description(surf_network_model_description, network_model_name);
49     surf_network_model_description[network_id].model_init_preparse();
50   }
51
52   XBT_DEBUG("Call host_model_init");
53   int host_id = find_model_description(surf_host_model_description, host_model_name);
54   surf_host_model_description[host_id].model_init_preparse();
55
56   XBT_DEBUG("Call vm_model_init");
57   /* ideally we should get back the pointer to CpuModel from model_init_preparse(), but this
58    * requires changing the declaration of surf_cpu_model_description.
59    * To be reviewed in the future */
60   surf_vm_model_init_HL13(
61       simgrid::s4u::Engine::get_instance()->get_netzone_root()->get_impl()->get_cpu_pm_model().get());
62
63   XBT_DEBUG("Call disk_model_init");
64   int disk_id = find_model_description(surf_disk_model_description, disk_model_name);
65   surf_disk_model_description[disk_id].model_init_preparse();
66 }
67
68 NetZoneImpl::NetZoneImpl(const std::string& name) : piface_(this), name_(name)
69 {
70   /* workaroud: first netzoneImpl will be the root netzone.
71    * Without globals and with current surf_*_model_description init functions, we need
72    * the root netzone to exist when creating the models.
73    * This was usually done at sg_platf.cpp, during XML parsing */
74   if (not s4u::Engine::get_instance()->get_netzone_root())
75     s4u::Engine::get_instance()->set_netzone_root(&piface_);
76
77   static bool surf_parse_models_setup_already_called = false;
78   if (not surf_parse_models_setup_already_called) {
79     simgrid::s4u::Engine::on_platform_creation();
80
81     /* Initialize the surf models. That must be done after we got all config, and before we need the models.
82      * That is, after the last <config> tag, if any, and before the first of cluster|peer|zone|trace|trace_connect
83      *
84      * I'm not sure for <trace> and <trace_connect>, there may be a bug here
85      * (FIXME: check it out by creating a file beginning with one of these tags)
86      * but cluster and peer come down to zone creations, so putting this verification here is correct.
87      */
88     surf_parse_models_setup_already_called = true;
89     surf_config_models_setup();
90   }
91
92   xbt_assert(nullptr == s4u::Engine::get_instance()->netpoint_by_name_or_null(get_name()),
93              "Refusing to create a second NetZone called '%s'.", get_cname());
94   netpoint_ = new NetPoint(name_, NetPoint::Type::NetZone);
95   XBT_DEBUG("NetZone '%s' created with the id '%u'", get_cname(), netpoint_->id());
96 }
97
98 NetZoneImpl::~NetZoneImpl()
99 {
100   for (auto const& nz : children_)
101     delete nz;
102
103   for (auto const& kv : bypass_routes_)
104     delete kv.second;
105
106   s4u::Engine::get_instance()->netpoint_unregister(netpoint_);
107 }
108
109 void NetZoneImpl::add_child(NetZoneImpl* new_zone)
110 {
111   xbt_assert(not sealed_, "Cannot add a new child to the sealed zone %s", get_cname());
112   children_.push_back(new_zone);
113 }
114
115 /** @brief Returns the list of the hosts found in this NetZone (not recursively)
116  *
117  * Only the hosts that are directly contained in this NetZone are retrieved,
118  * not the ones contained in sub-netzones.
119  */
120 std::vector<s4u::Host*> NetZoneImpl::get_all_hosts() const
121 {
122   std::vector<s4u::Host*> res;
123   for (auto const& card : get_vertices()) {
124     s4u::Host* host = s4u::Host::by_name_or_null(card->get_name());
125     if (host != nullptr)
126       res.push_back(host);
127   }
128   return res;
129 }
130 int NetZoneImpl::get_host_count() const
131 {
132   int count = 0;
133   for (auto const& card : get_vertices()) {
134     const s4u::Host* host = s4u::Host::by_name_or_null(card->get_name());
135     if (host != nullptr)
136       count++;
137   }
138   return count;
139 }
140
141 s4u::Disk* NetZoneImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
142 {
143   auto* l = disk_model_->create_disk(name, read_bandwidth, write_bandwidth);
144
145   return l->get_iface();
146 }
147
148 s4u::Link* NetZoneImpl::create_link(const std::string& name, const std::vector<double>& bandwidths,
149                                     s4u::Link::SharingPolicy policy)
150 {
151   auto* l = network_model_->create_link(name, bandwidths, policy);
152
153   return l->get_iface();
154 }
155
156 s4u::Host* NetZoneImpl::create_host(const std::string& name, const std::vector<double>& speed_per_pstate,
157                                     int core_amount)
158 {
159   if (hierarchy_ == RoutingMode::unset)
160     hierarchy_ = RoutingMode::base;
161
162   auto* res = (new surf::HostImpl(name))->get_iface();
163   res->set_netpoint((new NetPoint(name, NetPoint::Type::Host))->set_englobing_zone(this));
164
165   cpu_model_pm_->create_cpu(res, speed_per_pstate)->set_core_count(core_amount)->seal();
166
167   return res;
168 }
169
170 int NetZoneImpl::add_component(NetPoint* elm)
171 {
172   vertices_.push_back(elm);
173   return vertices_.size() - 1; // The rank of the newly created object
174 }
175
176 void NetZoneImpl::add_route(NetPoint* /*src*/, NetPoint* /*dst*/, NetPoint* /*gw_src*/, NetPoint* /*gw_dst*/,
177                             std::vector<resource::LinkImpl*>& /*link_list*/, bool /*symmetrical*/)
178 {
179   xbt_die("NetZone '%s' does not accept new routes (wrong class).", get_cname());
180 }
181
182 void NetZoneImpl::add_bypass_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
183                                    std::vector<resource::LinkImpl*>& link_list, bool /* symmetrical */)
184 {
185   /* Argument validity checks */
186   if (gw_dst) {
187     XBT_DEBUG("Load bypassNetzoneRoute from %s@%s to %s@%s", src->get_cname(), gw_src->get_cname(), dst->get_cname(),
188               gw_dst->get_cname());
189     xbt_assert(not link_list.empty(), "Bypass route between %s@%s and %s@%s cannot be empty.", src->get_cname(),
190                gw_src->get_cname(), dst->get_cname(), gw_dst->get_cname());
191     xbt_assert(bypass_routes_.find({src, dst}) == bypass_routes_.end(),
192                "The bypass route between %s@%s and %s@%s already exists.", src->get_cname(), gw_src->get_cname(),
193                dst->get_cname(), gw_dst->get_cname());
194   } else {
195     XBT_DEBUG("Load bypassRoute from %s to %s", src->get_cname(), dst->get_cname());
196     xbt_assert(not link_list.empty(), "Bypass route between %s and %s cannot be empty.", src->get_cname(),
197                dst->get_cname());
198     xbt_assert(bypass_routes_.find({src, dst}) == bypass_routes_.end(),
199                "The bypass route between %s and %s already exists.", src->get_cname(), dst->get_cname());
200   }
201
202   /* Build a copy that will be stored in the dict */
203   auto* newRoute = new BypassRoute(gw_src, gw_dst);
204   for (auto const& link : link_list)
205     newRoute->links.push_back(link);
206
207   /* Store it */
208   bypass_routes_.insert({{src, dst}, newRoute});
209 }
210
211 /** @brief Get the common ancestor and its first children in each line leading to src and dst
212  *
213  * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
214  * @verbatim
215  *         platform root
216  *               |
217  *              ...                <- possibly long path
218  *               |
219  *         common_ancestor
220  *           /        \
221  *          /          \
222  *         /            \          <- direct links
223  *        /              \
224  *       /                \
225  *  src_ancestor     dst_ancestor  <- must be different in the recursive case
226  *      |                   |
227  *     ...                 ...     <-- possibly long paths (one hop or more)
228  *      |                   |
229  *     src                 dst
230  *  @endverbatim
231  *
232  *  In the base case (when src and dst are in the same netzone), things are as follows:
233  *  @verbatim
234  *                  platform root
235  *                        |
236  *                       ...                      <-- possibly long path
237  *                        |
238  * common_ancestor==src_ancestor==dst_ancestor    <-- all the same value
239  *                   /        \
240  *                  /          \                  <-- direct links (exactly one hop)
241  *                 /            \
242  *              src              dst
243  *  @endverbatim
244  *
245  * A specific recursive case occurs when src is the ancestor of dst. In this case,
246  * the base case routing should be used so the common_ancestor is specifically set
247  * to src_ancestor==dst_ancestor.
248  * Naturally, things are completely symmetrical if dst is the ancestor of src.
249  * @verbatim
250  *            platform root
251  *                  |
252  *                 ...                <-- possibly long path
253  *                  |
254  *  src == src_ancestor==dst_ancestor==common_ancestor <-- same value
255  *                  |
256  *                 ...                <-- possibly long path (one hop or more)
257  *                  |
258  *                 dst
259  *  @endverbatim
260  */
261 static void find_common_ancestors(NetPoint* src, NetPoint* dst,
262                                   /* OUT */ NetZoneImpl** common_ancestor, NetZoneImpl** src_ancestor,
263                                   NetZoneImpl** dst_ancestor)
264 {
265   /* Deal with the easy base case */
266   if (src->get_englobing_zone() == dst->get_englobing_zone()) {
267     *common_ancestor = src->get_englobing_zone();
268     *src_ancestor    = *common_ancestor;
269     *dst_ancestor    = *common_ancestor;
270     return;
271   }
272
273   /* engage the full recursive search */
274
275   /* (1) find the path to root of src and dst*/
276   const NetZoneImpl* src_as = src->get_englobing_zone();
277   const NetZoneImpl* dst_as = dst->get_englobing_zone();
278
279   xbt_assert(src_as, "Host %s must be in a netzone", src->get_cname());
280   xbt_assert(dst_as, "Host %s must be in a netzone", dst->get_cname());
281
282   /* (2) find the path to the root routing component */
283   std::vector<NetZoneImpl*> path_src;
284   NetZoneImpl* current = src->get_englobing_zone();
285   while (current != nullptr) {
286     path_src.push_back(current);
287     current = current->get_parent();
288   }
289   std::vector<NetZoneImpl*> path_dst;
290   current = dst->get_englobing_zone();
291   while (current != nullptr) {
292     path_dst.push_back(current);
293     current = current->get_parent();
294   }
295
296   /* (3) find the common father.
297    * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
298    * So we move them down simultaneously as long as they point to the same content.
299    *
300    * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
301    */
302   NetZoneImpl* father = nullptr; // the netzone we dropped on the previous loop iteration
303   while (path_src.size() > 1 && path_dst.size() > 1 &&
304          path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
305     father = path_src.at(path_src.size() - 1);
306     path_src.pop_back();
307     path_dst.pop_back();
308   }
309
310   /* (4) we found the difference at least. Finalize the returned values */
311   *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
312   *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
313   if (*src_ancestor == *dst_ancestor) {             // src is the ancestor of dst, or the contrary
314     *common_ancestor = *src_ancestor;
315   } else {
316     *common_ancestor = father;
317   }
318 }
319
320 /* PRECONDITION: this is the common ancestor of src and dst */
321 bool NetZoneImpl::get_bypass_route(NetPoint* src, NetPoint* dst,
322                                    /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency)
323 {
324   // If never set a bypass route return nullptr without any further computations
325   if (bypass_routes_.empty())
326     return false;
327
328   /* Base case, no recursion is needed */
329   if (dst->get_englobing_zone() == this && src->get_englobing_zone() == this) {
330     if (bypass_routes_.find({src, dst}) != bypass_routes_.end()) {
331       const BypassRoute* bypassedRoute = bypass_routes_.at({src, dst});
332       for (resource::LinkImpl* const& link : bypassedRoute->links) {
333         links.push_back(link);
334         if (latency)
335           *latency += link->get_latency();
336       }
337       XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->get_cname(), dst->get_cname(),
338                 bypassedRoute->links.size());
339       return true;
340     }
341     return false;
342   }
343
344   /* Engage recursive search */
345
346   /* (1) find the path to the root routing component */
347   std::vector<NetZoneImpl*> path_src;
348   NetZoneImpl* current = src->get_englobing_zone();
349   while (current != nullptr) {
350     path_src.push_back(current);
351     current = current->parent_;
352   }
353
354   std::vector<NetZoneImpl*> path_dst;
355   current = dst->get_englobing_zone();
356   while (current != nullptr) {
357     path_dst.push_back(current);
358     current = current->parent_;
359   }
360
361   /* (2) find the common father */
362   while (path_src.size() > 1 && path_dst.size() > 1 &&
363          path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
364     path_src.pop_back();
365     path_dst.pop_back();
366   }
367
368   int max_index_src = path_src.size() - 1;
369   int max_index_dst = path_dst.size() - 1;
370
371   int max_index = std::max(max_index_src, max_index_dst);
372
373   /* (3) Search for a bypass making the path up to the ancestor useless */
374   const BypassRoute* bypassedRoute = nullptr;
375   std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*> key;
376   for (int max = 0; max <= max_index && not bypassedRoute; max++) {
377     for (int i = 0; i < max && not bypassedRoute; i++) {
378       if (i <= max_index_src && max <= max_index_dst) {
379         key      = {path_src.at(i)->netpoint_, path_dst.at(max)->netpoint_};
380         auto bpr = bypass_routes_.find(key);
381         if (bpr != bypass_routes_.end()) {
382           bypassedRoute = bpr->second;
383         }
384       }
385       if (not bypassedRoute && max <= max_index_src && i <= max_index_dst) {
386         key      = {path_src.at(max)->netpoint_, path_dst.at(i)->netpoint_};
387         auto bpr = bypass_routes_.find(key);
388         if (bpr != bypass_routes_.end()) {
389           bypassedRoute = bpr->second;
390         }
391       }
392     }
393
394     if (not bypassedRoute && max <= max_index_src && max <= max_index_dst) {
395       key      = {path_src.at(max)->netpoint_, path_dst.at(max)->netpoint_};
396       auto bpr = bypass_routes_.find(key);
397       if (bpr != bypass_routes_.end()) {
398         bypassedRoute = bpr->second;
399       }
400     }
401   }
402
403   /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
404   if (bypassedRoute) {
405     XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
406               "calls to getRoute",
407               src->get_cname(), dst->get_cname(), bypassedRoute->links.size());
408     if (src != key.first)
409       get_global_route(src, bypassedRoute->gw_src, links, latency);
410     for (resource::LinkImpl* const& link : bypassedRoute->links) {
411       links.push_back(link);
412       if (latency)
413         *latency += link->get_latency();
414     }
415     if (dst != key.second)
416       get_global_route(bypassedRoute->gw_dst, dst, links, latency);
417     return true;
418   }
419   XBT_DEBUG("No bypass route from '%s' to '%s'.", src->get_cname(), dst->get_cname());
420   return false;
421 }
422
423 void NetZoneImpl::get_global_route(NetPoint* src, NetPoint* dst,
424                                    /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency)
425 {
426   RouteCreationArgs route;
427
428   XBT_DEBUG("Resolve route from '%s' to '%s'", src->get_cname(), dst->get_cname());
429
430   /* Find how src and dst are interconnected */
431   NetZoneImpl* common_ancestor;
432   NetZoneImpl* src_ancestor;
433   NetZoneImpl* dst_ancestor;
434   find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
435   XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'", common_ancestor->get_cname(),
436             src_ancestor->get_cname(), dst_ancestor->get_cname());
437
438   /* Check whether a direct bypass is defined. If so, use it and bail out */
439   if (common_ancestor->get_bypass_route(src, dst, links, latency))
440     return;
441
442   /* If src and dst are in the same netzone, life is good */
443   if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
444     route.link_list = std::move(links);
445     common_ancestor->get_local_route(src, dst, &route, latency);
446     links = std::move(route.link_list);
447     return;
448   }
449
450   /* Not in the same netzone, no bypass. We'll have to find our path between the netzones recursively */
451
452   common_ancestor->get_local_route(src_ancestor->netpoint_, dst_ancestor->netpoint_, &route, latency);
453   xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr), "Bad gateways for route from '%s' to '%s'.",
454              src->get_cname(), dst->get_cname());
455
456   /* If source gateway is not our source, we have to recursively find our way up to this point */
457   if (src != route.gw_src)
458     get_global_route(src, route.gw_src, links, latency);
459   for (auto const& link : route.link_list)
460     links.push_back(link);
461
462   /* If dest gateway is not our destination, we have to recursively find our way from this point */
463   if (route.gw_dst != dst)
464     get_global_route(route.gw_dst, dst, links, latency);
465 }
466
467 void NetZoneImpl::seal()
468 {
469   do_seal(); // derived class' specific sealing procedure
470   sealed_ = true;
471 }
472
473 void NetZoneImpl::set_parent(NetZoneImpl* parent)
474 {
475   xbt_assert(sealed_ == false, "Impossible to set parent to an already sealed NetZone(%s)", this->get_cname());
476   parent_ = parent;
477   netpoint_->set_englobing_zone(parent_);
478 }
479
480 void NetZoneImpl::set_network_model(std::shared_ptr<resource::NetworkModel> netmodel)
481 {
482   xbt_assert(sealed_ == false, "Impossible to set network model to an already sealed NetZone(%s)", this->get_cname());
483   network_model_ = std::move(netmodel);
484 }
485
486 void NetZoneImpl::set_cpu_vm_model(std::shared_ptr<resource::CpuModel> cpu_model)
487 {
488   xbt_assert(sealed_ == false, "Impossible to set CPU model to an already sealed NetZone(%s)", this->get_cname());
489   cpu_model_vm_ = std::move(cpu_model);
490 }
491
492 void NetZoneImpl::set_cpu_pm_model(std::shared_ptr<resource::CpuModel> cpu_model)
493 {
494   xbt_assert(sealed_ == false, "Impossible to set CPU model to an already sealed NetZone(%s)", this->get_cname());
495   cpu_model_pm_ = std::move(cpu_model);
496 }
497
498 void NetZoneImpl::set_disk_model(std::shared_ptr<resource::DiskModel> disk_model)
499 {
500   xbt_assert(sealed_ == false, "Impossible to set disk model to an already sealed NetZone(%s)", this->get_cname());
501   disk_model_ = std::move(disk_model);
502 }
503
504 void NetZoneImpl::set_host_model(std::shared_ptr<surf::HostModel> host_model)
505 {
506   xbt_assert(sealed_ == false, "Impossible to set host model to an already sealed NetZone(%s)", this->get_cname());
507   host_model_ = std::move(host_model);
508 }
509
510 } // namespace routing
511 } // namespace kernel
512 } // namespace simgrid