Logo AND Algorithmique Numérique Distribuée

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