Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc++' into mc-merge
[simgrid.git] / examples / msg / cloud / migrate_vm.c
1 /* Copyright (c) 2007-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <stdio.h>
8 #include "msg/msg.h"
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17
18 static void vm_migrate(msg_vm_t vm, msg_host_t dst_pm)
19 {
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();
24
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),
27                   mig_end - mig_sta);
28 }
29
30 static int migration_worker_main(int argc, char *argv[])
31 {
32   xbt_assert(argc == 3);
33   char *vm_name = argv[1];
34   char *dst_pm_name = argv[2];
35
36   msg_vm_t vm = MSG_get_host_by_name(vm_name);
37   msg_host_t dst_pm = MSG_get_host_by_name(dst_pm_name);
38
39   vm_migrate(vm, dst_pm);
40
41   return 0;
42 }
43
44 static void vm_migrate_async(msg_vm_t vm, msg_host_t dst_pm)
45 {
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();
49
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);
55   argv[3] = NULL;
56
57   MSG_process_create_with_arguments(pr_name, migration_worker_main, NULL, host, 3, argv);
58 }
59
60 static int master_main(int argc, char *argv[])
61 {
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);
66   msg_vm_t vm0, vm1;
67   s_ws_params_t params;
68   memset(&params, 0, sizeof(params));
69
70
71
72   vm0 = MSG_vm_create_core(pm0, "VM0");
73   params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
74   MSG_host_set_params(vm0, &params);
75   MSG_vm_start(vm0);
76
77   XBT_INFO("Test: Migrate a VM with %llu Mbytes RAM", params.ramsize / 1000 / 1000);
78   vm_migrate(vm0, pm1);
79
80   MSG_vm_destroy(vm0);
81
82
83
84   vm0 = MSG_vm_create_core(pm0, "VM0");
85   params.ramsize = 1L * 1000 * 1000 * 100; // 100Mbytes
86   MSG_host_set_params(vm0, &params);
87   MSG_vm_start(vm0);
88
89   XBT_INFO("Test: Migrate a VM with %llu Mbytes RAM", params.ramsize / 1000 / 1000);
90   vm_migrate(vm0, pm1);
91
92   MSG_vm_destroy(vm0);
93
94
95
96   vm0 = MSG_vm_create_core(pm0, "VM0");
97   vm1 = MSG_vm_create_core(pm0, "VM1");
98
99   params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
100   MSG_host_set_params(vm0, &params);
101   MSG_host_set_params(vm1, &params);
102   MSG_vm_start(vm0);
103   MSG_vm_start(vm1);
104
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);
109
110   MSG_vm_destroy(vm0);
111   MSG_vm_destroy(vm1);
112
113
114
115   vm0 = MSG_vm_create_core(pm0, "VM0");
116   vm1 = MSG_vm_create_core(pm0, "VM1");
117
118   params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
119   MSG_host_set_params(vm0, &params);
120   MSG_host_set_params(vm1, &params);
121   MSG_vm_start(vm0);
122   MSG_vm_start(vm1);
123
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);
128
129   MSG_vm_destroy(vm0);
130   MSG_vm_destroy(vm1);
131
132
133   return 0;
134 }
135
136 static void launch_master(msg_host_t host)
137 {
138   const char *pr_name = "master_";
139   char **argv = xbt_new(char *, 2);
140   argv[0] = xbt_strdup(pr_name);
141   argv[1] = NULL;
142
143   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
144 }
145
146
147 int main(int argc, char *argv[])
148 {
149   /* Get the arguments */
150   MSG_init(&argc, argv);
151
152   /* load the platform file */
153   MSG_create_environment(argv[1]);
154
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);
157   launch_master(pm0);
158
159   int res = MSG_main();
160   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
161
162
163   return !(res == MSG_OK);
164 }