Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
87bd154583b1ffb92fe4f929c9073aa4db0fdd41
[simgrid.git] / src / surf / plugins / energy.cpp
1 /* Copyright (c) 2010, 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "energy.hpp"
8 #include "../cpu_cas01.hpp"
9 #include "../virtual_machine.hpp"
10
11 /** @addtogroup SURF_plugin_energy
12
13
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.
16
17 The energy consumption of a CPU depends directly of its current load. Specify that consumption in your platform file as follows:
18
19 \verbatim
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" />
23 </host>
24 \endverbatim
25
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).
29
30 If your CPU is using pstates, then you can provide one consumption interval per pstate.
31
32 \verbatim
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" />
36 </host>
37 \endverbatim
38
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.
43
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().
45
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().
48  */
49
50 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_energy, surf,
51                                 "Logging specific to the SURF energy plugin");
52
53 std::map<Host*, HostEnergy*> *surf_energy=NULL;
54
55 static void energyHostCreatedCallback(Host *host){
56   (*surf_energy)[host] = new HostEnergy(host);
57 }
58
59 static void energyVMCreatedCallback(VirtualMachine* vm) {
60   std::map<Host*, HostEnergy*>::iterator host_energy_it = surf_energy->find(vm->p_subWs);
61   xbt_assert(host_energy_it != surf_energy->end(), "The host is not in surf_energy.");
62   (*surf_energy)[vm] = host_energy_it->second;
63   host_energy_it->second->ref(); // protect the HostEnergy from getting deleted too early
64 }
65
66 /* Computes the consumption so far.  Called lazily on need. */
67 static void update_consumption(Host *host, HostEnergy *host_energy) {
68         double cpu_load = lmm_constraint_get_usage(host->p_cpu->getConstraint()) / host->p_cpu->m_speedPeak;
69         double start_time = host_energy->last_updated;
70         double finish_time = surf_get_clock();
71
72         double previous_energy = host_energy->total_energy;
73
74         double instantaneous_consumption;
75         if (host->getState() == SURF_RESOURCE_OFF)
76                 instantaneous_consumption = host_energy->watts_off;
77         else
78                 instantaneous_consumption = host_energy->getCurrentWattsValue(cpu_load);
79
80         double energy_this_step = instantaneous_consumption*(finish_time-start_time);
81
82         host_energy->total_energy = previous_energy + energy_this_step;
83         host_energy->last_updated = finish_time;
84
85         XBT_DEBUG("[cpu_update_energy] period=[%.2f-%.2f]; current power peak=%.0E flop/s; consumption change: %.2f J -> %.2f J",
86                   start_time, finish_time, host->p_cpu->m_speedPeak, previous_energy, energy_this_step);
87 }
88
89 static void energyHostDestructedCallback(Host *host){
90   std::map<Host*, HostEnergy*>::iterator host_energy_it = surf_energy->find(host);
91   xbt_assert(host_energy_it != surf_energy->end(), "The host is not in surf_energy.");
92
93   HostEnergy *host_energy = host_energy_it->second;
94   update_consumption(host, host_energy);
95
96   if (host_energy_it->second->refcount == 1) // Don't display anything for virtual CPUs
97           XBT_INFO("Total energy of host %s: %f Joules", host->getName(), host_energy->getConsumedEnergy());
98   host_energy_it->second->unref();
99   surf_energy->erase(host_energy_it);
100 }
101
102 static void energyCpuActionStateChangedCallback(CpuAction *action, e_surf_action_state_t old, e_surf_action_state_t cur){
103   const char *name = getActionCpu(action)->getName();
104   Host *host = static_cast<Host*>(surf_host_resource_priv(sg_host_by_name(name)));
105
106   HostEnergy *host_energy = (*surf_energy)[host];
107
108   if(host_energy->last_updated < surf_get_clock())
109           update_consumption(host, host_energy);
110 }
111
112 static void energyStateChangedCallback(Host *host, e_surf_resource_state_t oldState, e_surf_resource_state_t newState){
113   HostEnergy *host_energy = (*surf_energy)[host];
114
115   if(host_energy->last_updated < surf_get_clock())
116           update_consumption(host, host_energy);
117 }
118
119 static void sg_energy_plugin_exit()
120 {
121   delete surf_energy;
122   surf_energy = NULL;
123 }
124
125 /** \ingroup SURF_plugin_energy
126  * \brief Enable energy plugin
127  * \details Enable energy plugin to get joules consumption of each cpu. You should call this function before #MSG_init().
128  */
129 void sg_energy_plugin_init() {
130   if (surf_energy == NULL) {
131     surf_energy = new std::map<Host*, HostEnergy*>();
132     surf_callback_connect(hostCreatedCallbacks, energyHostCreatedCallback);
133     surf_callback_connect(VMCreatedCallbacks, energyVMCreatedCallback);
134     surf_callback_connect(hostDestructedCallbacks, energyHostDestructedCallback);
135     surf_callback_connect(cpuActionStateChangedCallbacks, energyCpuActionStateChangedCallback);
136     surf_callback_connect(surfExitCallbacks, sg_energy_plugin_exit);
137     surf_callback_connect(hostStateChangedCallbacks, energyStateChangedCallback);
138   }
139 }
140
141 /**
142  *
143  */
144 HostEnergy::HostEnergy(Host *ptr)
145 {
146   host = ptr;
147   total_energy = 0;
148   power_range_watts_list = getWattsRangeList();
149   last_updated = surf_get_clock();
150
151   if (host->getProperties() != NULL) {
152         char* off_power_str = (char*)xbt_dict_get_or_null(host->getProperties(), "watt_off");
153         if (off_power_str != NULL)
154                 watts_off = atof(off_power_str);
155         else
156                 watts_off = 0;
157   }
158
159 }
160
161 HostEnergy::~HostEnergy(){
162   unsigned int iter;
163   xbt_dynar_t power_tuple = NULL;
164   xbt_dynar_foreach(power_range_watts_list, iter, power_tuple)
165     xbt_dynar_free(&power_tuple);
166   xbt_dynar_free(&power_range_watts_list);
167 }
168
169
170 double HostEnergy::getWattMinAt(int pstate) {
171   xbt_dynar_t power_range_list = power_range_watts_list;
172   xbt_assert(power_range_watts_list, "No power range properties specified for host %s", host->getName());
173   xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, static_cast<CpuCas01*>(host->p_cpu)->getPState(), xbt_dynar_t);
174   double min_power = xbt_dynar_get_as(current_power_values, 0, double);
175   return min_power;
176 }
177 double HostEnergy::getWattMaxAt(int pstate) {
178   xbt_dynar_t power_range_list = power_range_watts_list;
179   xbt_assert(power_range_watts_list, "No power range properties specified for host %s", host->getName());
180   xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, static_cast<CpuCas01*>(host->p_cpu)->getPState(), xbt_dynar_t);
181   double max_power = xbt_dynar_get_as(current_power_values, 1, double);
182   return max_power;
183 }
184
185 /**
186  * Computes the power consumed by the host according to the current pstate and processor load
187  *
188  */
189 double HostEnergy::getCurrentWattsValue(double cpu_load)
190 {
191         xbt_dynar_t power_range_list = power_range_watts_list;
192         xbt_assert(power_range_watts_list, "No power range properties specified for host %s", host->getName());
193
194     /* retrieve the power values associated with the current pstate */
195     xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, static_cast<CpuCas01*>(host->p_cpu)->getPState(), xbt_dynar_t);
196
197     /* min_power corresponds to the idle power (cpu load = 0) */
198     /* max_power is the power consumed at 100% cpu load       */
199     double min_power = xbt_dynar_get_as(current_power_values, 0, double);
200     double max_power = xbt_dynar_get_as(current_power_values, 1, double);
201     double power_slope = max_power - min_power;
202
203     double current_power = min_power + cpu_load * power_slope;
204
205         XBT_DEBUG("[get_current_watts] min_power=%f, max_power=%f, slope=%f", min_power, max_power, power_slope);
206     XBT_DEBUG("[get_current_watts] Current power (watts) = %f, load = %f", current_power, cpu_load);
207
208         return current_power;
209 }
210
211 double HostEnergy::getConsumedEnergy()
212 {
213
214         if(last_updated < surf_get_clock())
215                 update_consumption(host, this);
216         return total_energy;
217
218 }
219
220 xbt_dynar_t HostEnergy::getWattsRangeList()
221 {
222         xbt_dynar_t power_range_list;
223         xbt_dynar_t power_tuple;
224         int i = 0, pstate_nb=0;
225         xbt_dynar_t current_power_values;
226         double min_power, max_power;
227
228         if (host->getProperties() == NULL)
229                 return NULL;
230
231         char* all_power_values_str = (char*)xbt_dict_get_or_null(host->getProperties(), "watt_per_state");
232
233         if (all_power_values_str == NULL)
234                 return NULL;
235
236
237         power_range_list = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
238         xbt_dynar_t all_power_values = xbt_str_split(all_power_values_str, ",");
239
240         pstate_nb = xbt_dynar_length(all_power_values);
241         for (i=0; i< pstate_nb; i++)
242         {
243                 /* retrieve the power values associated with the current pstate */
244                 current_power_values = xbt_str_split(xbt_dynar_get_as(all_power_values, i, char*), ":");
245                 xbt_assert(xbt_dynar_length(current_power_values) > 1,
246                                 "Power properties incorrectly defined - could not retrieve min and max power values for host %s",
247                                 host->getName());
248
249                 /* min_power corresponds to the idle power (cpu load = 0) */
250                 /* max_power is the power consumed at 100% cpu load       */
251                 min_power = atof(xbt_dynar_get_as(current_power_values, 0, char*));
252                 max_power = atof(xbt_dynar_get_as(current_power_values, 1, char*));
253
254                 power_tuple = xbt_dynar_new(sizeof(double), NULL);
255                 xbt_dynar_push_as(power_tuple, double, min_power);
256                 xbt_dynar_push_as(power_tuple, double, max_power);
257
258                 xbt_dynar_push_as(power_range_list, xbt_dynar_t, power_tuple);
259                 xbt_dynar_free(&current_power_values);
260         }
261         xbt_dynar_free(&all_power_values);
262         return power_range_list;
263 }