Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add/update copyright notices.
[simgrid.git] / examples / msg / cloud / masterslave_virtual_machines.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 /** @addtogroup MSG_examples
18  * 
19  *  - <b>cloud/masterslave_virtual_machines.c: Master/workers
20  *    example on a cloud</b>. The classical example revisited to demonstrate the use of virtual machines.
21  */
22
23 const double task_comp_size = 10000000;
24 const double task_comm_size = 10000000;
25
26
27 int master_fun(int argc, char *argv[]);
28 int worker_fun(int argc, char *argv[]);
29
30
31 static void work_batch(int workers_count)
32 {
33   int i;
34   for (i = 0; i < workers_count; i++) {
35     char *tname = bprintf("Task%02d", i);
36     char *mbox =  bprintf("MBOX:WRK%02d", i);
37
38     msg_task_t task = MSG_task_create(tname, task_comp_size, task_comm_size, NULL);
39
40     XBT_INFO("send task(%s) to mailbox(%s)", tname, mbox);
41     MSG_task_send(task, mbox);
42
43     free(tname);
44     free(mbox);
45   }
46 }
47
48 int master_fun(int argc, char *argv[])
49 {
50   msg_vm_t vm;
51   unsigned int i;
52   int workers_count = argc - 1;
53
54   msg_host_t *pms = xbt_new(msg_host_t, workers_count);
55   xbt_dynar_t vms = xbt_dynar_new(sizeof(msg_vm_t), NULL);
56
57   /* Retrieve the PMs that will launch worker processes. */
58   for (i = 1; i < argc; i++)
59     pms[i - 1] = MSG_get_host_by_name(argv[i]);
60
61
62   /* Launch VMs and worker processes. One VM per PM, and one worker process per VM. */
63
64   XBT_INFO("Launch %d VMs", workers_count);
65   for (i=0; i< workers_count; i++) {
66     char *vm_name = bprintf("VM%02d", i);
67     char *pr_name = bprintf("WRK%02d", i);
68     char *mbox = bprintf("MBOX:WRK%02d", i);
69
70     char **wrk_argv = xbt_new(char*, 3);
71     wrk_argv[0] = pr_name;
72     wrk_argv[1] = mbox;
73     wrk_argv[2] = NULL;
74
75     XBT_INFO("create %s", vm_name);
76     msg_vm_t vm = MSG_vm_create_core(pms[i], vm_name);
77
78     s_ws_params_t params;
79     memset(&params, 0, sizeof(params));
80     params.ramsize = 1L * 1024 * 1024 * 1024; // 1Gbytes
81     MSG_host_set_params(vm, &params);
82
83     MSG_vm_start(vm);
84     xbt_dynar_push(vms, &vm);
85
86     XBT_INFO("put %s on %s", pr_name, vm_name);
87     MSG_process_create_with_arguments(pr_name, worker_fun, NULL, vm, 2, wrk_argv);
88   }
89
90
91   /* Send a bunch of work to every one */
92   XBT_INFO("Send a task to %d worker process", workers_count);
93   work_batch(workers_count);
94
95   XBT_INFO("Suspend all VMs");
96   xbt_dynar_foreach(vms, i, vm) {
97     const char *vm_name = MSG_host_get_name(vm);
98     XBT_INFO("suspend %s", vm_name);
99     MSG_vm_suspend(vm);
100   }
101
102   XBT_INFO("Wait a while");
103   MSG_process_sleep(2);
104
105   XBT_INFO("Resume all VMs");
106   xbt_dynar_foreach(vms, i, vm) {
107     MSG_vm_resume(vm);
108   }
109
110
111   XBT_INFO("Sleep long enough for everyone to be done with previous batch of work");
112   MSG_process_sleep(1000 - MSG_get_clock());
113
114   XBT_INFO("Add one more process per VM");
115   xbt_dynar_foreach(vms, i, vm) {
116     unsigned int index = i + xbt_dynar_length(vms);
117     char *vm_name = bprintf("VM%02d", i);
118     char *pr_name = bprintf("WRK%02d", index);
119     char *mbox = bprintf("MBOX:WRK%02d", index);
120
121     char **wrk_argv = xbt_new(char*, 3);
122     wrk_argv[0] = pr_name;
123     wrk_argv[1] = mbox;
124     wrk_argv[2] = NULL;
125
126     XBT_INFO("put %s on %s", pr_name, vm_name);
127     MSG_process_create_with_arguments(pr_name, worker_fun, NULL, vm, 2, wrk_argv);
128   }
129
130   XBT_INFO("Send a task to %d worker process", workers_count * 2);
131   work_batch(workers_count * 2);
132
133   XBT_INFO("Migrate all VMs to PM(%s)", MSG_host_get_name(pms[1]));
134   xbt_dynar_foreach(vms, i, vm) {
135     MSG_vm_migrate(vm, pms[1]);
136   }
137
138   /* Migration with default policy is called (i.e. live migration with pre-copy strategy) */
139   /* If you want to use other policy such as post-copy or cold migration, you should add a third parameter that defines the policy */
140   XBT_INFO("Migrate all VMs to PM(%s)", MSG_host_get_name(pms[2]));
141   xbt_dynar_foreach(vms, i, vm) {
142     // MSG_vm_suspend(vm);
143     MSG_vm_migrate(vm, pms[2]);
144     // MSG_vm_resume(vm);
145   }
146
147
148   XBT_INFO("Shutdown the half of worker processes gracefuly. The remaining half will be forcibly killed");
149   for (i = 0; i < workers_count; i++) {
150     char mbox[64];
151     sprintf(mbox, "MBOX:WRK%02d", i);
152     msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
153     MSG_task_send(finalize, mbox);
154   }
155
156   XBT_INFO("Wait a while before effective shutdown.");
157   MSG_process_sleep(2);
158
159
160   XBT_INFO("Shutdown and destroy all the VMs. The remaining worker processes will be forcibly killed.");
161   xbt_dynar_foreach(vms, i, vm) {
162     XBT_INFO("shutdown %s", MSG_host_get_name(vm));
163     MSG_vm_shutdown(vm);
164     XBT_INFO("destroy %s", MSG_host_get_name(vm));
165     MSG_vm_destroy(vm);
166   }
167
168   XBT_INFO("Goodbye now!");
169   free(pms);
170   xbt_dynar_free(&vms);
171
172   return 0;
173 }
174
175 /** Receiver function  */
176 int worker_fun(int argc, char *argv[])
177 {
178   xbt_assert(argc == 2, "need mbox in arguments");
179
180   char *mbox = argv[1];
181   const char *pr_name = MSG_process_get_name(MSG_process_self());
182   XBT_INFO("%s is listenning on mailbox(%s)", pr_name, mbox);
183
184   for (;;) {
185     msg_task_t task = NULL;
186
187     msg_error_t res = MSG_task_receive(&task, mbox);
188     if (res != MSG_OK) {
189       XBT_CRITICAL("MSG_task_get failed");
190       DIE_IMPOSSIBLE;
191     }
192
193     XBT_INFO("%s received task(%s) from mailbox(%s)",
194         pr_name, MSG_task_get_name(task), mbox);
195
196     if (!strcmp(MSG_task_get_name(task), "finalize")) {
197       MSG_task_destroy(task);
198       break;
199     }
200
201     MSG_task_execute(task);
202     XBT_INFO("%s executed task(%s)", pr_name, MSG_task_get_name(task));
203     MSG_task_destroy(task);
204   }
205
206   return 0;
207 }
208
209
210 /** Main function */
211 int main(int argc, char *argv[])
212 {
213   const int nb_hosts = 3;
214
215   MSG_init(&argc, argv);
216   if (argc != 2) {
217     printf("Usage: %s example/msg/msg_platform.xml\n", argv[0]);
218     return 1;
219   }
220
221   /* Load the platform file */
222   MSG_create_environment(argv[1]);
223
224   /* Retrieve hosts from the platform file */
225   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
226
227   if (xbt_dynar_length(hosts_dynar) <= nb_hosts) {
228     XBT_CRITICAL("need %d hosts", nb_hosts);
229     return 1;
230   }
231
232   msg_host_t master_pm = NULL;
233   char **master_argv = xbt_new(char *, 12);
234   master_argv[0] = xbt_strdup("master");
235   master_argv[11] = NULL;
236
237   unsigned int i;
238   msg_host_t host;
239   xbt_dynar_foreach(hosts_dynar, i, host) {
240     if (i == 0) {
241       master_pm = host;
242       continue;
243     }
244
245     master_argv[i] = xbt_strdup(MSG_host_get_name(host));
246
247     if (i == nb_hosts)
248       break;
249   }
250
251   msg_error_t res = 1;
252   if (master_pm!=NULL){
253     MSG_process_create_with_arguments("master", master_fun, NULL, master_pm, nb_hosts + 1, master_argv);
254
255     res = MSG_main();
256     XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
257   }
258   xbt_dynar_free(&hosts_dynar);
259
260   return !(res == MSG_OK);
261 }