Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d64a4573b6661c0f918493e06f1846d90f97bb92
[simgrid.git] / src / s4u / s4u_Link.cpp
1 /* Copyright (c) 2013-2023. 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_object_access(pimpl_, [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("Impossible to set latency for link: " + get_name() + ". Invalid value: " + value);
86   }
87   return set_latency(d_value);
88 }
89
90 double Link::get_bandwidth() const
91 {
92   return this->pimpl_->get_bandwidth();
93 }
94
95 Link* Link::set_bandwidth(double value)
96 {
97   kernel::actor::simcall_object_access(pimpl_, [this, value] { pimpl_->set_bandwidth(value); });
98   return this;
99 }
100
101 Link* Link::set_sharing_policy(Link::SharingPolicy policy, const NonLinearResourceCb& cb)
102 {
103   if (policy == SharingPolicy::SPLITDUPLEX || policy == SharingPolicy::WIFI)
104     throw std::invalid_argument("Impossible to set wifi or split-duplex for the link: " + get_name() +
105                                 ". Use appropriate create function in NetZone.");
106
107   kernel::actor::simcall_object_access(pimpl_, [this, policy, &cb] { pimpl_->set_sharing_policy(policy, cb); });
108   return this;
109 }
110 Link::SharingPolicy Link::get_sharing_policy() const
111 {
112   return this->pimpl_->get_sharing_policy();
113 }
114
115 void Link::set_host_wifi_rate(const s4u::Host* host, int level) const
116 {
117   auto* wlink = dynamic_cast<kernel::resource::WifiLinkImpl*>(pimpl_);
118   xbt_assert(wlink != nullptr, "Link %s does not seem to be a wifi link.", get_cname());
119   wlink->set_host_rate(host, level);
120 }
121
122 int Link::get_concurrency_limit() const
123 {
124   return pimpl_->get_concurrency_limit();
125 }
126
127 Link* Link::set_concurrency_limit(int limit)
128 {
129   kernel::actor::simcall_object_access(pimpl_, [this, limit] { pimpl_->set_concurrency_limit(limit); });
130   return this;
131 }
132
133 double Link::get_load() const
134 {
135   return this->pimpl_->get_constraint()->get_load();
136 }
137
138 void Link::turn_on()
139 {
140   kernel::actor::simcall_answered([this]() {
141     this->pimpl_->turn_on();
142     on_state_change(*this);
143     on_this_state_change(*this);
144   });
145 }
146 void Link::turn_off()
147 {
148   kernel::actor::simcall_answered([this]() {
149     this->pimpl_->turn_off();
150     on_state_change(*this);
151     on_this_state_change(*this);
152   });
153 }
154 Link* Link::seal()
155 {
156   kernel::actor::simcall_answered([this]() { this->pimpl_->seal(); });
157   s4u::Link::on_creation(*this); // notify the signal
158   return this;
159 }
160
161 bool Link::is_on() const
162 {
163   return this->pimpl_->is_on();
164 }
165
166 Link* Link::set_state_profile(kernel::profile::Profile* profile)
167 {
168   xbt_assert(not pimpl_->is_sealed(), "Cannot set a state profile once the Link is sealed");
169   kernel::actor::simcall_object_access(pimpl_, [this, profile]() { this->pimpl_->set_state_profile(profile); });
170   return this;
171 }
172
173 Link* Link::set_bandwidth_profile(kernel::profile::Profile* profile)
174 {
175   xbt_assert(not pimpl_->is_sealed(), "Cannot set a bandwidth profile once the Link is sealed");
176   kernel::actor::simcall_object_access(pimpl_, [this, profile]() { this->pimpl_->set_bandwidth_profile(profile); });
177   return this;
178 }
179
180 Link* Link::set_latency_profile(kernel::profile::Profile* profile)
181 {
182   xbt_assert(not pimpl_->is_sealed(), "Cannot set a latency profile once the Link is sealed");
183   kernel::actor::simcall_object_access(pimpl_, [this, profile]() { this->pimpl_->set_latency_profile(profile); });
184   return this;
185 }
186
187 const char* Link::get_property(const std::string& key) const
188 {
189   return this->pimpl_->get_property(key);
190 }
191 Link* Link::set_property(const std::string& key, const std::string& value)
192 {
193   kernel::actor::simcall_object_access(pimpl_, [this, &key, &value] { this->pimpl_->set_property(key, value); });
194   return this;
195 }
196
197 const std::unordered_map<std::string, std::string>* Link::get_properties() const
198 {
199   return this->pimpl_->get_properties();
200 }
201
202 Link* Link::set_properties(const std::unordered_map<std::string, std::string>& properties)
203 {
204   kernel::actor::simcall_object_access(pimpl_, [this, &properties] { this->pimpl_->set_properties(properties); });
205   return this;
206 }
207
208 Link* SplitDuplexLink::get_link_up() const
209 {
210   const auto* pimpl = dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_);
211   xbt_assert(pimpl, "Requesting link_up from a non split-duplex link: %s", get_cname());
212   return pimpl->get_link_up();
213 }
214
215 Link* SplitDuplexLink::get_link_down() const
216 {
217   const auto* pimpl = dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_);
218   xbt_assert(pimpl, "Requesting link_down from a non split-duplex link: %s", get_cname());
219   return pimpl->get_link_down();
220 }
221
222 SplitDuplexLink* SplitDuplexLink::by_name(const std::string& name)
223 {
224   return Engine::get_instance()->split_duplex_link_by_name(name);
225 }
226
227 } // namespace s4u
228 } // namespace simgrid
229
230 /* **************************** Public C interface *************************** */
231
232 const char* sg_link_get_name(const_sg_link_t link)
233 {
234   return link->get_cname();
235 }
236
237 sg_link_t sg_link_by_name(const char* name)
238 {
239   return simgrid::s4u::Link::by_name(name);
240 }
241
242 int sg_link_is_shared(const_sg_link_t link)
243 {
244   return link->is_shared();
245 }
246
247 double sg_link_get_bandwidth(const_sg_link_t link)
248 {
249   return link->get_bandwidth();
250 }
251
252 void sg_link_set_bandwidth(sg_link_t link, double value)
253 {
254   link->set_bandwidth(value);
255 }
256
257 double sg_link_get_latency(const_sg_link_t link)
258 {
259   return link->get_latency();
260 }
261
262 void sg_link_set_latency(sg_link_t link, double value)
263 {
264   link->set_latency(value);
265 }
266
267 void* sg_link_get_data(const_sg_link_t link)
268 {
269   return link->get_data<void>();
270 }
271
272 void sg_link_set_data(sg_link_t link, void* data)
273 {
274   link->set_data(data);
275 }
276
277 size_t sg_link_count()
278 {
279   return simgrid::s4u::Engine::get_instance()->get_link_count();
280 }
281
282 sg_link_t* sg_link_list()
283 {
284   std::vector<simgrid::s4u::Link*> links = simgrid::s4u::Engine::get_instance()->get_all_links();
285
286   auto* res = xbt_new(sg_link_t, links.size());
287   std::copy(begin(links), end(links), res);
288
289   return res;
290 }