Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update python/clusters-multicpu to the new API.
[simgrid.git] / src / plugins / vm / VmLiveMigration.cpp
index 65bc4e5..be895e1 100644 (file)
@@ -1,18 +1,18 @@
-/* Copyright (c) 2013-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2013-2023. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include "src/plugins/vm/VmLiveMigration.hpp"
-#include "simgrid/Exception.hpp"
+#include <simgrid/Exception.hpp>
+#include <simgrid/plugins/live_migration.h>
+
 #include "src/instr/instr_private.hpp"
-#include "src/plugins/vm/VirtualMachineImpl.hpp"
-#include "src/plugins/vm/VmHostExt.hpp"
+#include "src/kernel/resource/VirtualMachineImpl.hpp"
+#include "src/plugins/vm/VmLiveMigration.hpp"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(vm_live_migration, s4u, "S4U virtual machines live migration");
 
-namespace simgrid {
-namespace vm {
+namespace simgrid::plugin::vm {
 xbt::Extension<s4u::Host, VmMigrationExt> VmMigrationExt::EXTENSION_ID;
 
 void VmMigrationExt::ensureVmMigrationExtInstalled()
@@ -27,15 +27,13 @@ void MigrationRx::operator()()
   bool received_finalize = false;
 
   std::string finalize_task_name =
-      std::string("__mig_stage3:") + vm_->get_cname() + "(" + src_pm_->get_cname() + "-" + dst_pm_->get_cname() + ")";
+      "__mig_stage3:" + vm_->get_name() + "(" + src_pm_->get_name() + "-" + dst_pm_->get_name() + ")";
 
   while (not received_finalize) {
-    const std::string* payload = mbox->get<std::string>();
+    auto payload = mbox->get_unique<std::string>();
 
     if (finalize_task_name == *payload)
       received_finalize = true;
-
-    delete payload;
   }
 
   // Here Stage 1, 2  and 3 have been performed.
@@ -43,14 +41,14 @@ void MigrationRx::operator()()
 
   /* Update the vm location */
   /* precopy migration makes the VM temporally paused */
-  xbt_assert(vm_->get_state() == s4u::VirtualMachine::state::SUSPENDED);
+  xbt_assert(vm_->get_state() == s4u::VirtualMachine::State::SUSPENDED);
 
   /* Update the vm location and resume it */
   vm_->set_pm(dst_pm_);
   vm_->resume();
 
   // Now the VM is running on the new host (the migration is completed) (even if the SRC crash)
-  vm_->get_impl()->end_migration();
+  vm_->get_vm_impl()->end_migration();
   XBT_DEBUG("VM(%s) moved from PM(%s) to PM(%s)", vm_->get_cname(), src_pm_->get_cname(), dst_pm_->get_cname());
 
   if (TRACE_vm_is_enabled()) {
@@ -74,7 +72,7 @@ void MigrationRx::operator()()
   }
   // Inform the SRC that the migration has been correctly performed
   auto* payload = new std::string("__mig_stage4:");
-  *payload      = *payload + vm_->get_cname() + "(" + src_pm_->get_cname() + "-" + dst_pm_->get_cname() + ")";
+  *payload      = *payload + vm_->get_name() + "(" + src_pm_->get_name() + "-" + dst_pm_->get_name() + ")";
 
   mbox_ctl->put(payload, 0);
 
@@ -95,22 +93,20 @@ static sg_size_t get_updated_size(double computed, double dp_rate, sg_size_t dp_
 sg_size_t MigrationTx::sendMigrationData(sg_size_t size, int stage, int stage2_round, double mig_speed, double timeout)
 {
   sg_size_t sent   = size;
-  auto* msg        = new std::string("__mig_stage");
-  *msg             = *msg + std::to_string(stage) + ":" + vm_->get_cname() + "(" + src_pm_->get_cname() + "-" +
-         dst_pm_->get_cname() + ")";
-
+  auto msg = std::make_unique<std::string>(xbt::string_printf("__mig_stage%d:%s(%s-%s)", stage, vm_->get_cname(),
+                                                              src_pm_->get_cname(), dst_pm_->get_cname()));
   double clock_sta = s4u::Engine::get_clock();
 
-  s4u::CommPtr comm = mbox->put_init(msg, size);
+  s4u::CommPtr comm = mbox->put_init(msg.get(), size);
   if (mig_speed > 0)
     comm->set_rate(mig_speed);
   try {
     comm->wait_for(timeout);
+    msg.release();
   } catch (const Exception&) {
     auto remaining = static_cast<sg_size_t>(comm->get_remaining());
     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::get_clock();
@@ -271,34 +267,35 @@ void MigrationTx::operator()()
   // effectively the VM on the DST node.
   XBT_DEBUG("mig: tx_done");
 }
-} // namespace vm
-} // namespace simgrid
+} // namespace simgrid::plugin::vm
+
+using simgrid::plugin::vm::VmMigrationExt;
 
 static void onVirtualMachineShutdown(simgrid::s4u::VirtualMachine const& vm)
 {
-  if (vm.get_impl()->is_migrating()) {
-    vm.extension<simgrid::vm::VmMigrationExt>()->rx_->kill();
-    vm.extension<simgrid::vm::VmMigrationExt>()->tx_->kill();
-    vm.extension<simgrid::vm::VmMigrationExt>()->issuer_->kill();
-    vm.get_impl()->end_migration();
+  if (vm.get_vm_impl()->is_migrating()) {
+    vm.extension<VmMigrationExt>()->rx_->kill();
+    vm.extension<VmMigrationExt>()->tx_->kill();
+    vm.extension<VmMigrationExt>()->issuer_->kill();
+    vm.get_vm_impl()->end_migration();
   }
 }
 
 void sg_vm_live_migration_plugin_init()
 {
   sg_vm_dirty_page_tracking_init();
-  simgrid::vm::VmMigrationExt::ensureVmMigrationExtInstalled();
-  simgrid::s4u::VirtualMachine::on_shutdown.connect(&onVirtualMachineShutdown);
+  VmMigrationExt::ensureVmMigrationExtInstalled();
+  simgrid::s4u::VirtualMachine::on_shutdown_cb(&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();
+  simgrid::s4u::VmHostExt::ensureVmExtInstalled();
 
   /* For the moment, intensity_rate is the percentage against the migration bandwidth */
 
-  auto* vm = new simgrid::s4u::VirtualMachine(name, pm, coreAmount, static_cast<sg_size_t>(ramsize) * 1024 * 1024);
+  auto* vm = pm->create_vm(name, 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->get_ramsize() * 0.9); // assume working set memory is 90% of ramsize
   sg_vm_set_migration_speed(vm, mig_netspeed * 1024 * 1024.0);
@@ -310,7 +307,7 @@ simgrid::s4u::VirtualMachine* sg_vm_create_migratable(simgrid::s4u::Host* pm, co
 
 int sg_vm_is_migrating(const simgrid::s4u::VirtualMachine* vm)
 {
-  return vm->get_impl()->is_migrating();
+  return vm->get_vm_impl()->is_migrating();
 }
 
 void sg_vm_migrate(simgrid::s4u::VirtualMachine* vm, simgrid::s4u::Host* dst_pm)
@@ -325,38 +322,34 @@ void sg_vm_migrate(simgrid::s4u::VirtualMachine* vm, simgrid::s4u::Host* dst_pm)
     throw simgrid::VmFailureException(
         XBT_THROW_POINT, simgrid::xbt::string_printf("Cannot migrate VM '%s' to host '%s', which is offline.",
                                                      vm->get_cname(), dst_pm->get_cname()));
-  if (vm->get_state() != simgrid::s4u::VirtualMachine::state::RUNNING)
+  if (vm->get_state() != simgrid::s4u::VirtualMachine::State::RUNNING)
     throw simgrid::VmFailureException(
         XBT_THROW_POINT,
         simgrid::xbt::string_printf("Cannot migrate VM '%s' that is not running yet.", vm->get_cname()));
-  if (vm->get_impl()->is_migrating())
+  if (vm->get_vm_impl()->is_migrating())
     throw simgrid::VmFailureException(
         XBT_THROW_POINT,
         simgrid::xbt::string_printf("Cannot migrate VM '%s' that is already migrating.", vm->get_cname()));
 
-  vm->get_impl()->start_migration();
-  simgrid::s4u::VirtualMachine::on_migration_start(*vm);
+  vm->start_migration();
 
-  std::string rx_name =
-      std::string("__pr_mig_rx:") + vm->get_cname() + "(" + src_pm->get_cname() + "-" + dst_pm->get_cname() + ")";
-  std::string tx_name =
-      std::string("__pr_mig_tx:") + vm->get_cname() + "(" + src_pm->get_cname() + "-" + dst_pm->get_cname() + ")";
+  std::string rx_name = "__pr_mig_rx:" + vm->get_name() + "(" + src_pm->get_name() + "-" + dst_pm->get_name() + ")";
+  std::string tx_name = "__pr_mig_tx:" + vm->get_name() + "(" + src_pm->get_name() + "-" + dst_pm->get_name() + ")";
 
   simgrid::s4u::ActorPtr rx =
-      simgrid::s4u::Actor::create(rx_name.c_str(), dst_pm, simgrid::vm::MigrationRx(vm, dst_pm));
+      simgrid::s4u::Actor::create(rx_name.c_str(), dst_pm, simgrid::plugin::vm::MigrationRx(vm, dst_pm));
   simgrid::s4u::ActorPtr tx =
-      simgrid::s4u::Actor::create(tx_name.c_str(), src_pm, simgrid::vm::MigrationTx(vm, dst_pm));
+      simgrid::s4u::Actor::create(tx_name.c_str(), src_pm, simgrid::plugin::vm::MigrationTx(vm, dst_pm));
 
-  vm->extension_set<simgrid::vm::VmMigrationExt>(new simgrid::vm::VmMigrationExt(simgrid::s4u::Actor::self(), rx, tx));
+  vm->extension_set<VmMigrationExt>(new 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::Mailbox* mbox_ctl = simgrid::s4u::Mailbox::by_name(
-      std::string("__mbox_mig_ctl:") + vm->get_cname() + "(" + src_pm->get_cname() + "-" + dst_pm->get_cname() + ")");
-  delete mbox_ctl->get<std::string>();
+  simgrid::s4u::Mailbox* mbox_ctl = simgrid::s4u::Mailbox::by_name("__mbox_mig_ctl:" + vm->get_name() + "(" +
+                                                                   src_pm->get_name() + "-" + dst_pm->get_name() + ")");
+  mbox_ctl->get_unique<std::string>();
   tx->join();
   rx->join();
 
-  vm->get_impl()->end_migration();
-  simgrid::s4u::VirtualMachine::on_migration_end(*vm);
+  vm->end_migration();
 }