1 /* Copyright (c) 2006-2023. 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/Exception.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include <simgrid/kernel/routing/NetZoneImpl.hpp>
9 #include <simgrid/s4u/Engine.hpp>
10 #include <simgrid/s4u/NetZone.hpp>
11 #include <simgrid/simix.hpp>
12 #include <simgrid/zone.h>
13 #include <xbt/parse_units.hpp>
15 #include "src/kernel/resource/NetworkModel.hpp"
17 namespace simgrid::s4u {
19 xbt::signal<void(NetZone const&)> NetZone::on_creation;
20 xbt::signal<void(NetZone const&)> NetZone::on_seal;
22 const std::unordered_map<std::string, std::string>* NetZone::get_properties() const
24 return pimpl_->get_properties();
27 /** Retrieve the property value (or nullptr if not set) */
28 const char* NetZone::get_property(const std::string& key) const
30 return pimpl_->get_property(key);
33 void NetZone::set_property(const std::string& key, const std::string& value)
35 kernel::actor::simcall_answered([this, &key, &value] { pimpl_->set_property(key, value); });
38 /** @brief Returns the list of direct children (no grand-children) */
39 std::vector<NetZone*> NetZone::get_children() const
41 std::vector<NetZone*> res;
42 for (auto* child : pimpl_->get_children())
43 res.push_back(child->get_iface());
47 const std::string& NetZone::get_name() const
49 return pimpl_->get_name();
52 const char* NetZone::get_cname() const
54 return pimpl_->get_cname();
57 NetZone* NetZone::get_parent() const
59 return pimpl_->get_parent()->get_iface();
62 NetZone* NetZone::set_parent(const NetZone* parent)
64 kernel::actor::simcall_answered([this, parent] { pimpl_->set_parent(parent->get_impl()); });
68 /** @brief Returns the list of the hosts found in this NetZone (not recursively)
70 * Only the hosts that are directly contained in this NetZone are retrieved,
71 * not the ones contained in sub-netzones.
73 std::vector<Host*> NetZone::get_all_hosts() const
75 return pimpl_->get_all_hosts();
78 size_t NetZone::get_host_count() const
80 return pimpl_->get_host_count();
83 unsigned long NetZone::add_component(kernel::routing::NetPoint* elm)
85 return pimpl_->add_component(elm);
88 void NetZone::add_route(const NetZone* src, const NetZone* dst, const std::vector<const Link*>& links)
90 std::vector<LinkInRoute> links_direct;
91 std::vector<LinkInRoute> links_reverse;
92 for (auto* l : links) {
93 links_direct.emplace_back(LinkInRoute(l, LinkInRoute::Direction::UP));
94 links_reverse.emplace_back(LinkInRoute(l, LinkInRoute::Direction::DOWN));
96 pimpl_->add_route(src ? src->get_netpoint() : nullptr, dst ? dst->get_netpoint(): nullptr,
97 src ? src->get_gateway() : nullptr, dst ? dst->get_gateway() : nullptr,
99 pimpl_->add_route(dst ? dst->get_netpoint(): nullptr, src ? src->get_netpoint() : nullptr,
100 dst ? dst->get_gateway() : nullptr, src ? src->get_gateway() : nullptr,
101 links_reverse, false);
104 void NetZone::add_route(const NetZone* src, const NetZone* dst, const std::vector<LinkInRoute>& link_list, bool symmetrical)
106 pimpl_->add_route(src ? src->get_netpoint() : nullptr, dst ? dst->get_netpoint(): nullptr,
107 src ? src->get_gateway() : nullptr, dst ? dst->get_gateway() : nullptr,
108 link_list, symmetrical);
111 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
112 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
113 const std::vector<LinkInRoute>& link_list, bool symmetrical) //XBT_ATTRIB_DEPRECATED_v339
115 pimpl_->add_route(src, dst, gw_src, gw_dst, link_list, symmetrical);
118 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
119 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
120 const std::vector<const Link*>& links) //XBT_ATTRIB_DEPRECATED_v339
122 std::vector<LinkInRoute> links_direct;
123 std::vector<LinkInRoute> links_reverse;
124 for (auto* l : links) {
125 links_direct.emplace_back(LinkInRoute(l, LinkInRoute::Direction::UP));
126 links_reverse.emplace_back(LinkInRoute(l, LinkInRoute::Direction::DOWN));
128 pimpl_->add_route(src, dst, gw_src, gw_dst, links_direct, false);
129 pimpl_->add_route(dst, src, gw_dst, gw_src, links_reverse, false);
132 void NetZone::add_route(const Host* src, const Host* dst, const std::vector<LinkInRoute>& link_list, bool symmetrical)
134 pimpl_->add_route(src ? src->get_netpoint(): nullptr, dst ? dst->get_netpoint(): nullptr, nullptr, nullptr,
135 link_list, symmetrical);
139 void NetZone::add_route(const Host* src, const Host* dst, const std::vector<const Link*>& links)
141 std::vector<LinkInRoute> links_direct;
142 std::vector<LinkInRoute> links_reverse;
143 for (auto* l : links) {
144 links_direct.emplace_back(LinkInRoute(l, LinkInRoute::Direction::UP));
145 links_reverse.emplace_back(LinkInRoute(l, LinkInRoute::Direction::DOWN));
147 pimpl_->add_route(src ? src->get_netpoint(): nullptr, dst ? dst->get_netpoint(): nullptr, nullptr, nullptr,
148 links_direct, false);
149 pimpl_->add_route(dst ? dst->get_netpoint(): nullptr, src ? src->get_netpoint(): nullptr, nullptr, nullptr,
150 links_reverse, false);
153 void NetZone::add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
154 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
155 const std::vector<LinkInRoute>& link_list)
157 pimpl_->add_bypass_route(src, dst, gw_src, gw_dst, link_list);
160 void NetZone::extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
161 std::map<std::string, xbt_edge_t, std::less<>>* edges)
163 for (auto const& child : get_children())
164 child->extract_xbt_graph(graph, nodes, edges);
166 pimpl_->get_graph(graph, nodes, edges);
169 NetZone* NetZone::seal()
171 kernel::actor::simcall_answered([this] { pimpl_->seal(); });
174 void NetZone::set_latency_factor_cb(
175 std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
176 const std::vector<s4u::Link*>& /*links*/,
177 const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const
179 kernel::actor::simcall_answered([this, &cb]() { pimpl_->get_network_model()->set_lat_factor_cb(cb); });
181 void NetZone::set_bandwidth_factor_cb(
182 std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
183 const std::vector<s4u::Link*>& /*links*/,
184 const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const
186 kernel::actor::simcall_answered([this, &cb]() { pimpl_->get_network_model()->set_bw_factor_cb(cb); });
189 s4u::Host* NetZone::create_host(const std::string& name, double speed)
191 return create_host(name, std::vector<double>{speed});
194 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<double>& speed_per_pstate)
196 return kernel::actor::simcall_answered(
197 [this, &name, &speed_per_pstate] { return pimpl_->create_host(name, speed_per_pstate); });
200 s4u::Host* NetZone::create_host(const std::string& name, const std::string& speed)
202 return create_host(name, std::vector<std::string>{speed});
205 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate)
207 return create_host(name, Host::convert_pstate_speed_vector(speed_per_pstate));
210 s4u::Link* NetZone::create_link(const std::string& name, double bandwidth)
212 return create_link(name, std::vector<double>{bandwidth});
215 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<double>& bandwidths)
217 return kernel::actor::simcall_answered([this, &name, &bandwidths] { return pimpl_->create_link(name, bandwidths); });
220 s4u::Link* NetZone::create_link(const std::string& name, const std::string& bandwidth)
222 return create_link(name, std::vector<std::string>{bandwidth});
225 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, const std::string& bandwidth)
229 speed = xbt_parse_get_bandwidth("", 0, bandwidth, "");
230 } catch (const simgrid::ParseError&) {
231 throw std::invalid_argument("Impossible to create split-duplex link: " + name +
232 ". Invalid bandwidth: " + bandwidth);
234 return create_split_duplex_link(name, speed);
237 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, double bandwidth)
239 return kernel::actor::simcall_answered(
240 [this, &name, &bandwidth] { return pimpl_->create_split_duplex_link(name, std::vector<double>{bandwidth}); });
243 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<std::string>& bandwidths)
245 std::vector<double> bw;
246 bw.reserve(bandwidths.size());
247 for (const auto& speed_str : bandwidths) {
249 double speed = xbt_parse_get_bandwidth("", 0, speed_str, "");
251 } catch (const simgrid::ParseError&) {
252 throw std::invalid_argument("Impossible to create link: " + name + ". Invalid bandwidth: " + speed_str);
255 return create_link(name, bw);
258 kernel::routing::NetPoint* NetZone::create_router(const std::string& name)
260 return kernel::actor::simcall_answered([this, &name] { return pimpl_->create_router(name); });
263 kernel::routing::NetPoint* NetZone::get_netpoint() const
265 return pimpl_->get_netpoint();
268 kernel::routing::NetPoint* NetZone::get_gateway() const
270 return pimpl_->get_gateway();
273 kernel::routing::NetPoint* NetZone::get_gateway(const std::string& name) const
275 return pimpl_->get_gateway(name);
278 void NetZone::set_gateway(kernel::routing::NetPoint* router)
280 set_gateway("default", router);
283 void NetZone::set_gateway(const std::string& name, kernel::routing::NetPoint* router)
285 kernel::actor::simcall_answered([this, name, router] { pimpl_->set_gateway(name, router); });
288 kernel::resource::NetworkModel* NetZone::get_network_model() const
290 return pimpl_->get_network_model().get();
292 } // namespace simgrid::s4u
294 /* **************************** Public C interface *************************** */
296 sg_netzone_t sg_zone_get_root()
298 return simgrid::s4u::Engine::get_instance()->get_netzone_root();
301 const char* sg_zone_get_name(const_sg_netzone_t netzone)
303 return netzone->get_cname();
306 sg_netzone_t sg_zone_get_by_name(const char* name)
308 return simgrid::s4u::Engine::get_instance()->netzone_by_name_or_null(name);
311 void sg_zone_get_sons(const_sg_netzone_t netzone, xbt_dict_t whereto)
313 for (auto const& elem : netzone->get_children()) {
314 xbt_dict_set(whereto, elem->get_cname(), elem);
318 const char* sg_zone_get_property_value(const_sg_netzone_t netzone, const char* name)
320 return netzone->get_property(name);
323 void sg_zone_set_property_value(sg_netzone_t netzone, const char* name, const char* value)
325 netzone->set_property(name, value);
328 void sg_zone_get_hosts(const_sg_netzone_t netzone, xbt_dynar_t whereto)
330 /* converts vector to dynar */
331 std::vector<simgrid::s4u::Host*> hosts = netzone->get_all_hosts();
332 for (auto const& host : hosts)
333 xbt_dynar_push(whereto, &host);