Logo AND Algorithmique Numérique Distribuée

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