1 /* Copyright (c) 2007-2012. The SimGrid Team. All rights reserved. */
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. */
8 #include "xbt/sysdep.h" /* calloc, printf */
10 /* Create a log channel to have nice outputs. */
12 #include "xbt/asserts.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14 "Messages specific for this msg example");
16 /** @addtogroup MSG_examples
18 * - <b>cloud/masterslave_virtual_machines.c: Master/workers
19 * example on a cloud</b>. The classical example revisited to demonstrate the use of virtual machines.
22 const double task_comp_size = 10000000;
23 const double task_comm_size = 10000000;
26 int master_fun(int argc, char *argv[]);
27 int worker_fun(int argc, char *argv[]);
30 static void work_batch(int workers_count)
33 for (i = 0; i < workers_count; i++) {
34 char *tname = bprintf("Task%02d", i);
35 char *mbox = bprintf("MBOX:WRK%02d", i);
37 msg_task_t task = MSG_task_create(tname, task_comp_size, task_comm_size, NULL);
39 XBT_INFO("send task(%s) to mailbox(%s)", tname, mbox);
40 MSG_task_send(task, mbox);
47 int master_fun(int argc, char *argv[])
51 int workers_count = argc - 1;
53 msg_host_t *pms = xbt_new(msg_host_t, workers_count);
54 xbt_dynar_t vms = xbt_dynar_new(sizeof(msg_vm_t), NULL);
56 /* Retrive the PMs that will launch worker processes. */
57 for (i = 1; i < argc; i++)
58 pms[i - 1] = MSG_get_host_by_name(argv[i]);
61 /* Launch VMs and worker processes. One VM per PM, and one worker process per VM. */
63 XBT_INFO("Launch %ld VMs", workers_count);
64 for (i=0; i< workers_count; i++) {
65 char *vm_name = bprintf("VM%02d", i);
66 char *pr_name = bprintf("WRK%02d", i);
67 char *mbox = bprintf("MBOX:WRK%02d", i);
69 char **wrk_argv = xbt_new(char*, 3);
70 wrk_argv[0] = pr_name;
74 XBT_INFO("create %s", vm_name);
75 msg_vm_t vm = MSG_vm_create_core(pms[i], vm_name);
77 xbt_dynar_push(vms, &vm);
79 XBT_INFO("put %s on %s", pr_name, vm_name);
80 MSG_process_create_with_arguments(pr_name, worker_fun, NULL, vm, 2, wrk_argv);
84 /* Send a bunch of work to every one */
85 XBT_INFO("Send a task to %d worker process", workers_count);
86 work_batch(workers_count);
88 XBT_INFO("Suspend all VMs");
89 xbt_dynar_foreach(vms, i, vm) {
90 const char *vm_name = MSG_host_get_name(vm);
91 XBT_INFO("suspend %s", vm_name);
95 XBT_INFO("Wait a while");
98 XBT_INFO("Resume all VMs");
99 xbt_dynar_foreach(vms, i, vm) {
104 XBT_INFO("Sleep long enough for everyone to be done with previous batch of work");
105 MSG_process_sleep(1000 - MSG_get_clock());
107 XBT_INFO("Add one more process per VM");
108 xbt_dynar_foreach(vms, i, vm) {
109 unsigned int index = i + xbt_dynar_length(vms);
110 char *vm_name = bprintf("VM%02d", i);
111 char *pr_name = bprintf("WRK%02d", index);
112 char *mbox = bprintf("MBOX:WRK%02d", index);
114 char **wrk_argv = xbt_new(char*, 3);
115 wrk_argv[0] = pr_name;
119 XBT_INFO("put %s on %s", pr_name, vm_name);
120 MSG_process_create_with_arguments(pr_name, worker_fun, NULL, vm, 2, wrk_argv);
123 XBT_INFO("Send a task to %d worker process", workers_count * 2);
124 work_batch(workers_count * 2);
126 XBT_INFO("Migrate all VMs to PM(%s)", MSG_host_get_name(pms[1]));
127 xbt_dynar_foreach(vms, i, vm) {
128 MSG_vm_migrate(vm, pms[1]);
131 /* Migration with default policy is called (i.e. live migration with pre-copy strategy) */
132 /* If you want to use other policy such as post-copy or cold migration, you should add a third parameter that defines the policy */
133 XBT_INFO("Migrate all VMs to PM(%s)", MSG_host_get_name(pms[2]));
134 xbt_dynar_foreach(vms, i, vm) {
135 // MSG_vm_suspend(vm);
136 MSG_vm_migrate(vm, pms[2]);
137 // MSG_vm_resume(vm);
141 XBT_INFO("Shutdown the half of worker processes gracefuly. The remaining half will be forcibly killed");
142 for (i = 0; i < workers_count; i++) {
144 sprintf(mbox, "MBOX:WRK%02d", i);
145 msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
146 MSG_task_send(finalize, mbox);
149 XBT_INFO("Wait a while before effective shutdown.");
150 MSG_process_sleep(2);
153 XBT_INFO("Shutdown and destroy all the VMs. The remaining worker processes will be forcibly killed.");
154 xbt_dynar_foreach(vms, i, vm) {
155 XBT_INFO("shutdown %s", MSG_host_get_name(vm));
157 XBT_INFO("destroy %s", MSG_host_get_name(vm));
161 XBT_INFO("Goodbye now!");
163 xbt_dynar_free(&vms);
168 /** Receiver function */
169 int worker_fun(int argc, char *argv[])
171 xbt_assert(argc == 2, "need mbox in arguments");
173 char *mbox = argv[1];
174 const char *pr_name = MSG_process_get_name(MSG_process_self());
175 XBT_INFO("%s is listenning on mailbox(%s)", pr_name, mbox);
178 msg_task_t task = NULL;
180 msg_error_t res = MSG_task_receive(&task, mbox);
182 XBT_CRITICAL("MSG_task_get failed");
186 XBT_INFO("%s received task(%s) from mailbox(%s)",
187 pr_name, MSG_task_get_name(task), mbox);
189 if (!strcmp(MSG_task_get_name(task), "finalize")) {
190 MSG_task_destroy(task);
194 MSG_task_execute(task);
195 XBT_INFO("%s executed task(%s)", pr_name, MSG_task_get_name(task));
196 MSG_task_destroy(task);
204 int main(int argc, char *argv[])
206 const int nb_hosts = 3;
208 MSG_init(&argc, argv);
210 printf("Usage: %s example/msg/msg_platform.xml\n", argv[0]);
214 /* Load the platform file */
215 MSG_create_environment(argv[1]);
217 /* Retrieve hosts from the platform file */
218 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
220 if (xbt_dynar_length(hosts_dynar) <= nb_hosts) {
221 XBT_CRITICAL("need %d hosts", nb_hosts);
225 msg_host_t master_pm;
226 char **master_argv = xbt_new(char *, 12);
227 master_argv[0] = xbt_strdup("master");
228 master_argv[11] = NULL;
232 xbt_dynar_foreach(hosts_dynar, i, host) {
238 master_argv[i] = xbt_strdup(MSG_host_get_name(host));
245 MSG_process_create_with_arguments("master", master_fun, NULL, master_pm, nb_hosts + 1, master_argv);
247 msg_error_t res = MSG_main();
248 XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
250 xbt_dynar_free(&hosts_dynar);
252 return !(res == MSG_OK);