1 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved. */
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. */
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"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_route);
23 NetZoneImpl::NetZoneImpl(NetZoneImpl* father, const std::string& name, resource::NetworkModel* network_model)
24 : piface_(this), father_(father), name_(name), network_model_(network_model)
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());
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]);
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());
44 NetZoneImpl::~NetZoneImpl()
46 for (auto const& nz : children_)
49 for (auto const& kv : bypass_routes_)
52 s4u::Engine::get_instance()->netpoint_unregister(netpoint_);
55 /** @brief Returns the list of the hosts found in this NetZone (not recursively)
57 * Only the hosts that are directly contained in this NetZone are retrieved,
58 * not the ones contained in sub-netzones.
60 std::vector<s4u::Host*> NetZoneImpl::get_all_hosts() const
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());
70 int NetZoneImpl::get_host_count() const
73 for (auto const& card : get_vertices()) {
74 const s4u::Host* host = s4u::Host::by_name_or_null(card->get_name());
81 s4u::Disk* NetZoneImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
83 auto* l = disk_model_->create_disk(name, read_bandwidth, write_bandwidth);
85 return l->get_iface();
88 s4u::Link* NetZoneImpl::create_link(const std::string& name, const std::vector<double>& bandwidths,
89 s4u::Link::SharingPolicy policy)
91 auto* l = network_model_->create_link(name, bandwidths, policy);
93 return l->get_iface();
96 s4u::Host* NetZoneImpl::create_host(const std::string& name, const std::vector<double>& speed_per_pstate,
99 if (hierarchy_ == RoutingMode::unset)
100 hierarchy_ = RoutingMode::base;
102 auto* res = new s4u::Host(name);
103 res->set_netpoint(new NetPoint(name, NetPoint::Type::Host, this));
105 cpu_model_pm_->create_cpu(res, speed_per_pstate)->set_core_count(core_amount)->seal();
110 int NetZoneImpl::add_component(NetPoint* elm)
112 vertices_.push_back(elm);
113 return vertices_.size() - 1; // The rank of the newly created object
116 void NetZoneImpl::add_route(NetPoint* /*src*/, NetPoint* /*dst*/, NetPoint* /*gw_src*/, NetPoint* /*gw_dst*/,
117 std::vector<resource::LinkImpl*>& /*link_list*/, bool /*symmetrical*/)
119 xbt_die("NetZone '%s' does not accept new routes (wrong class).", get_cname());
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 */)
125 /* Argument validity checks */
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());
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(),
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());
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);
148 bypass_routes_.insert({{src, dst}, newRoute});
151 /** @brief Get the common ancestor and its first children in each line leading to src and dst
153 * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
157 * ... <- possibly long path
162 * / \ <- direct links
165 * src_ancestor dst_ancestor <- must be different in the recursive case
167 * ... ... <-- possibly long paths (one hop or more)
172 * In the base case (when src and dst are in the same netzone), things are as follows:
176 * ... <-- possibly long path
178 * common_ancestor==src_ancestor==dst_ancestor <-- all the same value
180 * / \ <-- direct links (exactly one hop)
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.
192 * ... <-- possibly long path
194 * src == src_ancestor==dst_ancestor==common_ancestor <-- same value
196 * ... <-- possibly long path (one hop or more)
201 static void find_common_ancestors(NetPoint* src, NetPoint* dst,
202 /* OUT */ NetZoneImpl** common_ancestor, NetZoneImpl** src_ancestor,
203 NetZoneImpl** dst_ancestor)
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;
213 /* engage the full recursive search */
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();
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());
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();
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();
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.
240 * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
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);
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;
256 *common_ancestor = father;
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)
264 // If never set a bypass route return nullptr without any further computations
265 if (bypass_routes_.empty())
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);
275 *latency += link->get_latency();
277 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->get_cname(), dst->get_cname(),
278 bypassedRoute->links.size());
284 /* Engage recursive search */
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_;
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_;
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)) {
308 int max_index_src = path_src.size() - 1;
309 int max_index_dst = path_dst.size() - 1;
311 int max_index = std::max(max_index_src, max_index_dst);
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;
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;
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;
343 /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
345 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
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);
353 *latency += link->get_latency();
355 if (dst != key.second)
356 get_global_route(bypassedRoute->gw_dst, dst, links, latency);
359 XBT_DEBUG("No bypass route from '%s' to '%s'.", src->get_cname(), dst->get_cname());
363 void NetZoneImpl::get_global_route(NetPoint* src, NetPoint* dst,
364 /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency)
366 RouteCreationArgs route;
368 XBT_DEBUG("Resolve route from '%s' to '%s'", src->get_cname(), dst->get_cname());
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());
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))
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);
390 /* Not in the same netzone, no bypass. We'll have to find our path between the netzones recursively */
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());
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);
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);
406 } // namespace routing
407 } // namespace kernel
408 } // namespace simgrid