1 /* Copyright (c) 2013-2021. The SimGrid Team. All rights reserved. */
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. */
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"
17 #include "xbt/parse_units.hpp"
21 template class xbt::Extendable<s4u::Link>;
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;
33 Link* Link::by_name(const std::string& name)
35 return Engine::get_instance()->link_by_name(name);
38 kernel::resource::LinkImpl* Link::get_impl() const
41 get_sharing_policy() != SharingPolicy::SPLITDUPLEX,
42 "Impossible to get a LinkImpl* from a Split-Duplex link. You should call this method to each UP/DOWN member");
43 return dynamic_cast<kernel::resource::LinkImpl*>(pimpl_);
46 Link* Link::by_name_or_null(const std::string& name)
48 return Engine::get_instance()->link_by_name_or_null(name);
51 const std::string& Link::get_name() const
53 return this->pimpl_->get_name();
55 const char* Link::get_cname() const
57 return this->pimpl_->get_cname();
59 bool Link::is_used() const
61 return this->pimpl_->is_used();
64 bool Link::is_shared() const
66 return this->pimpl_->get_sharing_policy() != SharingPolicy::FATPIPE;
69 double Link::get_latency() const
71 return this->pimpl_->get_latency();
74 Link* Link::set_latency(double value)
76 kernel::actor::simcall([this, value] { pimpl_->set_latency(value); });
80 Link* Link::set_latency(const std::string& value)
84 d_value = xbt_parse_get_time("", 0, value, "");
85 } catch (const simgrid::ParseError&) {
86 throw std::invalid_argument(std::string("Impossible to set latency for link: ") + get_name() +
87 std::string(". Invalid value: ") + value);
89 return set_latency(d_value);
92 double Link::get_bandwidth() const
94 return this->pimpl_->get_bandwidth();
97 Link* Link::set_bandwidth(double value)
99 kernel::actor::simcall([this, value] { pimpl_->set_bandwidth(value); });
103 Link* Link::set_sharing_policy(Link::SharingPolicy policy)
105 if (policy == SharingPolicy::SPLITDUPLEX)
106 throw std::invalid_argument(std::string("Impossible to set split-duplex for the link: ") + get_name() +
107 std::string(". Use NetZone::create_split_duplex_link."));
109 kernel::actor::simcall([this, policy] { pimpl_->set_sharing_policy(policy); });
112 Link::SharingPolicy Link::get_sharing_policy() const
114 return this->pimpl_->get_sharing_policy();
117 void Link::set_host_wifi_rate(const s4u::Host* host, int level) const
119 auto* wlink = dynamic_cast<kernel::resource::NetworkWifiLink*>(pimpl_);
120 xbt_assert(wlink != nullptr, "Link %s does not seem to be a wifi link.", get_cname());
121 wlink->set_host_rate(host, level);
124 Link* Link::set_concurrency_limit(int limit)
126 kernel::actor::simcall([this, limit] { pimpl_->set_concurrency_limit(limit); });
130 double Link::get_usage() const
132 return this->pimpl_->get_constraint()->get_usage();
137 kernel::actor::simcall([this]() { this->pimpl_->turn_on(); });
139 void Link::turn_off()
141 kernel::actor::simcall([this]() { this->pimpl_->turn_off(); });
145 kernel::actor::simcall([this]() { this->pimpl_->seal(); });
149 bool Link::is_on() const
151 return this->pimpl_->is_on();
154 Link* Link::set_state_profile(kernel::profile::Profile* profile)
156 xbt_assert(not pimpl_->is_sealed(), "Cannot set a state profile once the Link is sealed");
157 kernel::actor::simcall([this, profile]() { this->pimpl_->set_state_profile(profile); });
161 Link* Link::set_bandwidth_profile(kernel::profile::Profile* profile)
163 xbt_assert(not pimpl_->is_sealed(), "Cannot set a bandwidth profile once the Link is sealed");
164 kernel::actor::simcall([this, profile]() { this->pimpl_->set_bandwidth_profile(profile); });
168 Link* Link::set_latency_profile(kernel::profile::Profile* profile)
170 xbt_assert(not pimpl_->is_sealed(), "Cannot set a latency profile once the Link is sealed");
171 kernel::actor::simcall([this, profile]() { this->pimpl_->set_latency_profile(profile); });
175 const char* Link::get_property(const std::string& key) const
177 return this->pimpl_->get_property(key);
179 Link* Link::set_property(const std::string& key, const std::string& value)
181 kernel::actor::simcall([this, &key, &value] { this->pimpl_->set_property(key, value); });
185 const std::unordered_map<std::string, std::string>* Link::get_properties() const
187 return this->pimpl_->get_properties();
190 Link* Link::set_properties(const std::unordered_map<std::string, std::string>& properties)
192 kernel::actor::simcall([this, &properties] { this->pimpl_->set_properties(properties); });
196 Link* SplitDuplexLink::get_link_up() const
198 kernel::resource::SplitDuplexLinkImpl* pimpl = dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_);
199 xbt_assert(pimpl, "Requesting link_up from a non split-duplex link: %s", get_cname());
200 return pimpl->get_link_up();
203 Link* SplitDuplexLink::get_link_down() const
205 kernel::resource::SplitDuplexLinkImpl* pimpl = dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_);
206 xbt_assert(pimpl, "Requesting link_down from a non split-duplex link: %s", get_cname());
207 return pimpl->get_link_down();
210 SplitDuplexLink* SplitDuplexLink::by_name(const std::string& name)
212 return Engine::get_instance()->split_duplex_link_by_name(name);
216 } // namespace simgrid
218 /* **************************** Public C interface *************************** */
220 const char* sg_link_get_name(const_sg_link_t link)
222 return link->get_cname();
225 const char* sg_link_name(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
227 return sg_link_get_name(link);
230 sg_link_t sg_link_by_name(const char* name)
232 return simgrid::s4u::Link::by_name(name);
235 int sg_link_is_shared(const_sg_link_t link)
237 return link->is_shared();
240 double sg_link_get_bandwidth(const_sg_link_t link)
242 return link->get_bandwidth();
245 void sg_link_set_bandwidth(sg_link_t link, double value)
247 link->set_bandwidth(value);
250 double sg_link_bandwidth(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
252 return sg_link_get_bandwidth(link);
255 void sg_link_bandwidth_set(sg_link_t link, double value) // XBT_ATTRIB_DEPRECATED_v330
257 sg_link_set_bandwidth(link, value);
260 double sg_link_get_latency(const_sg_link_t link)
262 return link->get_latency();
265 void sg_link_set_latency(sg_link_t link, double value)
267 link->set_latency(value);
270 double sg_link_latency(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
272 return sg_link_get_latency(link);
275 void sg_link_latency_set(sg_link_t link, double value) // XBT_ATTRIB_DEPRECATED_v330
277 sg_link_set_latency(link, value);
280 void* sg_link_get_data(const_sg_link_t link)
282 return link->get_data();
285 void sg_link_set_data(sg_link_t link, void* data)
287 link->set_data(data);
290 void* sg_link_data(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
292 return sg_link_get_data(link);
295 void sg_link_data_set(sg_link_t link, void* data) // XBT_ATTRIB_DEPRECATED_v330
297 sg_link_set_data(link, data);
300 size_t sg_link_count()
302 return simgrid::s4u::Engine::get_instance()->get_link_count();
305 sg_link_t* sg_link_list()
307 std::vector<simgrid::s4u::Link*> links = simgrid::s4u::Engine::get_instance()->get_all_links();
309 auto* res = xbt_new(sg_link_t, links.size());
310 std::copy(begin(links), end(links), res);