Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move all VM+energy code into callbacks
authorMartin Quinson <martin.quinson@loria.fr>
Sat, 14 Nov 2015 23:35:20 +0000 (00:35 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Sat, 14 Nov 2015 23:35:23 +0000 (00:35 +0100)
This keeps the main files clean of any energy related code.

src/surf/cpu_interface.cpp
src/surf/cpu_interface.hpp
src/surf/plugins/energy.cpp
src/surf/plugins/energy.hpp
src/surf/virtual_machine.cpp
src/surf/virtual_machine.hpp
src/surf/vm_hl13.cpp

index 1493337..585f48c 100644 (file)
@@ -144,7 +144,6 @@ void CpuModel::updateActionsStateFull(double now, double delta)
 
 Cpu::Cpu(){
   surf_callback_emit(cpuCreatedCallbacks, this);
-  physCpu = NULL; 
 }
 
 Cpu::Cpu(Model *model, const char *name, xbt_dict_t props,
@@ -157,7 +156,6 @@ Cpu::Cpu(Model *model, const char *name, xbt_dict_t props,
  , p_constraintCoreId(NULL)
 {
   surf_callback_emit(cpuCreatedCallbacks, this);
-  physCpu = NULL; 
 }
 
 Cpu::Cpu(Model *model, const char *name, xbt_dict_t props,
@@ -185,7 +183,6 @@ Cpu::Cpu(Model *model, const char *name, xbt_dict_t props,
       p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], m_powerScale * m_powerPeak);
     }
   }
-  physCpu = NULL; 
 }
 
 Cpu::~Cpu(){
@@ -228,22 +225,6 @@ void Cpu::setState(e_surf_resource_state_t state)
   surf_callback_emit(cpuStateChangedCallbacks, this, old, state);
 }
 
-void Cpu::setPhysicalCPU(Cpu *physCpu)
-{
-  if (!surf_energy)
-         return;
-  this->physCpu = physCpu;
-  XBT_DEBUG("The CPU is virtual so associate the cpu energy to the physical cpu instead of creating a new one");
-  std::map<Cpu*, CpuEnergy*>::iterator cpu_energy_it = surf_energy->find(physCpu);
-  xbt_assert(cpu_energy_it != surf_energy->end(), "The cpu is not in surf_energy.");
-  (*surf_energy)[this] = cpu_energy_it->second;
-}
-
-Cpu* Cpu::getPhysicalCPU(void)
-{
-   return physCpu;
-}
-
 /**********
  * Action *
  **********/
index e5c82bd..40e3659 100644 (file)
@@ -61,7 +61,6 @@ XBT_PUBLIC(void) cpu_add_traces();
  */
 XBT_PUBLIC_CLASS CpuModel : public Model {
 public:
-  /** @brief Constructor */
   CpuModel() : Model() {};
 
   /**
@@ -197,38 +196,11 @@ public:
   void addTraces(void);
   int m_core;
   double m_powerPeak;            /*< CPU power peak */
-  double m_powerScale;           /*< Percentage of CPU disponible */
+  double m_powerScale;           /*< Percentage of CPU available */
 
   /* Note (hypervisor): */
   lmm_constraint_t *p_constraintCore;
   void **p_constraintCoreId;
-
-  // ////
-  // Adrien - Oct 2015. Additional code related to VM energy extensions
-  /* TODO: 
-     1./ create a VirtualCpuModel that inherits from CpuModel or create
-     a VirtualCpuCas01Model that inherit from CpuCas01Model
-         => Question: not sure which one is really useful :( The objective is to
-     be able to create a VirtualCpu instead of a Cpu (i.e. a CpuCas01 to be exact). 
-     2. create VirtualCpu class that inherits from Cpu or create
-     a VirtualCpuCas01 class that inherits from CpuCas01 (same issue as the one described above).      
-         Such modifications would enable us to remove the isVirtual method and
-     actually use the polymorphism feature of C++. It will also enable us 
-     to overwrite the cpu_energy hashmap in energyCpuCreateCallBack instead of invoking
-     setVirtual(). 
-     So to make a long story short, it will be cleaner from the software viewpoint 
-     and patches welcome ;) - Adrien October 20 2015 
-  */
-  Cpu *physCpu = NULL;
-
-       /** @brief Specify the physical CPU of the current CPU (which is a vCPU) */
-       void setPhysicalCPU(Cpu *physCpu);
-
-       /** @brief Get the physical if the current one is a vCPU, or NULL if it's already a physical CPU */
-       Cpu* getPhysicalCPU(void);
-   
-   // Adrien - Oct 2015. End of additional code related to VM energy extensions
-   // ////
 };
 
 /**********
index a8e9398..c65b6f8 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "energy.hpp"
 #include "../cpu_cas01.hpp"
+#include "../virtual_machine.hpp"
 
 /** @addtogroup SURF_plugin_energy
 
@@ -55,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) {
@@ -86,13 +93,10 @@ static void energyCpuDestructedCallback(Cpu *cpu){
   CpuEnergy *cpu_energy = cpu_energy_it->second;
   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);
-  }
-
+  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){
@@ -125,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);
index 18af57f..f3d700c 100644 (file)
@@ -32,6 +32,10 @@ public:
   double total_energy;                                 /*< Total energy consumed by the host */
   double last_updated;                                 /*< Timestamp of the last energy update event*/
   Cpu *cpu;
+
+  void unref() {if (--refcount == 0) delete this;}
+  void ref() {refcount++;}
+  int refcount = 1;
 };
 
 #endif /* ENERGY_CALLBACK_HPP_ */
index 8f38911..0f2de4d 100644 (file)
@@ -37,8 +37,6 @@ VirtualMachine::VirtualMachine(Model *model, const char *name, xbt_dict_t props,
   VMModel::ws_vms.push_back(*this);
 
   xbt_lib_set(host_lib, name, SURF_HOST_LEVEL, this);
-
-  surf_callback_emit(VMCreatedCallbacks, this);
 }
 
 /*
index ebc420b..7860de6 100644 (file)
@@ -26,17 +26,17 @@ class XBT_PRIVATE VirtualMachine;
  *************/
 
 /** @ingroup SURF_callbacks
- * @brief Callbacks fired after VM creation. Signature: `void(VM*)`
+ * @brief Callbacks fired after VM creation. Signature: `void(VirtualMachine*)`
  */
 extern XBT_PRIVATE surf_callback(void, VirtualMachine*) VMCreatedCallbacks;
 
 /** @ingroup SURF_callbacks
- * @brief Callbacks fired after VM destruction. Signature: `void(VM*)`
+ * @brief Callbacks fired after VM destruction. Signature: `void(VirtualMachine*)`
  */
 extern XBT_PRIVATE surf_callback(void, VirtualMachine*) VMDestructedCallbacks;
 
 /** @ingroup SURF_callbacks
- * @brief Callbacks after VM State changes. Signature: `void(VMAction*)`
+ * @brief Callbacks after VM State changes. Signature: `void(VirtualMachine*)`
  */
 extern XBT_PRIVATE surf_callback(void, VirtualMachine*) VMStateChangedCallbacks;
 
@@ -90,7 +90,7 @@ public:
 
   /* The vm object of the lower layer */
   CpuAction *p_action;
-  Host *p_subWs;  // Pointer to the ''host'' OS
+  Host *p_subWs;
   e_surf_vm_state_t p_currentState;
 public:
   boost::intrusive::list_member_hook<> vm_hook;
index f35a2c7..82c3591 100644 (file)
@@ -176,7 +176,7 @@ VMHL13::VMHL13(VMModel *model, const char* name, xbt_dict_t props,
                                                   surf_resource_t host_PM)
  : VirtualMachine(model, name, props, NULL, NULL)
 {
-  Host *sub_ws = static_cast<Host*>(surf_host_resource_priv(host_PM));
+  Host *sub_ws = surf_host_resource_priv(host_PM);
 
   /* Currently, we assume a VM has no storage. */
   p_storage = NULL;
@@ -208,8 +208,6 @@ VMHL13::VMHL13(VMModel *model, const char* name, xbt_dict_t props,
       NULL,                       // host->state_trace,
       NULL);                       // host->properties,
 
-       p_cpu->setPhysicalCPU(sub_cpu);
-
   /* We create cpu_action corresponding to a VM process on the host operating system. */
   /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
   // vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(host_PM, GUESTOS_NOISE);
@@ -217,6 +215,7 @@ VMHL13::VMHL13(VMModel *model, const char* name, xbt_dict_t props,
 
   XBT_INFO("Create VM(%s)@PM(%s) with %ld mounted disks", name, sub_ws->getName(), xbt_dynar_length(p_storage));
 
+  surf_callback_emit(VMCreatedCallbacks, this);
 }
 
 /*