Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
one step toward a live migration plugin
[simgrid.git] / examples / msg / cloud-migration / cloud-migration.c
1 /* Copyright (c) 2007-2015. 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 "simgrid/msg.h"
8 #include "simgrid/plugins/live_migration.h"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 static void vm_migrate(msg_vm_t vm, msg_host_t dst_pm)
13 {
14   msg_host_t src_pm = MSG_vm_get_pm(vm);
15   double mig_sta = MSG_get_clock();
16   MSG_vm_migrate(vm, dst_pm);
17   double mig_end = MSG_get_clock();
18
19   XBT_INFO("%s migrated: %s->%s in %g s", MSG_vm_get_name(vm),
20       MSG_host_get_name(src_pm), MSG_host_get_name(dst_pm),
21       mig_end - mig_sta);
22 }
23
24 static int migration_worker_main(int argc, char *argv[])
25 {
26   xbt_assert(argc == 3);
27   char *vm_name = argv[1];
28   char *dst_pm_name = argv[2];
29
30   msg_vm_t vm       = (msg_vm_t)MSG_host_by_name(vm_name);
31   msg_host_t dst_pm = MSG_host_by_name(dst_pm_name);
32
33   vm_migrate(vm, dst_pm);
34
35   return 0;
36 }
37
38 static void vm_migrate_async(msg_vm_t vm, msg_host_t dst_pm)
39 {
40   const char *vm_name = MSG_vm_get_name(vm);
41   const char *dst_pm_name = MSG_host_get_name(dst_pm);
42   msg_host_t host = MSG_host_self();
43
44   const char *pr_name = "mig_wrk";
45   char **argv = xbt_new(char *, 4);
46   argv[0] = xbt_strdup(pr_name);
47   argv[1] = xbt_strdup(vm_name);
48   argv[2] = xbt_strdup(dst_pm_name);
49   argv[3] = NULL;
50
51   MSG_process_create_with_arguments(pr_name, migration_worker_main, NULL, host, 3, argv);
52 }
53
54 static int master_main(int argc, char *argv[])
55 {
56   msg_host_t pm0 = MSG_host_by_name("Fafard");
57   msg_host_t pm1 = MSG_host_by_name("Tremblay");
58   msg_host_t pm2 = MSG_host_by_name("Bourassa");
59
60   msg_vm_t vm0 = MSG_vm_create_core(pm0, "VM0");
61   MSG_vm_set_ramsize(vm0, 1e9); // 1Gbytes
62   MSG_vm_start(vm0);
63
64   XBT_INFO("Test: Migrate a VM with %zu Mbytes RAM", MSG_vm_get_ramsize(vm0) / 1000 / 1000);
65   vm_migrate(vm0, pm1);
66
67   MSG_vm_destroy(vm0);
68
69   vm0 = MSG_vm_create_core(pm0, "VM0");
70   MSG_vm_set_ramsize(vm0, 1e8); // 100Mbytes
71   MSG_vm_start(vm0);
72
73   XBT_INFO("Test: Migrate a VM with %zu Mbytes RAM", MSG_vm_get_ramsize(vm0) / 1000 / 1000);
74   vm_migrate(vm0, pm1);
75
76   MSG_vm_destroy(vm0);
77
78   vm0 = MSG_vm_create_core(pm0, "VM0");
79   msg_vm_t vm1 = MSG_vm_create_core(pm0, "VM1");
80
81   MSG_vm_set_ramsize(vm0, 1e9); // 1Gbytes
82   MSG_vm_set_ramsize(vm1, 1e9); // 1Gbytes
83   MSG_vm_start(vm0);
84   MSG_vm_start(vm1);
85
86   XBT_INFO("Test: Migrate two VMs at once from PM0 to PM1");
87   vm_migrate_async(vm0, pm1);
88   vm_migrate_async(vm1, pm1);
89   MSG_process_sleep(10000);
90
91   MSG_vm_destroy(vm0);
92   MSG_vm_destroy(vm1);
93
94   vm0 = MSG_vm_create_core(pm0, "VM0");
95   vm1 = MSG_vm_create_core(pm0, "VM1");
96
97   MSG_vm_set_ramsize(vm0, 1e9); // 1Gbytes
98   MSG_vm_set_ramsize(vm1, 1e9); // 1Gbytes
99   MSG_vm_start(vm0);
100   MSG_vm_start(vm1);
101
102   XBT_INFO("Test: Migrate two VMs at once to different PMs");
103   vm_migrate_async(vm0, pm1);
104   vm_migrate_async(vm1, pm2);
105   MSG_process_sleep(10000);
106
107   MSG_vm_destroy(vm0);
108   MSG_vm_destroy(vm1);
109
110   return 0;
111 }
112
113 static void launch_master(msg_host_t host)
114 {
115   const char *pr_name = "master_";
116   char **argv = xbt_new(char *, 2);
117   argv[0] = xbt_strdup(pr_name);
118   argv[1] = NULL;
119
120   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
121 }
122
123 int main(int argc, char *argv[])
124 {
125   /* Get the arguments */
126   MSG_init(&argc, argv);
127   MSG_vm_live_migration_plugin_init();
128
129   /* load the platform file */
130   MSG_create_environment(argv[1]);
131
132   msg_host_t pm0 =  MSG_host_by_name("Fafard");
133   launch_master(pm0);
134
135   int res = MSG_main();
136   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
137
138   return !(res == MSG_OK);
139 }