Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add few comments - Adrien
[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 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 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 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 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 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   char *pr_name = NULL;
100
101   pr_name = "comm_tx";
102   argv = xbt_new(char *, 3);
103   argv[0] = xbt_strdup(pr_name);
104   argv[1] = xbt_strdup(mbox);
105   argv[2] = NULL;
106
107   MSG_process_create_with_arguments(pr_name, communication_tx_fun, NULL, tx_host, 2, argv);
108
109   pr_name = "comm_rx";
110   argv = xbt_new(char *, 3);
111   argv[0] = xbt_strdup(pr_name);
112   argv[1] = xbt_strdup(mbox);
113   argv[2] = NULL;
114
115   MSG_process_create_with_arguments(pr_name, communication_rx_fun, NULL, rx_host, 2, argv);
116
117   xbt_free(mbox);
118 }
119
120
121 int master_main(int argc, char *argv[])
122 {
123   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
124   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
125   msg_host_t pm1 = xbt_dynar_get_as(hosts_dynar, 1, msg_host_t);
126   msg_host_t pm2 = xbt_dynar_get_as(hosts_dynar, 2, msg_host_t);
127   msg_vm_t vm0, vm1;
128
129   XBT_INFO("## Test 1 (started): check computation on normal PM");
130   XBT_INFO("### Put a task on a PM");
131   launch_computation_worker(pm0);
132   MSG_process_sleep(2);
133
134   XBT_INFO("### Put two tasks on a PM");
135   launch_computation_worker(pm0);
136   launch_computation_worker(pm0);
137   MSG_process_sleep(2);
138
139   XBT_INFO("### Put a task on each PM");
140   launch_computation_worker(pm0);
141   launch_computation_worker(pm1);
142   MSG_process_sleep(2);
143   XBT_INFO("## Test 1 (ended)");
144
145   XBT_INFO("## Test 2  (started): check impact of running a task inside a VM (there is no degradation for the moment)");
146   XBT_INFO("### Put a VM on a PM, and put a task to the VM");
147   vm0 = MSG_vm_create_core(pm0, "VM0");
148   MSG_vm_start(vm0);
149   launch_computation_worker(vm0);
150   MSG_process_sleep(2);
151   MSG_vm_destroy(vm0);
152   XBT_INFO("## Test 2 (ended)");
153   
154   XBT_INFO("## Test 3  (started): check impact of running a task collocated with a VM (there is no VM noise for the moment)");
155   XBT_INFO("### Put a VM on a PM, and put a task to the PM (FIXME: broken)");
156   vm0 = MSG_vm_create_core(pm0, "VM0");
157   MSG_vm_start(vm0);
158   launch_computation_worker(pm0);
159   MSG_process_sleep(2);
160   MSG_vm_destroy(vm0);
161   XBT_INFO("## Test 3 (ended)");
162
163   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  \
164                                                                                         degradation for the VMs. Hence, the time should be equals to the time of test 1");
165   XBT_INFO("### Put two VMs on a PM, and put a task to each VM");
166   vm0 = MSG_vm_create_core(pm0, "VM0");
167   vm1 = MSG_vm_create_core(pm0, "VM1");
168   MSG_vm_start(vm0);
169   MSG_vm_start(vm1);
170   launch_computation_worker(vm0);
171   launch_computation_worker(vm1);
172   MSG_process_sleep(2);
173   MSG_vm_destroy(vm0);
174   MSG_vm_destroy(vm1);
175
176   XBT_INFO("### Put a VM on each PM, and put a task to each VM");
177   vm0 = MSG_vm_create_core(pm0, "VM0");
178   vm1 = MSG_vm_create_core(pm1, "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   XBT_INFO("## Test 4 (ended)");
187
188   
189   XBT_INFO("## Test 5  (started): Analyse network impact");
190   XBT_INFO("### Make a connection between PM0 and PM1");
191   launch_communication_worker(pm0, pm1);
192   MSG_process_sleep(5);
193
194   XBT_INFO("### Make two connection between PM0 and PM1");
195   launch_communication_worker(pm0, pm1);
196   launch_communication_worker(pm0, pm1);
197   MSG_process_sleep(5);
198
199   XBT_INFO("### Make a connection between PM0 and VM0@PM0");
200   vm0 = MSG_vm_create_core(pm0, "VM0");
201   MSG_vm_start(vm0);
202   launch_communication_worker(pm0, vm0);
203   MSG_process_sleep(5);
204   MSG_vm_destroy(vm0);
205
206   XBT_INFO("### Make a connection between PM0 and VM0@PM1");
207   vm0 = MSG_vm_create_core(pm1, "VM0");
208   MSG_vm_start(vm0);
209   launch_communication_worker(pm0, vm0);
210   MSG_process_sleep(5);
211   MSG_vm_destroy(vm0);
212
213   XBT_INFO("### Make two connections between PM0 and VM0@PM1");
214   vm0 = MSG_vm_create_core(pm1, "VM0");
215   MSG_vm_start(vm0);
216   launch_communication_worker(pm0, vm0);
217   launch_communication_worker(pm0, vm0);
218   MSG_process_sleep(5);
219   MSG_vm_destroy(vm0);
220
221   XBT_INFO("### Make a connection between PM0 and VM0@PM1, and also make a connection between PM0 and PM1");
222   vm0 = MSG_vm_create_core(pm1, "VM0");
223   MSG_vm_start(vm0);
224   launch_communication_worker(pm0, vm0);
225   launch_communication_worker(pm0, pm1);
226   MSG_process_sleep(5);
227   MSG_vm_destroy(vm0);
228
229   XBT_INFO("### Make a connection between VM0@PM0 and PM1@PM1, and also make a connection between VM0@PM0 and VM1@PM1");
230   vm0 = MSG_vm_create_core(pm0, "VM0");
231   vm1 = MSG_vm_create_core(pm1, "VM1");
232   MSG_vm_start(vm0);
233   MSG_vm_start(vm1);
234   launch_communication_worker(vm0, vm1);
235   launch_communication_worker(vm0, vm1);
236   MSG_process_sleep(5);
237   MSG_vm_destroy(vm0);
238   MSG_vm_destroy(vm1);
239   XBT_INFO("## Test 5 (ended)");
240
241
242   XBT_INFO("## Test 6 (started): Check migration impact (not yet implemented neither on the CPU resource nor on the network one");
243   XBT_INFO("### Relocate VM0 between PM0 and PM1");
244   vm0 = MSG_vm_create_core(pm0, "VM0");
245   MSG_vm_start(vm0);
246   launch_communication_worker(vm0, pm2);
247   MSG_process_sleep(0.01);
248   MSG_vm_migrate(vm0, pm1);
249   MSG_process_sleep(0.01);
250   MSG_vm_migrate(vm0, pm0);
251   MSG_process_sleep(5);
252   MSG_vm_destroy(vm0);
253   XBT_INFO("## Test 6 (ended)");
254   
255   return 0;
256 }
257
258 void launch_master(msg_host_t host)
259 {
260   const char *pr_name = "master_";
261   char **argv = xbt_new(char *, 2);
262   argv[0] = xbt_strdup(pr_name);
263   argv[1] = NULL;
264
265   msg_process_t pr = MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
266 }
267
268
269 int main(int argc, char *argv[])
270 {
271   /* Get the arguments */
272   MSG_init(&argc, argv);
273
274   /* load the platform file */
275   MSG_create_environment(argv[1]);
276
277   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
278   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
279   launch_master(pm0);
280
281   int res = MSG_main();
282   XBT_INFO("Simulation time %g", MSG_get_clock());
283
284   XBT_INFO("bye");
285
286   return 0;
287 }