Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make the sendsend tests at least fail fast to not hinder my workflow
[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_usage() const
134 {
135   return this->pimpl_->get_constraint()->get_usage();
136 }
137
138 void Link::turn_on()
139 {
140   kernel::actor::simcall_answered([this]() { this->pimpl_->turn_on(); });
141 }
142 void Link::turn_off()
143 {
144   kernel::actor::simcall_answered([this]() { this->pimpl_->turn_off(); });
145 }
146 Link* Link::seal()
147 {
148   kernel::actor::simcall_answered([this]() { this->pimpl_->seal(); });
149   s4u::Link::on_creation(*this); // notify the signal
150   return this;
151 }
152
153 bool Link::is_on() const
154 {
155   return this->pimpl_->is_on();
156 }
157
158 Link* Link::set_state_profile(kernel::profile::Profile* profile)
159 {
160   xbt_assert(not pimpl_->is_sealed(), "Cannot set a state profile once the Link is sealed");
161   kernel::actor::simcall_object_access(pimpl_, [this, profile]() { this->pimpl_->set_state_profile(profile); });
162   return this;
163 }
164
165 Link* Link::set_bandwidth_profile(kernel::profile::Profile* profile)
166 {
167   xbt_assert(not pimpl_->is_sealed(), "Cannot set a bandwidth profile once the Link is sealed");
168   kernel::actor::simcall_object_access(pimpl_, [this, profile]() { this->pimpl_->set_bandwidth_profile(profile); });
169   return this;
170 }
171
172 Link* Link::set_latency_profile(kernel::profile::Profile* profile)
173 {
174   xbt_assert(not pimpl_->is_sealed(), "Cannot set a latency profile once the Link is sealed");
175   kernel::actor::simcall_object_access(pimpl_, [this, profile]() { this->pimpl_->set_latency_profile(profile); });
176   return this;
177 }
178
179 const char* Link::get_property(const std::string& key) const
180 {
181   return this->pimpl_->get_property(key);
182 }
183 Link* Link::set_property(const std::string& key, const std::string& value)
184 {
185   kernel::actor::simcall_object_access(pimpl_, [this, &key, &value] { this->pimpl_->set_property(key, value); });
186   return this;
187 }
188
189 const std::unordered_map<std::string, std::string>* Link::get_properties() const
190 {
191   return this->pimpl_->get_properties();
192 }
193
194 Link* Link::set_properties(const std::unordered_map<std::string, std::string>& properties)
195 {
196   kernel::actor::simcall_object_access(pimpl_, [this, &properties] { this->pimpl_->set_properties(properties); });
197   return this;
198 }
199
200 Link* SplitDuplexLink::get_link_up() const
201 {
202   const auto* pimpl = dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_);
203   xbt_assert(pimpl, "Requesting link_up from a non split-duplex link: %s", get_cname());
204   return pimpl->get_link_up();
205 }
206
207 Link* SplitDuplexLink::get_link_down() const
208 {
209   const auto* pimpl = dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_);
210   xbt_assert(pimpl, "Requesting link_down from a non split-duplex link: %s", get_cname());
211   return pimpl->get_link_down();
212 }
213
214 SplitDuplexLink* SplitDuplexLink::by_name(const std::string& name)
215 {
216   return Engine::get_instance()->split_duplex_link_by_name(name);
217 }
218
219 } // namespace s4u
220 } // namespace simgrid
221
222 /* **************************** Public C interface *************************** */
223
224 const char* sg_link_get_name(const_sg_link_t link)
225 {
226   return link->get_cname();
227 }
228
229 sg_link_t sg_link_by_name(const char* name)
230 {
231   return simgrid::s4u::Link::by_name(name);
232 }
233
234 int sg_link_is_shared(const_sg_link_t link)
235 {
236   return link->is_shared();
237 }
238
239 double sg_link_get_bandwidth(const_sg_link_t link)
240 {
241   return link->get_bandwidth();
242 }
243
244 void sg_link_set_bandwidth(sg_link_t link, double value)
245 {
246   link->set_bandwidth(value);
247 }
248
249 double sg_link_get_latency(const_sg_link_t link)
250 {
251   return link->get_latency();
252 }
253
254 void sg_link_set_latency(sg_link_t link, double value)
255 {
256   link->set_latency(value);
257 }
258
259 void* sg_link_get_data(const_sg_link_t link)
260 {
261   return link->get_data<void>();
262 }
263
264 void sg_link_set_data(sg_link_t link, void* data)
265 {
266   link->set_data(data);
267 }
268
269 size_t sg_link_count()
270 {
271   return simgrid::s4u::Engine::get_instance()->get_link_count();
272 }
273
274 sg_link_t* sg_link_list()
275 {
276   std::vector<simgrid::s4u::Link*> links = simgrid::s4u::Engine::get_instance()->get_all_links();
277
278   auto* res = xbt_new(sg_link_t, links.size());
279   std::copy(begin(links), end(links), res);
280
281   return res;
282 }