1 /* Copyright (c) 2010, 2012-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include <src/surf/plugins/energy.hpp>
8 #include <src/surf/cpu_interface.hpp>
9 #include <src/surf/virtual_machine.hpp>
11 /** @addtogroup SURF_plugin_energy
14 This is the energy plugin, enabling to account not only for computation time,
15 but also for the dissipated energy in the simulated platform.
17 The energy consumption of a CPU depends directly of its current load. Specify that consumption in your platform file as follows:
20 <host id="HostA" power="100.0Mf" >
21 <prop id="watt_per_state" value="100.0:200.0" />
22 <prop id="watt_off" value="10" />
26 The first property means that when your host is up and running, but without anything to do, it will dissipate 100 Watts.
27 If it's fully loaded, it will dissipate 200 Watts. If its load is at 50%, then it will dissipate 150 Watts.
28 The second property means that when your host is turned off, it will dissipate only 10 Watts (please note that these values are arbitrary).
30 If your CPU is using pstates, then you can provide one consumption interval per pstate.
33 <host id="HostB" power="100.0Mf,50.0Mf,20.0Mf" pstate="0" >
34 <prop id="watt_per_state" value="95.0:200.0, 93.0:170.0, 90.0:150.0" />
35 <prop id="watt_off" value="10" />
39 That host has 3 levels of performance with the following performance: 100 Mflop/s, 50 Mflop/s or 20 Mflop/s.
40 It starts at pstate 0 (ie, at 100 Mflop/s). In this case, you have to specify one interval per pstate in the watt_per_state property.
41 In this example, the idle consumption is 95 Watts, 93 Watts and 90 Watts in each pstate while the CPU burn consumption are at 200 Watts,
42 170 Watts and 150 Watts respectively.
44 To change the pstate of a given CPU, use the following functions: #MSG_host_get_nb_pstates(), #MSG_host_set_pstate(), #MSG_host_get_power_peak_at().
46 To simulate the energy-related elements, first call the #sg_energy_plugin_init() before your #MSG_init(),
47 and then use the following function to retrieve the consumption of a given host: #MSG_host_get_consumed_energy().
50 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_energy, surf,
51 "Logging specific to the SURF energy plugin");
53 std::map<simgrid::surf::Host*, simgrid::energy::HostEnergy*> *simgrid::energy::surf_energy = NULL;
55 using simgrid::energy::HostEnergy;
57 /* Computes the consumption so far. Called lazily on need. */
58 static void update_consumption(simgrid::surf::Host *host, HostEnergy *host_energy) {
59 double start_time = host_energy->last_updated;
60 double finish_time = surf_get_clock();
62 if (host->p_cpu->m_speedPeak == 0)
63 // Some users declare a pstate of speed 0 flops (eg to model boot time).
64 // We consider that the machine is then fully loaded. That's arbitrary but it avoids a NaN
67 cpu_load = lmm_constraint_get_usage(host->p_cpu->getConstraint()) / host->p_cpu->m_speedPeak;
69 if (cpu_load > 1) // A machine with a load > 1 consumes as much as a fully loaded machine, not mores
72 double previous_energy = host_energy->total_energy;
74 double instantaneous_consumption;
76 instantaneous_consumption = host_energy->watts_off;
78 instantaneous_consumption = host_energy->getCurrentWattsValue(cpu_load);
80 double energy_this_step = instantaneous_consumption*(finish_time-start_time);
82 host_energy->total_energy = previous_energy + energy_this_step;
83 host_energy->last_updated = finish_time;
85 XBT_DEBUG("[update_energy of %s] period=[%.2f-%.2f]; current power peak=%.0E flop/s; consumption change: %.2f J -> %.2f J",
86 host->getName(), start_time, finish_time, host->p_cpu->m_speedPeak, previous_energy, energy_this_step);
89 /** \ingroup SURF_plugin_energy
90 * \brief Enable energy plugin
91 * \details Enable energy plugin to get joules consumption of each cpu. You should call this function before #MSG_init().
93 void sg_energy_plugin_init() {
94 if (simgrid::energy::surf_energy == NULL) {
96 simgrid::energy::surf_energy = new std::map<simgrid::surf::Host*, simgrid::energy::HostEnergy*>();
98 /* The following attaches an anonymous function to the Host::onCreation signal */
99 /* Search for "C++ lambda" for more information on the syntax used here */
100 simgrid::surf::Host::onCreation.connect([](simgrid::surf::Host *host) {
101 if (dynamic_cast<simgrid::surf::VirtualMachine*>(host)) // Ignore virtual machines
104 (*simgrid::energy::surf_energy)[host] = new HostEnergy(host);
107 simgrid::surf::Host::onDestruction.connect([](simgrid::surf::Host *host) {
108 if (dynamic_cast<simgrid::surf::VirtualMachine*>(host)) // Ignore virtual machines
111 std::map<simgrid::surf::Host*, HostEnergy*>::iterator host_energy_it = simgrid::energy::surf_energy->find(host);
112 xbt_assert(host_energy_it != simgrid::energy::surf_energy->end(), "The host is not in surf_energy.");
114 HostEnergy *host_energy = host_energy_it->second;
115 update_consumption(host, host_energy);
117 XBT_INFO("Total energy of host %s: %f Joules", host->getName(), host_energy->getConsumedEnergy());
118 host_energy_it->second->unref();
119 simgrid::energy::surf_energy->erase(host_energy_it);
122 simgrid::surf::CpuAction::onStateChange.connect([](simgrid::surf::CpuAction *action,
123 e_surf_action_state_t old,
124 e_surf_action_state_t cur) {
125 const char *name = getActionCpu(action)->getName();
126 simgrid::surf::Host *host = surf_host_resource_priv(sg_host_by_name(name));
127 simgrid::surf::VirtualMachine *vm = dynamic_cast<simgrid::surf::VirtualMachine*>(host);
128 if (vm) // If it's a VM, take the corresponding PM
129 host = surf_host_resource_priv(vm->getPm());
131 HostEnergy *host_energy = (*simgrid::energy::surf_energy)[host];
133 if(host_energy->last_updated < surf_get_clock())
134 update_consumption(host, host_energy);
138 simgrid::surf::Host::onStateChange.connect([] (simgrid::surf::Host *host) {
139 if (dynamic_cast<simgrid::surf::VirtualMachine*>(host)) // Ignore virtual machines
142 HostEnergy *host_energy = (*simgrid::energy::surf_energy)[host];
144 if(host_energy->last_updated < surf_get_clock())
145 update_consumption(host, host_energy);
148 simgrid::surf::surfExitCallbacks.connect([]() {
149 delete simgrid::energy::surf_energy;
150 simgrid::energy::surf_energy = NULL;
161 HostEnergy::HostEnergy(simgrid::surf::Host *ptr)
165 power_range_watts_list = getWattsRangeList();
166 last_updated = surf_get_clock();
168 if (host->getProperties() != NULL) {
169 char* off_power_str = (char*)xbt_dict_get_or_null(host->getProperties(), "watt_off");
170 if (off_power_str != NULL)
171 watts_off = atof(off_power_str);
178 HostEnergy::~HostEnergy(){
180 xbt_dynar_t power_tuple = NULL;
181 xbt_dynar_foreach(power_range_watts_list, iter, power_tuple)
182 xbt_dynar_free(&power_tuple);
183 xbt_dynar_free(&power_range_watts_list);
187 double HostEnergy::getWattMinAt(int pstate) {
188 xbt_dynar_t power_range_list = power_range_watts_list;
189 xbt_assert(power_range_watts_list, "No power range properties specified for host %s", host->getName());
190 xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, host->p_cpu->getPState(), xbt_dynar_t);
191 double min_power = xbt_dynar_get_as(current_power_values, 0, double);
194 double HostEnergy::getWattMaxAt(int pstate) {
195 xbt_dynar_t power_range_list = power_range_watts_list;
196 xbt_assert(power_range_watts_list, "No power range properties specified for host %s", host->getName());
197 xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, host->p_cpu->getPState(), xbt_dynar_t);
198 double max_power = xbt_dynar_get_as(current_power_values, 1, double);
202 /** @brief Computes the power consumed by the host according to the current pstate and processor load */
203 double HostEnergy::getCurrentWattsValue(double cpu_load)
205 xbt_dynar_t power_range_list = power_range_watts_list;
206 xbt_assert(power_range_watts_list, "No power range properties specified for host %s", host->getName());
208 int pstate = host->p_cpu->getPState();
209 xbt_assert(pstate < (int)xbt_dynar_length(power_range_list),
210 "pstate %d >= power range amound %d",pstate,(int)xbt_dynar_length(power_range_list));
211 /* retrieve the power values associated with the current pstate */
212 xbt_dynar_t current_power_values = xbt_dynar_get_as( power_range_list, pstate, xbt_dynar_t);
214 /* min_power corresponds to the idle power (cpu load = 0) */
215 /* max_power is the power consumed at 100% cpu load */
216 double min_power = xbt_dynar_get_as(current_power_values, 0, double);
217 double max_power = xbt_dynar_get_as(current_power_values, 1, double);
218 double power_slope = max_power - min_power;
220 double current_power = min_power + cpu_load * power_slope;
222 XBT_DEBUG("[get_current_watts] min_power=%f, max_power=%f, slope=%f", min_power, max_power, power_slope);
223 XBT_DEBUG("[get_current_watts] Current power (watts) = %f, load = %f", current_power, cpu_load);
225 return current_power;
228 double HostEnergy::getConsumedEnergy()
230 if(last_updated < surf_get_clock())
231 update_consumption(host, this);
235 xbt_dynar_t HostEnergy::getWattsRangeList()
237 xbt_dynar_t power_range_list;
238 xbt_dynar_t power_tuple;
239 int i = 0, pstate_nb=0;
240 xbt_dynar_t current_power_values;
241 double min_power, max_power;
243 if (host->getProperties() == NULL)
246 char* all_power_values_str = (char*)xbt_dict_get_or_null(host->getProperties(), "watt_per_state");
248 if (all_power_values_str == NULL)
252 power_range_list = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
253 xbt_dynar_t all_power_values = xbt_str_split(all_power_values_str, ",");
255 pstate_nb = xbt_dynar_length(all_power_values);
256 for (i=0; i< pstate_nb; i++)
258 /* retrieve the power values associated with the current pstate */
259 current_power_values = xbt_str_split(xbt_dynar_get_as(all_power_values, i, char*), ":");
260 xbt_assert(xbt_dynar_length(current_power_values) > 1,
261 "Power properties incorrectly defined - could not retrieve min and max power values for host %s",
264 /* min_power corresponds to the idle power (cpu load = 0) */
265 /* max_power is the power consumed at 100% cpu load */
266 min_power = atof(xbt_dynar_get_as(current_power_values, 0, char*));
267 max_power = atof(xbt_dynar_get_as(current_power_values, 1, char*));
269 power_tuple = xbt_dynar_new(sizeof(double), NULL);
270 xbt_dynar_push_as(power_tuple, double, min_power);
271 xbt_dynar_push_as(power_tuple, double, max_power);
273 xbt_dynar_push_as(power_range_list, xbt_dynar_t, power_tuple);
274 xbt_dynar_free(¤t_power_values);
276 xbt_dynar_free(&all_power_values);
277 return power_range_list;