Logo AND Algorithmique Numérique Distribuée

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