Logo AND Algorithmique Numérique Distribuée

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