From 57de8a5ae71fe7d566821f51e9d6a72c7464296f Mon Sep 17 00:00:00 2001 From: Takahiro Hirofuchi Date: Thu, 28 Feb 2013 17:11:20 +0100 Subject: [PATCH] Add a simpler example for the VM API FIXME: It might be better that we move this example to the test directory. --- examples/msg/cloud/CMakeLists.txt | 10 +- examples/msg/cloud/simple_plat.xml | 23 +++ examples/msg/cloud/simple_vm.c | 274 +++++++++++++++++++++++++++++ 3 files changed, 302 insertions(+), 5 deletions(-) create mode 100644 examples/msg/cloud/simple_plat.xml create mode 100644 examples/msg/cloud/simple_vm.c diff --git a/examples/msg/cloud/CMakeLists.txt b/examples/msg/cloud/CMakeLists.txt index 534992d591..52a22868dd 100644 --- a/examples/msg/cloud/CMakeLists.txt +++ b/examples/msg/cloud/CMakeLists.txt @@ -3,13 +3,11 @@ cmake_minimum_required(VERSION 2.6) 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 definitions for compile -if(WIN32) - target_link_libraries(masterslave_virtual_machines simgrid ) -else() - target_link_libraries(masterslave_virtual_machines simgrid) -endif() +target_link_libraries(masterslave_virtual_machines simgrid) +target_link_libraries(simple_vm simgrid) set(tesh_files ${tesh_files} @@ -19,11 +17,13 @@ set(tesh_files set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/masterslave_virtual_machines.xml + ${CMAKE_CURRENT_SOURCE_DIR}/simple_plat.xml PARENT_SCOPE ) set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/masterslave_virtual_machines.c + ${CMAKE_CURRENT_SOURCE_DIR}/simple_vm.c PARENT_SCOPE ) set(bin_files diff --git a/examples/msg/cloud/simple_plat.xml b/examples/msg/cloud/simple_plat.xml new file mode 100644 index 0000000000..e6fed2fd14 --- /dev/null +++ b/examples/msg/cloud/simple_plat.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/msg/cloud/simple_vm.c b/examples/msg/cloud/simple_vm.c new file mode 100644 index 0000000000..f6ec0c471f --- /dev/null +++ b/examples/msg/cloud/simple_vm.c @@ -0,0 +1,274 @@ +/* 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"); + + +int computation_fun(int argc, char *argv[]) +{ + const char *pr_name = MSG_process_get_name(MSG_process_self()); + const char *host_name = MSG_host_get_name(MSG_host_self()); + + msg_task_t task = MSG_task_create("Task", 1000000, 1000000, NULL); + + double clock_sta = MSG_get_clock(); + MSG_task_execute(task); + double clock_end = MSG_get_clock(); + + XBT_INFO("%s:%s task executed %g", host_name, pr_name, clock_end - clock_sta); + + MSG_task_destroy(task); + + return 0; +} + +void launch_computation_worker(msg_host_t host) +{ + const char *pr_name = "compute"; + char **argv = xbt_new(char *, 2); + argv[0] = xbt_strdup(pr_name); + argv[1] = NULL; + + MSG_process_create_with_arguments(pr_name, computation_fun, NULL, host, 1, argv); +} + +struct task_priv { + msg_host_t tx_host; + msg_process_t tx_proc; + double clock_sta; +}; + +int communication_tx_fun(int argc, char *argv[]) +{ + xbt_assert(argc == 2); + const char *mbox = argv[1]; + + msg_task_t task = MSG_task_create("Task", 1000000, 1000000, NULL); + + struct task_priv *priv = xbt_new(struct task_priv, 1); + priv->tx_proc = MSG_process_self(); + priv->tx_host = MSG_host_self(); + priv->clock_sta = MSG_get_clock(); + + MSG_task_set_data(task, priv); + + MSG_task_send(task, mbox); + + return 0; +} + +int communication_rx_fun(int argc, char *argv[]) +{ + const char *pr_name = MSG_process_get_name(MSG_process_self()); + const char *host_name = MSG_host_get_name(MSG_host_self()); + xbt_assert(argc == 2); + const char *mbox = argv[1]; + + msg_task_t task = NULL; + MSG_task_recv(&task, mbox); + + struct task_priv *priv = MSG_task_get_data(task); + double clock_end = MSG_get_clock(); + + XBT_INFO("%s:%s to %s:%s => %g sec", + MSG_host_get_name(priv->tx_host), + MSG_process_get_name(priv->tx_proc), + host_name, pr_name, clock_end - priv->clock_sta); + + MSG_task_destroy(task); + + return 0; +} + +void launch_communication_worker(msg_host_t tx_host, msg_host_t rx_host) +{ + char *mbox = bprintf("MBOX:%s-%s", + MSG_host_get_name(tx_host), + MSG_host_get_name(rx_host)); + char **argv = NULL; + char *pr_name = NULL; + + pr_name = "comm_tx"; + argv = xbt_new(char *, 3); + argv[0] = xbt_strdup(pr_name); + argv[1] = xbt_strdup(mbox); + argv[2] = NULL; + + MSG_process_create_with_arguments(pr_name, communication_tx_fun, NULL, tx_host, 2, argv); + + pr_name = "comm_rx"; + argv = xbt_new(char *, 3); + argv[0] = xbt_strdup(pr_name); + argv[1] = xbt_strdup(mbox); + argv[2] = NULL; + + MSG_process_create_with_arguments(pr_name, communication_rx_fun, NULL, rx_host, 2, argv); + + xbt_free(mbox); +} + + +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; + + XBT_INFO("### Put a task on a PM"); + launch_computation_worker(pm0); + MSG_process_sleep(2); + + XBT_INFO("### Put two tasks on a PM"); + launch_computation_worker(pm0); + launch_computation_worker(pm0); + MSG_process_sleep(2); + + XBT_INFO("### Put a task on each PM"); + launch_computation_worker(pm0); + launch_computation_worker(pm1); + MSG_process_sleep(2); + + XBT_INFO("### Put a VM on a PM, and put a task to the VM"); + vm0 = MSG_vm_create_core(pm0, "VM0"); + MSG_vm_start(vm0); + launch_computation_worker(vm0); + MSG_process_sleep(2); + MSG_vm_destroy(vm0); + + XBT_INFO("### Put a VM on a PM, and put a task to the PM (FIXME: broken)"); + vm0 = MSG_vm_create_core(pm0, "VM0"); + MSG_vm_start(vm0); + launch_computation_worker(pm0); + MSG_process_sleep(2); + MSG_vm_destroy(vm0); + + XBT_INFO("### Put two VMs on a PM, and put a task to each VM"); + vm0 = MSG_vm_create_core(pm0, "VM0"); + vm1 = MSG_vm_create_core(pm0, "VM1"); + MSG_vm_start(vm0); + MSG_vm_start(vm1); + launch_computation_worker(vm0); + launch_computation_worker(vm1); + MSG_process_sleep(2); + MSG_vm_destroy(vm0); + MSG_vm_destroy(vm1); + + XBT_INFO("### Put a VM on each PM, and put a task to each VM"); + vm0 = MSG_vm_create_core(pm0, "VM0"); + vm1 = MSG_vm_create_core(pm1, "VM1"); + MSG_vm_start(vm0); + MSG_vm_start(vm1); + launch_computation_worker(vm0); + launch_computation_worker(vm1); + MSG_process_sleep(2); + MSG_vm_destroy(vm0); + MSG_vm_destroy(vm1); + + + XBT_INFO("### Make a connection between PM0 and PM1"); + launch_communication_worker(pm0, pm1); + MSG_process_sleep(5); + + XBT_INFO("### Make two connection between PM0 and PM1"); + launch_communication_worker(pm0, pm1); + launch_communication_worker(pm0, pm1); + MSG_process_sleep(5); + + XBT_INFO("### Make a connection between PM0 and VM0@PM0"); + vm0 = MSG_vm_create_core(pm0, "VM0"); + MSG_vm_start(vm0); + launch_communication_worker(pm0, vm0); + MSG_process_sleep(5); + MSG_vm_destroy(vm0); + + XBT_INFO("### Make a connection between PM0 and VM0@PM1"); + vm0 = MSG_vm_create_core(pm1, "VM0"); + MSG_vm_start(vm0); + launch_communication_worker(pm0, vm0); + MSG_process_sleep(5); + MSG_vm_destroy(vm0); + + XBT_INFO("### Make two connections between PM0 and VM0@PM1"); + vm0 = MSG_vm_create_core(pm1, "VM0"); + MSG_vm_start(vm0); + launch_communication_worker(pm0, vm0); + launch_communication_worker(pm0, vm0); + MSG_process_sleep(5); + MSG_vm_destroy(vm0); + + XBT_INFO("### Make a connection between PM0 and VM0@PM1, and also make a connection between PM0 and PM1"); + vm0 = MSG_vm_create_core(pm1, "VM0"); + MSG_vm_start(vm0); + launch_communication_worker(pm0, vm0); + launch_communication_worker(pm0, pm1); + MSG_process_sleep(5); + MSG_vm_destroy(vm0); + + XBT_INFO("### Make a connection between VM0@PM0 and PM1@PM1, and also make a connection between VM0@PM0 and VM1@PM1"); + vm0 = MSG_vm_create_core(pm0, "VM0"); + vm1 = MSG_vm_create_core(pm1, "VM1"); + MSG_vm_start(vm0); + MSG_vm_start(vm1); + launch_communication_worker(vm0, vm1); + launch_communication_worker(vm0, vm1); + MSG_process_sleep(5); + MSG_vm_destroy(vm0); + MSG_vm_destroy(vm1); + + + XBT_INFO("### Relocate VM0 between PM0 and PM1"); + vm0 = MSG_vm_create_core(pm0, "VM0"); + MSG_vm_start(vm0); + launch_communication_worker(vm0, pm2); + MSG_process_sleep(0.01); + MSG_vm_migrate(vm0, pm1); + MSG_process_sleep(0.01); + MSG_vm_migrate(vm0, pm0); + MSG_process_sleep(5); + MSG_vm_destroy(vm0); + + 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_t pr = 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("Simulation time %g", MSG_get_clock()); + + XBT_INFO("bye"); + + return 0; +} -- 2.20.1