Logo AND Algorithmique Numérique Distribuée

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