Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / s4u / s4u_Netzone.cpp
1 /* Copyright (c) 2006-2022. 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/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>
14
15 #include "src/kernel/resource/LinkImpl.hpp"
16 #include "src/kernel/resource/StandardLinkImpl.hpp"
17
18 namespace simgrid {
19 namespace s4u {
20
21 xbt::signal<void(bool symmetrical, kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
22                  kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
23                  std::vector<kernel::resource::StandardLinkImpl*> const& link_list)>
24     NetZone::on_route_creation;
25 xbt::signal<void(NetZone const&)> NetZone::on_creation;
26 xbt::signal<void(NetZone const&)> NetZone::on_seal;
27
28 const std::unordered_map<std::string, std::string>* NetZone::get_properties() const
29 {
30   return pimpl_->get_properties();
31 }
32
33 /** Retrieve the property value (or nullptr if not set) */
34 const char* NetZone::get_property(const std::string& key) const
35 {
36   return pimpl_->get_property(key);
37 }
38
39 void NetZone::set_property(const std::string& key, const std::string& value)
40 {
41   kernel::actor::simcall([this, &key, &value] { pimpl_->set_property(key, value); });
42 }
43
44 /** @brief Returns the list of direct children (no grand-children) */
45 std::vector<NetZone*> NetZone::get_children() const
46 {
47   std::vector<NetZone*> res;
48   for (auto child : pimpl_->get_children())
49     res.push_back(child->get_iface());
50   return res;
51 }
52
53 NetZone* NetZone::add_child(NetZone* new_zone) // XBT_ATTRIB_DEPRECATED_v332
54 {
55   new_zone->set_parent(this);
56   return this;
57 }
58
59 const std::string& NetZone::get_name() const
60 {
61   return pimpl_->get_name();
62 }
63
64 const char* NetZone::get_cname() const
65 {
66   return pimpl_->get_cname();
67 }
68
69 NetZone* NetZone::get_father() const // XBT_ATTRIB_DEPRECATED_v331
70 {
71   return pimpl_->get_parent()->get_iface();
72 }
73
74 NetZone* NetZone::get_parent() const
75 {
76   return pimpl_->get_parent()->get_iface();
77 }
78
79 NetZone* NetZone::set_parent(const NetZone* parent)
80 {
81   kernel::actor::simcall([this, parent] { pimpl_->set_parent(parent->get_impl()); });
82   return this;
83 }
84
85 /** @brief Returns the list of the hosts found in this NetZone (not recursively)
86  *
87  * Only the hosts that are directly contained in this NetZone are retrieved,
88  * not the ones contained in sub-netzones.
89  */
90 std::vector<Host*> NetZone::get_all_hosts() const
91 {
92   return pimpl_->get_all_hosts();
93 }
94
95 int NetZone::get_host_count() const
96 {
97   return pimpl_->get_host_count();
98 }
99
100 unsigned long NetZone::add_component(kernel::routing::NetPoint* elm)
101 {
102   return pimpl_->add_component(elm);
103 }
104
105 // XBT_ATTRIB_DEPRECATED_v332
106 std::vector<LinkInRoute>
107 NetZone::convert_to_linkInRoute(const std::vector<kernel::resource::StandardLinkImpl*>& link_list)
108 {
109   std::vector<LinkInRoute> links;
110   for (const auto* link : link_list) {
111     links.emplace_back(LinkInRoute(link->get_iface()));
112   }
113   return links;
114 }
115
116 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
117                         kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
118                         const std::vector<LinkInRoute>& link_list, bool symmetrical)
119 {
120   pimpl_->add_route(src, dst, gw_src, gw_dst, link_list, symmetrical);
121 }
122
123 // XBT_ATTRIB_DEPRECATED_v332
124 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
125                         kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
126                         const std::vector<kernel::resource::StandardLinkImpl*>& link_list, bool symmetrical)
127 {
128   pimpl_->add_route(src, dst, gw_src, gw_dst, convert_to_linkInRoute(link_list), symmetrical);
129 }
130
131 // XBT_ATTRIB_DEPRECATED_v332
132 void NetZone::add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
133                                kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
134                                std::vector<kernel::resource::StandardLinkImpl*>& link_list, bool /*symmetrical*/)
135 {
136   pimpl_->add_bypass_route(src, dst, gw_src, gw_dst, convert_to_linkInRoute(link_list));
137 }
138
139 void NetZone::add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
140                                kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
141                                const std::vector<LinkInRoute>& link_list)
142 {
143   pimpl_->add_bypass_route(src, dst, gw_src, gw_dst, link_list);
144 }
145
146 void NetZone::extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
147                                 std::map<std::string, xbt_edge_t, std::less<>>* edges)
148 {
149   for (auto const& child : get_children())
150     child->extract_xbt_graph(graph, nodes, edges);
151
152   pimpl_->get_graph(graph, nodes, edges);
153 }
154
155 NetZone* NetZone::seal()
156 {
157   kernel::actor::simcall([this] { pimpl_->seal(); });
158   return this;
159 }
160
161 s4u::Host* NetZone::create_host(const std::string& name, double speed)
162 {
163   return create_host(name, std::vector<double>{speed});
164 }
165
166 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<double>& speed_per_pstate)
167 {
168   return kernel::actor::simcall(
169       [this, &name, &speed_per_pstate] { return pimpl_->create_host(name, speed_per_pstate); });
170 }
171
172 s4u::Host* NetZone::create_host(const std::string& name, const std::string& speed)
173 {
174   return create_host(name, std::vector<std::string>{speed});
175 }
176
177 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate)
178 {
179   return create_host(name, Host::convert_pstate_speed_vector(speed_per_pstate));
180 }
181
182 s4u::Link* NetZone::create_link(const std::string& name, double bandwidth)
183 {
184   return create_link(name, std::vector<double>{bandwidth});
185 }
186
187 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<double>& bandwidths)
188 {
189   return kernel::actor::simcall([this, &name, &bandwidths] { return pimpl_->create_link(name, bandwidths); });
190 }
191
192 s4u::Link* NetZone::create_link(const std::string& name, const std::string& bandwidth)
193 {
194   return create_link(name, std::vector<std::string>{bandwidth});
195 }
196
197 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, const std::string& bandwidth)
198 {
199   double speed;
200   try {
201     speed = xbt_parse_get_bandwidth("", 0, bandwidth, "");
202   } catch (const simgrid::ParseError&) {
203     throw std::invalid_argument(std::string("Impossible to create split-duplex link: ") + name +
204                                 std::string(". Invalid bandwidth: ") + bandwidth);
205   }
206   return create_split_duplex_link(name, speed);
207 }
208
209 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, double bandwidth)
210 {
211   return kernel::actor::simcall(
212       [this, &name, &bandwidth] { return pimpl_->create_split_duplex_link(name, std::vector<double>{bandwidth}); });
213 }
214
215 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<std::string>& bandwidths)
216 {
217   std::vector<double> bw;
218   bw.reserve(bandwidths.size());
219   for (const auto& speed_str : bandwidths) {
220     try {
221       double speed = xbt_parse_get_bandwidth("", 0, speed_str, "");
222       bw.push_back(speed);
223     } catch (const simgrid::ParseError&) {
224       throw std::invalid_argument(std::string("Impossible to create link: ") + name +
225                                   std::string(". Invalid bandwidth: ") + speed_str);
226     }
227   }
228   return create_link(name, bw);
229 }
230
231 kernel::routing::NetPoint* NetZone::create_router(const std::string& name)
232 {
233   return kernel::actor::simcall([this, &name] { return pimpl_->create_router(name); });
234 }
235
236 kernel::routing::NetPoint* NetZone::get_netpoint()
237 {
238   return pimpl_->get_netpoint();
239 }
240
241 kernel::resource::NetworkModelIntf* NetZone::get_network_model() const
242 {
243   kernel::resource::NetworkModelIntf* model = pimpl_->get_network_model().get();
244   return model;
245 }
246 } // namespace s4u
247 } // namespace simgrid
248
249 /* **************************** Public C interface *************************** */
250
251 sg_netzone_t sg_zone_get_root()
252 {
253   return simgrid::s4u::Engine::get_instance()->get_netzone_root();
254 }
255
256 const char* sg_zone_get_name(const_sg_netzone_t netzone)
257 {
258   return netzone->get_cname();
259 }
260
261 sg_netzone_t sg_zone_get_by_name(const char* name)
262 {
263   return simgrid::s4u::Engine::get_instance()->netzone_by_name_or_null(name);
264 }
265
266 void sg_zone_get_sons(const_sg_netzone_t netzone, xbt_dict_t whereto)
267 {
268   for (auto const& elem : netzone->get_children()) {
269     xbt_dict_set(whereto, elem->get_cname(), elem);
270   }
271 }
272
273 const char* sg_zone_get_property_value(const_sg_netzone_t netzone, const char* name)
274 {
275   return netzone->get_property(name);
276 }
277
278 void sg_zone_set_property_value(sg_netzone_t netzone, const char* name, const char* value)
279 {
280   netzone->set_property(name, value);
281 }
282
283 void sg_zone_get_hosts(const_sg_netzone_t netzone, xbt_dynar_t whereto)
284 {
285   /* converts vector to dynar */
286   std::vector<simgrid::s4u::Host*> hosts = netzone->get_all_hosts();
287   for (auto const& host : hosts)
288     xbt_dynar_push(whereto, &host);
289 }