Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update cloud example to use MSG_vm_reboot. Update the tesh file
[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   int i;
49
50   /* Retrive the hostnames constituting our playground today */
51   for (i = 1; i < argc; i++) {
52     slaves[i - 1] = MSG_get_host_by_name(argv[i]);
53     xbt_assert(slaves[i - 1] != NULL, "Cannot use inexistent host %s", argv[i]);
54   }
55
56   /* Launch the sub processes: one VM per host, with one process inside each */
57
58   for (i=0;i<slaves_count;i++) {
59     char slavename[64];
60     sprintf(slavename,"Slave %d",i);
61     char**argv=xbt_new(char*,3);
62     argv[0] = xbt_strdup(slavename);
63     argv[1] = bprintf("%d",i);
64     argv[2] = NULL;
65     msg_vm_t vm = MSG_vm_start(slaves[i],2);
66     MSG_vm_bind(vm, MSG_process_create_with_arguments(slavename,slave_fun,NULL,slaves[i],2,argv));
67   }
68
69
70   xbt_dynar_t vms = MSG_vms_as_dynar();
71   XBT_INFO("Launched %ld VMs", xbt_dynar_length(vms));
72
73   /* Send a bunch of work to every one */
74   XBT_INFO("Send a first batch of work to every one");
75   work_batch(slaves_count);
76
77   XBT_INFO("Now suspend all VMs, just for fun");
78   for (i=0;i<xbt_dynar_length(vms);i++)
79     MSG_vm_suspend(xbt_dynar_get_as(vms,i,msg_vm_t));
80
81   XBT_INFO("Wait a while");
82   MSG_process_sleep(2);
83
84   XBT_INFO("Enough. Let's resume everybody.");
85   for (i=0;i<xbt_dynar_length(vms);i++)
86     MSG_vm_resume(xbt_dynar_get_as(vms,i,msg_vm_t));
87
88   XBT_INFO("Sleep long enough for everyone to be done with previous batch of work");
89   MSG_process_sleep(1000-MSG_get_clock());
90
91   XBT_INFO("Add one more process per VM");
92   for (i=0;i<xbt_dynar_length(vms);i++) {
93     msg_vm_t vm = xbt_dynar_get_as(vms,i,msg_vm_t);
94     char slavename[64];
95     sprintf(slavename,"Slave %ld",i+xbt_dynar_length(vms));
96     char**argv=xbt_new(char*,3);
97     argv[0] = xbt_strdup(slavename);
98     argv[1] = bprintf("%ld",i+xbt_dynar_length(vms));
99     argv[2] = NULL;
100     MSG_vm_bind(vm, MSG_process_create_with_arguments(slavename,slave_fun,NULL,slaves[i],2,argv));
101   }
102
103   XBT_INFO("Reboot all the VMs");
104   for (i=0;i<xbt_dynar_length(vms);i++)
105     MSG_vm_reboot(xbt_dynar_get_as(vms,i,msg_vm_t));
106
107   work_batch(slaves_count*2);
108
109   XBT_INFO("Migrate everyone to the second host.");
110   for (i=0;i<xbt_dynar_length(vms);i++)
111     MSG_vm_migrate(xbt_dynar_get_as(vms,i,msg_vm_t),slaves[1]);
112
113   XBT_INFO("Suspend everyone, move them to the third host, and resume them.");
114   for (i=0;i<xbt_dynar_length(vms);i++) {
115     msg_vm_t vm = xbt_dynar_get_as(vms,i,msg_vm_t);
116     MSG_vm_suspend(vm);
117     MSG_vm_migrate(vm,slaves[2]);
118     MSG_vm_resume(vm);
119   }
120
121
122   XBT_INFO("Let's shut down the simulation. 10 first processes will be shut down cleanly while the second half will forcefully get killed");
123   for (i = 0; i < slaves_count; i++) {
124     char mailbox_buffer[64];
125     sprintf(mailbox_buffer,"Slave_%d",i);
126     msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
127     MSG_task_send(finalize, mailbox_buffer);
128   }
129
130   for (i=0;i<xbt_dynar_length(vms);i++) {
131     msg_vm_t vm = xbt_dynar_get_as(vms,i,msg_vm_t);
132     MSG_vm_shutdown(vm);
133     MSG_vm_destroy(vm);
134   }
135
136   XBT_INFO("Goodbye now!");
137   free(slaves);
138   xbt_dynar_free(&vms);
139   return 0;
140 }                               /* end_of_master */
141
142 /** Receiver function  */
143 int slave_fun(int argc, char *argv[])
144 {
145   char *mailbox_name;
146   msg_task_t task = NULL;
147   _XBT_GNUC_UNUSED int res;
148   /* since the slaves will move around, use slave_%d as mailbox names instead of hostnames */
149   xbt_assert(argc>=2, "slave processes need to be given their rank as parameter");
150   mailbox_name=bprintf("Slave_%s",argv[1]);
151   XBT_INFO("Slave listenning on %s",argv[1]);
152   while (1) {
153     res = MSG_task_receive(&(task),mailbox_name);
154     xbt_assert(res == MSG_OK, "MSG_task_get failed");
155
156     XBT_INFO("Received \"%s\" from mailbox %s", MSG_task_get_name(task),mailbox_name);
157     if (!strcmp(MSG_task_get_name(task), "finalize")) {
158       MSG_task_destroy(task);
159       break;
160     }
161
162     MSG_task_execute(task);
163     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
164     MSG_task_destroy(task);
165     task = NULL;
166   }
167
168   free(mailbox_name);
169   return 0;
170 }                               /* end_of_slave */
171
172 /** Main function */
173 int main(int argc, char *argv[])
174 {
175   msg_error_t res = MSG_OK;
176   xbt_dynar_t hosts_dynar;
177   msg_host_t*hosts= xbt_new(msg_host_t,10);
178   char**hostnames= xbt_new(char*,10);
179   char**masterargv=xbt_new(char*,12);
180   int i;
181
182   /* Get the arguments */
183   MSG_init(&argc, argv);
184   if (argc < 2) {
185     printf("Usage: %s platform_file\n", argv[0]);
186     printf("example: %s msg_platform.xml\n", argv[0]);
187     exit(1);
188   } if (argc>2) {
189     printf("Usage: %s platform_file\n", argv[0]);
190     printf("Other parameters (such as the deployment file) are ignored.");
191   }
192
193   /* load the platform file */
194   MSG_create_environment(argv[1]);
195   /* Retrieve the 10 first hosts of the platform file */
196   hosts_dynar = MSG_hosts_as_dynar();
197   xbt_assert(xbt_dynar_length(hosts_dynar)>10,
198       "I need at least 10 hosts in the platform file, but %s contains only %ld hosts_dynar.",
199       argv[1],xbt_dynar_length(hosts_dynar));
200   for (i=0;i<10;i++) {
201     hosts[i] = xbt_dynar_get_as(hosts_dynar,i,msg_host_t);
202     hostnames[i] = xbt_strdup(MSG_host_get_name(hosts[i]));
203   }
204   masterargv[0]=xbt_strdup("master");
205   for (i=1;i<11;i++) {
206     masterargv[i] = xbt_strdup(MSG_host_get_name(hosts[i-1]));
207   }
208   masterargv[11]=NULL;
209   MSG_process_create_with_arguments("master",master,NULL,hosts[0],11,masterargv);
210   res = MSG_main();
211   XBT_INFO("Simulation time %g", MSG_get_clock());
212
213   MSG_clean();
214   free(hosts);
215   free(hostnames);
216   xbt_dynar_free(&hosts_dynar);
217
218   if (res == MSG_OK)
219     return 0;
220   else
221     return 1;
222 }                               /* end_of_main */