Logo AND Algorithmique Numérique Distribuée

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