Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
just reordering functions in file
[simgrid.git] / src / msg / msg_vm.cpp
index 88e602f..3fc0bf9 100644 (file)
@@ -11,8 +11,8 @@
 
 #include <xbt/ex.hpp>
 
+#include "simgrid/plugins/live_migration.h"
 #include "src/instr/instr_private.hpp"
-#include "src/msg/msg_private.hpp"
 #include "src/plugins/vm/VirtualMachineImpl.hpp"
 #include "src/plugins/vm/VmHostExt.hpp"
 
 
 extern "C" {
 
-struct s_dirty_page {
-  double prev_clock     = 0.0;
-  double prev_remaining = 0.0;
-  msg_task_t task       = nullptr;
-};
-typedef s_dirty_page* dirty_page_t;
-
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg, "Cloud-oriented parts of the MSG API");
 
 const char* MSG_vm_get_name(msg_vm_t vm)
@@ -36,6 +29,14 @@ const char* MSG_vm_get_name(msg_vm_t vm)
   return vm->getCname();
 }
 
+/** @brief Get the physical host of a given VM.
+ *  @ingroup msg_VMs
+ */
+msg_host_t MSG_vm_get_pm(msg_vm_t vm)
+{
+  return vm->getPm();
+}
+
 /** \ingroup m_vm_management
  * \brief Set the parameters of a given host
  *
@@ -68,6 +69,10 @@ size_t MSG_vm_get_ramsize(msg_vm_t vm)
 }
 
 /* **** Check state of a VM **** */
+void MSG_vm_set_bound(msg_vm_t vm, double bound)
+{
+  vm->setBound(bound);
+}
 static inline int __MSG_vm_is_state(msg_vm_t vm, e_surf_vm_state_t state)
 {
   return vm->pimpl_vm_ != nullptr && vm->getState() == state;
@@ -167,43 +172,51 @@ msg_vm_t MSG_vm_create_multicore(msg_host_t pm, const char* name, int coreAmount
   return vm;
 }
 
-/** @brief Destroy a VM. Destroy the VM object from the simulation.
+/** @brief Start a vm (i.e., boot the guest operating system)
  *  @ingroup msg_VMs
+ *
+ *  If the VM cannot be started (because of memory over-provisioning), an exception is generated.
  */
-void MSG_vm_destroy(msg_vm_t vm)
+void MSG_vm_start(msg_vm_t vm)
 {
-  if (vm->isMigrating())
-    THROWF(vm_error, 0, "Cannot destroy VM '%s', which is migrating.", vm->getCname());
-
-  /* First, terminate all processes on the VM if necessary */
-  if (MSG_vm_is_running(vm))
-    MSG_vm_shutdown(vm);
-
-  /* Then, destroy the VM object */
-  simgrid::simix::kernelImmediate([vm]() { vm->destroy(); });
-
+  vm->start();
   if (TRACE_msg_vm_is_enabled()) {
-    container_t container = simgrid::instr::Container::byName(vm->getName());
-    container->removeFromParent();
-    delete container;
+    simgrid::instr::StateType* state = simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE");
+    state->addEntityValue("start", "0 0 1"); // start is blue
+    state->pushEvent("start");
   }
 }
 
-/** @brief Start a vm (i.e., boot the guest operating system)
+/** @brief Immediately suspend the execution of all processes within the given VM.
  *  @ingroup msg_VMs
  *
- *  If the VM cannot be started (because of memory over-provisioning), an exception is generated.
+ * This function stops the execution of the VM. All the processes on this VM
+ * will pause. The state of the VM is preserved. We can later resume it again.
+ *
+ * No suspension cost occurs.
  */
-void MSG_vm_start(msg_vm_t vm)
+void MSG_vm_suspend(msg_vm_t vm)
 {
-  vm->start();
+  vm->suspend();
   if (TRACE_msg_vm_is_enabled()) {
     simgrid::instr::StateType* state = simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE");
-    state->addEntityValue("start", "0 0 1"); // start is blue
-    state->pushEvent("start");
+    state->addEntityValue("suspend", "1 0 0"); // suspend is red
+    state->pushEvent("suspend");
   }
 }
 
+/** @brief Resume the execution of the VM. All processes on the VM run again.
+ *  @ingroup msg_VMs
+ *
+ * No resume cost occurs.
+ */
+void MSG_vm_resume(msg_vm_t vm)
+{
+  vm->resume();
+  if (TRACE_msg_vm_is_enabled())
+    simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE")->popEvent();
+}
+
 /** @brief Immediately kills all processes within the given VM.
  *  @ingroup msg_VMs
  *
@@ -213,11 +226,28 @@ void MSG_vm_start(msg_vm_t vm)
  */
 void MSG_vm_shutdown(msg_vm_t vm)
 {
-  smx_actor_t issuer = SIMIX_process_self();
-  simgrid::simix::kernelImmediate([vm, issuer]() { vm->pimpl_vm_->shutdown(issuer); });
+  vm->shutdown();
+}
+
+/** @brief Destroy a VM. Destroy the VM object from the simulation.
+ *  @ingroup msg_VMs
+ */
+void MSG_vm_destroy(msg_vm_t vm)
+{
+  if (vm->isMigrating())
+    THROWF(vm_error, 0, "Cannot destroy VM '%s', which is migrating.", vm->getCname());
+
+  /* First, terminate all processes on the VM if necessary */
+  vm->shutdown();
 
-  // Make sure that processes in the VM are killed in this scheduling round before processing (eg with the VM destroy)
-  MSG_process_sleep(0.);
+  /* Then, destroy the VM object */
+  vm->destroy();
+
+  if (TRACE_msg_vm_is_enabled()) {
+    container_t container = simgrid::instr::Container::byName(vm->getName());
+    container->removeFromParent();
+    delete container;
+  }
 }
 
 static std::string get_mig_process_tx_name(msg_vm_t vm, msg_host_t src_pm, msg_host_t dst_pm)
@@ -339,116 +369,6 @@ static int migration_rx_fun(int argc, char *argv[])
   return 0;
 }
 
-static void start_dirty_page_tracking(msg_vm_t vm)
-{
-  vm->pimpl_vm_->dp_enabled = true;
-  if (vm->pimpl_vm_->dp_objs.empty())
-    return;
-
-  for (auto const& elm : vm->pimpl_vm_->dp_objs) {
-    dirty_page_t dp    = elm.second;
-    double remaining   = MSG_task_get_flops_amount(dp->task);
-    dp->prev_clock = MSG_get_clock();
-    dp->prev_remaining = remaining;
-    XBT_DEBUG("%s@%s remaining %f", elm.first.c_str(), vm->getCname(), remaining);
-  }
-}
-
-static void stop_dirty_page_tracking(msg_vm_t vm)
-{
-  vm->pimpl_vm_->dp_enabled = false;
-}
-
-static double get_computed(const std::string& key, msg_vm_t vm, dirty_page_t dp, double remaining, double clock)
-{
-  double computed = dp->prev_remaining - remaining;
-  double duration = clock - dp->prev_clock;
-
-  XBT_DEBUG("%s@%s: computed %f ops (remaining %f -> %f) in %f secs (%f -> %f)", key.c_str(), vm->getCname(), computed,
-            dp->prev_remaining, remaining, duration, dp->prev_clock, clock);
-
-  return computed;
-}
-
-static double lookup_computed_flop_counts(msg_vm_t vm, int stage_for_fancy_debug, int stage2_round_for_fancy_debug)
-{
-  double total = 0;
-
-  for (auto const& elm : vm->pimpl_vm_->dp_objs) {
-    const std::string& key = elm.first;
-    dirty_page_t dp  = elm.second;
-    double remaining       = MSG_task_get_flops_amount(dp->task);
-
-    double clock = MSG_get_clock();
-
-    total += get_computed(key, vm, dp, remaining, clock);
-
-    dp->prev_remaining = remaining;
-    dp->prev_clock = clock;
-  }
-
-  total += vm->pimpl_vm_->dp_updated_by_deleted_tasks;
-
-  XBT_DEBUG("mig-stage%d.%d: computed %f flop_counts (including %f by deleted tasks)", stage_for_fancy_debug,
-            stage2_round_for_fancy_debug, total, vm->pimpl_vm_->dp_updated_by_deleted_tasks);
-
-  vm->pimpl_vm_->dp_updated_by_deleted_tasks = 0;
-
-  return total;
-}
-
-// TODO Is this code redundant with the information provided by
-// msg_process_t MSG_process_create(const char *name, xbt_main_func_t code, void *data, msg_host_t host)
-/** @brief take care of the dirty page tracking, in case we're adding a task to a migrating VM */
-void MSG_host_add_task(msg_host_t host, msg_task_t task)
-{
-  simgrid::s4u::VirtualMachine* vm = dynamic_cast<simgrid::s4u::VirtualMachine*>(host);
-  if (vm == nullptr)
-    return;
-
-  double remaining = MSG_task_get_flops_amount(task);
-  std::string key  = simgrid::xbt::string_printf("%s-%p", task->name, task);
-
-  dirty_page_t dp = new s_dirty_page;
-  dp->task = task;
-  if (vm->pimpl_vm_->dp_enabled) {
-    dp->prev_clock = MSG_get_clock();
-    dp->prev_remaining = remaining;
-  }
-  vm->pimpl_vm_->dp_objs.insert({key, dp});
-  XBT_DEBUG("add %s on %s (remaining %f, dp_enabled %d)", key.c_str(), host->getCname(), remaining,
-            vm->pimpl_vm_->dp_enabled);
-}
-
-void MSG_host_del_task(msg_host_t host, msg_task_t task)
-{
-  simgrid::s4u::VirtualMachine* vm = dynamic_cast<simgrid::s4u::VirtualMachine*>(host);
-  if (vm == nullptr)
-    return;
-
-  std::string key = simgrid::xbt::string_printf("%s-%p", task->name, task);
-  dirty_page_t dp = nullptr;
-  auto dp_obj     = vm->pimpl_vm_->dp_objs.find(key);
-  if (dp_obj != vm->pimpl_vm_->dp_objs.end())
-    dp = dp_obj->second;
-  xbt_assert(dp && dp->task == task);
-
-  /* If we are in the middle of dirty page tracking, we record how much computation has been done until now, and keep
-   * the information for the lookup_() function that will called soon. */
-  if (vm->pimpl_vm_->dp_enabled) {
-    double remaining = MSG_task_get_flops_amount(task);
-    double clock = MSG_get_clock();
-    double updated = get_computed(key, vm, dp, remaining, clock); // was host instead of vm
-
-    vm->pimpl_vm_->dp_updated_by_deleted_tasks += updated;
-  }
-
-  vm->pimpl_vm_->dp_objs.erase(key);
-  delete dp;
-
-  XBT_DEBUG("del %s on %s", key.c_str(), host->getCname());
-}
-
 static sg_size_t send_migration_data(msg_vm_t vm, msg_host_t src_pm, msg_host_t dst_pm, sg_size_t size,
                                      const std::string& mbox, int stage, int stage2_round, double mig_speed,
                                      double timeout)
@@ -542,7 +462,7 @@ static int migration_tx_fun(int argc, char *argv[])
 
   /* Stage1: send all memory pages to the destination. */
   XBT_DEBUG("mig-stage1: remaining_size %zu", remaining_size);
-  start_dirty_page_tracking(ms->vm);
+  sg_vm_start_dirty_page_tracking(ms->vm);
 
   double computed_during_stage1 = 0;
   double clock_prev_send        = MSG_get_clock();
@@ -553,7 +473,7 @@ static int migration_tx_fun(int argc, char *argv[])
     XBT_VERB("Stage 1: Gonna send %llu bytes", ramsize);
     sg_size_t sent = send_migration_data(ms->vm, ms->src_pm, ms->dst_pm, ramsize, ms->mbox, 1, 0, mig_speed, -1);
     remaining_size -= sent;
-    computed_during_stage1 = lookup_computed_flop_counts(ms->vm, 1, 0);
+    computed_during_stage1 = sg_vm_lookup_computed_flops(ms->vm);
 
     if (sent < ramsize) {
       XBT_VERB("mig-stage1: timeout, force moving to stage 3");
@@ -564,7 +484,7 @@ static int migration_tx_fun(int argc, char *argv[])
   } catch (xbt_ex& e) {
     // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
     // Stop the dirty page tracking an return (there is no memory space to release)
-    stop_dirty_page_tracking(ms->vm);
+    sg_vm_stop_dirty_page_tracking(ms->vm);
     return 0;
   }
 
@@ -591,7 +511,7 @@ static int migration_tx_fun(int argc, char *argv[])
         /* just after stage1, nothing has been updated. But, we have to send the data updated during stage1 */
         updated_size = get_updated_size(computed_during_stage1, dp_rate, dp_cap);
       } else {
-        double computed = lookup_computed_flop_counts(ms->vm, 2, stage2_round);
+        double computed = sg_vm_lookup_computed_flops(ms->vm);
         updated_size    = get_updated_size(computed, dp_rate, dp_cap);
       }
 
@@ -615,7 +535,7 @@ static int migration_tx_fun(int argc, char *argv[])
         // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data
         // code)
         // Stop the dirty page tracking an return (there is no memory space to release)
-        stop_dirty_page_tracking(ms->vm);
+        sg_vm_stop_dirty_page_tracking(ms->vm);
         return 0;
       }
       double clock_post_send = MSG_get_clock();
@@ -637,7 +557,7 @@ static int migration_tx_fun(int argc, char *argv[])
                  updated_size, (clock_post_send - clock_prev_send));
         remaining_size -= sent;
 
-        double computed = lookup_computed_flop_counts(ms->vm, 2, stage2_round);
+        double computed = sg_vm_lookup_computed_flops(ms->vm);
         updated_size    = get_updated_size(computed, dp_rate, dp_cap);
         remaining_size += updated_size;
         break;
@@ -652,7 +572,7 @@ static int migration_tx_fun(int argc, char *argv[])
   pimpl->setState(SURF_VM_STATE_RUNNING); // FIXME: this bypass of the checks in suspend() is not nice
   pimpl->isMigrating = false;             // FIXME: this bypass of the checks in suspend() is not nice
   pimpl->suspend(SIMIX_process_self());
-  stop_dirty_page_tracking(ms->vm);
+  sg_vm_stop_dirty_page_tracking(ms->vm);
 
   try {
     XBT_DEBUG("Stage 3: Gonna send %zu bytes", remaining_size);
@@ -750,73 +670,4 @@ void MSG_vm_migrate(msg_vm_t vm, msg_host_t dst_pm)
   xbt_assert(get_mig_task_name(vm, src_pm, dst_pm, 4) == task->name);
   MSG_task_destroy(task);
 }
-
-/** @brief Immediately suspend the execution of all processes within the given VM.
- *  @ingroup msg_VMs
- *
- * This function stops the execution of the VM. All the processes on this VM
- * will pause. The state of the VM is preserved. We can later resume it again.
- *
- * No suspension cost occurs.
- */
-void MSG_vm_suspend(msg_vm_t vm)
-{
-  vm->suspend();
-  if (TRACE_msg_vm_is_enabled()) {
-    simgrid::instr::StateType* state = simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE");
-    state->addEntityValue("suspend", "1 0 0"); // suspend is red
-    state->pushEvent("suspend");
-  }
-}
-
-/** @brief Resume the execution of the VM. All processes on the VM run again.
- *  @ingroup msg_VMs
- *
- * No resume cost occurs.
- */
-void MSG_vm_resume(msg_vm_t vm)
-{
-  vm->resume();
-  if (TRACE_msg_vm_is_enabled())
-    simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE")->popEvent();
-}
-
-/** @brief Get the physical host of a given VM.
- *  @ingroup msg_VMs
- */
-msg_host_t MSG_vm_get_pm(msg_vm_t vm)
-{
-  return vm->getPm();
-}
-
-/** @brief Set a CPU bound for a given VM.
- *  @ingroup msg_VMs
- *
- * 1. Note that in some cases MSG_task_set_bound() may not intuitively work for VMs.
- *
- * For example,
- *  On PM0, there are Task1 and VM0.
- *  On VM0, there is Task2.
- * Now we bound 75% to Task1\@PM0 and bound 25% to Task2\@VM0.
- * Then,
- *  Task1\@PM0 gets 50%.
- *  Task2\@VM0 gets 25%.
- * This is NOT 75% for Task1\@PM0 and 25% for Task2\@VM0, respectively.
- *
- * This is because a VM has the dummy CPU action in the PM layer. Putting a task on the VM does not affect the bound of
- * the dummy CPU action. The bound of the dummy CPU action is unlimited.
- *
- * There are some solutions for this problem. One option is to update the bound of the dummy CPU action automatically.
- * It should be the sum of all tasks on the VM. But, this solution might be costly, because we have to scan all tasks
- * on the VM in share_resource() or we have to trap both the start and end of task execution.
- *
- * The current solution is to use MSG_vm_set_bound(), which allows us to directly set the bound of the dummy CPU action.
- *
- * 2. Note that bound == 0 means no bound (i.e., unlimited). But, if a host has multiple CPU cores, the CPU share of a
- *    computation task (or a VM) never exceeds the capacity of a CPU core.
- */
-void MSG_vm_set_bound(msg_vm_t vm, double bound)
-{
-  simgrid::simix::kernelImmediate([vm, bound]() { vm->pimpl_vm_->setBound(bound); });
-}
 }