Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move all VM+energy code into callbacks
[simgrid.git] / src / surf / plugins / energy.cpp
index d1d871f..c65b6f8 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "energy.hpp"
 #include "../cpu_cas01.hpp"
+#include "../virtual_machine.hpp"
 
 /** @addtogroup SURF_plugin_energy
 
@@ -46,7 +47,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,6 +56,12 @@ static void energyCpuCreatedCallback(Cpu *cpu){
   (*surf_energy)[cpu] = new CpuEnergy(cpu);
 }
 
+static void energyVMCreatedCallback(VirtualMachine* vm) {
+  std::map<Cpu*, CpuEnergy*>::iterator cpu_energy_it = surf_energy->find(vm->p_subWs->p_cpu);
+  xbt_assert(cpu_energy_it != surf_energy->end(), "The cpu is not in surf_energy.");
+  (*surf_energy)[vm->p_cpu] = cpu_energy_it->second;
+  cpu_energy_it->second->ref(); // protect the CpuEnergy from getting deleted too early
+}
 
 /* Computes the consumption so far.  Called lazily on need. */
 static void update_consumption(Cpu *cpu, CpuEnergy *cpu_energy) {
@@ -87,14 +93,10 @@ static void energyCpuDestructedCallback(Cpu *cpu){
   CpuEnergy *cpu_energy = cpu_energy_it->second;
   update_consumption(cpu, cpu_energy);
 
-  // Adrien - October 2015, Changes related to VM energy extensions
-  // Only report/delete and erase if the cpu_energy is related to a physical one
-  if(cpu->isVirtual() == 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);
-  }
-
+  if (cpu_energy_it->second->refcount == 1) // Don't display anything for virtual CPUs
+         XBT_INFO("Total energy of host %s: %f Joules", cpu->getName(), cpu_energy->getConsumedEnergy());
+  cpu_energy_it->second->unref();
+  surf_energy->erase(cpu_energy_it);
 }
 
 static void energyCpuActionStateChangedCallback(CpuAction *action, e_surf_action_state_t old, e_surf_action_state_t cur){
@@ -127,6 +129,7 @@ void sg_energy_plugin_init() {
   if (surf_energy == NULL) {
     surf_energy = new std::map<Cpu*, CpuEnergy*>();
     surf_callback_connect(cpuCreatedCallbacks, energyCpuCreatedCallback);
+    surf_callback_connect(VMCreatedCallbacks, energyVMCreatedCallback);
     surf_callback_connect(cpuDestructedCallbacks, energyCpuDestructedCallback);
     surf_callback_connect(cpuActionStateChangedCallbacks, energyCpuActionStateChangedCallback);
     surf_callback_connect(surfExitCallbacks, sg_energy_plugin_exit);