1 /* Copyright (c) 2006-2018. 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/surf/cpu_interface.hpp"
11 #include "src/surf/network_interface.hpp"
12 #include "src/surf/xml/platf_private.hpp"
13 #include "surf/surf.hpp"
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_route);
23 explicit BypassRoute(NetPoint* gwSrc, NetPoint* gwDst) : gw_src(gwSrc), gw_dst(gwDst) {}
26 std::vector<resource::LinkImpl*> links;
29 NetZoneImpl::NetZoneImpl(NetZone* father, std::string name) : NetZone(father, name)
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());
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());
38 NetZoneImpl::~NetZoneImpl()
40 for (auto const& kv : bypass_routes_)
43 simgrid::s4u::Engine::get_instance()->netpoint_unregister(netpoint_);
46 simgrid::s4u::Host* NetZoneImpl::create_host(const char* name, std::vector<double>* speedPerPstate, int coreAmount,
47 std::map<std::string, std::string>* props)
49 simgrid::s4u::Host* res = new simgrid::s4u::Host(name);
51 if (hierarchy_ == RoutingMode::unset)
52 hierarchy_ = RoutingMode::base;
54 res->pimpl_netpoint = new NetPoint(name, NetPoint::Type::Host, this);
56 surf_cpu_model_pm->create_cpu(res, speedPerPstate, coreAmount);
59 for (auto const& kv : *props)
60 res->set_property(kv.first, kv.second);
62 simgrid::s4u::Host::on_creation(*res); // notify the signal
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)
70 /* Argument validity checks */
72 XBT_DEBUG("Load bypassNetzoneRoute from %s@%s to %s@%s", src->get_cname(), gw_src->get_cname(), 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());
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(),
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());
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);
93 bypass_routes_.insert({{src, dst}, newRoute});
96 /** @brief Get the common ancestor and its first children in each line leading to src and dst
98 * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
102 * ... <- possibly long path
107 * / \ <- direct links
110 * src_ancestor dst_ancestor <- must be different in the recursive case
112 * ... ... <-- possibly long paths (one hop or more)
117 * In the base case (when src and dst are in the same netzone), things are as follows:
121 * ... <-- possibly long path
123 * common_ancestor==src_ancestor==dst_ancestor <-- all the same value
125 * / \ <-- direct links (exactly one hop)
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.
137 * ... <-- possibly long path
139 * src == src_ancestor==dst_ancestor==common_ancestor <-- same value
141 * ... <-- possibly long path (one hop or more)
146 static void find_common_ancestors(NetPoint* src, NetPoint* dst,
147 /* OUT */ NetZoneImpl** common_ancestor, NetZoneImpl** src_ancestor,
148 NetZoneImpl** dst_ancestor)
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;
158 /* engage the full recursive search */
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();
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());
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());
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());
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.
185 * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
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);
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;
201 *common_ancestor = father;
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)
209 // If never set a bypass route return nullptr without any further computations
210 if (bypass_routes_.empty())
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);
220 *latency += link->get_latency();
222 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->get_cname(), dst->get_cname(),
223 bypassedRoute->links.size());
229 /* Engage recursive search */
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_;
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_;
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)) {
253 int max_index_src = path_src.size() - 1;
254 int max_index_dst = path_dst.size() - 1;
256 int max_index = std::max(max_index_src, max_index_dst);
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;
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;
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;
294 /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
296 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
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);
304 *latency += link->get_latency();
306 if (dst != key.second)
307 get_global_route(bypassedRoute->gw_dst, dst, links, latency);
310 XBT_DEBUG("No bypass route from '%s' to '%s'.", src->get_cname(), dst->get_cname());
314 void NetZoneImpl::get_global_route(NetPoint* src, NetPoint* dst,
315 /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency)
317 RouteCreationArgs route;
319 XBT_DEBUG("Resolve route from '%s' to '%s'", src->get_cname(), dst->get_cname());
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());
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))
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);
341 /* Not in the same netzone, no bypass. We'll have to find our path between the netzones recursively */
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());
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);
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);