Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into hypervisor
[simgrid.git] / examples / msg / cloud / migrate_vm.c
1 /* Copyright (c) 2007-2012. The SimGrid Team. All rights reserved. */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <stdio.h>
7 #include "msg/msg.h"
8 #include "xbt/sysdep.h"         /* calloc, printf */
9
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 #include "xbt/asserts.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14                              "Messages specific for this msg example");
15
16
17 void vm_migrate(msg_vm_t vm, msg_host_t dst_pm)
18 {
19   msg_host_t src_pm = MSG_vm_get_pm(vm);
20   double mig_sta = MSG_get_clock();
21   MSG_vm_migrate(vm, dst_pm);
22   double mig_end = MSG_get_clock();
23
24   XBT_INFO("%s migrated: %s->%s in %g s", MSG_vm_get_name(vm),
25                   MSG_host_get_name(src_pm), MSG_host_get_name(dst_pm),
26                   mig_end - mig_sta);
27 }
28
29 int migration_worker_main(int argc, char *argv[])
30 {
31   xbt_assert(argc == 3);
32   char *vm_name = argv[1];
33   char *dst_pm_name = argv[2];
34
35   msg_vm_t vm = MSG_get_host_by_name(vm_name);
36   msg_host_t dst_pm = MSG_get_host_by_name(dst_pm_name);
37
38   vm_migrate(vm, dst_pm);
39
40   return 0;
41 }
42
43 void vm_migrate_async(msg_vm_t vm, msg_host_t dst_pm)
44 {
45   const char *vm_name = MSG_vm_get_name(vm);
46   const char *dst_pm_name = MSG_host_get_name(dst_pm);
47   msg_host_t host = MSG_host_self();
48
49   const char *pr_name = "mig_wrk";
50   char **argv = xbt_new(char *, 4);
51   argv[0] = xbt_strdup(pr_name);
52   argv[1] = xbt_strdup(vm_name);
53   argv[2] = xbt_strdup(dst_pm_name);
54   argv[3] = NULL;
55
56   MSG_process_create_with_arguments(pr_name, migration_worker_main, NULL, host, 3, argv);
57 }
58
59 int master_main(int argc, char *argv[])
60 {
61   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
62   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
63   msg_host_t pm1 = xbt_dynar_get_as(hosts_dynar, 1, msg_host_t);
64   msg_host_t pm2 = xbt_dynar_get_as(hosts_dynar, 2, msg_host_t);
65   msg_vm_t vm0, vm1;
66   s_ws_params_t params;
67   memset(&params, 0, sizeof(params));
68
69
70
71   vm0 = MSG_vm_create_core(pm0, "VM0");
72   params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
73   MSG_host_set_params(vm0, &params);
74   MSG_vm_start(vm0);
75
76   XBT_INFO("Test: Migrate a VM with %ld Mbytes RAM", params.ramsize / 1000 / 1000);
77   vm_migrate(vm0, pm1);
78
79   MSG_vm_destroy(vm0);
80
81
82
83   vm0 = MSG_vm_create_core(pm0, "VM0");
84   params.ramsize = 1L * 1000 * 1000 * 100; // 100Mbytes
85   MSG_host_set_params(vm0, &params);
86   MSG_vm_start(vm0);
87
88   XBT_INFO("Test: Migrate a VM with %ld Mbytes RAM", params.ramsize / 1000 / 1000);
89   vm_migrate(vm0, pm1);
90
91   MSG_vm_destroy(vm0);
92
93
94
95   vm0 = MSG_vm_create_core(pm0, "VM0");
96   vm1 = MSG_vm_create_core(pm0, "VM1");
97
98   params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
99   MSG_host_set_params(vm0, &params);
100   MSG_host_set_params(vm1, &params);
101   MSG_vm_start(vm0);
102   MSG_vm_start(vm1);
103
104   XBT_INFO("Test: Migrate two VMs at once from PM0 to PM1");
105   vm_migrate_async(vm0, pm1);
106   vm_migrate_async(vm1, pm1);
107   MSG_process_sleep(10000);
108
109   MSG_vm_destroy(vm0);
110   MSG_vm_destroy(vm1);
111
112
113
114   vm0 = MSG_vm_create_core(pm0, "VM0");
115   vm1 = MSG_vm_create_core(pm0, "VM1");
116
117   params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
118   MSG_host_set_params(vm0, &params);
119   MSG_host_set_params(vm1, &params);
120   MSG_vm_start(vm0);
121   MSG_vm_start(vm1);
122
123   XBT_INFO("Test: Migrate two VMs at once to different PMs");
124   vm_migrate_async(vm0, pm1);
125   vm_migrate_async(vm1, pm2);
126   MSG_process_sleep(10000);
127
128   MSG_vm_destroy(vm0);
129   MSG_vm_destroy(vm1);
130
131
132   return 0;
133 }
134
135 void launch_master(msg_host_t host)
136 {
137   const char *pr_name = "master_";
138   char **argv = xbt_new(char *, 2);
139   argv[0] = xbt_strdup(pr_name);
140   argv[1] = NULL;
141
142   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
143 }
144
145
146 int main(int argc, char *argv[])
147 {
148   /* Get the arguments */
149   MSG_init(&argc, argv);
150
151   /* load the platform file */
152   MSG_create_environment(argv[1]);
153
154   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
155   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
156   launch_master(pm0);
157
158   int res = MSG_main();
159   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
160
161
162   return !(res == MSG_OK);
163 }