Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please clang take 2
[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 "xbt/parse_units.hpp"
14
15 namespace simgrid {
16 namespace s4u {
17
18 xbt::signal<void(bool symmetrical, kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
19                  kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
20                  std::vector<kernel::resource::LinkImpl*> const& link_list)>
21     NetZone::on_route_creation;
22 xbt::signal<void(NetZone const&)> NetZone::on_creation;
23 xbt::signal<void(NetZone const&)> NetZone::on_seal;
24
25 const std::unordered_map<std::string, std::string>* NetZone::get_properties() const
26 {
27   return pimpl_->get_properties();
28 }
29
30 /** Retrieve the property value (or nullptr if not set) */
31 const char* NetZone::get_property(const std::string& key) const
32 {
33   return pimpl_->get_property(key);
34 }
35
36 void NetZone::set_property(const std::string& key, const std::string& value)
37 {
38   kernel::actor::simcall([this, &key, &value] { pimpl_->set_property(key, value); });
39 }
40
41 /** @brief Returns the list of direct children (no grand-children) */
42 std::vector<NetZone*> NetZone::get_children() const
43 {
44   std::vector<NetZone*> res;
45   for (auto child : pimpl_->get_children())
46     res.push_back(child->get_iface());
47   return res;
48 }
49
50 NetZone* NetZone::add_child(NetZone* new_zone)
51 {
52   new_zone->set_parent(this);
53   return this;
54 }
55
56 const std::string& NetZone::get_name() const
57 {
58   return pimpl_->get_name();
59 }
60
61 const char* NetZone::get_cname() const
62 {
63   return pimpl_->get_cname();
64 }
65
66 NetZone* NetZone::get_father()
67 {
68   return pimpl_->get_parent()->get_iface();
69 }
70
71 NetZone* NetZone::get_parent() const
72 {
73   return pimpl_->get_parent()->get_iface();
74 }
75
76 NetZone* NetZone::set_parent(const NetZone* parent)
77 {
78   kernel::actor::simcall([this, parent] { pimpl_->set_parent(parent->get_impl()); });
79   return this;
80 }
81
82 /** @brief Returns the list of the hosts found in this NetZone (not recursively)
83  *
84  * Only the hosts that are directly contained in this NetZone are retrieved,
85  * not the ones contained in sub-netzones.
86  */
87 std::vector<Host*> NetZone::get_all_hosts() const
88 {
89   return pimpl_->get_all_hosts();
90 }
91
92 int NetZone::get_host_count() const
93 {
94   return pimpl_->get_host_count();
95 }
96
97 int NetZone::add_component(kernel::routing::NetPoint* elm)
98 {
99   return pimpl_->add_component(elm);
100 }
101
102 std::vector<kernel::resource::LinkImpl*> NetZone::get_link_list_impl(const std::vector<Link*> link_list)
103 {
104   std::vector<kernel::resource::LinkImpl*> links;
105   for (const auto& link : link_list) {
106     links.push_back(link->get_impl());
107   }
108   return links;
109 }
110
111 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
112                         kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
113                         const std::vector<Link*>& link_list, bool symmetrical)
114 {
115   pimpl_->add_route(src, dst, gw_src, gw_dst, NetZone::get_link_list_impl(link_list), symmetrical);
116 }
117
118 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
119                         kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
120                         const std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical)
121 {
122   pimpl_->add_route(src, dst, gw_src, gw_dst, link_list, symmetrical);
123 }
124 void NetZone::add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
125                                kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
126                                std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical)
127 {
128   pimpl_->add_bypass_route(src, dst, gw_src, gw_dst, link_list, symmetrical);
129 }
130
131 void NetZone::extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
132                                 std::map<std::string, xbt_edge_t, std::less<>>* edges)
133 {
134   for (auto const& child : get_children())
135     child->extract_xbt_graph(graph, nodes, edges);
136
137   pimpl_->get_graph(graph, nodes, edges);
138 }
139
140 NetZone* NetZone::seal()
141 {
142   kernel::actor::simcall([this] { pimpl_->seal(); });
143   return this;
144 }
145
146 s4u::Host* NetZone::create_host(const std::string& name, double speed)
147 {
148   return create_host(name, std::vector<double>{speed});
149 }
150
151 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<double>& speed_per_pstate)
152 {
153   return kernel::actor::simcall(
154       [this, &name, &speed_per_pstate] { return pimpl_->create_host(name, speed_per_pstate); });
155 }
156
157 s4u::Host* NetZone::create_host(const std::string& name, const std::string& speed)
158 {
159   return create_host(name, std::vector<std::string>{speed});
160 }
161
162 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate)
163 {
164   return create_host(name, Host::convert_pstate_speed_vector(speed_per_pstate));
165 }
166
167 s4u::Link* NetZone::create_link(const std::string& name, double bandwidth)
168 {
169   return create_link(name, std::vector<double>{bandwidth});
170 }
171
172 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<double>& bandwidths)
173 {
174   return kernel::actor::simcall([this, &name, &bandwidths] { return pimpl_->create_link(name, bandwidths); });
175 }
176
177 s4u::Link* NetZone::create_link(const std::string& name, const std::string& bandwidth)
178 {
179   return create_link(name, std::vector<std::string>{bandwidth});
180 }
181
182 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<std::string>& bandwidths)
183 {
184   std::vector<double> bw;
185   bw.reserve(bandwidths.size());
186   for (const auto& speed_str : bandwidths) {
187     try {
188       double speed = xbt_parse_get_bandwidth("", 0, speed_str.c_str(), nullptr, "");
189       bw.push_back(speed);
190     } catch (const simgrid::ParseError&) {
191       throw std::invalid_argument(std::string("Impossible to create link: ") + name +
192                                   std::string(". Invalid bandwidth: ") + speed_str);
193     }
194   }
195   return create_link(name, bw);
196 }
197
198 kernel::routing::NetPoint* NetZone::create_router(const std::string& name)
199 {
200   return kernel::actor::simcall([this, &name] { return pimpl_->create_router(name); });
201 }
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 }