Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
unused variable
[simgrid.git] / src / surf / plugins / energy.cpp
index ca80808..a8e9398 100644 (file)
@@ -46,7 +46,6 @@ To simulate the energy-related elements, first call the #sg_energy_plugin_init()
 and then use the following function to retrieve the consumption of a given host: #MSG_host_get_consumed_energy().
  */
 
-XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_energy, surf,
                                 "Logging specific to the SURF energy plugin");
 
@@ -56,32 +55,28 @@ static void energyCpuCreatedCallback(Cpu *cpu){
   (*surf_energy)[cpu] = new CpuEnergy(cpu);
 }
 
-static void update_consumption_running(Cpu *cpu, CpuEnergy *cpu_energy) {
+
+/* Computes the consumption so far.  Called lazily on need. */
+static void update_consumption(Cpu *cpu, CpuEnergy *cpu_energy) {
        double cpu_load = lmm_constraint_get_usage(cpu->getConstraint()) / cpu->m_powerPeak;
        double start_time = cpu_energy->last_updated;
        double finish_time = surf_get_clock();
 
        double previous_energy = cpu_energy->total_energy;
-       double energy_this_step = cpu_energy->getCurrentWattsValue(cpu_load)*(finish_time-start_time);
-
-       cpu_energy->total_energy = previous_energy + energy_this_step;
-       cpu_energy->last_updated = finish_time;
 
-       XBT_DEBUG("[cpu_update_energy] period=[%.2f-%.2f]; current power peak=%.0E flop/s; consumption change: %.2f J -> %.2f J",
-                 start_time, finish_time, cpu->m_powerPeak, previous_energy, energy_this_step);
-}
-static void update_consumption_off(Cpu *cpu, CpuEnergy *cpu_energy) {
-       double start_time = cpu_energy->last_updated;
-       double finish_time = surf_get_clock();
+       double instantaneous_consumption;
+       if (cpu->getState() == SURF_RESOURCE_OFF)
+               instantaneous_consumption = cpu_energy->watts_off;
+       else
+               instantaneous_consumption = cpu_energy->getCurrentWattsValue(cpu_load);
 
-       double previous_energy = cpu_energy->total_energy;
-       double energy_this_step = cpu_energy->watts_off*(finish_time-start_time);
+       double energy_this_step = instantaneous_consumption*(finish_time-start_time);
 
        cpu_energy->total_energy = previous_energy + energy_this_step;
        cpu_energy->last_updated = finish_time;
 
-       XBT_DEBUG("[cpu_update_energy] off period=[%.2f-%.2f]; consumption change: %.2f J -> %.2f J",
-                 start_time, finish_time, previous_energy, energy_this_step);
+       XBT_DEBUG("[cpu_update_energy] period=[%.2f-%.2f]; current power peak=%.0E flop/s; consumption change: %.2f J -> %.2f J",
+                 start_time, finish_time, cpu->m_powerPeak, previous_energy, energy_this_step);
 }
 
 static void energyCpuDestructedCallback(Cpu *cpu){
@@ -89,34 +84,31 @@ static void energyCpuDestructedCallback(Cpu *cpu){
   xbt_assert(cpu_energy_it != surf_energy->end(), "The cpu is not in surf_energy.");
 
   CpuEnergy *cpu_energy = cpu_energy_it->second;
-  if (cpu->getState() == SURF_RESOURCE_OFF)
-         update_consumption_off(cpu, cpu_energy);
-  else
-         update_consumption_running(cpu, cpu_energy);
-
-  XBT_INFO("Total energy of host %s: %f Joules", cpu->getName(), cpu_energy->getConsumedEnergy());
-  delete cpu_energy_it->second;
-  surf_energy->erase(cpu_energy_it);
+  update_consumption(cpu, cpu_energy);
+
+  // Do nothing if that's a virtual CPU, only act for physical CPUs
+  if(cpu->getPhysicalCPU() == NULL){
+    XBT_INFO("Total energy of host %s: %f Joules", cpu->getName(), cpu_energy->getConsumedEnergy());
+    delete cpu_energy_it->second;
+    surf_energy->erase(cpu_energy_it);
+  }
+
 }
 
 static void energyCpuActionStateChangedCallback(CpuAction *action, e_surf_action_state_t old, e_surf_action_state_t cur){
   Cpu *cpu  = getActionCpu(action);
+
   CpuEnergy *cpu_energy = (*surf_energy)[cpu];
 
-  if(cpu_energy->last_updated < surf_get_clock()) {
-         update_consumption_running(cpu, cpu_energy);
-  }
+  if(cpu_energy->last_updated < surf_get_clock())
+         update_consumption(cpu, cpu_energy);
 }
 
 static void energyStateChangedCallback(Cpu *cpu, e_surf_resource_state_t oldState, e_surf_resource_state_t newState){
   CpuEnergy *cpu_energy = (*surf_energy)[cpu];
 
-  if(cpu_energy->last_updated < surf_get_clock()) {
-         if (oldState == SURF_RESOURCE_OFF)
-                 update_consumption_off(cpu, cpu_energy);
-         else
-                 update_consumption_running(cpu, cpu_energy);
-  }
+  if(cpu_energy->last_updated < surf_get_clock())
+         update_consumption(cpu, cpu_energy);
 }
 
 static void sg_energy_plugin_exit()
@@ -212,13 +204,11 @@ double CpuEnergy::getCurrentWattsValue(double cpu_load)
 
 double CpuEnergy::getConsumedEnergy()
 {
-       if(last_updated < surf_get_clock()) {
-               if (cpu->getState() == SURF_RESOURCE_OFF)
-                       update_consumption_off(cpu, this);
-               else
-                       update_consumption_running(cpu, this);
-       }
-  return total_energy;
+
+       if(last_updated < surf_get_clock())
+               update_consumption(cpu, this);
+       return total_energy;
+
 }
 
 xbt_dynar_t CpuEnergy::getWattsRangeList()