Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Close #105
[simgrid.git] / src / plugins / vm / VmLiveMigration.cpp
index 46286f9..bac67f3 100644 (file)
@@ -8,6 +8,7 @@
 #include <simgrid/s4u/VirtualMachine.hpp>
 #include <src/instr/instr_private.hpp>
 #include <src/plugins/vm/VirtualMachineImpl.hpp>
+#include <src/plugins/vm/VmHostExt.hpp>
 #include <src/plugins/vm/VmLiveMigration.hpp>
 #include <xbt/ex.hpp>
 
@@ -15,6 +16,13 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(vm_live_migration, "S4U virtual machines live migra
 
 namespace simgrid {
 namespace vm {
+simgrid::xbt::Extension<s4u::Host, VmMigrationExt> VmMigrationExt::EXTENSION_ID;
+
+void VmMigrationExt::ensureVmMigrationExtInstalled()
+{
+  if (not EXTENSION_ID.valid())
+    EXTENSION_ID = simgrid::s4u::Host::extension_create<VmMigrationExt>();
+}
 
 void MigrationRx::operator()()
 {
@@ -36,25 +44,16 @@ void MigrationRx::operator()()
   // Here Stage 1, 2  and 3 have been performed.
   // Hence complete the migration
 
-  // Copy the reference to the vm (if SRC crashes now, do_migration will free ms)
-  // This is clearly ugly but I (Adrien) need more time to do something cleaner (actually we should copy the whole ms
-  // structure at the beginning and free it at the end of each function)
-  simgrid::s4u::VirtualMachine* vm = vm_;
-  simgrid::s4u::Host* dst_pm       = dst_pm_;
-
-  // Make sure that we cannot get interrupted between the migrate and the resume to not end in an inconsistent state
-  simgrid::simix::kernelImmediate([vm, dst_pm]() {
-    /* Update the vm location */
-    /* precopy migration makes the VM temporally paused */
-    xbt_assert(vm->getState() == SURF_VM_STATE_SUSPENDED);
+  /* Update the vm location */
+  /* precopy migration makes the VM temporally paused */
+  xbt_assert(vm_->getState() == SURF_VM_STATE_SUSPENDED);
 
-    /* Update the vm location and resume it */
-    vm->pimpl_vm_->setPm(dst_pm);
-    vm->resume();
-  });
+  /* Update the vm location and resume it */
+  vm_->setPm(dst_pm_);
+  vm_->resume();
 
   // Now the VM is running on the new host (the migration is completed) (even if the SRC crash)
-  vm->pimpl_vm_->isMigrating = false;
+  vm_->getImpl()->isMigrating = false;
   XBT_DEBUG("VM(%s) moved from PM(%s) to PM(%s)", vm_->getCname(), src_pm_->getCname(), dst_pm_->getCname());
 
   if (TRACE_msg_vm_is_enabled()) {
@@ -63,19 +62,19 @@ void MigrationRx::operator()()
     counter++;
 
     // start link
-    container_t msg = simgrid::instr::Container::byName(vm->getName());
+    container_t msg = simgrid::instr::Container::byName(vm_->getName());
     simgrid::instr::Container::getRoot()->getLink("MSG_VM_LINK")->startEvent(msg, "M", key);
 
     // destroy existing container of this vm
-    container_t existing_container = simgrid::instr::Container::byName(vm->getName());
+    container_t existing_container = simgrid::instr::Container::byName(vm_->getName());
     existing_container->removeFromParent();
     delete existing_container;
 
     // create new container on the new_host location
-    new simgrid::instr::Container(vm->getCname(), "MSG_VM", simgrid::instr::Container::byName(dst_pm_->getName()));
+    new simgrid::instr::Container(vm_->getCname(), "MSG_VM", simgrid::instr::Container::byName(dst_pm_->getName()));
 
     // end link
-    msg = simgrid::instr::Container::byName(vm->getName());
+    msg = simgrid::instr::Container::byName(vm_->getName());
     simgrid::instr::Container::getRoot()->getLink("MSG_VM_LINK")->endEvent(msg, "M", key);
   }
   // Inform the SRC that the migration has been correctly performed
@@ -87,15 +86,15 @@ void MigrationRx::operator()()
   XBT_DEBUG("mig: rx_done");
 }
 
-static sg_size_t get_updated_size(double computed, double dp_rate, double dp_cap)
+static sg_size_t get_updated_size(double computed, double dp_rate, sg_size_t dp_cap)
 {
-  double updated_size = computed * dp_rate;
-  XBT_DEBUG("updated_size %f dp_rate %f", updated_size, dp_rate);
+  sg_size_t updated_size = static_cast<sg_size_t>(computed * dp_rate);
+  XBT_DEBUG("updated_size %llu dp_rate %f", updated_size, dp_rate);
   if (updated_size > dp_cap) {
     updated_size = dp_cap;
   }
 
-  return static_cast<sg_size_t>(updated_size);
+  return updated_size;
 }
 
 sg_size_t MigrationTx::sendMigrationData(sg_size_t size, int stage, int stage2_round, double mig_speed, double timeout)
@@ -119,6 +118,7 @@ sg_size_t MigrationTx::sendMigrationData(sg_size_t size, int stage, int stage2_r
       XBT_VERB("timeout (%lf s) in sending_migration_data, remaining %llu bytes of %llu", timeout, remaining, size);
       sent -= remaining;
     }
+    delete msg;
   }
 
   double clock_end    = s4u::Engine::getClock();
@@ -140,13 +140,12 @@ void MigrationTx::operator()()
   XBT_DEBUG("mig: tx_start");
 
   double host_speed = vm_->getPm()->getSpeed();
-  s_vm_params_t params;
-  vm_->getParameters(&params);
   const sg_size_t ramsize = vm_->getRamsize();
-  const double dp_rate    = host_speed ? (params.mig_speed * params.dp_intensity) / host_speed : 1;
-  const double dp_cap     = params.dp_cap;
-  const double mig_speed  = params.mig_speed;
-  double max_downtime     = params.max_downtime;
+  const double dp_rate =
+      host_speed ? (sg_vm_get_migration_speed(vm_) * sg_vm_get_dirty_page_intensity(vm_)) / host_speed : 1;
+  const sg_size_t dp_cap = sg_vm_get_working_set_memory(vm_);
+  const double mig_speed = sg_vm_get_migration_speed(vm_);
+  double max_downtime    = sg_vm_get_max_downtime(vm_);
 
   double mig_timeout = 10000000.0;
   bool skip_stage2   = false;
@@ -217,7 +216,7 @@ void MigrationTx::operator()()
         updated_size    = get_updated_size(computed, dp_rate, dp_cap);
       }
 
-      XBT_DEBUG("mig-stage 2:%d updated_size %llu computed_during_stage1 %f dp_rate %f dp_cap %f", stage2_round,
+      XBT_DEBUG("mig-stage 2:%d updated_size %llu computed_during_stage1 %f dp_rate %f dp_cap %llu", stage2_round,
                 updated_size, computed_during_stage1, dp_rate, dp_cap);
 
       /* Check whether the remaining size is below the threshold value. If so, move to stage 3. */
@@ -290,6 +289,46 @@ void MigrationTx::operator()()
 }
 
 SG_BEGIN_DECL()
+
+static void onVirtualMachineShutdown(simgrid::s4u::VirtualMachine* vm)
+{
+  if (vm->isMigrating()) {
+    vm->extension<simgrid::vm::VmMigrationExt>()->rx_->kill();
+    vm->extension<simgrid::vm::VmMigrationExt>()->tx_->kill();
+    vm->extension<simgrid::vm::VmMigrationExt>()->issuer_->kill();
+    vm->getImpl()->isMigrating = false;
+  }
+}
+
+void sg_vm_live_migration_plugin_init()
+{
+  sg_vm_dirty_page_tracking_init();
+  simgrid::vm::VmMigrationExt::ensureVmMigrationExtInstalled();
+  simgrid::s4u::VirtualMachine::onVmShutdown.connect(&onVirtualMachineShutdown);
+}
+
+simgrid::s4u::VirtualMachine* sg_vm_create_migratable(simgrid::s4u::Host* pm, const char* name, int coreAmount,
+                                                      int ramsize, int mig_netspeed, int dp_intensity)
+{
+  simgrid::vm::VmHostExt::ensureVmExtInstalled();
+
+  /* For the moment, intensity_rate is the percentage against the migration bandwidth */
+
+  msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, coreAmount, static_cast<sg_size_t>(ramsize) * 1024 * 1024);
+  sg_vm_set_dirty_page_intensity(vm, dp_intensity / 100.0);
+  sg_vm_set_working_set_memory(vm, vm->getRamsize() * 0.9); // assume working set memory is 90% of ramsize
+  sg_vm_set_migration_speed(vm, mig_netspeed * 1024 * 1024.0);
+
+  XBT_DEBUG("migspeed : %f intensity mem : %d", mig_netspeed * 1024 * 1024.0, dp_intensity);
+
+  return vm;
+}
+
+int sg_vm_is_migrating(simgrid::s4u::VirtualMachine* vm)
+{
+  return vm->isMigrating();
+}
+
 void sg_vm_migrate(simgrid::s4u::VirtualMachine* vm, simgrid::s4u::Host* dst_pm)
 {
   simgrid::s4u::Host* src_pm = vm->getPm();
@@ -303,6 +342,8 @@ void sg_vm_migrate(simgrid::s4u::VirtualMachine* vm, simgrid::s4u::Host* dst_pm)
   if (vm->isMigrating())
     THROWF(vm_error, 0, "Cannot migrate VM '%s' that is already migrating.", vm->getCname());
 
+  vm->getImpl()->isMigrating = true;
+
   std::string rx_name =
       std::string("__pr_mig_rx:") + vm->getCname() + "(" + src_pm->getCname() + "-" + dst_pm->getCname() + ")";
   std::string tx_name =
@@ -313,15 +354,16 @@ void sg_vm_migrate(simgrid::s4u::VirtualMachine* vm, simgrid::s4u::Host* dst_pm)
   simgrid::s4u::ActorPtr tx =
       simgrid::s4u::Actor::createActor(tx_name.c_str(), src_pm, simgrid::vm::MigrationTx(vm, dst_pm));
 
+  vm->extension_set<simgrid::vm::VmMigrationExt>(new simgrid::vm::VmMigrationExt(simgrid::s4u::Actor::self(), rx, tx));
+
   /* wait until the migration have finished or on error has occurred */
   XBT_DEBUG("wait for reception of the final ACK (i.e. migration has been correctly performed");
   simgrid::s4u::MailboxPtr mbox_ctl = simgrid::s4u::Mailbox::byName(
       std::string("__mbox_mig_ctl:") + vm->getCname() + "(" + src_pm->getCname() + "-" + dst_pm->getCname() + ")");
   delete static_cast<std::string*>(mbox_ctl->get());
-
   tx->join();
   rx->join();
 
-  vm->pimpl_vm_->isMigrating = false;
+  vm->getImpl()->isMigrating = false;
 }
 }