Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
callback mechanism
[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
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
14
15 namespace simgrid::kernel::resource {
16
17 /************
18  * Resource *
19  ************/
20
21 WifiLinkImpl::WifiLinkImpl(const std::string& name, const std::vector<double>& bandwidths, lmm::System* system)
22     : StandardLinkImpl(name)
23 {
24   this->set_constraint(system->constraint_new(this, 1));
25   for (auto bandwidth : bandwidths)
26     bandwidths_.push_back({bandwidth, 1.0, nullptr});
27
28   kernel::activity::CommImpl::on_start.connect(&update_bw_comm_start);
29   s4u::Link::on_communication_state_change_cb(&update_bw_comm_end);
30
31
32 }
33
34 void WifiLinkImpl::set_host_rate(const s4u::Host* host, int rate_level)
35 {
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;
39 }
40
41 double WifiLinkImpl::get_host_rate(const s4u::Host* host) const
42 {
43   auto host_rates_it = host_rates_.find(host->get_name());
44
45   if (host_rates_it == host_rates_.end())
46     return -1;
47
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());
55
56   Metric rate = bandwidths_[rate_id];
57   return rate.peak * rate.scale;
58 }
59
60 s4u::Link::SharingPolicy WifiLinkImpl::get_sharing_policy() const
61 {
62   return s4u::Link::SharingPolicy::WIFI;
63 }
64
65 size_t WifiLinkImpl::get_host_count() const
66 {
67   return host_rates_.size();
68 }
69
70 double WifiLinkImpl::wifi_link_dynamic_sharing(WifiLinkImpl* link, double capacity, int n)
71 {
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());
74   return ratio;
75 }
76
77 void WifiLinkImpl::inc_active_flux() {
78   xbt_assert(nb_active_flux_>=0, "Negative nb_active_flux should not exist");
79   nb_active_flux_++;
80 }
81
82 void WifiLinkImpl::dec_active_flux() {
83   xbt_assert(nb_active_flux_>0, "Negative nb_active_flux should not exist");
84   nb_active_flux_--;
85 }
86
87 void WifiLinkImpl::update_bw_comm_start(const kernel::activity::CommImpl& comm)
88 {
89   auto* action = static_cast<kernel::resource::NetworkAction*>(comm.surf_action_);
90
91   auto const* actionWifi = dynamic_cast<const simgrid::kernel::resource::WifiLinkAction*>(action);
92   if (actionWifi == nullptr)
93     return;
94
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();
99   }
100   if(link_dst != nullptr) {
101     link_dst->inc_active_flux();
102   }
103 }
104
105 void WifiLinkImpl::update_bw_comm_end(simgrid::kernel::resource::NetworkAction& action, simgrid::kernel::resource::Action::State state)
106 {
107   if(action.get_state() != kernel::resource::Action::State::FINISHED)
108     return;
109
110   auto const* actionWifi = dynamic_cast<const simgrid::kernel::resource::WifiLinkAction*>(&action);
111   if (actionWifi == nullptr)
112     return;
113
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();
118   }
119   if(link_dst != nullptr) {
120     link_dst->dec_active_flux();
121   }
122 }
123
124 double WifiLinkImpl::get_max_ratio(int nb_active_flux)
125 {
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);
130   }else{
131     new_peak = x0_;
132     XBT_DEBUG("Wi-Fi link peak=%lf",x0_);
133   }
134   // should be the new maximum bandwidth ratio (comparison between max throughput without concurrency and with it)
135   double propCap = new_peak/x0_;
136
137   return propCap;
138 }
139
140 bool WifiLinkImpl::toggle_callback()
141 {
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));
147   }
148   return use_callback_;
149 }
150
151 void WifiLinkImpl::set_latency(double value)
152 {
153   xbt_assert(value == 0, "Latency cannot be set for WiFi Links.");
154 }
155 } // namespace simgrid::kernel::resource