+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()
+{
+ double start_time = this->last_updated;
+ double finish_time = surf_get_clock();
+ double cpu_load;
+ if (host->pimpl_cpu->getPstateSpeedCurrent() <= 0)
+ // Some users declare a pstate of speed 0 flops (e.g., 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(host->pimpl_cpu->constraint()) / host->pimpl_cpu->getPstateSpeedCurrent();
+
+ /** Divide by the number of cores here **/
+ cpu_load /= host->pimpl_cpu->coreCount();
+
+ if (cpu_load > 1) // A machine with a load > 1 consumes as much as a fully loaded machine, not more
+ cpu_load = 1;
+
+ /* The problem with this model is that the load is always 0 or 1, never something less.
+ * Another possibility could be to model the total energy as
+ *
+ * X/(X+Y)*W_idle + Y/(X+Y)*W_burn
+ *
+ * where X is the amount of idling cores, and Y the amount of computing cores.
+ */
+
+ 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",
+ host->cname(), start_time, finish_time, host->pimpl_cpu->speed_.peak, previous_energy, energy_this_step);
+}
+
+HostEnergy::HostEnergy(simgrid::s4u::Host *ptr) : host(ptr), last_updated(surf_get_clock())
+{
+ initWattsRangeList();
+
+ const char* off_power_str = host->property("watt_off");
+ if (off_power_str != nullptr) {
+ char* msg = bprintf("Invalid value for property watt_off of host %s: %%s", host->cname());
+ this->watts_off = xbt_str_parse_double(off_power_str, msg);
+ xbt_free(msg);