Logo AND Algorithmique Numérique Distribuée

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