1 /* Copyright (c) 2017-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/plugins/energy.h>
7 #include <simgrid/s4u/Engine.hpp>
8 #include <simgrid/s4u/Link.hpp>
10 #include "src/kernel/activity/CommImpl.hpp"
11 #include "src/kernel/resource/StandardLinkImpl.hpp"
12 #include "src/kernel/resource/WifiLinkImpl.hpp"
14 #include <boost/algorithm/string/classification.hpp>
15 #include <boost/algorithm/string/split.hpp>
17 SIMGRID_REGISTER_PLUGIN(link_energy_wifi, "Energy wifi test", &sg_wifi_energy_plugin_init);
18 /** @defgroup plugin_link_energy_wifi Plugin WiFi energy
20 * This is the WiFi energy plugin, accounting for the dissipated energy of WiFi links.
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(link_energy_wifi, kernel, "Logging specific to the link energy wifi plugin");
25 namespace simgrid::plugin {
27 class XBT_PRIVATE LinkEnergyWifi {
28 // associative array keeping size of data already sent for a given flow (required for interleaved actions)
29 std::map<kernel::resource::WifiLinkAction*, std::pair<int, double>> flowTmp{};
31 // WiFi link the plugin instance is attached to
34 // dynamic energy accumulated since the simulation start (active durations consumption)
36 // static energy (no activity consumption)
39 // duration since previous energy update
40 double prev_update_{0.0};
42 // Same energy calibration values as ns3 by default
43 // https://www.nsnam.org/docs/release/3.30/doxygen/classns3_1_1_wifi_radio_energy_model.html#details
49 // constant taking beacons into account (can be specified by the user)
50 double control_duration_{0.0036};
52 // Measurements for report
53 double dur_TxRx_{0}; // Duration of transmission
54 double dur_idle_{0}; // Duration of idle time
55 bool valuesInit_{false};
58 static xbt::Extension<simgrid::s4u::Link, LinkEnergyWifi> EXTENSION_ID;
60 explicit LinkEnergyWifi(s4u::Link* ptr) : link_(ptr) {}
61 LinkEnergyWifi() = delete;
63 /** Update the energy consumed by link_ when transmissions start or end */
66 /** Update the energy consumed when link_ is destroyed */
67 void update_destroy();
70 * Fetches energy consumption values from the platform file.
71 * The user can specify:
72 * - wifi_watt_values: energy consumption in each state (IDLE:Tx:Rx:SLEEP)
73 * default: 0.82:1.14:0.94:0.10
74 * - controlDuration: duration of active beacon transmissions per second
77 void init_watts_range_list();
79 double get_consumed_energy(void) const { return eDyn_ + eStat_; }
80 /** Get the dynamic part of the energy for this link */
81 double get_energy_dynamic(void) const { return eDyn_; }
82 double get_energy_static(void) const { return eStat_; }
83 double get_duration_comm(void) const { return dur_TxRx_; }
84 double get_duration_idle(void) const { return dur_idle_; }
86 /** Set the power consumed by this link while idle */
87 void set_power_idle(double value) { pIdle_ = value; }
88 /** Set the power consumed by this link while transmitting */
89 void set_power_tx(double value) { pTx_ = value; }
90 /** Set the power consumed by this link while receiving */
91 void set_power_rx(double value) { pRx_ = value; }
92 /** Set the power consumed by this link while sleeping */
93 void set_power_sleep(double value) { pSleep_ = value; }
96 xbt::Extension<s4u::Link, LinkEnergyWifi> LinkEnergyWifi::EXTENSION_ID;
98 void LinkEnergyWifi::update_destroy()
100 auto const* wifi_link = static_cast<kernel::resource::WifiLinkImpl*>(link_->get_impl());
101 double duration = simgrid::s4u::Engine::get_clock() - prev_update_;
102 prev_update_ = simgrid::s4u::Engine::get_clock();
104 dur_idle_ += duration;
106 // add IDLE energy usage, as well as beacons consumption since previous update
107 const auto host_count = static_cast<double>(wifi_link->get_host_count());
108 eDyn_ += duration * control_duration_ * host_count * pRx_;
109 eStat_ += (duration - (duration * control_duration_)) * pIdle_ * (host_count + 1);
111 XBT_DEBUG("finish eStat_ += %f * %f * (%f+1) | eStat = %f", duration, pIdle_, host_count, eStat_);
114 void LinkEnergyWifi::update()
116 init_watts_range_list();
118 double duration = simgrid::s4u::Engine::get_clock() - prev_update_;
119 prev_update_ = simgrid::s4u::Engine::get_clock();
121 // we don't update for null durations
125 auto const* wifi_link = static_cast<kernel::resource::WifiLinkImpl*>(link_->get_impl());
127 const kernel::lmm::Element* elem = nullptr;
130 * We update the energy consumed by each flow active on the link since the previous update.
132 * To do this, we need to know how much time each flow has been effectively sending data on the WiFi link since the
133 * previous update (durUsage). We compute this value using the size of the flow, the amount of data already spent
134 * (using flowTmp), as well as the bandwidth used by the flow since the previous update (using LMM variables). Since
135 * flows are sharing the medium, the total active duration on the link is equal to the transmission/reception duration
136 * used by the flow with the longest active time since the previous update
139 while (const auto* var = wifi_link->get_constraint()->get_variable(&elem)) {
140 auto* action = static_cast<kernel::resource::WifiLinkAction*>(var->get_id());
141 XBT_DEBUG("cost: %f action value: %f link rate 1: %f link rate 2: %f", action->get_cost(), action->get_rate(),
142 wifi_link->get_host_rate(&action->get_src()), wifi_link->get_host_rate(&action->get_dst()));
144 if (action->get_rate() != 0.0) {
145 auto it = flowTmp.find(action);
147 // if the flow has not been registered, initialize it: 0 bytes sent, and not updated since its creation timestamp
148 if(it == flowTmp.end())
149 flowTmp[action] = std::pair<int,double>(0, action->get_start_time());
151 it = flowTmp.find(action);
154 * The active duration of the link is equal to the amount of data it had to send divided by the bandwidth on the link.
155 * If this is longer than the duration since the previous update, active duration = now - previous_update
157 double du = // durUsage on the current flow
158 (action->get_cost() - it->second.first) / action->get_rate();
160 if (du > simgrid::s4u::Engine::get_clock() - it->second.second)
161 du = simgrid::s4u::Engine::get_clock() - it->second.second;
163 // if the flow has been more active than the others
167 // update the amount of data already sent by the flow
168 it->second.first += du * action->get_rate();
169 it->second.second = simgrid::s4u::Engine::get_clock();
171 // important: if the transmission finished, remove it (needed for performance and multi-message flows)
172 if(it->second.first >= action->get_cost())
177 XBT_DEBUG("durUsage: %f", durUsage);
180 const auto host_count = static_cast<double>(wifi_link->get_host_count());
181 eDyn_ += duration * control_duration_ * host_count * pRx_;
184 * Same principle as ns3:
185 * - if tx or rx, update P_{dyn}
186 * - if idle i.e. get_usage = 0, update P_{stat}
187 * P_{tot} = P_{dyn}+P_{stat}
189 if (link_->get_usage() != 0.0) {
190 eDyn_ += /*duration * */ durUsage * ((host_count * pRx_) + pTx_);
191 eStat_ += (duration - durUsage) * pIdle_ * (host_count + 1);
192 XBT_DEBUG("eDyn += %f * ((%f * %f) + %f) | eDyn = %f (durusage =%f)", durUsage, host_count, pRx_, pTx_, eDyn_,
194 dur_TxRx_ += duration;
196 dur_idle_ += duration;
197 eStat_ += (duration - (duration * control_duration_)) * pIdle_ * (host_count + 1);
200 XBT_DEBUG("eStat_ += %f * %f * (%f+1) | eStat = %f", duration, pIdle_, host_count, eStat_);
203 void LinkEnergyWifi::init_watts_range_list()
210 Set to 0 if you do not want to compute beacons,
211 otherwise to the duration of beacons transmissions per second
213 if (const char* beacons_factor = this->link_->get_property("control_duration")) {
215 control_duration_ = std::stod(beacons_factor);
216 } catch (const std::invalid_argument&) {
217 throw std::invalid_argument(std::string("Invalid beacons factor value for link ") + this->link_->get_cname());
221 const char* all_power_values_str = this->link_->get_property("wifi_watt_values");
222 if (all_power_values_str != nullptr)
224 std::vector<std::string> all_power_values;
225 boost::split(all_power_values, all_power_values_str, boost::is_any_of(","));
227 for (auto current_power_values_str : all_power_values) {
228 /* retrieve the power values associated */
229 std::vector<std::string> current_power_values;
230 boost::split(current_power_values, current_power_values_str, boost::is_any_of(":"));
231 xbt_assert(current_power_values.size() == 4,
232 "Power properties incorrectly defined - could not retrieve idle, Tx, Rx, Sleep power values for link %s",
233 this->link_->get_cname());
235 /* min_power corresponds to the idle power (link load = 0) */
236 /* max_power is the power consumed at 100% link load */
238 pSleep_ = std::stod(current_power_values.at(3));
239 } catch (const std::invalid_argument&) {
240 throw std::invalid_argument(std::string("Invalid idle power value for link ") + this->link_->get_cname());
243 pRx_ = std::stod(current_power_values.at(2));
244 } catch (const std::invalid_argument&) {
245 throw std::invalid_argument(std::string("Invalid idle power value for link ") + this->link_->get_cname());
248 pTx_ = std::stod(current_power_values.at(1));
249 } catch (const std::invalid_argument&) {
250 throw std::invalid_argument(std::string("Invalid idle power value for link ") + this->link_->get_cname());
253 pIdle_ = std::stod(current_power_values.at(0));
254 } catch (const std::invalid_argument&) {
255 throw std::invalid_argument(std::string("Invalid busy power value for link ") + this->link_->get_cname());
258 XBT_DEBUG("Values aa initialized with: pSleep=%f pIdle=%f pTx=%f pRx=%f", pSleep_, pIdle_, pTx_, pRx_);
263 } // namespace simgrid::plugin
265 using simgrid::plugin::LinkEnergyWifi;
266 /* **************************** events callback *************************** */
267 static void on_communication(const simgrid::kernel::activity::CommImpl& comm)
269 for (const auto* link : comm.get_traversed_links()) {
270 if (link != nullptr && link->get_sharing_policy() == simgrid::s4u::Link::SharingPolicy::WIFI) {
271 auto* link_energy = link->extension<LinkEnergyWifi>();
272 XBT_DEBUG("Update %s on Comm Start/End", link->get_cname());
273 link_energy->update();
278 void sg_wifi_energy_plugin_init()
280 if (LinkEnergyWifi::EXTENSION_ID.valid())
283 XBT_INFO("Activating the wifi_energy plugin.");
284 LinkEnergyWifi::EXTENSION_ID = simgrid::s4u::Link::extension_create<LinkEnergyWifi>();
287 * Attaching to events:
288 * - Link::on_creation to initialize the plugin
289 * - Link::on_destruction to produce final energy results
290 * - Link::on_communication_state_change: to account for the energy when communications are updated
291 * - CommImpl::on_start and CommImpl::on_completion: to account for the energy during communications
293 simgrid::s4u::Link::on_creation_cb([](simgrid::s4u::Link& link) {
294 // verify the link is appropriate to WiFi energy computations
295 if (link.get_sharing_policy() == simgrid::s4u::Link::SharingPolicy::WIFI) {
296 XBT_DEBUG("Wifi Link: %s, initialization of wifi energy plugin", link.get_cname());
297 auto* plugin = new LinkEnergyWifi(&link);
298 link.extension_set(plugin);
300 XBT_DEBUG("Not Wifi Link: %s, wifi energy on link not computed", link.get_cname());
304 simgrid::s4u::Link::on_destruction_cb([](simgrid::s4u::Link const& link) {
305 // output energy values if WiFi link
306 if (link.get_sharing_policy() == simgrid::s4u::Link::SharingPolicy::WIFI) {
307 link.extension<LinkEnergyWifi>()->update_destroy();
309 "Link %s destroyed, consumed: %f J dyn: %f stat: %f durIdle: %f durTxRx: %f", link.get_cname(),
310 link.extension<LinkEnergyWifi>()->get_consumed_energy(),
311 link.extension<LinkEnergyWifi>()->get_energy_dynamic(), link.extension<LinkEnergyWifi>()->get_energy_static(),
312 link.extension<LinkEnergyWifi>()->get_duration_idle(), link.extension<LinkEnergyWifi>()->get_duration_comm());
316 simgrid::s4u::Link::on_communication_state_change_cb([](simgrid::kernel::resource::NetworkAction const& action,
317 simgrid::kernel::resource::Action::State /* previous */) {
318 // update WiFi links encountered during the communication
319 for (auto const* link : action.get_links()) {
320 if (link != nullptr && link->get_sharing_policy() == simgrid::s4u::Link::SharingPolicy::WIFI) {
321 link->get_iface()->extension<LinkEnergyWifi>()->update();
326 simgrid::kernel::activity::CommImpl::on_start.connect(&on_communication);
327 simgrid::kernel::activity::CommImpl::on_completion.connect(&on_communication);