Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[ENERGY] Add API call for querying current consumption
[simgrid.git] / src / surf / plugins / host_energy.cpp
index 332f4a1..9698183 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "simgrid/s4u/Engine.hpp"
 
+#include <algorithm>
 #include <boost/algorithm/string/classification.hpp>
 #include <boost/algorithm/string/split.hpp>
 #include <string>
@@ -177,6 +178,13 @@ HostEnergy::HostEnergy(simgrid::s4u::Host* ptr) : host(ptr), last_updated(surf_g
     xbt_free(msg);
   }
   /* watts_off is 0 by default */
+
+  if (ptr->coreCount() == 1)
+    xbt_assert(std::all_of(power_range_watts_list.begin(), power_range_watts_list.end(),
+                           [](PowerRange power_range) { return power_range.min == power_range.max; }),
+               "You only have one core in host %s, but the \
+      energy consumption for one core does not match the energy consumption for all (here: 1) cores). This is an error in your platform, please fix it.",
+               host->cname());
 }
 
 HostEnergy::~HostEnergy() = default;
@@ -207,8 +215,8 @@ double HostEnergy::getCurrentWattsValue(double cpu_load)
   double power_slope   = 0;
 
   if (cpu_load > 0) { /* Something is going on, the machine is not idle */
-    min_power = range.min;
-    max_power = range.max;
+    double min_power = range.min;
+    double max_power = range.max;
 
     /**
      * The min_power states how much we consume when only one single
@@ -220,15 +228,15 @@ double HostEnergy::getCurrentWattsValue(double cpu_load)
      * i.e., we need min_power + (maxCpuLoad-1/coreCount)*power_slope == max_power
      * (maxCpuLoad is by definition 1)
      */
+    double power_slope;
     int coreCount         = host->coreCount();
     double coreReciprocal = static_cast<double>(1) / static_cast<double>(coreCount);
-    if (coreCount > 1) {
+    if (coreCount > 1)
       power_slope = (max_power - min_power) / (1 - coreReciprocal);
-      current_power = min_power + (cpu_load - coreReciprocal) * power_slope;
-    } else {
-      current_power = max_power;
-    }
+    else
+      power_slope = 0; // Should be 0, since max_power == min_power (in this case)
 
+    current_power = min_power + (cpu_load - coreReciprocal) * power_slope;
   } else { /* Our machine is idle, take the dedicated value! */
     current_power = range.idle;
   }
@@ -426,4 +434,13 @@ double sg_host_get_wattmax_at(sg_host_t host, int pstate)
   return host->extension<HostEnergy>()->getWattMaxAt(pstate);
 }
 
+/** @brief Returns the current consumption of the host */
+double sg_host_get_current_consumption(sg_host_t host)
+{
+  xbt_assert(HostEnergy::EXTENSION_ID.valid(),
+             "The Energy plugin is not active. Please call sg_energy_plugin_init() during initialization.");
+  double cpu_load = lmm_constraint_get_usage(host->pimpl_cpu->constraint()) / host->speed();
+  return host->extension<HostEnergy>()->getCurrentWattsValue(cpu_load);
+}
+
 SG_END_DECL()