Logo AND Algorithmique Numérique Distribuée

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