Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
some more documentation to the energy plugin
[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
10 /** @addtogroup SURF_plugin_energy
11  *
12  *
13  *  This is the energy plugin, enabling to account not only for computation time,
14  *  but also for the dissipated energy in the simulated platform.
15  *
16  *  The energy consumption of a CPU depends directly of its current load. Specify that consumption in your platform file as follows:
17  *
18  *  \beginverbatim
19  *  <host id="HostA" power="100.0Mf" >
20  *      <prop id="watt_per_state" value="100.0:200.0" />
21  *      <prop id="watt_off" value="10" />
22  *  </host>
23  *  \endverbatim
24  *
25  *  The first property means that when your host is up and running, but without anything to do, it will dissipate 100 Watts.
26  *  If it's fully loaded, it will dissipate 200 Watts. If its load is at 50%, then it will dissipate 150 Watts.
27  *  The second property means that when your host is turned off, it will dissipate only 10 Watts (please note that these values are arbitrary).
28  *
29  *  If your CPU is using pstates, then you can provide one consumption interval per pstate.
30  *
31  *  \beginverbatim
32  *  <host id="HostB" power="100.0Mf,50.0Mf,20.0Mf" pstate="0" >
33  *      <prop id="watt_per_state" value="95.0:200.0, 93.0:170.0, 90.0:150.0" />
34  *      <prop id="watt_off" value="10" />
35  *  </host>
36  *  \endverbatim
37  *
38  *  That host has 3 levels of performance with the following performance: 100 Mflop/s, 50 Mflop/s or 20 Mflop/s.
39  *  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.
40  *  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,
41  *  170 Watts and 150 Watts respectively.
42  *
43  *  To change the pstate of a given CPU, use the following functions: #MSG_host_get_pstate_number, #MSG_host_set_pstate(), #MSG_host_get_power_peak_at().
44  *
45  *  To get the amount of dissipated energy, use the following function: #MSG_host_get_consumed_energy().
46  */
47
48 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
49 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_energy, surf,
50                                 "Logging specific to the SURF energy plugin");
51
52 std::map<CpuPtr, CpuEnergyPtr> *surf_energy=NULL;
53
54 static void energyCpuCreatedCallback(CpuPtr cpu){
55   (*surf_energy)[cpu] = new CpuEnergy(cpu);
56 }
57
58 static void update_consumption_running(CpuPtr cpu, CpuEnergyPtr cpu_energy) {
59         double cpu_load = lmm_constraint_get_usage(cpu->getConstraint()) / cpu->m_powerPeak;
60         double start_time = cpu_energy->last_updated;
61         double finish_time = surf_get_clock();
62
63         double previous_energy = cpu_energy->total_energy;
64         double energy_this_step = cpu_energy->getCurrentWattsValue(cpu_load)*(finish_time-start_time);
65
66         cpu_energy->total_energy = previous_energy + energy_this_step;
67         cpu_energy->last_updated = finish_time;
68
69         XBT_DEBUG("[cpu_update_energy] period=[%.2f-%.2f]; current power peak=%.0E flop/s; consumption change: %.2f J -> %.2f J",
70                   start_time, finish_time, cpu->m_powerPeak, previous_energy, energy_this_step);
71 }
72 static void update_consumption_off(CpuPtr cpu, CpuEnergyPtr cpu_energy) {
73         double start_time = cpu_energy->last_updated;
74         double finish_time = surf_get_clock();
75
76         double previous_energy = cpu_energy->total_energy;
77         double energy_this_step = cpu_energy->watts_off*(finish_time-start_time);
78
79         cpu_energy->total_energy = previous_energy + energy_this_step;
80         cpu_energy->last_updated = finish_time;
81
82         XBT_DEBUG("[cpu_update_energy] off period=[%.2f-%.2f]; consumption change: %.2f J -> %.2f J",
83                   start_time, finish_time, previous_energy, energy_this_step);
84 }
85
86 static void energyCpuDestructedCallback(CpuPtr cpu){
87   std::map<CpuPtr, CpuEnergyPtr>::iterator cpu_energy_it = surf_energy->find(cpu);
88   xbt_assert(cpu_energy_it != surf_energy->end(), "The cpu is not in surf_energy.");
89
90   CpuEnergyPtr cpu_energy = cpu_energy_it->second;
91   if (cpu->getState() == SURF_RESOURCE_OFF)
92           update_consumption_off(cpu, cpu_energy);
93   else
94           update_consumption_running(cpu, cpu_energy);
95
96   XBT_INFO("Total energy of host %s: %f Joules", cpu->getName(), cpu_energy->getConsumedEnergy());
97   delete cpu_energy_it->second;
98   surf_energy->erase(cpu_energy_it);
99 }
100
101 static void energyCpuActionStateChangedCallback(CpuActionPtr action, e_surf_action_state_t old, e_surf_action_state_t cur){
102   CpuPtr cpu  = getActionCpu(action);
103   CpuEnergyPtr cpu_energy = (*surf_energy)[cpu];
104
105   if(cpu_energy->last_updated < surf_get_clock()) {
106           update_consumption_running(cpu, cpu_energy);
107   }
108 }
109
110 static void energyStateChangedCallback(CpuPtr cpu, e_surf_resource_state_t oldState, e_surf_resource_state_t newState){
111   CpuEnergyPtr cpu_energy = (*surf_energy)[cpu];
112
113   if(cpu_energy->last_updated < surf_get_clock()) {
114           if (oldState == SURF_RESOURCE_OFF)
115                   update_consumption_off(cpu, cpu_energy);
116           else
117                   update_consumption_running(cpu, cpu_energy);
118   }
119 }
120
121 static void sg_energy_plugin_exit()
122 {
123   delete surf_energy;
124   surf_energy = NULL;
125 }
126
127 /** \ingroup SURF_plugin_energy
128  * \brief Enable energy plugin
129  * \details Enable energy plugin to get joules consumption of each cpu.
130  */
131 void sg_energy_plugin_init() {
132   if (surf_energy == NULL) {
133     surf_energy = new std::map<CpuPtr, CpuEnergyPtr>();
134     surf_callback_connect(cpuCreatedCallbacks, energyCpuCreatedCallback);
135     surf_callback_connect(cpuDestructedCallbacks, energyCpuDestructedCallback);
136     surf_callback_connect(cpuActionStateChangedCallbacks, energyCpuActionStateChangedCallback);
137     surf_callback_connect(surfExitCallbacks, sg_energy_plugin_exit);
138     surf_callback_connect(cpuStateChangedCallbacks, energyStateChangedCallback);
139   }
140 }
141
142 /**
143  *
144  */
145 CpuEnergy::CpuEnergy(CpuPtr ptr)
146  : cpu(ptr)
147 {
148   total_energy = 0;
149   power_range_watts_list = getWattsRangeList();
150   last_updated = surf_get_clock();
151
152   if (cpu->getProperties() != NULL) {
153         char* off_power_str = (char*)xbt_dict_get_or_null(cpu->getProperties(), "watt_off");
154         if (off_power_str != NULL)
155                 watts_off = atof(off_power_str);
156         else
157                 watts_off = 0;
158   }
159
160 }
161
162 CpuEnergy::~CpuEnergy(){
163   unsigned int iter;
164   xbt_dynar_t power_tuple = NULL;
165   xbt_dynar_foreach(power_range_watts_list, iter, power_tuple)
166     xbt_dynar_free(&power_tuple);
167   xbt_dynar_free(&power_range_watts_list);
168 }
169
170 /**
171  * Computes the power consumed by the host according to the current pstate and processor load
172  *
173  */
174 double CpuEnergy::getCurrentWattsValue(double cpu_load)
175 {
176         xbt_dynar_t power_range_list = power_range_watts_list;
177
178         if (power_range_list == NULL)
179         {
180                 XBT_DEBUG("No power range properties specified for host %s", cpu->getName());
181                 return 0;
182         }
183         /*xbt_assert(xbt_dynar_length(power_range_list) == xbt_dynar_length(cpu->p_powerPeakList),
184                                                 "The number of power ranges in the properties does not match the number of pstates for host %s",
185                                                 cpu->getName());*/
186
187     /* retrieve the power values associated with the current pstate */
188     xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, static_cast<CpuCas01Ptr>(cpu)->getPState(), xbt_dynar_t);
189
190     /* min_power corresponds to the idle power (cpu load = 0) */
191     /* max_power is the power consumed at 100% cpu load       */
192     double min_power = xbt_dynar_get_as(current_power_values, 0, double);
193     double max_power = xbt_dynar_get_as(current_power_values, 1, double);
194     double power_slope = max_power - min_power;
195
196     double current_power = min_power + cpu_load * power_slope;
197
198         XBT_DEBUG("[get_current_watts] min_power=%f, max_power=%f, slope=%f", min_power, max_power, power_slope);
199     XBT_DEBUG("[get_current_watts] Current power (watts) = %f, load = %f", current_power, cpu_load);
200
201         return current_power;
202 }
203
204 double CpuEnergy::getConsumedEnergy()
205 {
206         if(last_updated < surf_get_clock()) {
207                 if (cpu->getState() == SURF_RESOURCE_OFF)
208                         update_consumption_off(cpu, this);
209                 else
210                         update_consumption_running(cpu, this);
211         }
212   return total_energy;
213 }
214
215 xbt_dynar_t CpuEnergy::getWattsRangeList()
216 {
217         xbt_dynar_t power_range_list;
218         xbt_dynar_t power_tuple;
219         int i = 0, pstate_nb=0;
220         xbt_dynar_t current_power_values;
221         double min_power, max_power;
222
223         if (cpu->getProperties() == NULL)
224                 return NULL;
225
226         char* all_power_values_str = (char*)xbt_dict_get_or_null(cpu->getProperties(), "watt_per_state");
227
228         if (all_power_values_str == NULL)
229                 return NULL;
230
231
232         power_range_list = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
233         xbt_dynar_t all_power_values = xbt_str_split(all_power_values_str, ",");
234
235         pstate_nb = xbt_dynar_length(all_power_values);
236         for (i=0; i< pstate_nb; i++)
237         {
238                 /* retrieve the power values associated with the current pstate */
239                 current_power_values = xbt_str_split(xbt_dynar_get_as(all_power_values, i, char*), ":");
240                 xbt_assert(xbt_dynar_length(current_power_values) > 1,
241                                 "Power properties incorrectly defined - could not retrieve min and max power values for host %s",
242                                 cpu->getName());
243
244                 /* min_power corresponds to the idle power (cpu load = 0) */
245                 /* max_power is the power consumed at 100% cpu load       */
246                 min_power = atof(xbt_dynar_get_as(current_power_values, 0, char*));
247                 max_power = atof(xbt_dynar_get_as(current_power_values, 1, char*));
248
249                 power_tuple = xbt_dynar_new(sizeof(double), NULL);
250                 xbt_dynar_push_as(power_tuple, double, min_power);
251                 xbt_dynar_push_as(power_tuple, double, max_power);
252
253                 xbt_dynar_push_as(power_range_list, xbt_dynar_t, power_tuple);
254                 xbt_dynar_free(&current_power_values);
255         }
256         xbt_dynar_free(&all_power_values);
257         return power_range_list;
258 }