Logo AND Algorithmique Numérique Distribuée

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