1 /* Copyright (c) 2007-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 #include "simgrid/msg.h"
9 #include "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
18 static void vm_migrate(msg_vm_t vm, msg_host_t dst_pm)
20 msg_host_t src_pm = MSG_vm_get_pm(vm);
21 double mig_sta = MSG_get_clock();
22 MSG_vm_migrate(vm, dst_pm);
23 double mig_end = MSG_get_clock();
25 XBT_INFO("%s migrated: %s->%s in %g s", MSG_vm_get_name(vm),
26 MSG_host_get_name(src_pm), MSG_host_get_name(dst_pm),
30 static int migration_worker_main(int argc, char *argv[])
32 xbt_assert(argc == 3);
33 char *vm_name = argv[1];
34 char *dst_pm_name = argv[2];
36 msg_vm_t vm = MSG_host_by_name(vm_name);
37 msg_host_t dst_pm = MSG_host_by_name(dst_pm_name);
39 vm_migrate(vm, dst_pm);
44 static void vm_migrate_async(msg_vm_t vm, msg_host_t dst_pm)
46 const char *vm_name = MSG_vm_get_name(vm);
47 const char *dst_pm_name = MSG_host_get_name(dst_pm);
48 msg_host_t host = MSG_host_self();
50 const char *pr_name = "mig_wrk";
51 char **argv = xbt_new(char *, 4);
52 argv[0] = xbt_strdup(pr_name);
53 argv[1] = xbt_strdup(vm_name);
54 argv[2] = xbt_strdup(dst_pm_name);
57 MSG_process_create_with_arguments(pr_name, migration_worker_main, NULL, host, 3, argv);
60 static int master_main(int argc, char *argv[])
62 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
63 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
64 msg_host_t pm1 = xbt_dynar_get_as(hosts_dynar, 1, msg_host_t);
65 msg_host_t pm2 = xbt_dynar_get_as(hosts_dynar, 2, msg_host_t);
68 memset(¶ms, 0, sizeof(params));
72 vm0 = MSG_vm_create_core(pm0, "VM0");
73 params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
74 MSG_host_set_params(vm0, ¶ms);
77 XBT_INFO("Test: Migrate a VM with %llu Mbytes RAM", params.ramsize / 1000 / 1000);
84 vm0 = MSG_vm_create_core(pm0, "VM0");
85 params.ramsize = 1L * 1000 * 1000 * 100; // 100Mbytes
86 MSG_host_set_params(vm0, ¶ms);
89 XBT_INFO("Test: Migrate a VM with %llu Mbytes RAM", params.ramsize / 1000 / 1000);
96 vm0 = MSG_vm_create_core(pm0, "VM0");
97 vm1 = MSG_vm_create_core(pm0, "VM1");
99 params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
100 MSG_host_set_params(vm0, ¶ms);
101 MSG_host_set_params(vm1, ¶ms);
105 XBT_INFO("Test: Migrate two VMs at once from PM0 to PM1");
106 vm_migrate_async(vm0, pm1);
107 vm_migrate_async(vm1, pm1);
108 MSG_process_sleep(10000);
115 vm0 = MSG_vm_create_core(pm0, "VM0");
116 vm1 = MSG_vm_create_core(pm0, "VM1");
118 params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
119 MSG_host_set_params(vm0, ¶ms);
120 MSG_host_set_params(vm1, ¶ms);
124 XBT_INFO("Test: Migrate two VMs at once to different PMs");
125 vm_migrate_async(vm0, pm1);
126 vm_migrate_async(vm1, pm2);
127 MSG_process_sleep(10000);
136 static void launch_master(msg_host_t host)
138 const char *pr_name = "master_";
139 char **argv = xbt_new(char *, 2);
140 argv[0] = xbt_strdup(pr_name);
143 MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
147 int main(int argc, char *argv[])
149 /* Get the arguments */
150 MSG_init(&argc, argv);
152 /* load the platform file */
153 MSG_create_environment(argv[1]);
155 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
156 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
159 int res = MSG_main();
160 XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
163 return !(res == MSG_OK);