1 /* Copyright (c) 2017-2023. 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/Comm.hpp>
8 #include <simgrid/s4u/Engine.hpp>
9 #include <simgrid/s4u/Link.hpp>
11 #include "src/kernel/activity/CommImpl.hpp"
12 #include "src/kernel/resource/StandardLinkImpl.hpp"
13 #include "src/kernel/resource/WifiLinkImpl.hpp"
14 #include "src/simgrid/module.hpp"
16 #include <boost/algorithm/string/classification.hpp>
17 #include <boost/algorithm/string/split.hpp>
19 SIMGRID_REGISTER_PLUGIN(link_energy_wifi, "Energy wifi test", &sg_wifi_energy_plugin_init);
20 /** @defgroup plugin_link_energy_wifi Plugin WiFi energy
22 * This is the WiFi energy plugin, accounting for the dissipated energy of WiFi links.
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(link_energy_wifi, kernel, "Logging specific to the link energy wifi plugin");
27 namespace simgrid::plugin {
29 class XBT_PRIVATE LinkEnergyWifi {
30 // associative array keeping size of data already sent for a given flow (required for interleaved actions)
31 std::map<kernel::resource::WifiLinkAction*, std::pair<int, double>> flowTmp{};
33 // WiFi link the plugin instance is attached to
36 // dynamic energy accumulated since the simulation start (active durations consumption)
38 // static energy (no activity consumption)
41 // duration since previous energy update
42 double prev_update_{0.0};
44 // Same energy calibration values as ns3 by default
45 // https://www.nsnam.org/docs/release/3.30/doxygen/classns3_1_1_wifi_radio_energy_model.html#details
51 // constant taking beacons into account (can be specified by the user)
52 double control_duration_{0.0036};
54 // Measurements for report
55 double dur_TxRx_{0}; // Duration of transmission
56 double dur_idle_{0}; // Duration of idle time
57 bool valuesInit_{false};
60 static xbt::Extension<simgrid::s4u::Link, LinkEnergyWifi> EXTENSION_ID;
62 explicit LinkEnergyWifi(s4u::Link* ptr) : link_(ptr) {}
63 LinkEnergyWifi() = delete;
65 /** Update the energy consumed by link_ when transmissions start or end */
68 /** Update the energy consumed when link_ is destroyed */
69 void update_destroy();
72 * Fetches energy consumption values from the platform file.
73 * The user can specify:
74 * - wifi_watt_values: energy consumption in each state (IDLE:Tx:Rx:SLEEP)
75 * default: 0.82:1.14:0.94:0.10
76 * - controlDuration: duration of active beacon transmissions per second
79 void init_watts_range_list();
81 double get_consumed_energy(void) const { return eDyn_ + eStat_; }
82 /** Get the dynamic part of the energy for this link */
83 double get_energy_dynamic(void) const { return eDyn_; }
84 double get_energy_static(void) const { return eStat_; }
85 double get_duration_comm(void) const { return dur_TxRx_; }
86 double get_duration_idle(void) const { return dur_idle_; }
88 /** Set the power consumed by this link while idle */
89 void set_power_idle(double value) { pIdle_ = value; }
90 /** Set the power consumed by this link while transmitting */
91 void set_power_tx(double value) { pTx_ = value; }
92 /** Set the power consumed by this link while receiving */
93 void set_power_rx(double value) { pRx_ = value; }
94 /** Set the power consumed by this link while sleeping */
95 void set_power_sleep(double value) { pSleep_ = value; }
98 xbt::Extension<s4u::Link, LinkEnergyWifi> LinkEnergyWifi::EXTENSION_ID;
100 void LinkEnergyWifi::update_destroy()
102 auto const* wifi_link = static_cast<kernel::resource::WifiLinkImpl*>(link_->get_impl());
103 double duration = simgrid::s4u::Engine::get_clock() - prev_update_;
104 prev_update_ = simgrid::s4u::Engine::get_clock();
106 dur_idle_ += duration;
108 // add IDLE energy usage, as well as beacons consumption since previous update
109 const auto host_count = static_cast<double>(wifi_link->get_host_count());
110 eDyn_ += duration * control_duration_ * host_count * pRx_;
111 eStat_ += (duration - (duration * control_duration_)) * pIdle_ * (host_count + 1);
113 XBT_DEBUG("finish eStat_ += %f * %f * (%f+1) | eStat = %f", duration, pIdle_, host_count, eStat_);
116 void LinkEnergyWifi::update()
118 init_watts_range_list();
120 double duration = simgrid::s4u::Engine::get_clock() - prev_update_;
121 prev_update_ = simgrid::s4u::Engine::get_clock();
123 // we don't update for null durations
127 auto const* wifi_link = static_cast<kernel::resource::WifiLinkImpl*>(link_->get_impl());
129 const kernel::lmm::Element* elem = nullptr;
132 * We update the energy consumed by each flow active on the link since the previous update.
134 * To do this, we need to know how much time each flow has been effectively sending data on the WiFi link since the
135 * previous update (durUsage). We compute this value using the size of the flow, the amount of data already spent
136 * (using flowTmp), as well as the bandwidth used by the flow since the previous update (using LMM variables). Since
137 * flows are sharing the medium, the total active duration on the link is equal to the transmission/reception duration
138 * used by the flow with the longest active time since the previous update
141 while (const auto* var = wifi_link->get_constraint()->get_variable(&elem)) {
142 auto* action = static_cast<kernel::resource::WifiLinkAction*>(var->get_id());
143 XBT_DEBUG("cost: %f action value: %f link rate 1: %f link rate 2: %f", action->get_cost(), action->get_rate(),
144 wifi_link->get_host_rate(&action->get_src()), wifi_link->get_host_rate(&action->get_dst()));
146 if (action->get_rate() != 0.0) {
147 auto it = flowTmp.find(action);
149 // if the flow has not been registered, initialize it: 0 bytes sent, and not updated since its creation timestamp
150 if(it == flowTmp.end())
151 flowTmp[action] = std::pair<int,double>(0, action->get_start_time());
153 it = flowTmp.find(action);
156 * The active duration of the link is equal to the amount of data it had to send divided by the bandwidth on the link.
157 * If this is longer than the duration since the previous update, active duration = now - previous_update
159 double du = // durUsage on the current flow
160 (action->get_cost() - it->second.first) / action->get_rate();
162 if (du > simgrid::s4u::Engine::get_clock() - it->second.second)
163 du = simgrid::s4u::Engine::get_clock() - it->second.second;
165 // if the flow has been more active than the others
169 // update the amount of data already sent by the flow
170 it->second.first += du * action->get_rate();
171 it->second.second = simgrid::s4u::Engine::get_clock();
173 // important: if the transmission finished, remove it (needed for performance and multi-message flows)
174 if(it->second.first >= action->get_cost())
179 XBT_DEBUG("durUsage: %f", durUsage);
182 const auto host_count = static_cast<double>(wifi_link->get_host_count());
183 eDyn_ += duration * control_duration_ * host_count * pRx_;
186 * Same principle as ns3:
187 * - if tx or rx, update P_{dyn}
188 * - if idle i.e. get_usage = 0, update P_{stat}
189 * P_{tot} = P_{dyn}+P_{stat}
191 if (link_->get_load() != 0.0) {
192 eDyn_ += /*duration * */ durUsage * ((host_count * pRx_) + pTx_);
193 eStat_ += (duration - durUsage) * pIdle_ * (host_count + 1);
194 XBT_DEBUG("eDyn += %f * ((%f * %f) + %f) | eDyn = %f (durusage =%f)", durUsage, host_count, pRx_, pTx_, eDyn_,
196 dur_TxRx_ += duration;
198 dur_idle_ += duration;
199 eStat_ += (duration - (duration * control_duration_)) * pIdle_ * (host_count + 1);
202 XBT_DEBUG("eStat_ += %f * %f * (%f+1) | eStat = %f", duration, pIdle_, host_count, eStat_);
205 void LinkEnergyWifi::init_watts_range_list()
212 Set to 0 if you do not want to compute beacons,
213 otherwise to the duration of beacons transmissions per second
215 if (const char* beacons_factor = this->link_->get_property("control_duration")) {
217 control_duration_ = std::stod(beacons_factor);
218 } catch (const std::invalid_argument&) {
219 throw std::invalid_argument("Invalid beacons factor value for link " + this->link_->get_name());
223 const char* all_power_values_str = this->link_->get_property("wifi_watt_values");
224 if (all_power_values_str != nullptr)
226 std::vector<std::string> all_power_values;
227 boost::split(all_power_values, all_power_values_str, boost::is_any_of(","));
229 for (auto current_power_values_str : all_power_values) {
230 /* retrieve the power values associated */
231 std::vector<std::string> current_power_values;
232 boost::split(current_power_values, current_power_values_str, boost::is_any_of(":"));
233 xbt_assert(current_power_values.size() == 4,
234 "Power properties incorrectly defined - could not retrieve idle, Tx, Rx, Sleep power values for link %s",
235 this->link_->get_cname());
237 /* min_power corresponds to the idle power (link load = 0) */
238 /* max_power is the power consumed at 100% link load */
240 pSleep_ = std::stod(current_power_values.at(3));
241 } catch (const std::invalid_argument&) {
242 throw std::invalid_argument("Invalid idle power value for link " + this->link_->get_name());
245 pRx_ = std::stod(current_power_values.at(2));
246 } catch (const std::invalid_argument&) {
247 throw std::invalid_argument("Invalid idle power value for link " + this->link_->get_name());
250 pTx_ = std::stod(current_power_values.at(1));
251 } catch (const std::invalid_argument&) {
252 throw std::invalid_argument("Invalid idle power value for link " + this->link_->get_name());
255 pIdle_ = std::stod(current_power_values.at(0));
256 } catch (const std::invalid_argument&) {
257 throw std::invalid_argument("Invalid busy power value for link " + this->link_->get_name());
260 XBT_DEBUG("Values aa initialized with: pSleep=%f pIdle=%f pTx=%f pRx=%f", pSleep_, pIdle_, pTx_, pRx_);
265 } // namespace simgrid::plugin
267 using simgrid::plugin::LinkEnergyWifi;
268 /* **************************** events callback *************************** */
269 static void on_communication(const simgrid::s4u::Comm& comm)
271 const auto* pimpl = static_cast<simgrid::kernel::activity::CommImpl*>(comm.get_impl());
272 for (auto const* link : pimpl->get_traversed_links()) {
273 if (link != nullptr && link->get_sharing_policy() == simgrid::s4u::Link::SharingPolicy::WIFI) {
274 auto* link_energy = link->extension<LinkEnergyWifi>();
275 XBT_DEBUG("Update %s on Comm Start/End", link->get_cname());
276 link_energy->update();
281 void sg_wifi_energy_plugin_init()
283 if (LinkEnergyWifi::EXTENSION_ID.valid())
286 XBT_INFO("Activating the wifi_energy plugin.");
287 LinkEnergyWifi::EXTENSION_ID = simgrid::s4u::Link::extension_create<LinkEnergyWifi>();
290 * Attaching to events:
291 * - Link::on_creation to initialize the plugin
292 * - Link::on_destruction to produce final energy results
293 * - Link::on_communication_state_change: to account for the energy when communications are updated
294 * - CommImpl::on_start and CommImpl::on_completion: to account for the energy during communications
296 simgrid::s4u::Link::on_creation_cb([](simgrid::s4u::Link& link) {
297 // verify the link is appropriate to WiFi energy computations
298 if (link.get_sharing_policy() == simgrid::s4u::Link::SharingPolicy::WIFI) {
299 XBT_DEBUG("Wifi Link: %s, initialization of wifi energy plugin", link.get_cname());
300 auto* plugin = new LinkEnergyWifi(&link);
301 link.extension_set(plugin);
303 XBT_DEBUG("Not Wifi Link: %s, wifi energy on link not computed", link.get_cname());
307 simgrid::s4u::Link::on_destruction_cb([](simgrid::s4u::Link const& link) {
308 // output energy values if WiFi link
309 if (link.get_sharing_policy() == simgrid::s4u::Link::SharingPolicy::WIFI) {
310 link.extension<LinkEnergyWifi>()->update_destroy();
312 "Link %s destroyed, consumed: %f J dyn: %f stat: %f durIdle: %f durTxRx: %f", link.get_cname(),
313 link.extension<LinkEnergyWifi>()->get_consumed_energy(),
314 link.extension<LinkEnergyWifi>()->get_energy_dynamic(), link.extension<LinkEnergyWifi>()->get_energy_static(),
315 link.extension<LinkEnergyWifi>()->get_duration_idle(), link.extension<LinkEnergyWifi>()->get_duration_comm());
319 simgrid::s4u::Link::on_communication_state_change_cb([](simgrid::kernel::resource::NetworkAction const& action,
320 simgrid::kernel::resource::Action::State /* previous */) {
321 // update WiFi links encountered during the communication
322 for (auto const* link : action.get_links()) {
323 if (link != nullptr && link->get_sharing_policy() == simgrid::s4u::Link::SharingPolicy::WIFI) {
324 link->get_iface()->extension<LinkEnergyWifi>()->update();
329 simgrid::s4u::Comm::on_start_cb(&on_communication);
330 simgrid::s4u::Comm::on_completion_cb(&on_communication);