1 /* Copyright (c) 2013-2023. 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. */
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>
13 #include "src/kernel/resource/LinkImpl.hpp"
14 #include "src/kernel/resource/SplitDuplexLinkImpl.hpp"
15 #include "src/kernel/resource/WifiLinkImpl.hpp"
19 template class xbt::Extendable<s4u::Link>;
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_onoff;
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;
30 Link* Link::by_name(const std::string& name)
32 return Engine::get_instance()->link_by_name(name);
35 kernel::resource::StandardLinkImpl* Link::get_impl() const
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"));
45 Link* Link::by_name_or_null(const std::string& name)
47 return Engine::get_instance()->link_by_name_or_null(name);
50 const std::string& Link::get_name() const
52 return this->pimpl_->get_name();
54 const char* Link::get_cname() const
56 return this->pimpl_->get_cname();
58 bool Link::is_used() const
60 return this->pimpl_->is_used();
63 bool Link::is_shared() const
65 return this->pimpl_->get_sharing_policy() != SharingPolicy::FATPIPE;
68 double Link::get_latency() const
70 return this->pimpl_->get_latency();
73 Link* Link::set_latency(double value)
75 kernel::actor::simcall_object_access(pimpl_, [this, value] { pimpl_->set_latency(value); });
79 Link* Link::set_latency(const std::string& value)
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);
87 return set_latency(d_value);
90 double Link::get_bandwidth() const
92 return this->pimpl_->get_bandwidth();
95 Link* Link::set_bandwidth(double value)
97 kernel::actor::simcall_object_access(pimpl_, [this, value] { pimpl_->set_bandwidth(value); });
101 Link* Link::set_sharing_policy(Link::SharingPolicy policy, const NonLinearResourceCb& cb)
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.");
107 kernel::actor::simcall_object_access(pimpl_, [this, policy, &cb] { pimpl_->set_sharing_policy(policy, cb); });
110 Link::SharingPolicy Link::get_sharing_policy() const
112 return this->pimpl_->get_sharing_policy();
115 void Link::set_host_wifi_rate(const s4u::Host* host, int level) const
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);
122 int Link::get_concurrency_limit() const
124 return pimpl_->get_concurrency_limit();
127 Link* Link::set_concurrency_limit(int limit)
129 kernel::actor::simcall_object_access(pimpl_, [this, limit] { pimpl_->set_concurrency_limit(limit); });
133 double Link::get_load() const
135 return this->pimpl_->get_constraint()->get_load();
140 kernel::actor::simcall_answered([this]() { this->pimpl_->turn_on(); });
142 void Link::turn_off()
144 kernel::actor::simcall_answered([this]() { this->pimpl_->turn_off(); });
148 kernel::actor::simcall_answered([this]() { this->pimpl_->seal(); });
149 s4u::Link::on_creation(*this); // notify the signal
153 bool Link::is_on() const
155 return this->pimpl_->is_on();
158 Link* Link::set_state_profile(kernel::profile::Profile* profile)
160 xbt_assert(not pimpl_->is_sealed(), "Cannot set a state profile once the Link is sealed");
161 kernel::actor::simcall_object_access(pimpl_, [this, profile]() { this->pimpl_->set_state_profile(profile); });
165 Link* Link::set_bandwidth_profile(kernel::profile::Profile* profile)
167 xbt_assert(not pimpl_->is_sealed(), "Cannot set a bandwidth profile once the Link is sealed");
168 kernel::actor::simcall_object_access(pimpl_, [this, profile]() { this->pimpl_->set_bandwidth_profile(profile); });
172 Link* Link::set_latency_profile(kernel::profile::Profile* profile)
174 xbt_assert(not pimpl_->is_sealed(), "Cannot set a latency profile once the Link is sealed");
175 kernel::actor::simcall_object_access(pimpl_, [this, profile]() { this->pimpl_->set_latency_profile(profile); });
179 const char* Link::get_property(const std::string& key) const
181 return this->pimpl_->get_property(key);
183 Link* Link::set_property(const std::string& key, const std::string& value)
185 kernel::actor::simcall_object_access(pimpl_, [this, &key, &value] { this->pimpl_->set_property(key, value); });
189 const std::unordered_map<std::string, std::string>* Link::get_properties() const
191 return this->pimpl_->get_properties();
194 Link* Link::set_properties(const std::unordered_map<std::string, std::string>& properties)
196 kernel::actor::simcall_object_access(pimpl_, [this, &properties] { this->pimpl_->set_properties(properties); });
200 Link* SplitDuplexLink::get_link_up() const
202 const auto* pimpl = dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_);
203 xbt_assert(pimpl, "Requesting link_up from a non split-duplex link: %s", get_cname());
204 return pimpl->get_link_up();
207 Link* SplitDuplexLink::get_link_down() const
209 const auto* pimpl = dynamic_cast<kernel::resource::SplitDuplexLinkImpl*>(pimpl_);
210 xbt_assert(pimpl, "Requesting link_down from a non split-duplex link: %s", get_cname());
211 return pimpl->get_link_down();
214 SplitDuplexLink* SplitDuplexLink::by_name(const std::string& name)
216 return Engine::get_instance()->split_duplex_link_by_name(name);
220 } // namespace simgrid
222 /* **************************** Public C interface *************************** */
224 const char* sg_link_get_name(const_sg_link_t link)
226 return link->get_cname();
229 sg_link_t sg_link_by_name(const char* name)
231 return simgrid::s4u::Link::by_name(name);
234 int sg_link_is_shared(const_sg_link_t link)
236 return link->is_shared();
239 double sg_link_get_bandwidth(const_sg_link_t link)
241 return link->get_bandwidth();
244 void sg_link_set_bandwidth(sg_link_t link, double value)
246 link->set_bandwidth(value);
249 double sg_link_get_latency(const_sg_link_t link)
251 return link->get_latency();
254 void sg_link_set_latency(sg_link_t link, double value)
256 link->set_latency(value);
259 void* sg_link_get_data(const_sg_link_t link)
261 return link->get_data<void>();
264 void sg_link_set_data(sg_link_t link, void* data)
266 link->set_data(data);
269 size_t sg_link_count()
271 return simgrid::s4u::Engine::get_instance()->get_link_count();
274 sg_link_t* sg_link_list()
276 std::vector<simgrid::s4u::Link*> links = simgrid::s4u::Engine::get_instance()->get_all_links();
278 auto* res = xbt_new(sg_link_t, links.size());
279 std::copy(begin(links), end(links), res);