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(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
89 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
90 const std::vector<LinkInRoute>& link_list, bool symmetrical)
92 pimpl_->add_route(src, dst, gw_src, gw_dst, link_list, symmetrical);
95 void NetZone::add_route(const Host* src, const Host* dst, const std::vector<LinkInRoute>& link_list, bool symmetrical)
97 pimpl_->add_route(src->get_netpoint(), dst->get_netpoint(), nullptr, nullptr, link_list, symmetrical);
100 void NetZone::add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
101 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
102 const std::vector<LinkInRoute>& link_list)
104 pimpl_->add_bypass_route(src, dst, gw_src, gw_dst, link_list);
107 void NetZone::extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
108 std::map<std::string, xbt_edge_t, std::less<>>* edges)
110 for (auto const& child : get_children())
111 child->extract_xbt_graph(graph, nodes, edges);
113 pimpl_->get_graph(graph, nodes, edges);
116 NetZone* NetZone::seal()
118 kernel::actor::simcall_answered([this] { pimpl_->seal(); });
121 void NetZone::set_latency_factor_cb(
122 std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
123 const std::vector<s4u::Link*>& /*links*/,
124 const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const
126 kernel::actor::simcall_answered([this, &cb]() { pimpl_->get_network_model()->set_lat_factor_cb(cb); });
128 void NetZone::set_bandwidth_factor_cb(
129 std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
130 const std::vector<s4u::Link*>& /*links*/,
131 const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const
133 kernel::actor::simcall_answered([this, &cb]() { pimpl_->get_network_model()->set_bw_factor_cb(cb); });
136 s4u::Host* NetZone::create_host(const std::string& name, double speed)
138 return create_host(name, std::vector<double>{speed});
141 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<double>& speed_per_pstate)
143 return kernel::actor::simcall_answered(
144 [this, &name, &speed_per_pstate] { return pimpl_->create_host(name, speed_per_pstate); });
147 s4u::Host* NetZone::create_host(const std::string& name, const std::string& speed)
149 return create_host(name, std::vector<std::string>{speed});
152 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate)
154 return create_host(name, Host::convert_pstate_speed_vector(speed_per_pstate));
157 s4u::Link* NetZone::create_link(const std::string& name, double bandwidth)
159 return create_link(name, std::vector<double>{bandwidth});
162 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<double>& bandwidths)
164 return kernel::actor::simcall_answered([this, &name, &bandwidths] { return pimpl_->create_link(name, bandwidths); });
167 s4u::Link* NetZone::create_link(const std::string& name, const std::string& bandwidth)
169 return create_link(name, std::vector<std::string>{bandwidth});
172 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, const std::string& bandwidth)
176 speed = xbt_parse_get_bandwidth("", 0, bandwidth, "");
177 } catch (const simgrid::ParseError&) {
178 throw std::invalid_argument("Impossible to create split-duplex link: " + name +
179 ". Invalid bandwidth: " + bandwidth);
181 return create_split_duplex_link(name, speed);
184 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, double bandwidth)
186 return kernel::actor::simcall_answered(
187 [this, &name, &bandwidth] { return pimpl_->create_split_duplex_link(name, std::vector<double>{bandwidth}); });
190 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<std::string>& bandwidths)
192 std::vector<double> bw;
193 bw.reserve(bandwidths.size());
194 for (const auto& speed_str : bandwidths) {
196 double speed = xbt_parse_get_bandwidth("", 0, speed_str, "");
198 } catch (const simgrid::ParseError&) {
199 throw std::invalid_argument("Impossible to create link: " + name + ". Invalid bandwidth: " + speed_str);
202 return create_link(name, bw);
205 kernel::routing::NetPoint* NetZone::create_router(const std::string& name)
207 return kernel::actor::simcall_answered([this, &name] { return pimpl_->create_router(name); });
210 kernel::routing::NetPoint* NetZone::get_netpoint()
212 return pimpl_->get_netpoint();
215 kernel::resource::NetworkModel* NetZone::get_network_model() const
217 return pimpl_->get_network_model().get();
219 } // namespace simgrid::s4u
221 /* **************************** Public C interface *************************** */
223 sg_netzone_t sg_zone_get_root()
225 return simgrid::s4u::Engine::get_instance()->get_netzone_root();
228 const char* sg_zone_get_name(const_sg_netzone_t netzone)
230 return netzone->get_cname();
233 sg_netzone_t sg_zone_get_by_name(const char* name)
235 return simgrid::s4u::Engine::get_instance()->netzone_by_name_or_null(name);
238 void sg_zone_get_sons(const_sg_netzone_t netzone, xbt_dict_t whereto)
240 for (auto const& elem : netzone->get_children()) {
241 xbt_dict_set(whereto, elem->get_cname(), elem);
245 const char* sg_zone_get_property_value(const_sg_netzone_t netzone, const char* name)
247 return netzone->get_property(name);
250 void sg_zone_set_property_value(sg_netzone_t netzone, const char* name, const char* value)
252 netzone->set_property(name, value);
255 void sg_zone_get_hosts(const_sg_netzone_t netzone, xbt_dynar_t whereto)
257 /* converts vector to dynar */
258 std::vector<simgrid::s4u::Host*> hosts = netzone->get_all_hosts();
259 for (auto const& host : hosts)
260 xbt_dynar_push(whereto, &host);