1 /* Copyright (c) 2007-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
13 /** @addtogroup MSG_examples
15 * - <b>cloud/master_worker_vm.c: Master/workers
16 * example on a cloud</b>. The classical example revisited to demonstrate the use of virtual machines.
19 const double task_comp_size = 10000000;
20 const double task_comm_size = 10000000;
22 static void send_tasks(int nb_workers)
24 for (int i = 0; i < nb_workers; i++) {
25 char *tname = bprintf("Task%02d", i);
26 char *mbox = bprintf("MBOX:WRK%02d", i);
28 msg_task_t task = MSG_task_create(tname, task_comp_size, task_comm_size, NULL);
30 XBT_INFO("Send task(%s) to mailbox(%s)", tname, mbox);
31 MSG_task_send(task, mbox);
38 static int worker_fun(int argc, char *argv[])
40 const char *pr_name = MSG_process_get_name(MSG_process_self());
41 char mbox[MAXMBOXLEN];
42 snprintf(mbox, MAXMBOXLEN, "MBOX:%s", pr_name);
44 XBT_INFO("%s is listening on mailbox(%s)", pr_name, mbox);
47 msg_task_t task = NULL;
49 msg_error_t res = MSG_task_receive(&task, mbox);
50 xbt_assert(res == MSG_OK, "MSG_task_get failed");
52 XBT_INFO("%s received task(%s) from mailbox(%s)", pr_name, MSG_task_get_name(task), mbox);
54 if (strcmp(MSG_task_get_name(task), "finalize") == 0) {
55 MSG_task_destroy(task);
59 MSG_task_execute(task);
60 XBT_INFO("%s executed task(%s)", pr_name, MSG_task_get_name(task));
61 MSG_task_destroy(task);
66 static int master_fun(int argc, char *argv[])
71 xbt_dynar_t worker_pms = MSG_process_get_data(MSG_process_self());
72 int nb_workers = xbt_dynar_length(worker_pms);
74 xbt_dynar_t vms = xbt_dynar_new(sizeof(msg_vm_t), NULL);
76 /* Launch VMs and worker processes. One VM per PM, and one worker process per VM. */
77 XBT_INFO("# Launch %d VMs", nb_workers);
78 for (int i = 0; i< nb_workers; i++) {
79 char *vm_name = bprintf("VM%02d", i);
80 char *pr_name = bprintf("WRK%02d", i);
82 msg_host_t pm = xbt_dynar_get_as(worker_pms, i, msg_host_t);
84 XBT_INFO("create %s on PM(%s)", vm_name, MSG_host_get_name(pm));
85 msg_vm_t vm = MSG_vm_create_core(pm, vm_name);
88 memset(¶ms, 0, sizeof(params));
89 params.ramsize = 1L * 1024 * 1024 * 1024; // 1Gbytes
90 MSG_host_set_params(vm, ¶ms);
93 xbt_dynar_push(vms, &vm);
95 XBT_INFO("put a process (%s) on %s", pr_name, vm_name);
96 MSG_process_create(pr_name, worker_fun, NULL, vm);
102 /* Send a bunch of work to every one */
103 XBT_INFO("# Send a task to %d worker process", nb_workers);
104 send_tasks(nb_workers);
106 XBT_INFO("# Suspend all VMs");
107 xbt_dynar_foreach(vms, i, vm) {
108 XBT_INFO("suspend %s", MSG_host_get_name(vm));
112 XBT_INFO("# Wait a while");
113 MSG_process_sleep(2);
115 XBT_INFO("# Resume all VMs");
116 xbt_dynar_foreach(vms, i, vm) {
120 XBT_INFO("# Sleep long enough for everyone to be done with previous batch of work");
121 MSG_process_sleep(10 - MSG_get_clock());
123 XBT_INFO("# Add one more process on each VM");
124 xbt_dynar_foreach(vms, i, vm) {
125 unsigned int index = i + xbt_dynar_length(vms);
126 char *vm_name = bprintf("VM%02d", i);
127 char *pr_name = bprintf("WRK%02d", index);
129 XBT_INFO("put a process (%s) on %s", pr_name, vm_name);
130 MSG_process_create(pr_name, worker_fun, NULL, vm);
136 XBT_INFO("# Send a task to %d worker process", nb_workers * 2);
137 send_tasks(nb_workers * 2);
139 msg_host_t worker_pm0 = xbt_dynar_get_as(worker_pms, 0, msg_host_t);
140 msg_host_t worker_pm1 = xbt_dynar_get_as(worker_pms, 1, msg_host_t);
142 XBT_INFO("# Migrate all VMs to PM(%s)", MSG_host_get_name(worker_pm0));
143 xbt_dynar_foreach(vms, i, vm) {
144 MSG_vm_migrate(vm, worker_pm0);
147 XBT_INFO("# Migrate all VMs to PM(%s)", MSG_host_get_name(worker_pm1));
148 xbt_dynar_foreach(vms, i, vm) {
149 MSG_vm_migrate(vm, worker_pm1);
152 XBT_INFO("# Shutdown the half of worker processes gracefully. The remaining half will be forcibly killed.");
153 for (i = 0; i < nb_workers; i++) {
154 char mbox[MAXMBOXLEN];
155 snprintf(mbox, MAXMBOXLEN, "MBOX:WRK%02d", i);
156 msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
157 MSG_task_send(finalize, mbox);
160 XBT_INFO("# Wait a while before effective shutdown.");
161 MSG_process_sleep(2);
163 XBT_INFO("# Shutdown and destroy all the VMs. The remaining worker processes will be forcibly killed.");
164 xbt_dynar_foreach(vms, i, vm) {
165 XBT_INFO("shutdown %s", MSG_host_get_name(vm));
167 XBT_INFO("destroy %s", MSG_host_get_name(vm));
171 XBT_INFO("# Goodbye now!");
172 xbt_dynar_free(&vms);
176 /** Receiver function */
177 int main(int argc, char *argv[])
179 const int nb_workers = 2;
181 MSG_init(&argc, argv);
182 xbt_assert(argc >1,"Usage: %s example/platforms/cluster.xml\n", argv[0]);
184 /* Load the platform file */
185 MSG_create_environment(argv[1]);
187 /* Retrieve hosts from the platform file */
188 xbt_dynar_t pms = MSG_hosts_as_dynar();
190 /* we need a master node and worker nodes */
191 xbt_assert(xbt_dynar_length(pms) > nb_workers,"need %d hosts", nb_workers + 1);
193 /* the first pm is the master, the others are workers */
194 msg_host_t master_pm = xbt_dynar_get_as(pms, 0, msg_host_t);
196 xbt_dynar_t worker_pms = xbt_dynar_new(sizeof(msg_host_t), NULL);
197 for (int i = 1; i < nb_workers + 1; i++) {
198 msg_host_t pm = xbt_dynar_get_as(pms, i, msg_host_t);
199 xbt_dynar_push(worker_pms, &pm);
201 xbt_dynar_free(&pms);
203 /* Start the master process on the master pm. */
204 MSG_process_create("master", master_fun, worker_pms, master_pm);
206 msg_error_t res = MSG_main();
207 XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
209 xbt_dynar_free(&worker_pms);
211 return !(res == MSG_OK);