Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Replace redundant type with 'auto'.
[simgrid.git] / src / s4u / s4u_Link.cpp
1 /* Copyright (c) 2013-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 <algorithm>
7
8 #include "simgrid/Exception.hpp"
9 #include "simgrid/s4u/Engine.hpp"
10 #include "simgrid/s4u/Link.hpp"
11 #include "simgrid/sg_config.hpp"
12 #include "simgrid/simix.hpp"
13 #include "src/kernel/lmm/maxmin.hpp"
14 #include "src/surf/network_interface.hpp"
15 #include "src/surf/network_wifi.hpp"
16 #include "xbt/log.h"
17 #include "xbt/parse_units.hpp"
18
19 namespace simgrid {
20
21 template class xbt::Extendable<s4u::Link>;
22
23 namespace s4u {
24
25 xbt::signal<void(Link&)> Link::on_creation;
26 xbt::signal<void(Link const&)> Link::on_destruction;
27 xbt::signal<void(Link const&)> Link::on_state_change;
28 xbt::signal<void(Link const&)> Link::on_bandwidth_change;
29 xbt::signal<void(kernel::resource::NetworkAction&)> Link::on_communicate;
30 xbt::signal<void(kernel::resource::NetworkAction&, kernel::resource::Action::State)>
31     Link::on_communication_state_change;
32
33 Link* Link::by_name(const std::string& name)
34 {
35   return Engine::get_instance()->link_by_name(name);
36 }
37
38 Link* Link::by_name_or_null(const std::string& name)
39 {
40   return Engine::get_instance()->link_by_name_or_null(name);
41 }
42
43 const std::string& Link::get_name() const
44 {
45   return this->pimpl_->get_name();
46 }
47 const char* Link::get_cname() const
48 {
49   return this->pimpl_->get_cname();
50 }
51 bool Link::is_used() const
52 {
53   return this->pimpl_->is_used();
54 }
55
56 bool Link::is_shared() const
57 {
58   return this->pimpl_->get_sharing_policy() != SharingPolicy::FATPIPE;
59 }
60
61 double Link::get_latency() const
62 {
63   return this->pimpl_->get_latency();
64 }
65
66 Link* Link::set_latency(double value)
67 {
68   kernel::actor::simcall([this, value] { pimpl_->set_latency(value); });
69   return this;
70 }
71
72 Link* Link::set_latency(const std::string& value)
73 {
74   double d_value = 0.0;
75   try {
76     d_value = xbt_parse_get_time("", 0, value, "");
77   } catch (const simgrid::ParseError&) {
78     throw std::invalid_argument(std::string("Impossible to set latency for link: ") + get_name() +
79                                 std::string(". Invalid value: ") + value);
80   }
81   return set_latency(d_value);
82 }
83
84 double Link::get_bandwidth() const
85 {
86   return this->pimpl_->get_bandwidth();
87 }
88
89 Link* Link::set_bandwidth(double value)
90 {
91   kernel::actor::simcall([this, value] { pimpl_->set_bandwidth(value); });
92   return this;
93 }
94
95 Link* Link::set_sharing_policy(Link::SharingPolicy policy)
96 {
97   if (policy == SharingPolicy::SPLITDUPLEX)
98     throw std::invalid_argument(std::string("Impossible to set split-duplex for the link: ") + get_name() +
99                                 std::string(". You should create a link-up and link-down to emulate this behavior"));
100
101   kernel::actor::simcall([this, policy] { pimpl_->set_sharing_policy(policy); });
102   return this;
103 }
104 Link::SharingPolicy Link::get_sharing_policy() const
105 {
106   return this->pimpl_->get_sharing_policy();
107 }
108
109 void Link::set_host_wifi_rate(const s4u::Host* host, int level) const
110 {
111   auto* wlink = dynamic_cast<kernel::resource::NetworkWifiLink*>(pimpl_);
112   xbt_assert(wlink != nullptr, "Link %s does not seem to be a wifi link.", get_cname());
113   wlink->set_host_rate(host, level);
114 }
115
116 double Link::get_usage() const
117 {
118   return this->pimpl_->get_constraint()->get_usage();
119 }
120
121 void Link::turn_on()
122 {
123   kernel::actor::simcall([this]() { this->pimpl_->turn_on(); });
124 }
125 void Link::turn_off()
126 {
127   kernel::actor::simcall([this]() { this->pimpl_->turn_off(); });
128 }
129 Link* Link::seal()
130 {
131   kernel::actor::simcall([this]() { this->pimpl_->seal(); });
132   return this;
133 }
134
135 bool Link::is_on() const
136 {
137   return this->pimpl_->is_on();
138 }
139
140 Link* Link::set_state_profile(kernel::profile::Profile* profile)
141 {
142   xbt_assert(not pimpl_->is_sealed(), "Cannot set a state profile once the Link is sealed");
143   kernel::actor::simcall([this, profile]() { this->pimpl_->set_state_profile(profile); });
144   return this;
145 }
146
147 Link* Link::set_bandwidth_profile(kernel::profile::Profile* profile)
148 {
149   xbt_assert(not pimpl_->is_sealed(), "Cannot set a bandwidth profile once the Link is sealed");
150   kernel::actor::simcall([this, profile]() { this->pimpl_->set_bandwidth_profile(profile); });
151   return this;
152 }
153
154 Link* Link::set_latency_profile(kernel::profile::Profile* profile)
155 {
156   xbt_assert(not pimpl_->is_sealed(), "Cannot set a latency profile once the Link is sealed");
157   kernel::actor::simcall([this, profile]() { this->pimpl_->set_latency_profile(profile); });
158   return this;
159 }
160
161 const char* Link::get_property(const std::string& key) const
162 {
163   return this->pimpl_->get_property(key);
164 }
165 Link* Link::set_property(const std::string& key, const std::string& value)
166 {
167   kernel::actor::simcall([this, &key, &value] { this->pimpl_->set_property(key, value); });
168   return this;
169 }
170
171 const std::unordered_map<std::string, std::string>* Link::get_properties() const
172 {
173   return this->pimpl_->get_properties();
174 }
175
176 Link* Link::set_properties(const std::unordered_map<std::string, std::string>& properties)
177 {
178   kernel::actor::simcall([this, &properties] { this->pimpl_->set_properties(properties); });
179   return this;
180 }
181
182 } // namespace s4u
183 } // namespace simgrid
184
185 /* **************************** Public C interface *************************** */
186
187 const char* sg_link_get_name(const_sg_link_t link)
188 {
189   return link->get_cname();
190 }
191
192 const char* sg_link_name(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
193 {
194   return sg_link_get_name(link);
195 }
196
197 sg_link_t sg_link_by_name(const char* name)
198 {
199   return simgrid::s4u::Link::by_name(name);
200 }
201
202 int sg_link_is_shared(const_sg_link_t link)
203 {
204   return link->is_shared();
205 }
206
207 double sg_link_get_bandwidth(const_sg_link_t link)
208 {
209   return link->get_bandwidth();
210 }
211
212 void sg_link_set_bandwidth(sg_link_t link, double value)
213 {
214   link->set_bandwidth(value);
215 }
216
217 double sg_link_bandwidth(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
218 {
219   return sg_link_get_bandwidth(link);
220 }
221
222 void sg_link_bandwidth_set(sg_link_t link, double value) // XBT_ATTRIB_DEPRECATED_v330
223 {
224   sg_link_set_bandwidth(link, value);
225 }
226
227 double sg_link_get_latency(const_sg_link_t link)
228 {
229   return link->get_latency();
230 }
231
232 void sg_link_set_latency(sg_link_t link, double value)
233 {
234   link->set_latency(value);
235 }
236
237 double sg_link_latency(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
238 {
239   return sg_link_get_latency(link);
240 }
241
242 void sg_link_latency_set(sg_link_t link, double value) // XBT_ATTRIB_DEPRECATED_v330
243 {
244   sg_link_set_latency(link, value);
245 }
246
247 void* sg_link_get_data(const_sg_link_t link)
248 {
249   return link->get_data();
250 }
251
252 void sg_link_set_data(sg_link_t link, void* data)
253 {
254   link->set_data(data);
255 }
256
257 void* sg_link_data(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
258 {
259   return sg_link_get_data(link);
260 }
261
262 void sg_link_data_set(sg_link_t link, void* data) // XBT_ATTRIB_DEPRECATED_v330
263 {
264   sg_link_set_data(link, data);
265 }
266
267 size_t sg_link_count()
268 {
269   return simgrid::s4u::Engine::get_instance()->get_link_count();
270 }
271
272 sg_link_t* sg_link_list()
273 {
274   std::vector<simgrid::s4u::Link*> links = simgrid::s4u::Engine::get_instance()->get_all_links();
275
276   auto* res = xbt_new(sg_link_t, links.size());
277   std::copy(begin(links), end(links), res);
278
279   return res;
280 }