Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
No vm when ptask on
[simgrid.git] / src / surf / workstation.cpp
index a8a4ec1..c6bec86 100644 (file)
@@ -1,4 +1,6 @@
 #include "workstation.hpp"
+#include "vm_workstation.hpp"
+#include "cpu_cas01.hpp"
 #include "simgrid/sg_config.h"
 
 extern "C" {
@@ -10,6 +12,14 @@ WorkstationModelPtr surf_workstation_model = NULL;
 
 //FIXME:Faire hériter ou composer de cup et network
 
+/*************
+ * CallBacks *
+ *************/
+
+static void workstation_new(sg_platf_host_cbarg_t host){
+  surf_workstation_model->createResource(host->id);
+}
+
 /*********
  * Model *
  *********/
@@ -17,33 +27,36 @@ WorkstationModelPtr surf_workstation_model = NULL;
 void surf_workstation_model_init_current_default(void)
 {
   surf_workstation_model = new WorkstationModel();
-  xbt_cfg_setdefault_boolean(_sg_cfg_set, "network/crosstraffic", xbt_strdup("yes"));
+  xbt_cfg_setdefault_boolean(_sg_cfg_set, "network/crosstraffic", "yes");
   surf_cpu_model_init_Cas01();
   surf_network_model_init_LegrandVelho();
+  surf_workstation_model->p_cpuModel = surf_cpu_model_pm;
 
-  xbt_dynar_push(model_list, &surf_workstation_model);
-  //FIXME:sg_platf_host_add_cb(workstation_new);
+  ModelPtr model = static_cast<ModelPtr>(surf_workstation_model);
+  xbt_dynar_push(model_list, &model);
+  xbt_dynar_push(model_list_invoke, &model);
+  sg_platf_host_add_cb(workstation_new);
 }
 
 void surf_workstation_model_init_compound()
 {
 
-  xbt_assert(surf_cpu_model, "No CPU model defined yet!");
+  xbt_assert(surf_cpu_model_pm, "No CPU model defined yet!");
   xbt_assert(surf_network_model, "No network model defined yet!");
   surf_workstation_model = new WorkstationModel();
-  xbt_dynar_push(model_list, &surf_workstation_model);
-  //FIXME:sg_platf_host_add_cb(workstation_new);
+
+  ModelPtr model = static_cast<ModelPtr>(surf_workstation_model);
+  xbt_dynar_push(model_list, &model);
+  xbt_dynar_push(model_list_invoke, &model);
+  sg_platf_host_add_cb(workstation_new);
 }
 
 WorkstationModel::WorkstationModel() : Model("Workstation") {
-  //FIXME:xbt_cfg_setdefault_boolean(_sg_cfg_set, "network/crosstraffic", xbt_strdup("yes"));
-  //FIXME:surf_cpu_model_init_Cas01();
-  //FIXME:surf_network_model_init_LegrandVelho();
-
-  xbt_dynar_push(model_list, this);
-  //FIXME:sg_platf_host_add_cb(workstation_new);
+  p_cpuModel = surf_cpu_model_pm;
 }
 
+WorkstationModel::~WorkstationModel() {
+}
 
 void WorkstationModel::parseInit(sg_platf_host_cbarg_t host){
   createResource(host->id);
@@ -54,14 +67,80 @@ WorkstationCLM03Ptr WorkstationModel::createResource(string name){
   WorkstationCLM03Ptr workstation = new WorkstationCLM03(surf_workstation_model, name.c_str(), NULL,
                  (xbt_dynar_t)xbt_lib_get_or_null(storage_lib, name.c_str(), ROUTING_STORAGE_HOST_LEVEL),
                  (RoutingEdgePtr)xbt_lib_get_or_null(host_lib, name.c_str(), ROUTING_HOST_LEVEL),
-                 (CpuPtr)xbt_lib_get_or_null(host_lib, name.c_str(), SURF_CPU_LEVEL));
+                 dynamic_cast<CpuPtr>(static_cast<ResourcePtr>(xbt_lib_get_or_null(host_lib, name.c_str(), SURF_CPU_LEVEL))));
   XBT_DEBUG("Create workstation %s with %ld mounted disks", name.c_str(), xbt_dynar_length(workstation->p_storage));
-  xbt_lib_set(host_lib, name.c_str(), SURF_WKS_LEVEL, workstation);
+  xbt_lib_set(host_lib, name.c_str(), SURF_WKS_LEVEL, static_cast<ResourcePtr>(workstation));
   return workstation;
 }
 
+/* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
+ * constraint (capacity) of the VM in the PM layer. If the VM does not have any
+ * active task, the dummy CPU action must be deactivated, so that the VM does
+ * not get any CPU share in the PM layer. */
+void WorkstationModel::adjustWeightOfDummyCpuActions()
+{
+  /* iterate for all hosts including virtual machines */
+  xbt_lib_cursor_t cursor;
+  char *key;
+  void **ind_host;
+
+  xbt_lib_foreach(host_lib, cursor, key, ind_host) {
+    WorkstationCLM03Ptr ws_clm03 = dynamic_cast<WorkstationCLM03Ptr>(
+                                      static_cast<ResourcePtr>(ind_host[SURF_WKS_LEVEL]));
+    CpuCas01LmmPtr cpu_cas01 = dynamic_cast<CpuCas01LmmPtr>(
+                               static_cast<ResourcePtr>(ind_host[SURF_CPU_LEVEL]));
+
+    if (!ws_clm03)
+      continue;
+    /* skip if it is not a virtual machine */
+    if (ws_clm03->p_model != static_cast<ModelPtr>(surf_vm_workstation_model))
+      continue;
+    xbt_assert(cpu_cas01, "cpu-less workstation");
+
+    /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
+    WorkstationVM2013Ptr ws_vm2013 = dynamic_cast<WorkstationVM2013Ptr>(ws_clm03);
+
+    int is_active = lmm_constraint_used(cpu_cas01->p_model->p_maxminSystem, cpu_cas01->p_constraint);
+    // int is_active_old = constraint_is_active(cpu_cas01);
+
+    // {
+    //   xbt_assert(is_active == is_active_old, "%d %d", is_active, is_active_old);
+    // }
+
+    if (is_active) {
+      /* some tasks exist on this VM */
+      XBT_DEBUG("set the weight of the dummy CPU action on PM to 1");
+
+      /* FIXME: we shoud use lmm_update_variable_weight() ? */
+      /* FIXME: If we assgign 1.05 and 0.05, the system makes apparently wrong values. */
+      surf_action_set_priority(ws_vm2013->p_action, 1);
+
+    } else {
+      /* no task exits on this VM */
+      XBT_DEBUG("set the weight of the dummy CPU action on PM to 0");
+
+      surf_action_set_priority(ws_vm2013->p_action, 0);
+    }
+  }
+}
+
 double WorkstationModel::shareResources(double now){
-  return -1.0;
+  adjustWeightOfDummyCpuActions();
+
+  double min_by_cpu = p_cpuModel->shareResources(now);
+  double min_by_net = surf_network_model->shareResources(now);
+
+  XBT_DEBUG("model %p, %s min_by_cpu %f, %s min_by_net %f",
+      this, surf_cpu_model_pm->m_name.c_str(), min_by_cpu, surf_network_model->m_name.c_str(), min_by_net);
+
+  if (min_by_cpu >= 0.0 && min_by_net >= 0.0)
+    return min(min_by_cpu, min_by_net);
+  else if (min_by_cpu >= 0.0)
+    return min_by_cpu;
+  else if (min_by_net >= 0.0)
+    return min_by_net;
+  else
+    return min_by_cpu;  /* probably min_by_cpu == min_by_net == -1 */
 }
 
 void WorkstationModel::updateActionsState(double now, double delta){
@@ -109,7 +188,7 @@ xbt_dynar_t WorkstationModel::getRoute(WorkstationCLM03Ptr src, WorkstationCLM03
 }
 
 ActionPtr WorkstationModel::communicate(WorkstationCLM03Ptr src, WorkstationCLM03Ptr dst, double size, double rate){
-  surf_network_model->communicate(src->p_netElm, dst->p_netElm, size, rate);
+  return surf_network_model->communicate(src->p_netElm, dst->p_netElm, size, rate);
 }
 
 
@@ -153,31 +232,71 @@ double WorkstationCLM03::getAvailableSpeed(){
   return p_cpu->getAvailableSpeed();
 }
 
+double WorkstationCLM03::getCurrentPowerPeak()
+{
+  return p_cpu->getCurrentPowerPeak();
+}
+
+double WorkstationCLM03::getPowerPeakAt(int pstate_index)
+{
+  return p_cpu->getPowerPeakAt(pstate_index);
+}
+
+int WorkstationCLM03::getNbPstates()
+{
+  return p_cpu->getNbPstates();
+}
+
+void WorkstationCLM03::setPowerPeakAt(int pstate_index)
+{
+       p_cpu->setPowerPeakAt(pstate_index);
+}
+
+double WorkstationCLM03::getConsumedEnergy()
+{
+  return p_cpu->getConsumedEnergy();
+}
+
+
 xbt_dict_t WorkstationCLM03::getProperties()
 {
   return p_cpu->m_properties;
 }
 
 
-StoragePtr WorkstationCLM03::findStorageOnMountList(const char* storage)
+StoragePtr WorkstationCLM03::findStorageOnMountList(const char* mount)
 {
   StoragePtr st = NULL;
   s_mount_t mnt;
   unsigned int cursor;
 
-  XBT_DEBUG("Search for storage name '%s' on '%s'",storage,m_name);
+  XBT_DEBUG("Search for storage name '%s' on '%s'", mount, m_name);
   xbt_dynar_foreach(p_storage,cursor,mnt)
   {
     XBT_DEBUG("See '%s'",mnt.name);
-    if(!strcmp(storage,mnt.name)){
-      st = (StoragePtr)mnt.id;
+    if(!strcmp(mount,mnt.name)){
+      st = dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(mnt.storage));
       break;
     }
   }
-  if(!st) xbt_die("Can't find mount '%s' for '%s'",storage,m_name);
+  if(!st) xbt_die("Can't find mount '%s' for '%s'", mount, m_name);
   return st;
 }
 
+xbt_dict_t WorkstationCLM03::getStorageList()
+{
+  s_mount_t mnt;
+  unsigned int i;
+  xbt_dict_t storage_list = xbt_dict_new_homogeneous(NULL);
+  char *storage_name = NULL;
+
+  xbt_dynar_foreach(p_storage,i,mnt){
+    storage_name = (char *)dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(mnt.storage))->m_name;
+    xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
+  }
+  return storage_list;
+}
+
 ActionPtr WorkstationCLM03::open(const char* mount, const char* path) {
   StoragePtr st = findStorageOnMountList(mount);
   XBT_DEBUG("OPEN on disk '%s'", st->m_name);
@@ -185,21 +304,21 @@ ActionPtr WorkstationCLM03::open(const char* mount, const char* path) {
 }
 
 ActionPtr WorkstationCLM03::close(surf_file_t fd) {
-  StoragePtr st = findStorageOnMountList(fd->storage);
+  StoragePtr st = findStorageOnMountList(fd->mount);
   XBT_DEBUG("CLOSE on disk '%s'",st->m_name);
   return st->close(fd);
 }
 
-ActionPtr WorkstationCLM03::read(void* ptr, size_t size, surf_file_t fd) {
-  StoragePtr st = findStorageOnMountList(fd->storage);
+ActionPtr WorkstationCLM03::read(surf_file_t fd, sg_size_t size) {
+  StoragePtr st = findStorageOnMountList(fd->mount);
   XBT_DEBUG("READ on disk '%s'",st->m_name);
-  return st->read(ptr, size, fd);
+  return st->read(fd, size);
 }
 
-ActionPtr WorkstationCLM03::write(const void* ptr, size_t size, surf_file_t fd) {
-  StoragePtr st = findStorageOnMountList(fd->storage);
+ActionPtr WorkstationCLM03::write(surf_file_t fd, sg_size_t size) {
+  StoragePtr st = findStorageOnMountList(fd->mount);
   XBT_DEBUG("WRITE on disk '%s'",st->m_name);
-  return st->write(ptr, size, fd);
+  return st->write(fd, size);
 }
 
 int WorkstationCLM03::unlink(surf_file_t fd) {
@@ -208,7 +327,7 @@ int WorkstationCLM03::unlink(surf_file_t fd) {
     return 0;
   } else {
 //    XBT_INFO("%s %zu", fd->storage, fd->size);
-    StoragePtr st = findStorageOnMountList(fd->storage);
+    StoragePtr st = findStorageOnMountList(fd->mount);
     /* Check if the file is on this storage */
     if (!xbt_dict_get_or_null(st->p_content, fd->name)){
       XBT_WARN("File %s is not on disk %s. Impossible to unlink", fd->name,
@@ -222,7 +341,7 @@ int WorkstationCLM03::unlink(surf_file_t fd) {
       xbt_dict_remove(st->p_content, fd->name);
 
       free(fd->name);
-      free(fd->storage);
+      free(fd->mount);
       xbt_free(fd);
       return 1;
     }
@@ -235,10 +354,76 @@ ActionPtr WorkstationCLM03::ls(const char* mount, const char *path){
   return st->ls(path);
 }
 
-size_t WorkstationCLM03::getSize(surf_file_t fd){
+sg_size_t WorkstationCLM03::getSize(surf_file_t fd){
   return fd->size;
 }
 
+xbt_dynar_t WorkstationCLM03::getInfo( surf_file_t fd)
+{
+  StoragePtr st = findStorageOnMountList(fd->mount);
+  sg_size_t *psize = xbt_new(sg_size_t, 1);
+  *psize = fd->size;
+  xbt_dynar_t info = xbt_dynar_new(sizeof(void*), NULL);
+  xbt_dynar_push_as(info, sg_size_t *, psize);
+  xbt_dynar_push_as(info, void *, fd->mount);
+  xbt_dynar_push_as(info, void *, (void *)st->m_name);
+  xbt_dynar_push_as(info, void *, st->p_typeId);
+  xbt_dynar_push_as(info, void *, st->p_contentType);
+
+  return info;
+}
+
+sg_size_t WorkstationCLM03::getFreeSize(const char* name)
+{
+  StoragePtr st = findStorageOnMountList(name);
+  return st->m_size - st->m_usedSize;
+}
+
+sg_size_t WorkstationCLM03::getUsedSize(const char* name)
+{
+  StoragePtr st = findStorageOnMountList(name);
+  return st->m_usedSize;
+}
+
+e_surf_resource_state_t WorkstationCLM03Lmm::getState() {
+  return WorkstationCLM03::getState();
+}
+
+xbt_dynar_t WorkstationCLM03::getVms()
+{
+  xbt_dynar_t dyn = xbt_dynar_new(sizeof(smx_host_t), NULL);
+
+  /* iterate for all hosts including virtual machines */
+  xbt_lib_cursor_t cursor;
+  char *key;
+  void **ind_host;
+  xbt_lib_foreach(host_lib, cursor, key, ind_host) {
+    WorkstationCLM03Ptr ws_clm03 = dynamic_cast<WorkstationCLM03Ptr>(static_cast<ResourcePtr>(ind_host[SURF_WKS_LEVEL]));
+    if (!ws_clm03)
+      continue;
+    /* skip if it is not a virtual machine */
+    if (ws_clm03->p_model != static_cast<ModelPtr>(surf_vm_workstation_model))
+      continue;
+
+    /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
+    WorkstationVM2013Ptr ws_vm2013 = dynamic_cast<WorkstationVM2013Ptr>(ws_clm03);
+    if (this == ws_vm2013-> p_subWs)
+      xbt_dynar_push(dyn, &ws_vm2013->p_subWs);
+  }
+
+  return dyn;
+}
+
+void WorkstationCLM03::getParams(ws_params_t params)
+{
+  memcpy(params, &p_params, sizeof(s_ws_params_t));
+}
+
+void WorkstationCLM03::setParams(ws_params_t params)
+{
+  /* may check something here. */
+  memcpy(&p_params, params, sizeof(s_ws_params_t));
+}
 /**********
  * Action *
  **********/