Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
always have display_timing activated for smpi, as it is quite useful
[simgrid.git] / examples / msg / cloud / masterslave_virtual_machines.c
1 /* Copyright (c) 2007-2012. The SimGrid Team. All rights reserved.                             */
2
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. */
5
6 #include <stdio.h>
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 */
9
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 #include "xbt/asserts.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14                              "Messages specific for this msg example");
15
16 /** @addtogroup MSG_examples
17  * 
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.
20  */
21
22 double task_comp_size = 10000000;
23 double task_comm_size = 10000000;
24
25
26 int master(int argc, char *argv[]);
27 int slave_fun(int argc, char *argv[]);
28
29 static void work_batch(int slaves_count) {
30   int i;
31   for (i = 0; i < slaves_count; i++) {
32     char taskname_buffer[64];
33     char mailbox_buffer[64];
34
35     sprintf(taskname_buffer, "Task_%d", i);
36     sprintf(mailbox_buffer,"Slave_%d",i);
37
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),
40         mailbox_buffer);
41   }
42 }
43
44 int master(int argc, char *argv[]) {
45   int slaves_count = 10;
46   msg_host_t *slaves = xbt_new(msg_host_t,10);
47
48   msg_vm_t vm;
49   unsigned int i;
50
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]);
55   }
56
57   /* Launch the sub processes: one VM per host, with one process inside each */
58
59   for (i=0;i<slaves_count;i++) {
60     char slavename[64];
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);
65     argv[2] = NULL;
66
67     char vmName[64];
68     snprintf(vmName, 64, "vm_%d", i);
69
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));
72   }
73
74
75   xbt_dynar_t vms = MSG_vms_as_dynar();
76   XBT_INFO("Launched %ld VMs", xbt_dynar_length(vms));
77
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);
81
82   XBT_INFO("Now suspend all VMs, just for fun");
83
84   xbt_dynar_foreach(vms,i,vm) {
85     MSG_vm_suspend(vm);
86   }
87
88   XBT_INFO("Wait a while");
89   MSG_process_sleep(2);
90
91   XBT_INFO("Enough. Let's resume everybody.");
92   xbt_dynar_foreach(vms,i,vm) {
93     MSG_vm_resume(vm);
94   }
95   XBT_INFO("Sleep long enough for everyone to be done with previous batch of work");
96   MSG_process_sleep(1000-MSG_get_clock());
97
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);
101     char slavename[64];
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));
106     argv[2] = NULL;
107     MSG_vm_bind(vm, MSG_process_create_with_arguments(slavename,slave_fun,NULL,slaves[i],2,argv));
108   }
109
110   XBT_INFO("Reboot all the VMs");
111   xbt_dynar_foreach(vms,i,vm) {
112     MSG_vm_reboot(vm);
113   }
114
115   work_batch(slaves_count*2);
116
117   XBT_INFO("Migrate everyone to the second host.");
118   xbt_dynar_foreach(vms,i,vm) {
119     MSG_vm_migrate(vm,slaves[1]);
120   }
121   XBT_INFO("Suspend everyone, move them to the third host, and resume them.");
122   xbt_dynar_foreach(vms,i,vm) {
123     MSG_vm_suspend(vm);
124     MSG_vm_migrate(vm,slaves[2]);
125     MSG_vm_resume(vm);
126   }
127
128
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);
135   }
136
137   xbt_dynar_foreach(vms,i,vm) {
138     MSG_vm_shutdown(vm);
139     MSG_vm_destroy(vm);
140   }
141
142   XBT_INFO("Goodbye now!");
143   free(slaves);
144   xbt_dynar_free(&vms);
145   return 0;
146 }                               /* end_of_master */
147
148 /** Receiver function  */
149 int slave_fun(int argc, char *argv[])
150 {
151   char mailbox_name[128];
152   msg_task_t task = NULL;
153   _XBT_GNUC_UNUSED int res;
154   /* since the slaves will move around, use slave_%d as mailbox names instead of hostnames */
155   xbt_assert(argc>=2, "slave processes need to be given their rank as parameter");
156   sprintf(mailbox_name,"Slave_%s",argv[1]);
157   XBT_INFO("Slave listenning on %s",argv[1]);
158   while (1) {
159     res = MSG_task_receive(&(task),mailbox_name);
160     xbt_assert(res == MSG_OK, "MSG_task_get failed");
161
162     XBT_INFO("Received \"%s\" from mailbox %s", MSG_task_get_name(task),mailbox_name);
163     if (!strcmp(MSG_task_get_name(task), "finalize")) {
164       MSG_task_destroy(task);
165       break;
166     }
167
168     MSG_task_execute(task);
169     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
170     MSG_task_destroy(task);
171     task = NULL;
172   }
173
174   return 0;
175 }                               /* end_of_slave */
176
177 /** Main function */
178 int main(int argc, char *argv[])
179 {
180   msg_error_t res = MSG_OK;
181   xbt_dynar_t hosts_dynar;
182   msg_host_t*hosts= xbt_new(msg_host_t,10);
183   char**hostnames= xbt_new(char*,10);
184   char**masterargv=xbt_new(char*,12);
185   int i;
186
187   /* Get the arguments */
188   MSG_init(&argc, argv);
189   if (argc < 2) {
190     printf("Usage: %s platform_file\n", argv[0]);
191     printf("example: %s msg_platform.xml\n", argv[0]);
192     exit(1);
193   } if (argc>2) {
194     printf("Usage: %s platform_file\n", argv[0]);
195     printf("Other parameters (such as the deployment file) are ignored.");
196   }
197
198   /* load the platform file */
199   MSG_create_environment(argv[1]);
200   /* Retrieve the 10 first hosts of the platform file */
201   hosts_dynar = MSG_hosts_as_dynar();
202   xbt_assert(xbt_dynar_length(hosts_dynar)>10,
203       "I need at least 10 hosts in the platform file, but %s contains only %ld hosts_dynar.",
204       argv[1],xbt_dynar_length(hosts_dynar));
205   for (i=0;i<10;i++) {
206     hosts[i] = xbt_dynar_get_as(hosts_dynar,i,msg_host_t);
207     hostnames[i] = xbt_strdup(MSG_host_get_name(hosts[i]));
208   }
209   masterargv[0]=xbt_strdup("master");
210   for (i=1;i<11;i++) {
211     masterargv[i] = xbt_strdup(MSG_host_get_name(hosts[i-1]));
212   }
213   masterargv[11]=NULL;
214   MSG_process_create_with_arguments("master",master,NULL,hosts[0],11,masterargv);
215   res = MSG_main();
216   XBT_INFO("Simulation time %g", MSG_get_clock());
217
218   free(hosts);
219   for (i=0;i<10;i++) 
220      free(hostnames[i]);
221   free(hostnames);
222   xbt_dynar_free(&hosts_dynar);
223
224   if (res == MSG_OK)
225     return 0;
226   else
227     return 1;
228 }                               /* end_of_main */