Logo AND Algorithmique Numérique Distribuée

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