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/kernel/lmm/maxmin.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 Link* Link::by_name_or_null(const std::string& name)
40 return Engine::get_instance()->link_by_name_or_null(name);
43 const std::string& Link::get_name() const
45 return this->pimpl_->get_name();
47 const char* Link::get_cname() const
49 return this->pimpl_->get_cname();
51 bool Link::is_used() const
53 return this->pimpl_->is_used();
56 bool Link::is_shared() const
58 return this->pimpl_->get_sharing_policy() != SharingPolicy::FATPIPE;
61 double Link::get_latency() const
63 return this->pimpl_->get_latency();
66 Link* Link::set_latency(double value)
68 kernel::actor::simcall([this, value] { pimpl_->set_latency(value); });
72 Link* Link::set_latency(const std::string& value)
76 d_value = xbt_parse_get_time("", 0, value.c_str(), nullptr, "");
77 } catch (const simgrid::ParseError&) {
78 throw std::invalid_argument(std::string("Impossible to set latency for link: ") + get_name() +
79 std::string(". Invalid value: ") + value);
81 return set_latency(d_value);
84 double Link::get_bandwidth() const
86 return this->pimpl_->get_bandwidth();
89 Link* Link::set_bandwidth(double value)
91 kernel::actor::simcall([this, value] { pimpl_->set_bandwidth(value); });
95 Link* Link::set_sharing_policy(Link::SharingPolicy policy)
97 kernel::actor::simcall([this, policy] { pimpl_->set_sharing_policy(policy); });
100 Link::SharingPolicy Link::get_sharing_policy() const
102 return this->pimpl_->get_sharing_policy();
105 void Link::set_host_wifi_rate(const s4u::Host* host, int level) const
107 auto* wlink = dynamic_cast<kernel::resource::NetworkWifiLink*>(pimpl_);
108 xbt_assert(wlink != nullptr, "Link %s does not seem to be a wifi link.", get_cname());
109 wlink->set_host_rate(host, level);
112 double Link::get_usage() const
114 return this->pimpl_->get_constraint()->get_usage();
119 kernel::actor::simcall([this]() { this->pimpl_->turn_on(); });
121 void Link::turn_off()
123 kernel::actor::simcall([this]() { this->pimpl_->turn_off(); });
127 kernel::actor::simcall([this]() { this->pimpl_->seal(); });
131 bool Link::is_on() const
133 return this->pimpl_->is_on();
136 Link* Link::set_state_profile(kernel::profile::Profile* profile)
138 xbt_assert(not pimpl_->is_sealed(), "Cannot set a state profile once the Link is sealed");
139 kernel::actor::simcall([this, profile]() { this->pimpl_->set_state_profile(profile); });
143 Link* Link::set_bandwidth_profile(kernel::profile::Profile* profile)
145 xbt_assert(not pimpl_->is_sealed(), "Cannot set a bandwidth profile once the Link is sealed");
146 kernel::actor::simcall([this, profile]() { this->pimpl_->set_bandwidth_profile(profile); });
150 Link* Link::set_latency_profile(kernel::profile::Profile* profile)
152 xbt_assert(not pimpl_->is_sealed(), "Cannot set a latency profile once the Link is sealed");
153 kernel::actor::simcall([this, profile]() { this->pimpl_->set_latency_profile(profile); });
157 const char* Link::get_property(const std::string& key) const
159 return this->pimpl_->get_property(key);
161 Link* Link::set_property(const std::string& key, const std::string& value)
163 kernel::actor::simcall([this, &key, &value] { this->pimpl_->set_property(key, value); });
167 const std::unordered_map<std::string, std::string>* Link::get_properties() const
169 return this->pimpl_->get_properties();
172 Link* Link::set_properties(const std::unordered_map<std::string, std::string>& properties)
174 kernel::actor::simcall([this, &properties] { this->pimpl_->set_properties(properties); });
179 } // namespace simgrid
181 /* **************************** Public C interface *************************** */
183 const char* sg_link_get_name(const_sg_link_t link)
185 return link->get_cname();
188 const char* sg_link_name(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
190 return sg_link_get_name(link);
193 sg_link_t sg_link_by_name(const char* name)
195 return simgrid::s4u::Link::by_name(name);
198 int sg_link_is_shared(const_sg_link_t link)
200 return link->is_shared();
203 double sg_link_get_bandwidth(const_sg_link_t link)
205 return link->get_bandwidth();
208 void sg_link_set_bandwidth(sg_link_t link, double value)
210 link->set_bandwidth(value);
213 double sg_link_bandwidth(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
215 return sg_link_get_bandwidth(link);
218 void sg_link_bandwidth_set(sg_link_t link, double value) // XBT_ATTRIB_DEPRECATED_v330
220 sg_link_set_bandwidth(link, value);
223 double sg_link_get_latency(const_sg_link_t link)
225 return link->get_latency();
228 void sg_link_set_latency(sg_link_t link, double value)
230 link->set_latency(value);
233 double sg_link_latency(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
235 return sg_link_get_latency(link);
238 void sg_link_latency_set(sg_link_t link, double value) // XBT_ATTRIB_DEPRECATED_v330
240 sg_link_set_latency(link, value);
243 void* sg_link_get_data(const_sg_link_t link)
245 return link->get_data();
248 void sg_link_set_data(sg_link_t link, void* data)
250 link->set_data(data);
253 void* sg_link_data(const_sg_link_t link) // XBT_ATTRIB_DEPRECATED_v330
255 return sg_link_get_data(link);
258 void sg_link_data_set(sg_link_t link, void* data) // XBT_ATTRIB_DEPRECATED_v330
260 sg_link_set_data(link, data);
263 size_t sg_link_count()
265 return simgrid::s4u::Engine::get_instance()->get_link_count();
268 sg_link_t* sg_link_list()
270 std::vector<simgrid::s4u::Link*> links = simgrid::s4u::Engine::get_instance()->get_all_links();
272 sg_link_t* res = xbt_new(sg_link_t, links.size());
273 memcpy(res, links.data(), sizeof(sg_link_t) * links.size());