1 /* Copyright (c) 2019-2022. 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/s4u/Host.hpp>
8 #include "src/kernel/resource/WifiLinkImpl.hpp"
9 #include "src/surf/surf_interface.hpp"
10 #include "src/kernel/activity/CommImpl.hpp"
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
15 namespace simgrid::kernel::resource {
21 WifiLinkImpl::WifiLinkImpl(const std::string& name, const std::vector<double>& bandwidths, lmm::System* system)
22 : StandardLinkImpl(name)
24 this->set_constraint(system->constraint_new(this, 1));
25 for (auto bandwidth : bandwidths)
26 bandwidths_.push_back({bandwidth, 1.0, nullptr});
28 kernel::activity::CommImpl::on_start.connect(&update_bw_comm_start);
29 s4u::Link::on_communication_state_change_cb(&update_bw_comm_end);
34 void WifiLinkImpl::set_host_rate(const s4u::Host* host, int rate_level)
36 auto insert_done = host_rates_.insert(std::make_pair(host->get_name(), rate_level));
37 if (not insert_done.second)
38 insert_done.first->second = rate_level;
41 double WifiLinkImpl::get_host_rate(const s4u::Host* host) const
43 auto host_rates_it = host_rates_.find(host->get_name());
45 if (host_rates_it == host_rates_.end())
48 int rate_id = host_rates_it->second;
49 xbt_assert(rate_id >= 0,
50 "Negative host wifi rate levels are invalid but host '%s' uses %d as a rate level on link '%s'",
51 host->get_cname(), rate_id, this->get_cname());
52 xbt_assert(rate_id < (int)bandwidths_.size(),
53 "Link '%s' only has %zu wifi rate levels, so the provided level %d is invalid for host '%s'.",
54 this->get_cname(), bandwidths_.size(), rate_id, host->get_cname());
56 Metric rate = bandwidths_[rate_id];
57 return rate.peak * rate.scale;
60 s4u::Link::SharingPolicy WifiLinkImpl::get_sharing_policy() const
62 return s4u::Link::SharingPolicy::WIFI;
65 size_t WifiLinkImpl::get_host_count() const
67 return host_rates_.size();
70 double WifiLinkImpl::wifi_link_dynamic_sharing(WifiLinkImpl* link, double capacity, int n)
72 double ratio = link->get_max_ratio(n);
73 XBT_DEBUG("New ratio value concurrency %d: %lf of link capacity on link %s", n, ratio, link->get_name().c_str());
77 void WifiLinkImpl::inc_active_flux() {
78 xbt_assert(nb_active_flux_>=0, "Negative nb_active_flux should not exist");
82 void WifiLinkImpl::dec_active_flux() {
83 xbt_assert(nb_active_flux_>0, "Negative nb_active_flux should not exist");
87 void WifiLinkImpl::update_bw_comm_start(const kernel::activity::CommImpl& comm)
89 auto* action = static_cast<kernel::resource::NetworkAction*>(comm.surf_action_);
91 auto const* actionWifi = dynamic_cast<const simgrid::kernel::resource::WifiLinkAction*>(action);
92 if (actionWifi == nullptr)
95 auto* link_src = actionWifi->get_src_link();
96 auto* link_dst = actionWifi->get_dst_link();
97 if(link_src != nullptr) {
98 link_src->inc_active_flux();
100 if(link_dst != nullptr) {
101 link_dst->inc_active_flux();
105 void WifiLinkImpl::update_bw_comm_end(simgrid::kernel::resource::NetworkAction& action, simgrid::kernel::resource::Action::State state)
107 if(action.get_state() != kernel::resource::Action::State::FINISHED)
110 auto const* actionWifi = dynamic_cast<const simgrid::kernel::resource::WifiLinkAction*>(&action);
111 if (actionWifi == nullptr)
114 auto* link_src = actionWifi->get_src_link();
115 auto* link_dst = actionWifi->get_dst_link();
116 if(link_src != nullptr) {
117 link_src->dec_active_flux();
119 if(link_dst != nullptr) {
120 link_dst->dec_active_flux();
124 double WifiLinkImpl::get_max_ratio(int nb_active_flux)
126 double new_peak = -1;
127 if(nb_active_flux_ > conc_lim_){
128 new_peak = (nb_active_flux_-conc_lim_) * co_acc_ + x0_;
129 XBT_DEBUG("Wi-Fi link peak=(%d-%d)*%lf+%lf=%lf",nb_active_flux_,conc_lim_,co_acc_,x0_,new_peak);
132 XBT_DEBUG("Wi-Fi link peak=%lf",x0_);
134 // should be the new maximum bandwidth ratio (comparison between max throughput without concurrency and with it)
135 double propCap = new_peak/x0_;
140 bool WifiLinkImpl::toggle_callback()
142 if(! use_callback_) {
143 XBT_DEBUG("Activate throughput reduction mechanism");
144 use_callback_ = true;
145 this->set_sharing_policy(simgrid::s4u::Link::SharingPolicy::WIFI,
146 std::bind(&wifi_link_dynamic_sharing, this, std::placeholders::_1, std::placeholders::_2));
148 return use_callback_;
151 void WifiLinkImpl::set_latency(double value)
153 xbt_assert(value == 0, "Latency cannot be set for WiFi Links.");
155 } // namespace simgrid::kernel::resource