Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e4a4494a91e79d20052828e97dcc0cddf32876f0
[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/s4u/Engine.hpp"
9 #include "simgrid/s4u/Link.hpp"
10 #include "simgrid/sg_config.hpp"
11 #include "simgrid/simix.hpp"
12 #include "src/kernel/lmm/maxmin.hpp"
13 #include "src/surf/network_interface.hpp"
14 #include "src/surf/network_wifi.hpp"
15 #include "xbt/log.h"
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&)> Link::on_communicate;
28 xbt::signal<void(kernel::resource::NetworkAction&, kernel::resource::Action::State)>
29     Link::on_communication_state_change;
30
31 Link* Link::by_name(const std::string& name)
32 {
33   return Engine::get_instance()->link_by_name(name);
34 }
35
36 Link* Link::by_name_or_null(const std::string& name)
37 {
38   return Engine::get_instance()->link_by_name_or_null(name);
39 }
40
41 const std::string& Link::get_name() const
42 {
43   return this->pimpl_->get_name();
44 }
45 const char* Link::get_cname() const
46 {
47   return this->pimpl_->get_cname();
48 }
49 bool Link::is_used() const
50 {
51   return this->pimpl_->is_used();
52 }
53
54 bool Link::is_shared() const
55 {
56   return this->pimpl_->get_sharing_policy() != SharingPolicy::FATPIPE;
57 }
58
59 double Link::get_latency() const
60 {
61   return this->pimpl_->get_latency();
62 }
63
64 Link* Link::set_latency(double value)
65 {
66   kernel::actor::simcall([this, value] { pimpl_->set_latency(value); });
67   return this;
68 }
69
70 double Link::get_bandwidth() const
71 {
72   return this->pimpl_->get_bandwidth();
73 }
74
75 Link* Link::set_bandwidth(double value)
76 {
77   kernel::actor::simcall([this, value] { pimpl_->set_bandwidth(value); });
78   return this;
79 }
80
81 Link::SharingPolicy Link::get_sharing_policy() const
82 {
83   return this->pimpl_->get_sharing_policy();
84 }
85
86 void Link::set_host_wifi_rate(const s4u::Host* host, int level) const
87 {
88   xbt_assert(pimpl_->get_sharing_policy() == Link::SharingPolicy::WIFI, "Link %s does not seem to be a wifi link.",
89              get_cname());
90   auto* wlink = dynamic_cast<kernel::resource::NetworkWifiLink*>(pimpl_);
91   xbt_assert(wlink != nullptr, "Cannot convert link %s into a wifi link.", get_cname());
92   wlink->set_host_rate(host, level);
93 }
94
95 double Link::get_usage() const
96 {
97   return this->pimpl_->get_constraint()->get_usage();
98 }
99
100 void Link::turn_on()
101 {
102   kernel::actor::simcall([this]() { this->pimpl_->turn_on(); });
103 }
104 void Link::turn_off()
105 {
106   kernel::actor::simcall([this]() { this->pimpl_->turn_off(); });
107 }
108 void Link::seal()
109 {
110   kernel::actor::simcall([this]() { this->pimpl_->seal(); });
111 }
112
113 bool Link::is_on() const
114 {
115   return this->pimpl_->is_on();
116 }
117
118 Link* Link::set_state_profile(kernel::profile::Profile* profile)
119 {
120   xbt_assert(not pimpl_->is_sealed(), "Cannot set a state profile once the Link is sealed");
121   kernel::actor::simcall([this, profile]() { this->pimpl_->set_state_profile(profile); });
122   return this;
123 }
124
125 Link* Link::set_bandwidth_profile(kernel::profile::Profile* profile)
126 {
127   xbt_assert(not pimpl_->is_sealed(), "Cannot set a bandwidth profile once the Link is sealed");
128   kernel::actor::simcall([this, profile]() { this->pimpl_->set_bandwidth_profile(profile); });
129   return this;
130 }
131
132 Link* Link::set_latency_profile(kernel::profile::Profile* profile)
133 {
134   xbt_assert(not pimpl_->is_sealed(), "Cannot set a latency profile once the Link is sealed");
135   kernel::actor::simcall([this, profile]() { this->pimpl_->set_latency_profile(profile); });
136   return this;
137 }
138
139 const char* Link::get_property(const std::string& key) const
140 {
141   return this->pimpl_->get_property(key);
142 }
143 Link* Link::set_property(const std::string& key, const std::string& value)
144 {
145   kernel::actor::simcall([this, &key, &value] { this->pimpl_->set_property(key, value); });
146   return this;
147 }
148
149 const std::unordered_map<std::string, std::string>* Link::get_properties() const
150 {
151   return this->pimpl_->get_properties();
152 }
153
154 Link* Link::set_properties(const std::unordered_map<std::string, std::string>& properties)
155 {
156   kernel::actor::simcall([this, &properties] { this->pimpl_->set_properties(properties); });
157   return this;
158 }
159
160 } // namespace s4u
161 } // namespace simgrid
162
163 /* **************************** Public C interface *************************** */
164
165 const char* sg_link_get_name(const_sg_link_t link)
166 {
167   return link->get_cname();
168 }
169
170 const char* sg_link_name(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
171 {
172   return sg_link_get_name(link);
173 }
174
175 sg_link_t sg_link_by_name(const char* name)
176 {
177   return simgrid::s4u::Link::by_name(name);
178 }
179
180 int sg_link_is_shared(const_sg_link_t link)
181 {
182   return link->is_shared();
183 }
184
185 double sg_link_get_bandwidth(const_sg_link_t link)
186 {
187   return link->get_bandwidth();
188 }
189
190 void sg_link_set_bandwidth(sg_link_t link, double value)
191 {
192   link->set_bandwidth(value);
193 }
194
195 double sg_link_bandwidth(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
196 {
197   return sg_link_get_bandwidth(link);
198 }
199
200 void sg_link_bandwidth_set(sg_link_t link, double value) // XBT_ATTRIB_DEPRECATED_v330
201 {
202   sg_link_set_bandwidth(link, value);
203 }
204
205 double sg_link_get_latency(const_sg_link_t link)
206 {
207   return link->get_latency();
208 }
209
210 void sg_link_set_latency(sg_link_t link, double value)
211 {
212   link->set_latency(value);
213 }
214
215 double sg_link_latency(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
216 {
217   return sg_link_get_latency(link);
218 }
219
220 void sg_link_latency_set(sg_link_t link, double value) // XBT_ATTRIB_DEPRECATED_v330
221 {
222   sg_link_set_latency(link, value);
223 }
224
225 void* sg_link_get_data(const_sg_link_t link)
226 {
227   return link->get_data();
228 }
229
230 void sg_link_set_data(sg_link_t link, void* data)
231 {
232   link->set_data(data);
233 }
234
235 void* sg_link_data(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
236 {
237   return sg_link_get_data(link);
238 }
239
240 void sg_link_data_set(sg_link_t link, void* data) // XBT_ATTRIB_DEPRECATED_v330
241 {
242   sg_link_set_data(link, data);
243 }
244
245 int sg_link_count()
246 {
247   return simgrid::s4u::Engine::get_instance()->get_link_count();
248 }
249
250 sg_link_t* sg_link_list()
251 {
252   std::vector<simgrid::s4u::Link*> links = simgrid::s4u::Engine::get_instance()->get_all_links();
253
254   sg_link_t* res = xbt_new(sg_link_t, links.size());
255   memcpy(res, links.data(), sizeof(sg_link_t) * links.size());
256
257   return res;
258 }