Logo AND Algorithmique Numérique Distribuée

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