Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge with master and fix conflicts
[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_INFO("Wait a while before effective shutdown.");
138   MSG_process_sleep(2);
139
140   xbt_dynar_foreach(vms,i,vm) {
141     MSG_vm_shutdown(vm);
142     MSG_vm_destroy(vm);
143   }
144
145   XBT_INFO("Goodbye now!");
146   free(slaves);
147   xbt_dynar_free(&vms);
148   return 0;
149 }                               /* end_of_master */
150
151 /** Receiver function  */
152 int slave_fun(int argc, char *argv[])
153 {
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]);
161   while (1) {
162     res = MSG_task_receive(&(task),mailbox_name);
163     xbt_assert(res == MSG_OK, "MSG_task_get failed");
164
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);
168       break;
169     }
170
171     MSG_task_execute(task);
172     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
173     MSG_task_destroy(task);
174     task = NULL;
175   }
176
177   return 0;
178 }                               /* end_of_slave */
179
180 /** Main function */
181 int main(int argc, char *argv[])
182 {
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);
188   int i;
189
190   /* Get the arguments */
191   MSG_init(&argc, argv);
192   if (argc < 2) {
193     printf("Usage: %s platform_file\n", argv[0]);
194     printf("example: %s msg_platform.xml\n", argv[0]);
195     exit(1);
196   } if (argc>2) {
197     printf("Usage: %s platform_file\n", argv[0]);
198     printf("Other parameters (such as the deployment file) are ignored.");
199   }
200
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));
208   for (i=0;i<10;i++) {
209     hosts[i] = xbt_dynar_get_as(hosts_dynar,i,msg_host_t);
210     hostnames[i] = xbt_strdup(MSG_host_get_name(hosts[i]));
211   }
212   masterargv[0]=xbt_strdup("master");
213   for (i=1;i<11;i++) {
214     masterargv[i] = xbt_strdup(MSG_host_get_name(hosts[i-1]));
215   }
216   masterargv[11]=NULL;
217   MSG_process_create_with_arguments("master",master,NULL,hosts[0],11,masterargv);
218   res = MSG_main();
219   XBT_INFO("Simulation time %g", MSG_get_clock());
220
221   free(hosts);
222   for (i=0;i<10;i++) 
223      free(hostnames[i]);
224   free(hostnames);
225   xbt_dynar_free(&hosts_dynar);
226
227   if (res == MSG_OK)
228     return 0;
229   else
230     return 1;
231 }                               /* end_of_main */