Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
one step to make VM usable with any Cpu model
[simgrid.git] / src / surf / host_interface.cpp
index 9433fe7..8e2f22c 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2014. The SimGrid Team.
+/* Copyright (c) 2013-2015. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -6,47 +6,38 @@
 
 #include "host_interface.hpp"
 
-#include "simix/smx_private.h"
+#include <simgrid/s4u/host.hpp>
+
+#include "src/simix/smx_private.h"
 #include "cpu_cas01.hpp"
 #include "simgrid/sg_config.h"
 
 #include "network_interface.hpp"
-#include "vm_interface.hpp"
+#include "virtual_machine.hpp"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_host, surf,
                                 "Logging specific to the SURF host module");
 
-HostModel *surf_host_model = NULL;
+simgrid::surf::HostModel *surf_host_model = NULL;
 
 /*************
  * Callbacks *
  *************/
 
-surf_callback(void, Host*) hostCreatedCallbacks;
-surf_callback(void, Host*) hostDestructedCallbacks;
-surf_callback(void, Host*, e_surf_resource_state_t, e_surf_resource_state_t) hostStateChangedCallbacks;
-surf_callback(void, HostAction*, e_surf_action_state_t, e_surf_action_state_t) hostActionStateChangedCallbacks;
+namespace simgrid {
+namespace surf {
 
-void host_parse_init(sg_platf_host_cbarg_t host)
-{
-  surf_host_model->createHost(host->id);
-}
-
-void host_add_traces(){
-  surf_host_model->addTraces();
-}
+simgrid::xbt::Extension<simgrid::s4u::Host, Host> Host::EXTENSION_ID;
 
 /*********
  * Model *
  *********/
-HostModel::HostModel(const char *name)
- : Model(name)
-{}
+Host *HostModel::createHost(const char *name,NetCard *netElm, Cpu *cpu, xbt_dict_t props){
+  xbt_dynar_t storageList = (xbt_dynar_t)xbt_lib_get_or_null(storage_lib, name, ROUTING_STORAGE_HOST_LEVEL);
 
-HostModel::HostModel()
-: Model("Host") {}
-
-HostModel::~HostModel() {
+  Host *host = new simgrid::surf::Host(surf_host_model, name, props, storageList, cpu);
+  XBT_DEBUG("Create host %s with %ld mounted disks", name, xbt_dynar_length(host->p_storage));
+  return host;
 }
 
 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
@@ -60,12 +51,10 @@ void HostModel::adjustWeightOfDummyCpuActions()
          VMModel::ws_vms.begin();
        iter !=  VMModel::ws_vms.end(); ++iter) {
 
-    VM *ws_vm = &*iter;
-    CpuCas01 *cpu_cas01 = static_cast<CpuCas01*>(ws_vm->p_cpu);
-    xbt_assert(cpu_cas01, "cpu-less host");
+    VirtualMachine *ws_vm = &*iter;
+    Cpu *cpu = ws_vm->p_cpu;
 
-    int is_active = lmm_constraint_used(cpu_cas01->getModel()->getMaxminSystem(), cpu_cas01->getConstraint());
-    // int is_active_old = constraint_is_active(cpu_cas01);
+    int is_active = lmm_constraint_used(cpu->getModel()->getMaxminSystem(), cpu->getConstraint());
 
     if (is_active) {
       /* some tasks exist on this VM */
@@ -84,82 +73,110 @@ void HostModel::adjustWeightOfDummyCpuActions()
   }
 }
 
+Action *HostModel::executeParallelTask(int host_nb,
+    sg_host_t *host_list,
+    double *flops_amount,
+    double *bytes_amount,
+    double rate){
+#define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
+  Action *action =NULL;
+  if ((host_nb == 1)
+      && (cost_or_zero(bytes_amount, 0) == 0.0)){
+    action = host_list[0]->pimpl_cpu->execute(flops_amount[0]);
+  } else if ((host_nb == 1)
+           && (cost_or_zero(flops_amount, 0) == 0.0)) {
+    action = surf_network_model->communicate(host_list[0]->pimpl_netcard,
+                                         host_list[0]->pimpl_netcard,
+                       bytes_amount[0], rate);
+  } else if ((host_nb == 2)
+             && (cost_or_zero(flops_amount, 0) == 0.0)
+             && (cost_or_zero(flops_amount, 1) == 0.0)) {
+    int i,nb = 0;
+    double value = 0.0;
+
+    for (i = 0; i < host_nb * host_nb; i++) {
+      if (cost_or_zero(bytes_amount, i) > 0.0) {
+        nb++;
+        value = cost_or_zero(bytes_amount, i);
+      }
+    }
+    if (nb == 1){
+      action = surf_network_model->communicate(host_list[0]->pimpl_netcard,
+                                           host_list[1]->pimpl_netcard,
+                         value, rate);
+    }
+  } else
+    THROW_UNIMPLEMENTED;      /* This model does not implement parallel tasks for more than 2 hosts */
+#undef cost_or_zero
+  xbt_free(host_list);
+  return action;
+}
+
 /************
  * Resource *
  ************/
-Host::Host(Model *model, const char *name, xbt_dict_t props,
-                                xbt_dynar_t storage, RoutingEdge *netElm, Cpu *cpu)
- : Resource(model, name, props)
- , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
+
+
+void Host::classInit()
 {
-  p_params.ramsize = 0;
-  surf_callback_emit(hostCreatedCallbacks, this);
+  if (!EXTENSION_ID.valid()) {
+    EXTENSION_ID = simgrid::s4u::Host::extension_create<simgrid::surf::Host>();
+  }
 }
 
-Host::Host(Model *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint,
-                                        xbt_dynar_t storage, RoutingEdge *netElm, Cpu *cpu)
- : Resource(model, name, props, constraint)
- , p_storage(storage), p_netElm(netElm), p_cpu(cpu)
+Host::Host(simgrid::surf::HostModel *model, const char *name, xbt_dict_t props,
+                                xbt_dynar_t storage, Cpu *cpu)
+ : Resource(model, name)
+ , PropertyHolder(props)
+ , p_storage(storage), p_cpu(cpu)
 {
   p_params.ramsize = 0;
-  surf_callback_emit(hostCreatedCallbacks, this);
-}
-
-Host::~Host(){
-  surf_callback_emit(hostDestructedCallbacks, this);
-}
-
-void Host::setState(e_surf_resource_state_t state){
-  e_surf_resource_state_t old = Resource::getState();
-  Resource::setState(state);
-  surf_callback_emit(hostStateChangedCallbacks, this, old, state);
-  p_cpu->setState(state);
 }
 
-int Host::getCore(){
-  return p_cpu->getCore();
-}
-
-double Host::getSpeed(double load){
-  return p_cpu->getSpeed(load);
-}
-
-double Host::getAvailableSpeed(){
-  return p_cpu->getAvailableSpeed();
-}
-
-double Host::getCurrentPowerPeak()
+Host::Host(simgrid::surf::HostModel *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint,
+                                        xbt_dynar_t storage, Cpu *cpu)
+ : Resource(model, name, constraint)
+ , PropertyHolder(props)
+ , p_storage(storage), p_cpu(cpu)
 {
-  return p_cpu->getCurrentPowerPeak();
+  p_params.ramsize = 0;
 }
 
-double Host::getPowerPeakAt(int pstate_index)
+/** @brief use destroy() instead of this destructor */
+Host::~Host()
 {
-  return p_cpu->getPowerPeakAt(pstate_index);
 }
 
-int Host::getNbPstates()
+void Host::attach(simgrid::s4u::Host* host)
 {
-  return p_cpu->getNbPstates();
+  if (p_host != nullptr)
+    xbt_die("Already attached to host %s", host->name().c_str());
+  host->extension_set(this);
+  p_host = host;
 }
 
-void Host::setPstate(int pstate_index)
-{
-       p_cpu->setPstate(pstate_index);
+bool Host::isOn() {
+  return p_cpu->isOn();
 }
-int Host::getPstate()
-{
-       return p_cpu->getPstate();
+bool Host::isOff() {
+  return p_cpu->isOff();
 }
-
-xbt_dict_t Host::getProperties()
-{
-  return p_cpu->getProperties();
+void Host::turnOn(){
+  if (isOff()) {
+    p_cpu->turnOn();
+    simgrid::s4u::Host::onStateChange(*this->p_host);
+  }
+}
+void Host::turnOff(){
+  if (isOn()) {
+    p_cpu->turnOff();
+    simgrid::s4u::Host::onStateChange(*this->p_host);
+  }
 }
 
-Storage *Host::findStorageOnMountList(const char* mount)
+simgrid::surf::Storage *Host::findStorageOnMountList(const char* mount)
 {
-  Storage *st = NULL;
+  simgrid::surf::Storage *st = NULL;
   s_mount_t mnt;
   unsigned int cursor;
 
@@ -168,7 +185,7 @@ Storage *Host::findStorageOnMountList(const char* mount)
   {
     XBT_DEBUG("See '%s'",mnt.name);
     if(!strcmp(mount,mnt.name)){
-      st = static_cast<Storage*>(mnt.storage);
+      st = static_cast<simgrid::surf::Storage*>(mnt.storage);
       break;
     }
   }
@@ -184,7 +201,7 @@ xbt_dict_t Host::getMountedStorageList()
   char *storage_name = NULL;
 
   xbt_dynar_foreach(p_storage,i,mnt){
-    storage_name = (char *)static_cast<Storage*>(mnt.storage)->getName();
+    storage_name = (char *)static_cast<simgrid::surf::Storage*>(mnt.storage)->getName();
     xbt_dict_set(storage_list,mnt.name,storage_name,NULL);
   }
   return storage_list;
@@ -198,7 +215,7 @@ xbt_dynar_t Host::getAttachedStorageList()
   xbt_dynar_t result = xbt_dynar_new(sizeof(void*), NULL);
   xbt_lib_foreach(storage_lib, cursor, key, data) {
     if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
-         Storage *storage = static_cast<Storage*>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
+         simgrid::surf::Storage *storage = static_cast<simgrid::surf::Storage*>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
          if(!strcmp((const char*)storage->p_attach,this->getName())){
            xbt_dynar_push_as(result, void *, (void*)storage->getName());
          }
@@ -209,7 +226,7 @@ xbt_dynar_t Host::getAttachedStorageList()
 
 Action *Host::open(const char* fullpath) {
 
-  Storage *st = NULL;
+  simgrid::surf::Storage *st = NULL;
   s_mount_t mnt;
   unsigned int cursor;
   size_t longest_prefix_length = 0;
@@ -228,7 +245,7 @@ Action *Host::open(const char* fullpath) {
     if(!strcmp(file_mount_name,mnt.name) && strlen(mnt.name)>longest_prefix_length)
     {/* The current mount name is found in the full path and is bigger than the previous*/
       longest_prefix_length = strlen(mnt.name);
-      st = static_cast<Storage*>(mnt.storage);
+      st = static_cast<simgrid::surf::Storage*>(mnt.storage);
     }
     free(file_mount_name);
   }
@@ -252,19 +269,19 @@ Action *Host::open(const char* fullpath) {
 }
 
 Action *Host::close(surf_file_t fd) {
-  Storage *st = findStorageOnMountList(fd->mount);
+  simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
   XBT_DEBUG("CLOSE %s on disk '%s'",fd->name, st->getName());
   return st->close(fd);
 }
 
 Action *Host::read(surf_file_t fd, sg_size_t size) {
-  Storage *st = findStorageOnMountList(fd->mount);
+  simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
   XBT_DEBUG("READ %s on disk '%s'",fd->name, st->getName());
   return st->read(fd, size);
 }
 
 Action *Host::write(surf_file_t fd, sg_size_t size) {
-  Storage *st = findStorageOnMountList(fd->mount);
+  simgrid::surf::Storage *st = findStorageOnMountList(fd->mount);
   XBT_DEBUG("WRITE %s on disk '%s'",fd->name, st->getName());
   return st->write(fd, size);
 }
@@ -275,7 +292,7 @@ int Host::unlink(surf_file_t fd) {
     return -1;
   } else {
 
-    Storage *st = findStorageOnMountList(fd->mount);
+    simgrid::surf::Storage *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,
@@ -302,7 +319,7 @@ sg_size_t Host::getSize(surf_file_t fd){
 
 xbt_dynar_t Host::getInfo( surf_file_t fd)
 {
-  Storage *st = findStorageOnMountList(fd->mount);
+  simgrid::surf::Storage *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);
@@ -368,38 +385,31 @@ int Host::fileMove(surf_file_t fd, const char* fullpath){
 
 xbt_dynar_t Host::getVms()
 {
-  xbt_dynar_t dyn = xbt_dynar_new(sizeof(VM*), NULL);
+  xbt_dynar_t dyn = xbt_dynar_new(sizeof(simgrid::surf::VirtualMachine*), NULL);
 
   /* iterate for all virtual machines */
-  for (VMModel::vm_list_t::iterator iter =
-         VMModel::ws_vms.begin();
-       iter !=  VMModel::ws_vms.end(); ++iter) {
+  for (simgrid::surf::VMModel::vm_list_t::iterator iter =
+         simgrid::surf::VMModel::ws_vms.begin();
+       iter !=  simgrid::surf::VMModel::ws_vms.end(); ++iter) {
 
-    VM *ws_vm = &*iter;
-    if (this == ws_vm->p_subWs)
+    simgrid::surf::VirtualMachine *ws_vm = &*iter;
+    if (this == ws_vm->p_hostPM->extension(simgrid::surf::Host::EXTENSION_ID))
       xbt_dynar_push(dyn, &ws_vm);
   }
 
   return dyn;
 }
 
-void Host::getParams(ws_params_t params)
+void Host::getParams(vm_params_t params)
 {
   *params = p_params;
 }
 
-void Host::setParams(ws_params_t params)
+void Host::setParams(vm_params_t params)
 {
   /* may check something here. */
   p_params = *params;
 }
 
-/**********
- * Action *
- **********/
-
-void HostAction::setState(e_surf_action_state_t state){
-  e_surf_action_state_t old = getState();
-  Action::setState(state);
-  surf_callback_emit(hostActionStateChangedCallbacks, this, old, state);
+}
 }