Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into hypervisor
[simgrid.git] / examples / msg / cloud / simple_vm.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"
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
17 static int computation_fun(int argc, char *argv[])
18 {
19   const char *pr_name = MSG_process_get_name(MSG_process_self());
20   const char *host_name = MSG_host_get_name(MSG_host_self());
21
22   msg_task_t task = MSG_task_create("Task", 1000000, 1000000, NULL);
23
24   double clock_sta = MSG_get_clock();
25   MSG_task_execute(task);
26   double clock_end = MSG_get_clock();
27
28   XBT_INFO("%s:%s task executed %g", host_name, pr_name, clock_end - clock_sta);
29
30   MSG_task_destroy(task);
31
32   return 0;
33 }
34
35 static void launch_computation_worker(msg_host_t host)
36 {
37   const char *pr_name = "compute";
38   char **argv = xbt_new(char *, 2);
39   argv[0] = xbt_strdup(pr_name);
40   argv[1] = NULL;
41
42   MSG_process_create_with_arguments(pr_name, computation_fun, NULL, host, 1, argv);
43 }
44
45 struct task_priv {
46   msg_host_t tx_host;
47   msg_process_t tx_proc;
48   double clock_sta;
49 };
50
51 static int communication_tx_fun(int argc, char *argv[])
52 {
53   xbt_assert(argc == 2);
54   const char *mbox = argv[1];
55
56   msg_task_t task = MSG_task_create("Task", 1000000, 1000000, NULL);
57
58   struct task_priv *priv = xbt_new(struct task_priv, 1);
59   priv->tx_proc = MSG_process_self();
60   priv->tx_host = MSG_host_self();
61   priv->clock_sta = MSG_get_clock();
62
63   MSG_task_set_data(task, priv);
64
65   MSG_task_send(task, mbox);
66
67   return 0;
68 }
69
70 static int communication_rx_fun(int argc, char *argv[])
71 {
72   const char *pr_name = MSG_process_get_name(MSG_process_self());
73   const char *host_name = MSG_host_get_name(MSG_host_self());
74   xbt_assert(argc == 2);
75   const char *mbox = argv[1];
76
77   msg_task_t task = NULL;
78   MSG_task_recv(&task, mbox);
79
80   struct task_priv *priv = MSG_task_get_data(task);
81   double clock_end = MSG_get_clock();
82
83   XBT_INFO("%s:%s to %s:%s => %g sec",
84       MSG_host_get_name(priv->tx_host),
85       MSG_process_get_name(priv->tx_proc),
86       host_name, pr_name, clock_end - priv->clock_sta);
87
88   MSG_task_destroy(task);
89
90   return 0;
91 }
92
93 static void launch_communication_worker(msg_host_t tx_host, msg_host_t rx_host)
94 {
95   char *mbox = bprintf("MBOX:%s-%s",
96       MSG_host_get_name(tx_host),
97       MSG_host_get_name(rx_host));
98   char **argv = NULL;
99   
100   const char *pr_name_tx =  "comm_tx";
101   argv = xbt_new(char *, 3);
102   argv[0] = xbt_strdup(pr_name_tx);
103   argv[1] = xbt_strdup(mbox);
104   argv[2] = NULL;
105
106   MSG_process_create_with_arguments(pr_name_tx, communication_tx_fun, NULL, tx_host, 2, argv);
107
108   const char *pr_name_rx =  "comm_rx";  
109   argv = xbt_new(char *, 3);
110   argv[0] = xbt_strdup(pr_name_rx);
111   argv[1] = xbt_strdup(mbox);
112   argv[2] = NULL;
113
114   MSG_process_create_with_arguments(pr_name_rx, communication_rx_fun, NULL, rx_host, 2, argv);
115
116   xbt_free(mbox);
117 }
118
119
120 static int master_main(int argc, char *argv[])
121 {
122   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
123   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
124   msg_host_t pm1 = xbt_dynar_get_as(hosts_dynar, 1, msg_host_t);
125   msg_host_t pm2 = xbt_dynar_get_as(hosts_dynar, 2, msg_host_t);
126   msg_vm_t vm0, vm1;
127
128
129   XBT_INFO("## Test 1 (started): check computation on normal PMs");
130
131   XBT_INFO("### Put a task on a PM");
132   launch_computation_worker(pm0);
133   MSG_process_sleep(2);
134
135   XBT_INFO("### Put two tasks on a PM");
136   launch_computation_worker(pm0);
137   launch_computation_worker(pm0);
138   MSG_process_sleep(2);
139
140   XBT_INFO("### Put a task on each PM");
141   launch_computation_worker(pm0);
142   launch_computation_worker(pm1);
143   MSG_process_sleep(2);
144
145   XBT_INFO("## Test 1 (ended)");
146
147
148   XBT_INFO("## Test 2 (started): check impact of running a task inside a VM (there is no degradation for the moment)");
149
150   XBT_INFO("### Put a VM on a PM, and put a task to the VM");
151   vm0 = MSG_vm_create_core(pm0, "VM0");
152   MSG_vm_start(vm0);
153   launch_computation_worker(vm0);
154   MSG_process_sleep(2);
155   MSG_vm_destroy(vm0);
156
157   XBT_INFO("## Test 2 (ended)");
158
159   
160   XBT_INFO("## Test 3 (started): check impact of running a task collocated with a VM (there is no VM noise for the moment)");
161
162   XBT_INFO("### Put a VM on a PM, and put a task to the PM");
163   vm0 = MSG_vm_create_core(pm0, "VM0");
164   MSG_vm_start(vm0);
165   launch_computation_worker(pm0);
166   MSG_process_sleep(2);
167   MSG_vm_destroy(vm0);
168
169   XBT_INFO("## Test 3 (ended)");
170
171
172   XBT_INFO("## Test 4 (started): compare the cost of running two tasks inside two different VMs collocated or not (for the moment, there is no degradation for the VMs. Hence, the time should be equals to the time of test 1");
173
174   XBT_INFO("### Put two VMs on a PM, and put a task to each VM");
175   vm0 = MSG_vm_create_core(pm0, "VM0");
176   vm1 = MSG_vm_create_core(pm0, "VM1");
177   MSG_vm_start(vm0);
178   MSG_vm_start(vm1);
179   launch_computation_worker(vm0);
180   launch_computation_worker(vm1);
181   MSG_process_sleep(2);
182   MSG_vm_destroy(vm0);
183   MSG_vm_destroy(vm1);
184
185   XBT_INFO("### Put a VM on each PM, and put a task to each VM");
186   vm0 = MSG_vm_create_core(pm0, "VM0");
187   vm1 = MSG_vm_create_core(pm1, "VM1");
188   MSG_vm_start(vm0);
189   MSG_vm_start(vm1);
190   launch_computation_worker(vm0);
191   launch_computation_worker(vm1);
192   MSG_process_sleep(2);
193   MSG_vm_destroy(vm0);
194   MSG_vm_destroy(vm1);
195   XBT_INFO("## Test 4 (ended)");
196
197   
198   XBT_INFO("## Test 5  (started): Analyse network impact");
199   XBT_INFO("### Make a connection between PM0 and PM1");
200   launch_communication_worker(pm0, pm1);
201   MSG_process_sleep(5);
202
203   XBT_INFO("### Make two connection between PM0 and PM1");
204   launch_communication_worker(pm0, pm1);
205   launch_communication_worker(pm0, pm1);
206   MSG_process_sleep(5);
207
208   XBT_INFO("### Make a connection between PM0 and VM0@PM0");
209   vm0 = MSG_vm_create_core(pm0, "VM0");
210   MSG_vm_start(vm0);
211   launch_communication_worker(pm0, vm0);
212   MSG_process_sleep(5);
213   MSG_vm_destroy(vm0);
214
215   XBT_INFO("### Make a connection between PM0 and VM0@PM1");
216   vm0 = MSG_vm_create_core(pm1, "VM0");
217   MSG_vm_start(vm0);
218   launch_communication_worker(pm0, vm0);
219   MSG_process_sleep(5);
220   MSG_vm_destroy(vm0);
221
222   XBT_INFO("### Make two connections between PM0 and VM0@PM1");
223   vm0 = MSG_vm_create_core(pm1, "VM0");
224   MSG_vm_start(vm0);
225   launch_communication_worker(pm0, vm0);
226   launch_communication_worker(pm0, vm0);
227   MSG_process_sleep(5);
228   MSG_vm_destroy(vm0);
229
230   XBT_INFO("### Make a connection between PM0 and VM0@PM1, and also make a connection between PM0 and PM1");
231   vm0 = MSG_vm_create_core(pm1, "VM0");
232   MSG_vm_start(vm0);
233   launch_communication_worker(pm0, vm0);
234   launch_communication_worker(pm0, pm1);
235   MSG_process_sleep(5);
236   MSG_vm_destroy(vm0);
237
238   XBT_INFO("### Make a connection between VM0@PM0 and PM1@PM1, and also make a connection between VM0@PM0 and VM1@PM1");
239   vm0 = MSG_vm_create_core(pm0, "VM0");
240   vm1 = MSG_vm_create_core(pm1, "VM1");
241   MSG_vm_start(vm0);
242   MSG_vm_start(vm1);
243   launch_communication_worker(vm0, vm1);
244   launch_communication_worker(vm0, vm1);
245   MSG_process_sleep(5);
246   MSG_vm_destroy(vm0);
247   MSG_vm_destroy(vm1);
248
249   XBT_INFO("## Test 5 (ended)");
250
251
252   XBT_INFO("## Test 6 (started): Check migration impact (not yet implemented neither on the CPU resource nor on the network one");
253   XBT_INFO("### Relocate VM0 between PM0 and PM1");
254   vm0 = MSG_vm_create_core(pm0, "VM0");
255   MSG_vm_start(vm0);
256   launch_communication_worker(vm0, pm2);
257   MSG_process_sleep(0.01);
258   MSG_vm_migrate(vm0, pm1);
259   MSG_process_sleep(0.01);
260   MSG_vm_migrate(vm0, pm0);
261   MSG_process_sleep(5);
262   MSG_vm_destroy(vm0);
263   XBT_INFO("## Test 6 (ended)");
264   
265   return 0;
266 }
267
268 static void launch_master(msg_host_t host)
269 {
270   const char *pr_name = "master_";
271   char **argv = xbt_new(char *, 2);
272   argv[0] = xbt_strdup(pr_name);
273   argv[1] = NULL;
274
275   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
276 }
277
278
279 int main(int argc, char *argv[])
280 {
281   /* Get the arguments */
282   MSG_init(&argc, argv);
283
284   /* load the platform file */
285   xbt_assert(argc == 2);
286   MSG_create_environment(argv[1]);
287
288   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
289   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
290   launch_master(pm0);
291
292   int res = MSG_main();
293   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
294
295
296   return !(res == MSG_OK);
297 }