Logo AND Algorithmique Numérique Distribuée

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