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. */
7 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
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/slaves
19 * example, à la cloud</b>. The classical example revisited to demonstrate the use of virtual machines.
22 double task_comp_size = 10000000;
23 double task_comm_size = 10000000;
26 int master(int argc, char *argv[]);
27 int slave_fun(int argc, char *argv[]);
29 static void work_batch(int slaves_count) {
31 for (i = 0; i < slaves_count; i++) {
32 char taskname_buffer[64];
33 char mailbox_buffer[64];
35 sprintf(taskname_buffer, "Task_%d", i);
36 sprintf(mailbox_buffer,"Slave_%d",i);
38 XBT_INFO("Sending \"%s\" to \"%s\"",taskname_buffer,mailbox_buffer);
39 MSG_task_send(MSG_task_create(taskname_buffer, task_comp_size, task_comm_size,NULL),
44 int master(int argc, char *argv[]) {
45 int slaves_count = 10;
46 msg_host_t *slaves = xbt_new(msg_host_t,10);
51 /* Retrive the hostnames constituting our playground today */
52 for (i = 1; i < argc; i++) {
53 slaves[i - 1] = MSG_get_host_by_name(argv[i]);
54 xbt_assert(slaves[i - 1] != NULL, "Cannot use inexistent host %s", argv[i]);
57 /* Launch the sub processes: one VM per host, with one process inside each */
59 for (i=0;i<slaves_count;i++) {
61 sprintf(slavename,"Slave %d",i);
62 char**argv=xbt_new(char*,3);
63 argv[0] = xbt_strdup(slavename);
64 argv[1] = bprintf("%d",i);
68 snprintf(vmName, 64, "vm_%d", i);
70 msg_vm_t vm = MSG_vm_start(slaves[i],vmName,2);
71 MSG_vm_bind(vm, MSG_process_create_with_arguments(slavename,slave_fun,NULL,slaves[i],2,argv));
75 xbt_dynar_t vms = MSG_vms_as_dynar();
76 XBT_INFO("Launched %ld VMs", xbt_dynar_length(vms));
78 /* Send a bunch of work to every one */
79 XBT_INFO("Send a first batch of work to every one");
80 work_batch(slaves_count);
82 XBT_INFO("Now suspend all VMs, just for fun");
84 xbt_dynar_foreach(vms,i,vm) {
88 XBT_INFO("Wait a while");
91 XBT_INFO("Enough. Let's resume everybody.");
92 xbt_dynar_foreach(vms,i,vm) {
95 XBT_INFO("Sleep long enough for everyone to be done with previous batch of work");
96 MSG_process_sleep(1000-MSG_get_clock());
98 XBT_INFO("Add one more process per VM");
99 xbt_dynar_foreach(vms,i,vm) {
100 msg_vm_t vm = xbt_dynar_get_as(vms,i,msg_vm_t);
102 sprintf(slavename,"Slave %ld",i+xbt_dynar_length(vms));
103 char**argv=xbt_new(char*,3);
104 argv[0] = xbt_strdup(slavename);
105 argv[1] = bprintf("%ld",i+xbt_dynar_length(vms));
107 MSG_vm_bind(vm, MSG_process_create_with_arguments(slavename,slave_fun,NULL,slaves[i],2,argv));
110 XBT_INFO("Reboot all the VMs");
111 xbt_dynar_foreach(vms,i,vm) {
115 work_batch(slaves_count*2);
117 XBT_INFO("Migrate everyone to the second host.");
118 xbt_dynar_foreach(vms,i,vm) {
119 MSG_vm_migrate(vm,slaves[1]);
121 XBT_INFO("Suspend everyone, move them to the third host, and resume them.");
122 xbt_dynar_foreach(vms,i,vm) {
124 MSG_vm_migrate(vm,slaves[2]);
129 XBT_INFO("Let's shut down the simulation. 10 first processes will be shut down cleanly while the second half will forcefully get killed");
130 for (i = 0; i < slaves_count; i++) {
131 char mailbox_buffer[64];
132 sprintf(mailbox_buffer,"Slave_%d",i);
133 msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
134 MSG_task_send(finalize, mailbox_buffer);
137 XBT_INFO("Wait a while before effective shutdown.");
138 MSG_process_sleep(2);
140 xbt_dynar_foreach(vms,i,vm) {
145 XBT_INFO("Goodbye now!");
147 xbt_dynar_free(&vms);
149 } /* end_of_master */
151 /** Receiver function */
152 int slave_fun(int argc, char *argv[])
154 char mailbox_name[128];
155 msg_task_t task = NULL;
156 _XBT_GNUC_UNUSED int res;
157 /* since the slaves will move around, use slave_%d as mailbox names instead of hostnames */
158 xbt_assert(argc>=2, "slave processes need to be given their rank as parameter");
159 sprintf(mailbox_name,"Slave_%s",argv[1]);
160 XBT_INFO("Slave listenning on %s",argv[1]);
162 res = MSG_task_receive(&(task),mailbox_name);
163 xbt_assert(res == MSG_OK, "MSG_task_get failed");
165 XBT_INFO("Received \"%s\" from mailbox %s", MSG_task_get_name(task),mailbox_name);
166 if (!strcmp(MSG_task_get_name(task), "finalize")) {
167 MSG_task_destroy(task);
171 MSG_task_execute(task);
172 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
173 MSG_task_destroy(task);
181 int main(int argc, char *argv[])
183 msg_error_t res = MSG_OK;
184 xbt_dynar_t hosts_dynar;
185 msg_host_t*hosts= xbt_new(msg_host_t,10);
186 char**hostnames= xbt_new(char*,10);
187 char**masterargv=xbt_new(char*,12);
190 /* Get the arguments */
191 MSG_init(&argc, argv);
193 printf("Usage: %s platform_file\n", argv[0]);
194 printf("example: %s msg_platform.xml\n", argv[0]);
197 printf("Usage: %s platform_file\n", argv[0]);
198 printf("Other parameters (such as the deployment file) are ignored.");
201 /* load the platform file */
202 MSG_create_environment(argv[1]);
203 /* Retrieve the 10 first hosts of the platform file */
204 hosts_dynar = MSG_hosts_as_dynar();
205 xbt_assert(xbt_dynar_length(hosts_dynar)>10,
206 "I need at least 10 hosts in the platform file, but %s contains only %ld hosts_dynar.",
207 argv[1],xbt_dynar_length(hosts_dynar));
209 hosts[i] = xbt_dynar_get_as(hosts_dynar,i,msg_host_t);
210 hostnames[i] = xbt_strdup(MSG_host_get_name(hosts[i]));
212 masterargv[0]=xbt_strdup("master");
214 masterargv[i] = xbt_strdup(MSG_host_get_name(hosts[i-1]));
217 MSG_process_create_with_arguments("master",master,NULL,hosts[0],11,masterargv);
219 XBT_INFO("Simulation time %g", MSG_get_clock());
225 xbt_dynar_free(&hosts_dynar);