+ total += priv->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, priv->dp_updated_by_deleted_tasks);
+
+
+
+ priv->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)
+void MSG_host_add_task(msg_host_t host, msg_task_t task)
+{
+ msg_host_priv_t priv = msg_host_resource_priv(host);
+ double remaining = MSG_task_get_remaining_computation(task);
+ char *key = bprintf("%s-%p", task->name, task);
+
+ dirty_page_t dp = xbt_new0(s_dirty_page, 1);
+ dp->task = task;
+
+ /* It should be okay that we add a task onto a migrating VM. */
+ if (priv->dp_enabled) {
+ dp->prev_clock = MSG_get_clock();
+ dp->prev_remaining = remaining;
+ }
+
+ xbt_assert(xbt_dict_get_or_null(priv->dp_objs, key) == NULL);
+ xbt_dict_set(priv->dp_objs, key, dp, NULL);
+ XBT_DEBUG("add %s on %s (remaining %f, dp_enabled %d)", key, sg_host_name(host), remaining, priv->dp_enabled);
+
+ xbt_free(key);
+}
+
+void MSG_host_del_task(msg_host_t host, msg_task_t task)
+{
+ msg_host_priv_t priv = msg_host_resource_priv(host);
+
+ char *key = bprintf("%s-%p", task->name, task);
+
+ dirty_page_t dp = xbt_dict_get_or_null(priv->dp_objs, key);
+ xbt_assert(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 (priv->dp_enabled) {
+ double remaining = MSG_task_get_remaining_computation(task);
+ double clock = MSG_get_clock();
+ // double updated = calc_updated_pages(key, host, dp, remaining, clock);
+ double updated = get_computed(key, host, dp, remaining, clock);
+
+ priv->dp_updated_by_deleted_tasks += updated;
+ }
+
+ xbt_dict_remove(priv->dp_objs, key);
+ xbt_free(dp);
+
+ XBT_DEBUG("del %s on %s", key, sg_host_name(host));
+
+ xbt_free(key);
+}
+
+
+
+
+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, int stage, int stage2_round, double mig_speed, double timeout)
+{
+ sg_size_t sent = 0;
+ char *task_name = get_mig_task_name(vm, src_pm, dst_pm, stage);
+ msg_task_t task = MSG_task_create(task_name, 0, size, NULL);
+
+ /* TODO: clean up */
+
+ double clock_sta = MSG_get_clock();
+
+ msg_error_t ret;
+ if (mig_speed > 0)
+ ret = MSG_task_send_with_timeout_bounded(task, mbox, timeout, mig_speed);
+ else
+ ret = MSG_task_send(task, mbox);
+
+ xbt_free(task_name);
+
+ if (ret == MSG_OK) {
+ sent = size;
+ } else if (ret == MSG_TIMEOUT) {
+ sg_size_t remaining = MSG_task_get_remaining_communication(task);
+ sent = size - remaining;
+ XBT_INFO("timeout (%lf s) in sending_migration_data, remaining %llu bytes of %llu",
+ timeout, remaining, size);
+ }
+
+ /* FIXME: why try-and-catch is used here? */
+ if(ret == MSG_HOST_FAILURE){
+ //XBT_INFO("SRC host failed during migration of %s (stage %d)", sg_host_name(vm), stage);
+ MSG_task_destroy(task);
+ THROWF(host_error, 0, "SRC host failed during migration of %s (stage %d)", sg_host_name(vm), stage);
+ }else if(ret == MSG_TRANSFER_FAILURE){
+ //XBT_INFO("DST host failed during migration of %s (stage %d)", sg_host_name(vm), stage);
+ MSG_task_destroy(task);
+ THROWF(host_error, 0, "DST host failed during migration of %s (stage %d)", sg_host_name(vm), stage);
+ }
+
+ double clock_end = MSG_get_clock();
+ double duration = clock_end - clock_sta;
+ double actual_speed = size / duration;
+
+ if (stage == 2)
+ XBT_DEBUG("mig-stage%d.%d: sent %llu duration %f actual_speed %f (target %f)", stage, stage2_round, size, duration, actual_speed, mig_speed);
+ else
+ XBT_DEBUG("mig-stage%d: sent %llu duration %f actual_speed %f (target %f)", stage, size, duration, actual_speed, mig_speed);
+
+ return sent;
+}
+
+static sg_size_t get_updated_size(double computed, double dp_rate, double dp_cap)
+{
+ double updated_size = computed * dp_rate;
+ XBT_DEBUG("updated_size %f dp_rate %f", updated_size, dp_rate);
+ if (updated_size > dp_cap) {
+ // XBT_INFO("mig-stage2.%d: %f bytes updated, but cap it with the working set size %f", stage2_round, updated_size, dp_cap);
+ updated_size = dp_cap;
+ }
+
+ return (sg_size_t) updated_size;
+}
+
+static double send_stage1(struct migration_session *ms,
+ sg_size_t ramsize, double mig_speed, double dp_rate, double dp_cap)
+{
+
+ // const long chunksize = (sg_size_t)1024 * 1024 * 100;
+ const sg_size_t chunksize = (sg_size_t)1024 * 1024 * 100000;
+ sg_size_t remaining = ramsize;
+ double computed_total = 0;
+
+ while (remaining > 0) {
+ sg_size_t datasize = chunksize;
+ if (remaining < chunksize)
+ datasize = remaining;
+
+ remaining -= datasize;
+ send_migration_data(ms->vm, ms->src_pm, ms->dst_pm, datasize, ms->mbox, 1, 0, mig_speed, -1);
+ double computed = lookup_computed_flop_counts(ms->vm, 1, 0);
+ computed_total += computed;
+ }
+
+ return computed_total;
+}
+
+
+
+static double get_threshold_value(double bandwidth, double max_downtime)
+{
+ return max_downtime * bandwidth;
+}
+
+static int migration_tx_fun(int argc, char *argv[])
+{
+ XBT_DEBUG("mig: tx_start");
+
+ // Note that the ms structure has been allocated in do_migration and hence should be freed in the same function ;)
+ struct migration_session *ms = MSG_process_get_data(MSG_process_self());
+
+ s_ws_params_t params;
+ simcall_host_get_params(ms->vm, ¶ms);
+ const sg_size_t ramsize = params.ramsize;
+ const sg_size_t devsize = params.devsize;
+ const int skip_stage1 = params.skip_stage1;
+ int skip_stage2 = params.skip_stage2;
+ const double dp_rate = params.dp_rate;
+ const double dp_cap = params.dp_cap;
+ const double mig_speed = params.mig_speed;
+ double max_downtime = params.max_downtime;
+
+ /* hard code it temporally. Fix Me */
+#define MIGRATION_TIMEOUT_DO_NOT_HARDCODE_ME 10000000.0
+ double mig_timeout = MIGRATION_TIMEOUT_DO_NOT_HARDCODE_ME;
+
+ double remaining_size = ramsize + devsize;
+ double threshold;
+
+ /* check parameters */
+ if (ramsize == 0)
+ XBT_WARN("migrate a VM, but ramsize is zero");
+
+ if (max_downtime == 0) {
+ XBT_WARN("use the default max_downtime value 30ms");
+ max_downtime = 0.03;
+ }
+
+ /* Stage1: send all memory pages to the destination. */
+ XBT_DEBUG("mig-stage1: remaining_size %f", remaining_size);
+ start_dirty_page_tracking(ms->vm);
+
+ double computed_during_stage1 = 0;
+ if (!skip_stage1) {
+ double clock_prev_send = MSG_get_clock();
+
+ TRY {
+ /* At stage 1, we do not need timeout. We have to send all the memory
+ * pages even though the duration of this tranfer exceeds the timeout
+ * value. */
+ 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);
+
+ if (sent < ramsize) {
+ XBT_INFO("mig-stage1: timeout, force moving to stage 3");
+ skip_stage2 = 1;
+ } else if (sent > ramsize)
+ XBT_CRITICAL("bug");
+
+ } CATCH_ANONYMOUS {
+ //hostfailure (if you want to know whether this is the SRC or the DST please 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);
+ return 0;
+ }
+
+ double clock_post_send = MSG_get_clock();
+ mig_timeout -= (clock_post_send - clock_prev_send);
+ if (mig_timeout < 0) {
+ XBT_INFO("The duration of stage 1 exceeds the timeout value (%lf > %lf), skip stage 2",
+ (clock_post_send - clock_prev_send), MIGRATION_TIMEOUT_DO_NOT_HARDCODE_ME);
+ skip_stage2 = 1;
+ }
+
+ /* estimate bandwidth */
+ double bandwidth = ramsize / (clock_post_send - clock_prev_send);
+ threshold = get_threshold_value(bandwidth, max_downtime);
+ XBT_DEBUG("actual bandwidth %f (MB/s), threshold %f", bandwidth / 1024 / 1024, threshold);