1 /* Copyright (c) 2012-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 * 1. add the support of trace
9 * 2. use parallel tasks to simulate CPU overhead and remove the experimental code generating micro computation tasks
14 #include "src/plugins/vm/VirtualMachineImpl.hpp"
15 #include "src/plugins/vm/VmHostExt.hpp"
16 #include "src/simix/ActorImpl.hpp"
17 #include <simgrid/s4u/VirtualMachine.hpp>
18 #include <simgrid/s4u/host.hpp>
20 #include "msg_private.h"
21 #include "xbt/sysdep.h"
23 #include "simgrid/host.h"
25 #include "src/simix/smx_host_private.h" /* don't ask me why the VM functions are in there (FIXME:KILLME) */
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg, "Cloud-oriented parts of the MSG API");
30 /* **** ******** GENERAL ********* **** */
32 /** \ingroup m_vm_management
33 * \brief Set the parameters of a given host
36 * \param params a parameter object
38 void MSG_vm_set_params(msg_vm_t vm, vm_params_t params)
40 static_cast<simgrid::s4u::VirtualMachine*>(vm)->setParameters(params);
43 /** \ingroup m_vm_management
44 * \brief Get the parameters of a given host
47 * \param params a prameter object
49 void MSG_vm_get_params(msg_vm_t vm, vm_params_t params)
51 static_cast<simgrid::s4u::VirtualMachine*>(vm)->parameters(params);
54 /* **** Check state of a VM **** */
55 static inline int __MSG_vm_is_state(msg_vm_t vm, e_surf_vm_state_t state)
57 return static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() == state;
60 /** @brief Returns whether the given VM has just created, not running.
63 int MSG_vm_is_created(msg_vm_t vm)
65 return __MSG_vm_is_state(vm, SURF_VM_STATE_CREATED);
68 /** @brief Returns whether the given VM is currently running
71 int MSG_vm_is_running(msg_vm_t vm)
73 return __MSG_vm_is_state(vm, SURF_VM_STATE_RUNNING);
76 /** @brief Returns whether the given VM is currently migrating
79 int MSG_vm_is_migrating(msg_vm_t vm)
81 return static_cast<simgrid::s4u::VirtualMachine*>(vm)->isMigrating();
84 /** @brief Returns whether the given VM is currently suspended, not running.
87 int MSG_vm_is_suspended(msg_vm_t vm)
89 return __MSG_vm_is_state(vm, SURF_VM_STATE_SUSPENDED);
92 /** @brief Returns whether the given VM is being saved (FIXME: live saving or not?).
95 int MSG_vm_is_saving(msg_vm_t vm)
97 return __MSG_vm_is_state(vm, SURF_VM_STATE_SAVING);
100 /** @brief Returns whether the given VM has been saved, not running.
103 int MSG_vm_is_saved(msg_vm_t vm)
105 return __MSG_vm_is_state(vm, SURF_VM_STATE_SAVED);
108 /** @brief Returns whether the given VM is being restored, not running.
111 int MSG_vm_is_restoring(msg_vm_t vm)
113 return __MSG_vm_is_state(vm, SURF_VM_STATE_RESTORING);
116 /* **** ******** MSG vm actions ********* **** */
117 /** @brief Create a new VM with specified parameters.
119 * @param pm Physical machine that will host the VM
121 * @param ncpus [TODO]
122 * @param ramsize [TODO]
123 * @param net_cap Maximal bandwidth that the VM can consume (in MByte/s)
124 * @param disk_path (unused) Path to the image that boots
125 * @param disksize (unused) will represent the size of the VM (will be used during migrations)
126 * @param mig_netspeed Amount of Mbyte/s allocated to the migration (cannot be larger than net_cap). Use 0 if unsure.
127 * @param dp_intensity Dirty page percentage according to migNetSpeed, [0-100]. Use 0 if unsure.
129 msg_vm_t MSG_vm_create(msg_host_t pm, const char* name, int ncpus, int ramsize, int net_cap, char* disk_path,
130 int disksize, int mig_netspeed, int dp_intensity)
132 simgrid::vm::VmHostExt::ensureVmExtInstalled();
134 /* For the moment, intensity_rate is the percentage against the migration bandwidth */
135 double host_speed = MSG_host_get_speed(pm);
136 double update_speed = ((double)dp_intensity/100) * mig_netspeed;
138 msg_vm_t vm = MSG_vm_create_core(pm, name);
139 s_vm_params_t params;
140 memset(¶ms, 0, sizeof(params));
141 params.ramsize = (sg_size_t)ramsize * 1024 * 1024;
142 //params.overcommit = 0;
144 params.skip_stage2 = 0;
145 params.max_downtime = 0.03;
146 params.dp_rate = (update_speed * 1024 * 1024) / host_speed;
147 params.dp_cap = params.ramsize * 0.9; // assume working set memory is 90% of ramsize
148 params.mig_speed = (double)mig_netspeed * 1024 * 1024; // mig_speed
150 //XBT_INFO("dp rate %f migspeed : %f intensity mem : %d, updatespeed %f, hostspeed %f",params.dp_rate,
151 // params.mig_speed, dp_intensity, update_speed, host_speed);
152 static_cast<simgrid::s4u::VirtualMachine*>(vm)->setParameters(¶ms);
157 /** @brief Create a new VM object. The VM is not yet started. The resource of the VM is allocated upon MSG_vm_start().
160 * A VM is treated as a host. The name of the VM must be unique among all hosts.
162 msg_vm_t MSG_vm_create_core(msg_host_t pm, const char* name)
164 xbt_assert(sg_host_by_name(name) == nullptr,
165 "Cannot create a VM named %s: this name is already used by an host or a VM", name);
167 return new simgrid::s4u::VirtualMachine(name, pm);
170 /** @brief Destroy a VM. Destroy the VM object from the simulation.
173 void MSG_vm_destroy(msg_vm_t vm)
175 if (MSG_vm_is_migrating(vm))
176 THROWF(vm_error, 0, "Cannot destroy VM '%s', which is migrating.", vm->cname());
178 /* First, terminate all processes on the VM if necessary */
179 if (MSG_vm_is_running(vm))
182 xbt_assert(MSG_vm_is_created(vm), "shutdown the given VM before destroying it");
184 /* Then, destroy the VM object */
185 simgrid::simix::kernelImmediate([vm]() {
189 if (TRACE_msg_vm_is_enabled()) {
190 container_t container = PJ_container_get(vm->cname());
191 PJ_container_remove_from_parent(container);
192 PJ_container_free(container);
196 /** @brief Start a vm (i.e., boot the guest operating system)
199 * If the VM cannot be started (because of memory overprovisionning), an exception is generated.
201 void MSG_vm_start(msg_vm_t vm)
203 simgrid::simix::kernelImmediate([vm]() {
204 simgrid::vm::VmHostExt::ensureVmExtInstalled();
206 simgrid::s4u::VirtualMachine* typedVM = static_cast<simgrid::s4u::VirtualMachine*>(vm);
207 simgrid::s4u::Host* pm = typedVM->pimpl_vm_->getPm();
208 if (pm->extension<simgrid::vm::VmHostExt>() == nullptr)
209 pm->extension_set(new simgrid::vm::VmHostExt());
211 long pm_ramsize = pm->extension<simgrid::vm::VmHostExt>()->ramsize;
212 int pm_overcommit = pm->extension<simgrid::vm::VmHostExt>()->overcommit;
213 long vm_ramsize = typedVM->getRamsize();
215 if (pm_ramsize && !pm_overcommit) { /* Only verify that we don't overcommit on need */
216 /* Retrieve the memory occupied by the VMs on that host. Yep, we have to traverse all VMs of all hosts for that */
217 long total_ramsize_of_vms = 0;
218 for (simgrid::s4u::VirtualMachine* ws_vm : simgrid::vm::VirtualMachineImpl::allVms_)
219 if (pm == ws_vm->pimpl_vm_->getPm())
220 total_ramsize_of_vms += ws_vm->pimpl_vm_->getRamsize();
222 if (vm_ramsize > pm_ramsize - total_ramsize_of_vms) {
223 XBT_WARN("cannnot start %s@%s due to memory shortage: vm_ramsize %ld, free %ld, pm_ramsize %ld (bytes).",
224 vm->cname(), pm->cname(), vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
225 THROWF(vm_error, 0, "Memory shortage on host '%s', VM '%s' cannot be started", pm->cname(), vm->cname());
229 typedVM->pimpl_vm_->setState(SURF_VM_STATE_RUNNING);
232 if (TRACE_msg_vm_is_enabled()) {
233 container_t vm_container = PJ_container_get(vm->cname());
234 type_t type = PJ_type_get("MSG_VM_STATE", vm_container->type);
235 val_t value = PJ_value_get_or_new("start", "0 0 1", type); // start is blue
236 new_pajePushState(MSG_get_clock(), vm_container, type, value);
240 /** @brief Immediately kills all processes within the given VM.
243 * Any memory that they allocated will be leaked, unless you used #MSG_process_on_exit().
245 * No extra delay occurs. If you want to simulate this too, you want to use a #MSG_process_sleep().
247 void MSG_vm_shutdown(msg_vm_t vm)
249 smx_actor_t issuer=SIMIX_process_self();
250 simgrid::simix::kernelImmediate([vm,issuer]() {
251 static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->shutdown(issuer);
254 // Make sure that the processes in the VM are killed in this scheduling round before processing
255 // (eg with the VM destroy)
256 MSG_process_sleep(0.);
259 static inline char *get_mig_process_tx_name(msg_vm_t vm, msg_host_t src_pm, msg_host_t dst_pm)
261 return bprintf("__pr_mig_tx:%s(%s-%s)", vm->cname(), src_pm->cname(), dst_pm->cname());
264 static inline char *get_mig_process_rx_name(msg_vm_t vm, msg_host_t src_pm, msg_host_t dst_pm)
266 return bprintf("__pr_mig_rx:%s(%s-%s)", vm->cname(), src_pm->cname(), dst_pm->cname());
269 static inline char *get_mig_task_name(msg_vm_t vm, msg_host_t src_pm, msg_host_t dst_pm, int stage)
271 return bprintf("__task_mig_stage%d:%s(%s-%s)", stage, vm->cname(), src_pm->cname(), dst_pm->cname());
274 struct migration_session {
279 /* The miration_rx process uses mbox_ctl to let the caller of do_migration()
280 * know the completion of the migration. */
282 /* The migration_rx and migration_tx processes use mbox to transfer migration data. */
286 static int migration_rx_fun(int argc, char *argv[])
288 XBT_DEBUG("mig: rx_start");
290 // The structure has been created in the do_migration function and should only be freed in the same place ;)
291 struct migration_session *ms = (migration_session *) MSG_process_get_data(MSG_process_self());
293 bool received_finalize = false;
295 char *finalize_task_name = get_mig_task_name(ms->vm, ms->src_pm, ms->dst_pm, 3);
296 while (!received_finalize) {
297 msg_task_t task = nullptr;
298 int ret = MSG_task_recv(&task, ms->mbox);
301 // An error occurred, clean the code and return
302 // The owner did not change, hence the task should be only destroyed on the other side
303 xbt_free(finalize_task_name);
307 if (strcmp(task->name, finalize_task_name) == 0)
308 received_finalize = 1;
310 MSG_task_destroy(task);
312 xbt_free(finalize_task_name);
314 // Here Stage 1, 2 and 3 have been performed.
315 // Hence complete the migration
317 // Copy the reference to the vm (if SRC crashes now, do_migration will free ms)
318 // This is clearly ugly but I (Adrien) need more time to do something cleaner (actually we should copy the whole ms
319 // structure at the beginning and free it at the end of each function)
320 simgrid::s4u::VirtualMachine* vm = static_cast<simgrid::s4u::VirtualMachine*>(ms->vm);
321 msg_host_t src_pm = ms->src_pm;
322 msg_host_t dst_pm = ms->dst_pm;
324 // Make sure that we cannot get interrupted between the migrate and the resume to not end in an inconsistent state
325 simgrid::simix::kernelImmediate([vm, src_pm, dst_pm]() {
326 /* Update the vm location */
327 /* precopy migration makes the VM temporally paused */
328 xbt_assert(vm->pimpl_vm_->getState() == SURF_VM_STATE_SUSPENDED);
330 /* Update the vm location and resume it */
331 vm->pimpl_vm_->setPm(dst_pm);
332 vm->pimpl_vm_->resume();
336 // Now the VM is running on the new host (the migration is completed) (even if the SRC crash)
337 vm->pimpl_vm_->isMigrating = false;
338 XBT_DEBUG("VM(%s) moved from PM(%s) to PM(%s)", ms->vm->cname(), ms->src_pm->cname(), ms->dst_pm->cname());
340 if (TRACE_msg_vm_is_enabled()) {
341 static long long int counter = 0;
342 char key[INSTR_DEFAULT_STR_SIZE];
343 snprintf(key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
346 container_t msg = PJ_container_get(vm->cname());
347 type_t type = PJ_type_get("MSG_VM_LINK", PJ_type_get_root());
348 new_pajeStartLink(MSG_get_clock(), PJ_container_get_root(), type, msg, "M", key);
350 // destroy existing container of this vm
351 container_t existing_container = PJ_container_get(vm->cname());
352 PJ_container_remove_from_parent(existing_container);
353 PJ_container_free(existing_container);
355 // create new container on the new_host location
356 PJ_container_new(vm->cname(), INSTR_MSG_VM, PJ_container_get(ms->dst_pm->cname()));
359 msg = PJ_container_get(vm->cname());
360 type = PJ_type_get("MSG_VM_LINK", PJ_type_get_root());
361 new_pajeEndLink(MSG_get_clock(), PJ_container_get_root(), type, msg, "M", key);
364 // Inform the SRC that the migration has been correctly performed
366 char *task_name = get_mig_task_name(ms->vm, ms->src_pm, ms->dst_pm, 4);
367 msg_task_t task = MSG_task_create(task_name, 0, 0, nullptr);
368 msg_error_t ret = MSG_task_send(task, ms->mbox_ctl);
369 // xbt_assert(ret == MSG_OK);
370 if(ret == MSG_HOST_FAILURE){
371 // The DST has crashed, this is a problem has the VM since we are not sure whether SRC is considering that the VM
372 // has been correctly migrated on the DST node
373 // TODO What does it mean ? What should we do ?
374 MSG_task_destroy(task);
375 } else if(ret == MSG_TRANSFER_FAILURE){
376 // The SRC has crashed, this is not a problem has the VM has been correctly migrated on the DST node
377 MSG_task_destroy(task);
382 XBT_DEBUG("mig: rx_done");
386 static void start_dirty_page_tracking(msg_vm_t vm)
388 simgrid::vm::VirtualMachineImpl* pimpl = static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_;
390 pimpl->dp_enabled = 1;
395 xbt_dict_cursor_t cursor = nullptr;
396 dirty_page_t dp = nullptr;
397 xbt_dict_foreach (pimpl->dp_objs, cursor, key, dp) {
398 double remaining = MSG_task_get_flops_amount(dp->task);
399 dp->prev_clock = MSG_get_clock();
400 dp->prev_remaining = remaining;
402 // XBT_INFO("%s@%s remaining %f", key, sg_host_name(vm), remaining);
406 static void stop_dirty_page_tracking(msg_vm_t vm)
408 static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->dp_enabled = 0;
411 static double get_computed(char *key, msg_vm_t vm, dirty_page_t dp, double remaining, double clock)
413 double computed = dp->prev_remaining - remaining;
414 double duration = clock - dp->prev_clock;
416 XBT_DEBUG("%s@%s: computed %f ops (remaining %f -> %f) in %f secs (%f -> %f)", key, vm->cname(), computed,
417 dp->prev_remaining, remaining, duration, dp->prev_clock, clock);
422 static double lookup_computed_flop_counts(msg_vm_t vm, int stage_for_fancy_debug, int stage2_round_for_fancy_debug)
424 simgrid::vm::VirtualMachineImpl* pimpl = static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_;
428 xbt_dict_cursor_t cursor = nullptr;
429 dirty_page_t dp = nullptr;
430 xbt_dict_foreach (pimpl->dp_objs, cursor, key, dp) {
431 double remaining = MSG_task_get_flops_amount(dp->task);
433 double clock = MSG_get_clock();
435 // total += calc_updated_pages(key, vm, dp, remaining, clock);
436 total += get_computed(key, vm, dp, remaining, clock);
438 dp->prev_remaining = remaining;
439 dp->prev_clock = clock;
442 total += pimpl->dp_updated_by_deleted_tasks;
444 XBT_DEBUG("mig-stage%d.%d: computed %f flop_counts (including %f by deleted tasks)", stage_for_fancy_debug,
445 stage2_round_for_fancy_debug, total, pimpl->dp_updated_by_deleted_tasks);
447 pimpl->dp_updated_by_deleted_tasks = 0;
452 // TODO Is this code redundant with the information provided by
453 // msg_process_t MSG_process_create(const char *name, xbt_main_func_t code, void *data, msg_host_t host)
454 /** @brief take care of the dirty page tracking, in case we're adding a task to a migrating VM */
455 void MSG_host_add_task(msg_host_t host, msg_task_t task)
457 simgrid::s4u::VirtualMachine* vm = dynamic_cast<simgrid::s4u::VirtualMachine*>(host);
460 simgrid::vm::VirtualMachineImpl* pimpl = static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_;
462 double remaining = MSG_task_get_flops_amount(task);
463 char *key = bprintf("%s-%p", task->name, task);
465 dirty_page_t dp = xbt_new0(s_dirty_page, 1);
467 if (pimpl->dp_enabled) {
468 dp->prev_clock = MSG_get_clock();
469 dp->prev_remaining = remaining;
472 pimpl->dp_objs = xbt_dict_new_homogeneous(nullptr);
473 xbt_assert(xbt_dict_get_or_null(pimpl->dp_objs, key) == nullptr);
474 xbt_dict_set(pimpl->dp_objs, key, dp, nullptr);
475 XBT_DEBUG("add %s on %s (remaining %f, dp_enabled %d)", key, host->cname(), remaining, pimpl->dp_enabled);
480 void MSG_host_del_task(msg_host_t host, msg_task_t task)
482 simgrid::s4u::VirtualMachine* vm = dynamic_cast<simgrid::s4u::VirtualMachine*>(host);
485 simgrid::vm::VirtualMachineImpl* pimpl = static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_;
487 char *key = bprintf("%s-%p", task->name, task);
488 dirty_page_t dp = (dirty_page_t)(pimpl->dp_objs ? xbt_dict_get_or_null(pimpl->dp_objs, key) : NULL);
489 xbt_assert(dp->task == task);
491 /* If we are in the middle of dirty page tracking, we record how much computation has been done until now, and keep
492 * the information for the lookup_() function that will called soon. */
493 if (pimpl->dp_enabled) {
494 double remaining = MSG_task_get_flops_amount(task);
495 double clock = MSG_get_clock();
496 // double updated = calc_updated_pages(key, host, dp, remaining, clock);
497 double updated = get_computed(key, host, dp, remaining, clock);
499 pimpl->dp_updated_by_deleted_tasks += updated;
502 xbt_dict_remove(pimpl->dp_objs, key);
505 XBT_DEBUG("del %s on %s", key, host->cname());
509 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, char* mbox,
510 int stage, int stage2_round, double mig_speed, double timeout)
513 char *task_name = get_mig_task_name(vm, src_pm, dst_pm, stage);
514 msg_task_t task = MSG_task_create(task_name, 0, (double)size, nullptr);
518 double clock_sta = MSG_get_clock();
522 ret = MSG_task_send_with_timeout_bounded(task, mbox, timeout, mig_speed);
524 ret = MSG_task_send(task, mbox);
530 } else if (ret == MSG_TIMEOUT) {
531 sg_size_t remaining = (sg_size_t)MSG_task_get_remaining_communication(task);
532 sent = size - remaining;
533 XBT_VERB("timeout (%lf s) in sending_migration_data, remaining %llu bytes of %llu", timeout, remaining, size);
536 /* FIXME: why try-and-catch is used here? */
537 if(ret == MSG_HOST_FAILURE){
538 //XBT_DEBUG("SRC host failed during migration of %s (stage %d)", sg_host_name(vm), stage);
539 MSG_task_destroy(task);
540 THROWF(host_error, 0, "SRC host failed during migration of %s (stage %d)", vm->cname(), stage);
541 }else if(ret == MSG_TRANSFER_FAILURE){
542 //XBT_DEBUG("DST host failed during migration of %s (stage %d)", sg_host_name(vm), stage);
543 MSG_task_destroy(task);
544 THROWF(host_error, 0, "DST host failed during migration of %s (stage %d)", vm->cname(), stage);
547 double clock_end = MSG_get_clock();
548 double duration = clock_end - clock_sta;
549 double actual_speed = size / duration;
552 XBT_DEBUG("mig-stage%d.%d: sent %llu duration %f actual_speed %f (target %f)", stage, stage2_round, size, duration,
553 actual_speed, mig_speed);
555 XBT_DEBUG("mig-stage%d: sent %llu duration %f actual_speed %f (target %f)", stage, size, duration, actual_speed,
561 static sg_size_t get_updated_size(double computed, double dp_rate, double dp_cap)
563 double updated_size = computed * dp_rate;
564 XBT_DEBUG("updated_size %f dp_rate %f", updated_size, dp_rate);
565 if (updated_size > dp_cap) {
566 // XBT_INFO("mig-stage2.%d: %f bytes updated, but cap it with the working set size %f", stage2_round, updated_size,
568 updated_size = dp_cap;
571 return (sg_size_t) updated_size;
574 static int migration_tx_fun(int argc, char *argv[])
576 XBT_DEBUG("mig: tx_start");
578 // Note that the ms structure has been allocated in do_migration and hence should be freed in the same function ;)
579 migration_session *ms = (migration_session *) MSG_process_get_data(MSG_process_self());
581 s_vm_params_t params;
582 static_cast<simgrid::s4u::VirtualMachine*>(ms->vm)->parameters(¶ms);
583 const sg_size_t ramsize = params.ramsize;
584 const sg_size_t devsize = params.devsize;
585 const int skip_stage1 = params.skip_stage1;
586 int skip_stage2 = params.skip_stage2;
587 const double dp_rate = params.dp_rate;
588 const double dp_cap = params.dp_cap;
589 const double mig_speed = params.mig_speed;
590 double max_downtime = params.max_downtime;
592 double mig_timeout = 10000000.0;
594 double remaining_size = (double) (ramsize + devsize);
595 double threshold = 0.0;
597 /* check parameters */
599 XBT_WARN("migrate a VM, but ramsize is zero");
601 if (max_downtime <= 0) {
602 XBT_WARN("use the default max_downtime value 30ms");
606 /* Stage1: send all memory pages to the destination. */
607 XBT_DEBUG("mig-stage1: remaining_size %f", remaining_size);
608 start_dirty_page_tracking(ms->vm);
610 double computed_during_stage1 = 0;
612 double clock_prev_send = MSG_get_clock();
615 /* At stage 1, we do not need timeout. We have to send all the memory pages even though the duration of this
616 * transfer exceeds the timeout value. */
617 XBT_VERB("Stage 1: Gonna send %llu", ramsize);
618 sg_size_t sent = send_migration_data(ms->vm, ms->src_pm, ms->dst_pm, ramsize, ms->mbox, 1, 0, mig_speed, -1);
619 remaining_size -= sent;
620 computed_during_stage1 = lookup_computed_flop_counts(ms->vm, 1, 0);
622 if (sent < ramsize) {
623 XBT_VERB("mig-stage1: timeout, force moving to stage 3");
625 } else if (sent > ramsize)
630 //hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
631 // Stop the dirty page tracking an return (there is no memory space to release)
632 stop_dirty_page_tracking(ms->vm);
636 double clock_post_send = MSG_get_clock();
637 mig_timeout -= (clock_post_send - clock_prev_send);
638 if (mig_timeout < 0) {
639 XBT_VERB("The duration of stage 1 exceeds the timeout value, skip stage 2");
643 /* estimate bandwidth */
644 double bandwidth = ramsize / (clock_post_send - clock_prev_send);
645 threshold = bandwidth * max_downtime;
646 XBT_DEBUG("actual bandwidth %f (MB/s), threshold %f", bandwidth / 1024 / 1024, threshold);
650 /* Stage2: send update pages iteratively until the size of remaining states becomes smaller than threshold value. */
653 int stage2_round = 0;
656 sg_size_t updated_size = 0;
657 if (stage2_round == 0) {
658 /* just after stage1, nothing has been updated. But, we have to send the data updated during stage1 */
659 updated_size = get_updated_size(computed_during_stage1, dp_rate, dp_cap);
661 double computed = lookup_computed_flop_counts(ms->vm, 2, stage2_round);
662 updated_size = get_updated_size(computed, dp_rate, dp_cap);
665 XBT_DEBUG("mig-stage 2:%d updated_size %llu computed_during_stage1 %f dp_rate %f dp_cap %f", stage2_round,
666 updated_size, computed_during_stage1, dp_rate, dp_cap);
668 /* Check whether the remaining size is below the threshold value. If so, move to stage 3. */
669 remaining_size += updated_size;
670 XBT_DEBUG("mig-stage2.%d: remaining_size %f (%s threshold %f)", stage2_round, remaining_size,
671 (remaining_size < threshold) ? "<" : ">", threshold);
672 if (remaining_size < threshold)
676 double clock_prev_send = MSG_get_clock();
678 XBT_DEBUG("Stage 2, gonna send %llu", updated_size);
679 sent = send_migration_data(ms->vm, ms->src_pm, ms->dst_pm, updated_size, ms->mbox, 2, stage2_round, mig_speed,
681 } catch (xbt_ex& e) {
682 // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data
684 // Stop the dirty page tracking an return (there is no memory space to release)
685 stop_dirty_page_tracking(ms->vm);
688 double clock_post_send = MSG_get_clock();
690 if (sent == updated_size) {
691 /* timeout did not happen */
692 double bandwidth = updated_size / (clock_post_send - clock_prev_send);
693 threshold = bandwidth * max_downtime;
694 XBT_DEBUG("actual bandwidth %f, threshold %f", bandwidth / 1024 / 1024, threshold);
695 remaining_size -= sent;
697 mig_timeout -= (clock_post_send - clock_prev_send);
698 xbt_assert(mig_timeout > 0);
700 } else if (sent < updated_size) {
701 /* When timeout happens, we move to stage 3. The size of memory pages
702 * updated before timeout must be added to the remaining size. */
703 XBT_VERB("mig-stage2.%d: timeout, force moving to stage 3. sent %llu / %llu, eta %lf", stage2_round, sent,
704 updated_size, (clock_post_send - clock_prev_send));
705 remaining_size -= sent;
707 double computed = lookup_computed_flop_counts(ms->vm, 2, stage2_round);
708 updated_size = get_updated_size(computed, dp_rate, dp_cap);
709 remaining_size += updated_size;
716 /* Stage3: stop the VM and copy the rest of states. */
717 XBT_DEBUG("mig-stage3: remaining_size %f", remaining_size);
718 simgrid::vm::VirtualMachineImpl* pimpl = static_cast<simgrid::s4u::VirtualMachine*>(ms->vm)->pimpl_vm_;
719 pimpl->setState(SURF_VM_STATE_RUNNING); // FIXME: this bypass of the checks in suspend() is not nice
720 pimpl->isMigrating = false; // FIXME: this bypass of the checks in suspend() is not nice
721 pimpl->suspend(SIMIX_process_self());
722 stop_dirty_page_tracking(ms->vm);
725 XBT_DEBUG("Stage 3: Gonna send %f", remaining_size);
726 send_migration_data(ms->vm, ms->src_pm, ms->dst_pm, (sg_size_t)remaining_size, ms->mbox, 3, 0, mig_speed, -1);
729 //hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
730 // Stop the dirty page tracking an return (there is no memory space to release)
731 static_cast<simgrid::s4u::VirtualMachine*>(ms->vm)->pimpl_vm_->resume();
735 // At that point the Migration is considered valid for the SRC node but remind that the DST side should relocate
736 // effectively the VM on the DST node.
737 XBT_DEBUG("mig: tx_done");
742 /** @brief Migrate the VM to the given host.
745 void MSG_vm_migrate(msg_vm_t vm, msg_host_t dst_pm)
748 * - One approach is ...
749 * We first create a new VM (i.e., destination VM) on the destination physical host. The destination VM will
750 * receive the state of the source
751 * VM over network. We will finally destroy the source VM.
752 * - This behavior is similar to the way of migration in the real world.
753 * Even before a migration is completed, we will see a destination VM, consuming resources.
754 * - We have to relocate all processes. The existing process migration code will work for this?
755 * - The name of the VM is a somewhat unique ID in the code. It is tricky for the destination VM?
757 * - Another one is ...
758 * We update the information of the given VM to place it to the destination physical host.
760 * The second one would be easier.
763 simgrid::s4u::VirtualMachine* typedVm = static_cast<simgrid::s4u::VirtualMachine*>(vm);
764 simgrid::vm::VirtualMachineImpl* pimpl = typedVm->pimpl_vm_;
765 msg_host_t src_pm = pimpl->getPm();
768 THROWF(vm_error, 0, "Cannot migrate VM '%s' from host '%s', which is offline.", vm->cname(), src_pm->cname());
770 THROWF(vm_error, 0, "Cannot migrate VM '%s' to host '%s', which is offline.", vm->cname(), dst_pm->cname());
771 if (!MSG_vm_is_running(vm))
772 THROWF(vm_error, 0, "Cannot migrate VM '%s' that is not running yet.", vm->cname());
773 if (typedVm->isMigrating())
774 THROWF(vm_error, 0, "Cannot migrate VM '%s' that is already migrating.", vm->cname());
776 pimpl->isMigrating = true;
778 struct migration_session *ms = xbt_new(struct migration_session, 1);
783 /* We have two mailboxes. mbox is used to transfer migration data between source and destination PMs. mbox_ctl is used
784 * to detect the completion of a migration. The names of these mailboxes must not conflict with others. */
785 ms->mbox_ctl = bprintf("__mbox_mig_ctl:%s(%s-%s)", vm->cname(), src_pm->cname(), dst_pm->cname());
786 ms->mbox = bprintf("__mbox_mig_src_dst:%s(%s-%s)", vm->cname(), src_pm->cname(), dst_pm->cname());
788 char *pr_rx_name = get_mig_process_rx_name(vm, src_pm, dst_pm);
789 char *pr_tx_name = get_mig_process_tx_name(vm, src_pm, dst_pm);
791 char** argv = xbt_new(char*, 2);
792 argv[0] = pr_rx_name;
794 MSG_process_create_with_arguments(pr_rx_name, migration_rx_fun, ms, dst_pm, 1, argv);
796 argv = xbt_new(char*, 2);
797 argv[0] = pr_tx_name;
799 MSG_process_create_with_arguments(pr_tx_name, migration_tx_fun, ms, src_pm, 1, argv);
801 /* wait until the migration have finished or on error has occurred */
802 XBT_DEBUG("wait for reception of the final ACK (i.e. migration has been correctly performed");
803 msg_task_t task = nullptr;
804 msg_error_t ret = MSG_TIMEOUT;
805 while (ret == MSG_TIMEOUT && dst_pm->isOn()) // The rx will tell me when he gots the VM
806 ret = MSG_task_receive_with_timeout(&task, ms->mbox_ctl, 4);
808 pimpl->isMigrating = false;
810 xbt_free(ms->mbox_ctl);
814 if (ret == MSG_HOST_FAILURE) {
815 // Note that since the communication failed, the owner did not change and the task should be destroyed on the
816 // other side. Hence, just throw the execption
817 XBT_ERROR("SRC crashes, throw an exception (m-control)");
818 // MSG_process_kill(tx_process); // Adrien, I made a merge on Nov 28th 2014, I'm not sure whether this line is
820 THROWF(host_error, 0, "Source host '%s' failed during the migration of VM '%s'.", src_pm->cname(), vm->cname());
821 } else if ((ret == MSG_TRANSFER_FAILURE) || (ret == MSG_TIMEOUT)) {
822 // MSG_TIMEOUT here means that MSG_host_is_avail() returned false.
823 XBT_ERROR("DST crashes, throw an exception (m-control)");
824 THROWF(host_error, 0, "Destination host '%s' failed during the migration of VM '%s'.", dst_pm->cname(),
828 char* expected_task_name = get_mig_task_name(vm, src_pm, dst_pm, 4);
829 xbt_assert(strcmp(task->name, expected_task_name) == 0);
830 xbt_free(expected_task_name);
831 MSG_task_destroy(task);
834 /** @brief Immediately suspend the execution of all processes within the given VM.
837 * This function stops the execution of the VM. All the processes on this VM
838 * will pause. The state of the VM is preserved. We can later resume it again.
840 * No suspension cost occurs.
842 void MSG_vm_suspend(msg_vm_t vm)
844 smx_actor_t issuer = SIMIX_process_self();
845 simgrid::simix::kernelImmediate([vm,issuer]() {
846 static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->suspend(issuer);
849 XBT_DEBUG("vm_suspend done");
851 if (TRACE_msg_vm_is_enabled()) {
852 container_t vm_container = PJ_container_get(vm->cname());
853 type_t type = PJ_type_get("MSG_VM_STATE", vm_container->type);
854 val_t value = PJ_value_get_or_new("suspend", "1 0 0", type); // suspend is red
855 new_pajePushState(MSG_get_clock(), vm_container, type, value);
859 /** @brief Resume the execution of the VM. All processes on the VM run again.
862 * No resume cost occurs.
864 void MSG_vm_resume(msg_vm_t vm)
866 static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->resume();
868 if (TRACE_msg_vm_is_enabled()) {
869 container_t vm_container = PJ_container_get(vm->cname());
870 type_t type = PJ_type_get("MSG_VM_STATE", vm_container->type);
871 new_pajePopState(MSG_get_clock(), vm_container, type);
876 /** @brief Immediately save the execution of all processes within the given VM.
879 * This function stops the execution of the VM. All the processes on this VM
880 * will pause. The state of the VM is preserved. We can later resume it again.
882 * FIXME: No suspension cost occurs. If you want to simulate this too, you want to use a \ref MSG_file_write() before
883 * or after, depending on the exact semantic of VM save to you.
885 void MSG_vm_save(msg_vm_t vm)
887 smx_actor_t issuer=SIMIX_process_self();
888 simgrid::simix::kernelImmediate([vm,issuer]() {
889 static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->save(issuer);
892 if (TRACE_msg_vm_is_enabled()) {
893 container_t vm_container = PJ_container_get(vm->cname());
894 type_t type = PJ_type_get("MSG_VM_STATE", vm_container->type);
895 val_t value = PJ_value_get_or_new("save", "0 1 0", type); // save is green
896 new_pajePushState(MSG_get_clock(), vm_container, type, value);
900 /** @brief Restore the execution of the VM. All processes on the VM run again.
903 * FIXME: No restore cost occurs. If you want to simulate this too, you want to use a \ref MSG_file_read() before or
904 * after, depending on the exact semantic of VM restore to you.
906 void MSG_vm_restore(msg_vm_t vm)
908 simgrid::simix::kernelImmediate([vm]() {
909 static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->restore();
912 if (TRACE_msg_vm_is_enabled()) {
913 container_t vm_container = PJ_container_get(vm->cname());
914 type_t type = PJ_type_get("MSG_VM_STATE", vm_container->type);
915 new_pajePopState(MSG_get_clock(), vm_container, type);
919 /** @brief Get the physical host of a given VM.
922 msg_host_t MSG_vm_get_pm(msg_vm_t vm)
924 return static_cast<simgrid::s4u::VirtualMachine*>(vm)->pm();
927 /** @brief Set a CPU bound for a given VM.
930 * 1. Note that in some cases MSG_task_set_bound() may not intuitively work for VMs.
933 * On PM0, there are Task1 and VM0.
934 * On VM0, there is Task2.
935 * Now we bound 75% to Task1\@PM0 and bound 25% to Task2\@VM0.
937 * Task1\@PM0 gets 50%.
938 * Task2\@VM0 gets 25%.
939 * This is NOT 75% for Task1\@PM0 and 25% for Task2\@VM0, respectively.
941 * 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
942 * the dummy CPU action. The bound of the dummy CPU action is unlimited.
944 * There are some solutions for this problem. One option is to update the bound of the dummy CPU action automatically.
945 * It should be the sum of all tasks on the VM. But, this solution might be costly, because we have to scan all tasks
946 * on the VM in share_resource() or we have to trap both the start and end of task execution.
948 * The current solution is to use MSG_vm_set_bound(), which allows us to directly set the bound of the dummy CPU action.
950 * 2. Note that bound == 0 means no bound (i.e., unlimited). But, if a host has multiple CPU cores, the CPU share of a
951 * computation task (or a VM) never exceeds the capacity of a CPU core.
953 void MSG_vm_set_bound(msg_vm_t vm, double bound)
955 simgrid::simix::kernelImmediate(
956 [vm, bound]() { static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->setBound(bound); });