Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / kernel / resource / WifiLinkImpl.cpp
1 /* Copyright (c) 2019-2023. 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 <simgrid/s4u/Comm.hpp>
7 #include <simgrid/s4u/Host.hpp>
8
9 #include "src/kernel/activity/CommImpl.hpp"
10 #include "src/kernel/resource/WifiLinkImpl.hpp"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
13
14 namespace simgrid::kernel::resource {
15
16 /************
17  * Resource *
18  ************/
19 static void update_bw_comm_start(const s4u::Comm& comm)
20 {
21   const auto* pimpl = static_cast<activity::CommImpl*>(comm.get_impl());
22
23   auto const* actionWifi = dynamic_cast<const kernel::resource::WifiLinkAction*>(pimpl->model_action_);
24   if (actionWifi == nullptr)
25     return;
26
27   if (auto* link_src = actionWifi->get_src_link()) {
28     link_src->inc_active_flux();
29   }
30   if (auto* link_dst = actionWifi->get_dst_link()) {
31     link_dst->inc_active_flux();
32   }
33 }
34
35 WifiLinkImpl::WifiLinkImpl(const std::string& name, const std::vector<double>& bandwidths, lmm::System* system)
36     : StandardLinkImpl(name)
37 {
38   this->set_constraint(system->constraint_new(this, 1));
39   for (auto bandwidth : bandwidths)
40     bandwidths_.push_back({bandwidth, 1.0, nullptr});
41   s4u::Comm::on_start_cb(&update_bw_comm_start);
42   s4u::Link::on_communication_state_change_cb(&update_bw_comm_end);
43 }
44
45 void WifiLinkImpl::set_host_rate(const s4u::Host* host, int rate_level)
46 {
47   host_rates_[host->get_name()] = rate_level;
48 }
49
50 double WifiLinkImpl::get_host_rate(const s4u::Host* host) const
51 {
52   auto host_rates_it = host_rates_.find(host->get_name());
53
54   if (host_rates_it == host_rates_.end())
55     return -1;
56
57   int rate_id = host_rates_it->second;
58   xbt_assert(rate_id >= 0,
59              "Negative host wifi rate levels are invalid but host '%s' uses %d as a rate level on link '%s'",
60              host->get_cname(), rate_id, this->get_cname());
61   xbt_assert(rate_id < (int)bandwidths_.size(),
62              "Link '%s' only has %zu wifi rate levels, so the provided level %d is invalid for host '%s'.",
63              this->get_cname(), bandwidths_.size(), rate_id, host->get_cname());
64
65   Metric rate = bandwidths_[rate_id];
66   return rate.peak * rate.scale;
67 }
68
69 s4u::Link::SharingPolicy WifiLinkImpl::get_sharing_policy() const
70 {
71   return s4u::Link::SharingPolicy::WIFI;
72 }
73
74 size_t WifiLinkImpl::get_host_count() const
75 {
76   return host_rates_.size();
77 }
78
79 double WifiLinkImpl::wifi_link_dynamic_sharing(const WifiLinkImpl& link, double /*capacity*/, int /*n*/)
80 {
81   double ratio = link.get_max_ratio();
82   XBT_DEBUG("New ratio value concurrency %d: %lf of link capacity on link %s", link.nb_active_flux_, ratio, link.get_name().c_str());
83   return ratio;
84 }
85
86 void WifiLinkImpl::inc_active_flux()
87 {
88   xbt_assert(nb_active_flux_ >= 0, "Negative nb_active_flux should not exist");
89   nb_active_flux_++;
90 }
91
92 void WifiLinkImpl::dec_active_flux()
93 {
94   xbt_assert(nb_active_flux_ > 0, "Negative nb_active_flux should not exist");
95   nb_active_flux_--;
96 }
97
98 void WifiLinkImpl::update_bw_comm_end(const simgrid::kernel::resource::NetworkAction& action,
99                                       simgrid::kernel::resource::Action::State /*state*/)
100 {
101   if (action.get_state() != kernel::resource::Action::State::FINISHED)
102     return;
103
104   auto const* actionWifi = dynamic_cast<const simgrid::kernel::resource::WifiLinkAction*>(&action);
105   if (actionWifi == nullptr)
106     return;
107
108   if (auto* link_src = actionWifi->get_src_link()) {
109     link_src->dec_active_flux();
110   }
111   if (auto* link_dst = actionWifi->get_dst_link()) {
112     link_dst->dec_active_flux();
113   }
114 }
115
116 double WifiLinkImpl::get_max_ratio() const
117 {
118   double new_peak;
119   if (nb_active_flux_ > conc_lim_) {
120     new_peak = (nb_active_flux_-conc_lim_) * co_acc_ + x0_;
121     XBT_DEBUG("Wi-Fi link peak=(%d-%d)*%lf+%lf=%lf", nb_active_flux_, conc_lim_, co_acc_, x0_, new_peak);
122   } else {
123     new_peak = x0_;
124     XBT_DEBUG("Wi-Fi link peak=%lf", x0_);
125   }
126   // should be the new maximum bandwidth ratio (comparison between max throughput without concurrency and with it)
127   double propCap = new_peak / x0_;
128
129   return propCap;
130 }
131
132 bool WifiLinkImpl::toggle_callback()
133 {
134   if (not use_callback_) {
135       XBT_DEBUG("Activate throughput reduction mechanism");
136     use_callback_ = true;
137     this->set_sharing_policy(
138         simgrid::s4u::Link::SharingPolicy::WIFI,
139         std::bind(&wifi_link_dynamic_sharing, std::cref(*this), std::placeholders::_1, std::placeholders::_2));
140   }
141   return use_callback_;
142 }
143
144 void WifiLinkImpl::set_latency(double value)
145 {
146   xbt_assert(value == 0, "Latency cannot be set for WiFi Links.");
147 }
148 } // namespace simgrid::kernel::resource