+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_energy, surf,
+ "Logging specific to the SURF energy plugin");
+
+using simgrid::energy::HostEnergy;
+
+namespace simgrid {
+namespace energy {
+
+simgrid::xbt::Extension<simgrid::s4u::Host, HostEnergy> HostEnergy::EXTENSION_ID;
+
+/* Computes the consumption so far. Called lazily on need. */
+void HostEnergy::update()
+{
+ simgrid::surf::HostImpl* surf_host = host->extension<simgrid::surf::HostImpl>();
+ double start_time = this->last_updated;
+ double finish_time = surf_get_clock();
+ double cpu_load;
+ if (surf_host->p_cpu->speed_.peak == 0)
+ // Some users declare a pstate of speed 0 flops (eg to model boot time).
+ // We consider that the machine is then fully loaded. That's arbitrary but it avoids a NaN
+ cpu_load = 1;
+ else
+ cpu_load = lmm_constraint_get_usage(surf_host->p_cpu->getConstraint())
+ / surf_host->p_cpu->speed_.peak;
+
+ if (cpu_load > 1) // A machine with a load > 1 consumes as much as a fully loaded machine, not mores
+ cpu_load = 1;
+
+ double previous_energy = this->total_energy;
+
+ double instantaneous_consumption;
+ if (host->isOff())
+ instantaneous_consumption = this->watts_off;
+ else
+ instantaneous_consumption = this->getCurrentWattsValue(cpu_load);
+
+ double energy_this_step = instantaneous_consumption*(finish_time-start_time);
+
+ this->total_energy = previous_energy + energy_this_step;
+ this->last_updated = finish_time;
+
+ XBT_DEBUG("[update_energy of %s] period=[%.2f-%.2f]; current power peak=%.0E flop/s; consumption change: %.2f J -> %.2f J",
+ surf_host->getName(), start_time, finish_time, surf_host->p_cpu->speed_.peak, previous_energy, energy_this_step);
+}
+
+HostEnergy::HostEnergy(simgrid::s4u::Host *ptr) :
+ host(ptr), last_updated(surf_get_clock())
+{
+ initWattsRangeList();
+
+ if (host->properties() != NULL) {
+ char* off_power_str = (char*)xbt_dict_get_or_null(host->properties(), "watt_off");
+ if (off_power_str != NULL) {
+ char *msg = bprintf("Invalid value for property watt_off of host %s: %%s",host->name().c_str());
+ watts_off = xbt_str_parse_double(off_power_str, msg);
+ xbt_free(msg);
+ }
+ else
+ watts_off = 0;