From bbeb90fbb39aeb654cb7b58ffc279e8823d40853 Mon Sep 17 00:00:00 2001 From: Takahiro Hirofuchi Date: Mon, 11 Mar 2013 13:48:52 +0100 Subject: [PATCH 1/1] Add migrate_vm example. --- examples/msg/cloud/CMakeLists.txt | 3 + .../msg/cloud/masterslave_virtual_machines.c | 2 +- examples/msg/cloud/migrate_vm.c | 163 ++++++++++++++++++ examples/msg/cloud/simple_vm.c | 5 +- src/msg/msg_vm.c | 12 +- 5 files changed, 178 insertions(+), 7 deletions(-) create mode 100644 examples/msg/cloud/migrate_vm.c diff --git a/examples/msg/cloud/CMakeLists.txt b/examples/msg/cloud/CMakeLists.txt index 52a22868dd..a19ba8bd20 100644 --- a/examples/msg/cloud/CMakeLists.txt +++ b/examples/msg/cloud/CMakeLists.txt @@ -4,10 +4,12 @@ set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}") add_executable(masterslave_virtual_machines "masterslave_virtual_machines.c") add_executable(simple_vm "simple_vm.c") +add_executable(migrate_vm "migrate_vm.c") ### Add definitions for compile target_link_libraries(masterslave_virtual_machines simgrid) target_link_libraries(simple_vm simgrid) +target_link_libraries(migrate_vm simgrid) set(tesh_files ${tesh_files} @@ -24,6 +26,7 @@ set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/masterslave_virtual_machines.c ${CMAKE_CURRENT_SOURCE_DIR}/simple_vm.c + ${CMAKE_CURRENT_SOURCE_DIR}/migrate_vm.c PARENT_SCOPE ) set(bin_files diff --git a/examples/msg/cloud/masterslave_virtual_machines.c b/examples/msg/cloud/masterslave_virtual_machines.c index 4a6c6573b7..e0d4374f26 100644 --- a/examples/msg/cloud/masterslave_virtual_machines.c +++ b/examples/msg/cloud/masterslave_virtual_machines.c @@ -245,7 +245,7 @@ int main(int argc, char *argv[]) MSG_process_create_with_arguments("master", master_fun, NULL, master_pm, nb_hosts + 1, master_argv); msg_error_t res = MSG_main(); - XBT_INFO("Simulation time %g", MSG_get_clock()); + XBT_INFO("Bye (simulation time %g)", MSG_get_clock()); xbt_dynar_free(&hosts_dynar); diff --git a/examples/msg/cloud/migrate_vm.c b/examples/msg/cloud/migrate_vm.c new file mode 100644 index 0000000000..0ada43f10d --- /dev/null +++ b/examples/msg/cloud/migrate_vm.c @@ -0,0 +1,163 @@ +/* Copyright (c) 2007-2012. 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 +#include "msg/msg.h" +#include "xbt/sysdep.h" /* calloc, printf */ + +/* Create a log channel to have nice outputs. */ +#include "xbt/log.h" +#include "xbt/asserts.h" +XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, + "Messages specific for this msg example"); + + +void vm_migrate(msg_vm_t vm, msg_host_t dst_pm) +{ + msg_host_t src_pm = MSG_vm_get_pm(vm); + double mig_sta = MSG_get_clock(); + MSG_vm_migrate(vm, dst_pm); + double mig_end = MSG_get_clock(); + + XBT_INFO("%s migrated: %s->%s in %g s", MSG_vm_get_name(vm), + MSG_host_get_name(src_pm), MSG_host_get_name(dst_pm), + mig_end - mig_sta); +} + +int migration_worker_main(int argc, char *argv[]) +{ + xbt_assert(argc == 3); + char *vm_name = argv[1]; + char *dst_pm_name = argv[2]; + + msg_vm_t vm = MSG_get_host_by_name(vm_name); + msg_host_t dst_pm = MSG_get_host_by_name(dst_pm_name); + + vm_migrate(vm, dst_pm); + + return 0; +} + +void vm_migrate_async(msg_vm_t vm, msg_host_t dst_pm) +{ + const char *vm_name = MSG_vm_get_name(vm); + const char *dst_pm_name = MSG_host_get_name(dst_pm); + msg_host_t host = MSG_host_self(); + + const char *pr_name = "mig_wrk"; + char **argv = xbt_new(char *, 4); + argv[0] = xbt_strdup(pr_name); + argv[1] = xbt_strdup(vm_name); + argv[2] = xbt_strdup(dst_pm_name); + argv[3] = NULL; + + MSG_process_create_with_arguments(pr_name, migration_worker_main, NULL, host, 3, argv); +} + +int master_main(int argc, char *argv[]) +{ + xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar(); + msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t); + msg_host_t pm1 = xbt_dynar_get_as(hosts_dynar, 1, msg_host_t); + msg_host_t pm2 = xbt_dynar_get_as(hosts_dynar, 2, msg_host_t); + msg_vm_t vm0, vm1; + s_ws_params_t params; + memset(¶ms, 0, sizeof(params)); + + + + vm0 = MSG_vm_create_core(pm0, "VM0"); + params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes + MSG_host_set_params(vm0, ¶ms); + MSG_vm_start(vm0); + + XBT_INFO("Test: Migrate a VM with %ld Mbytes RAM", params.ramsize / 1000 / 1000); + vm_migrate(vm0, pm1); + + MSG_vm_destroy(vm0); + + + + vm0 = MSG_vm_create_core(pm0, "VM0"); + params.ramsize = 1L * 1000 * 1000 * 100; // 100Mbytes + MSG_host_set_params(vm0, ¶ms); + MSG_vm_start(vm0); + + XBT_INFO("Test: Migrate a VM with %ld Mbytes RAM", params.ramsize / 1000 / 1000); + vm_migrate(vm0, pm1); + + MSG_vm_destroy(vm0); + + + + vm0 = MSG_vm_create_core(pm0, "VM0"); + vm1 = MSG_vm_create_core(pm0, "VM1"); + + params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes + MSG_host_set_params(vm0, ¶ms); + MSG_host_set_params(vm1, ¶ms); + MSG_vm_start(vm0); + MSG_vm_start(vm1); + + XBT_INFO("Test: Migrate two VMs at once from PM0 to PM1"); + vm_migrate_async(vm0, pm1); + vm_migrate_async(vm1, pm1); + MSG_process_sleep(10000); + + MSG_vm_destroy(vm0); + MSG_vm_destroy(vm1); + + + + vm0 = MSG_vm_create_core(pm0, "VM0"); + vm1 = MSG_vm_create_core(pm0, "VM1"); + + params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes + MSG_host_set_params(vm0, ¶ms); + MSG_host_set_params(vm1, ¶ms); + MSG_vm_start(vm0); + MSG_vm_start(vm1); + + XBT_INFO("Test: Migrate two VMs at once to different PMs"); + vm_migrate_async(vm0, pm1); + vm_migrate_async(vm1, pm2); + MSG_process_sleep(10000); + + MSG_vm_destroy(vm0); + MSG_vm_destroy(vm1); + + + return 0; +} + +void launch_master(msg_host_t host) +{ + const char *pr_name = "master_"; + char **argv = xbt_new(char *, 2); + argv[0] = xbt_strdup(pr_name); + argv[1] = NULL; + + MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv); +} + + +int main(int argc, char *argv[]) +{ + /* Get the arguments */ + MSG_init(&argc, argv); + + /* load the platform file */ + MSG_create_environment(argv[1]); + + xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar(); + msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t); + launch_master(pm0); + + int res = MSG_main(); + XBT_INFO("Bye (simulation time %g)", MSG_get_clock()); + + + return !(res == MSG_OK); +} diff --git a/examples/msg/cloud/simple_vm.c b/examples/msg/cloud/simple_vm.c index 16b4844aff..548cf70f2b 100644 --- a/examples/msg/cloud/simple_vm.c +++ b/examples/msg/cloud/simple_vm.c @@ -290,9 +290,8 @@ int main(int argc, char *argv[]) launch_master(pm0); int res = MSG_main(); - XBT_INFO("Simulation time %g", MSG_get_clock()); + XBT_INFO("Bye (simulation time %g)", MSG_get_clock()); - XBT_INFO("bye"); - return 0; + return !(res == MSG_OK); } diff --git a/src/msg/msg_vm.c b/src/msg/msg_vm.c index 29d79721a6..d4f97c6ea5 100644 --- a/src/msg/msg_vm.c +++ b/src/msg/msg_vm.c @@ -280,7 +280,7 @@ static int migration_rx_fun(int argc, char *argv[]) { - msg_task_t task = MSG_task_create("fin", 0, 0, NULL); + msg_task_t task = MSG_task_create("quit_migration", 0, 0, NULL); msg_error_t ret = MSG_task_send(task, mbox_ctl); xbt_assert(ret == MSG_OK); } @@ -322,9 +322,13 @@ static void create_dummy_task(msg_vm_t vm, msg_host_t old_pm, msg_host_t new_pm, if (ramsize == 0) XBT_WARN("migrate a VM, but ramsize is zero"); + /* We have two mailboxes. mbox is used to transfer migration data between + * source and destiantion PMs. mbox_ctl is used to detect the completion of a + * migration. The names of these mailboxes must not conflict with others. */ + char *suffix = bprintf("mig-%s(%s-%s)", vm->key, old_pm->key, new_pm->key); - char *mbox = bprintf("MBOX:%s", suffix); - char *mbox_ctl = bprintf("MBOX:%s:CTL", suffix); + char *mbox = bprintf("__MBOX:%s", suffix); + char *mbox_ctl = bprintf("__MBOX:%s:CTL", suffix); { const char *pr_name = "mig_tx"; @@ -350,10 +354,12 @@ static void create_dummy_task(msg_vm_t vm, msg_host_t old_pm, msg_host_t new_pm, msg_process_t pr = MSG_process_create_with_arguments(pr_name, migration_rx_fun, NULL, new_pm, nargvs - 1, argv); } + /* wait until the migration have finished */ { msg_task_t task = NULL; msg_error_t ret = MSG_task_recv(&task, mbox_ctl); xbt_assert(ret == MSG_OK); + xbt_assert(strcmp(task->name, "quit_migration") == 0); } xbt_free(suffix); -- 2.20.1