Logo AND Algorithmique Numérique Distribuée

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