Logo AND Algorithmique Numérique Distribuée

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