Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cc0694f11020600998df0ebcaae83796f703150c
[simgrid.git] / examples / c / cloud-simple / cloud-simple.c
1 /* Copyright (c) 2007-2020. 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/actor.h"
7 #include "simgrid/engine.h"
8 #include "simgrid/host.h"
9 #include "simgrid/mailbox.h"
10 #include "simgrid/plugins/live_migration.h"
11 #include "simgrid/vm.h"
12 #include "xbt/log.h"
13 #include "xbt/str.h"
14 #include "xbt/sysdep.h"
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(cloud_simple, "Messages specific for this example");
17
18 static void computation_fun(int argc, char* argv[])
19 {
20   const char* pr_name   = sg_actor_get_name(sg_actor_self());
21   const char* host_name = sg_host_get_name(sg_host_self());
22
23   double clock_sta = simgrid_get_clock();
24   sg_actor_execute(1000000);
25   double clock_end = simgrid_get_clock();
26
27   XBT_INFO("%s:%s task executed %g", host_name, pr_name, clock_end - clock_sta);
28 }
29
30 static void launch_computation_worker(sg_host_t host)
31 {
32   sg_actor_create("compute", host, computation_fun, 0, NULL);
33 }
34
35 struct task_priv {
36   sg_host_t tx_host;
37   sg_actor_t tx_proc;
38   double clock_sta;
39 };
40
41 static void communication_tx_fun(int argc, char* argv[])
42 {
43   xbt_assert(argc == 2);
44   sg_mailbox_t mbox = sg_mailbox_by_name(argv[1]);
45
46   struct task_priv* priv = xbt_new(struct task_priv, 1);
47   priv->tx_proc          = sg_actor_self();
48   priv->tx_host          = sg_host_self();
49   priv->clock_sta        = simgrid_get_clock();
50
51   sg_mailbox_put(mbox, priv, 1000000);
52 }
53
54 static void communication_rx_fun(int argc, char* argv[])
55 {
56   const char* pr_name   = sg_actor_get_name(sg_actor_self());
57   const char* host_name = sg_host_get_name(sg_host_self());
58   xbt_assert(argc == 2);
59   sg_mailbox_t mbox = sg_mailbox_by_name(argv[1]);
60
61   struct task_priv* priv = (struct task_priv*)sg_mailbox_get(mbox);
62   double clock_end       = simgrid_get_clock();
63
64   XBT_INFO("%s:%s to %s:%s => %g sec", sg_host_get_name(priv->tx_host), sg_actor_get_name(priv->tx_proc), host_name,
65            pr_name, clock_end - priv->clock_sta);
66
67   free(priv);
68 }
69
70 static void launch_communication_worker(sg_host_t tx_host, sg_host_t rx_host)
71 {
72   char* mbox = bprintf("MBOX:%s-%s", sg_host_get_name(tx_host), sg_host_get_name(rx_host));
73
74   const char* tx_argv[] = {"comm_tx", mbox, NULL};
75   sg_actor_create("comm_tx", tx_host, communication_tx_fun, 2, tx_argv);
76
77   const char* rx_argv[] = {"comm_rx", mbox, NULL};
78   sg_actor_create("comm_rx", rx_host, communication_rx_fun, 2, rx_argv);
79
80   xbt_free(mbox);
81 }
82
83 static void master_main(int argc, char* argv[])
84 {
85   sg_host_t pm0 = sg_host_by_name("Fafard");
86   sg_host_t pm1 = sg_host_by_name("Tremblay");
87   sg_host_t pm2 = sg_host_by_name("Bourassa");
88
89   XBT_INFO("## Test 1 (started): check computation on normal PMs");
90
91   XBT_INFO("### Put a task on a PM");
92   launch_computation_worker(pm0);
93   sg_actor_sleep_for(2);
94
95   XBT_INFO("### Put two tasks on a PM");
96   launch_computation_worker(pm0);
97   launch_computation_worker(pm0);
98   sg_actor_sleep_for(2);
99
100   XBT_INFO("### Put a task on each PM");
101   launch_computation_worker(pm0);
102   launch_computation_worker(pm1);
103   sg_actor_sleep_for(2);
104
105   XBT_INFO("## Test 1 (ended)");
106
107   XBT_INFO("## Test 2 (started): check impact of running a task inside a VM (there is no degradation for the moment)");
108
109   XBT_INFO("### Put a VM on a PM, and put a task to the VM");
110   sg_vm_t vm0 = sg_vm_create_core(pm0, "VM0");
111   sg_vm_start(vm0);
112   launch_computation_worker((sg_host_t)vm0);
113   sg_actor_sleep_for(2);
114   sg_vm_destroy(vm0);
115
116   XBT_INFO("## Test 2 (ended)");
117
118   XBT_INFO(
119       "## Test 3 (started): check impact of running a task collocated with a VM (there is no VM noise for the moment)");
120
121   XBT_INFO("### Put a VM on a PM, and put a task to the PM");
122   vm0 = sg_vm_create_core(pm0, "VM0");
123   sg_vm_start(vm0);
124   launch_computation_worker(pm0);
125   sg_actor_sleep_for(2);
126   sg_vm_destroy(vm0);
127
128   XBT_INFO("## Test 3 (ended)");
129
130   XBT_INFO("## Test 4 (started): compare the cost of running two tasks inside two different VMs collocated or not (for"
131            " the moment, there is no degradation for the VMs. Hence, the time should be equals to the time of test 1");
132
133   XBT_INFO("### Put two VMs on a PM, and put a task to each VM");
134   vm0         = sg_vm_create_core(pm0, "VM0");
135   sg_vm_t vm1 = sg_vm_create_core(pm0, "VM1");
136   sg_vm_start(vm0);
137   sg_vm_start(vm1);
138   launch_computation_worker((sg_host_t)vm0);
139   launch_computation_worker((sg_host_t)vm1);
140   sg_actor_sleep_for(2);
141   sg_vm_destroy(vm0);
142   sg_vm_destroy(vm1);
143
144   XBT_INFO("### Put a VM on each PM, and put a task to each VM");
145   vm0 = sg_vm_create_core(pm0, "VM0");
146   vm1 = sg_vm_create_core(pm1, "VM1");
147   sg_vm_start(vm0);
148   sg_vm_start(vm1);
149   launch_computation_worker((sg_host_t)vm0);
150   launch_computation_worker((sg_host_t)vm1);
151   sg_actor_sleep_for(2);
152   sg_vm_destroy(vm0);
153   sg_vm_destroy(vm1);
154   XBT_INFO("## Test 4 (ended)");
155
156   XBT_INFO("## Test 5  (started): Analyse network impact");
157   XBT_INFO("### Make a connection between PM0 and PM1");
158   launch_communication_worker(pm0, pm1);
159   sg_actor_sleep_for(5);
160
161   XBT_INFO("### Make two connection between PM0 and PM1");
162   launch_communication_worker(pm0, pm1);
163   launch_communication_worker(pm0, pm1);
164   sg_actor_sleep_for(5);
165
166   XBT_INFO("### Make a connection between PM0 and VM0@PM0");
167   vm0 = sg_vm_create_core(pm0, "VM0");
168   sg_vm_start(vm0);
169   launch_communication_worker(pm0, (sg_host_t)vm0);
170   sg_actor_sleep_for(5);
171   sg_vm_destroy(vm0);
172
173   XBT_INFO("### Make a connection between PM0 and VM0@PM1");
174   vm0 = sg_vm_create_core(pm1, "VM0");
175   sg_vm_start(vm0);
176   launch_communication_worker(pm0, (sg_host_t)vm0);
177   sg_actor_sleep_for(5);
178   sg_vm_destroy(vm0);
179
180   XBT_INFO("### Make two connections between PM0 and VM0@PM1");
181   vm0 = sg_vm_create_core(pm1, "VM0");
182   sg_vm_start(vm0);
183   launch_communication_worker(pm0, (sg_host_t)vm0);
184   launch_communication_worker(pm0, (sg_host_t)vm0);
185   sg_actor_sleep_for(5);
186   sg_vm_destroy(vm0);
187
188   XBT_INFO("### Make a connection between PM0 and VM0@PM1, and also make a connection between PM0 and PM1");
189   vm0 = sg_vm_create_core(pm1, "VM0");
190   sg_vm_start(vm0);
191   launch_communication_worker(pm0, (sg_host_t)vm0);
192   launch_communication_worker(pm0, pm1);
193   sg_actor_sleep_for(5);
194   sg_vm_destroy(vm0);
195
196   XBT_INFO("### Make a connection between VM0@PM0 and PM1@PM1, and also make a connection between VM0@PM0 and VM1@PM1");
197   vm0 = sg_vm_create_core(pm0, "VM0");
198   vm1 = sg_vm_create_core(pm1, "VM1");
199   sg_vm_start(vm0);
200   sg_vm_start(vm1);
201   launch_communication_worker((sg_host_t)vm0, (sg_host_t)vm1);
202   launch_communication_worker((sg_host_t)vm0, (sg_host_t)vm1);
203   sg_actor_sleep_for(5);
204   sg_vm_destroy(vm0);
205   sg_vm_destroy(vm1);
206
207   XBT_INFO("## Test 5 (ended)");
208
209   XBT_INFO("## Test 6 (started): Check migration impact (not yet implemented neither on the CPU resource nor on the"
210            " network one");
211   XBT_INFO("### Relocate VM0 between PM0 and PM1");
212   vm0 = sg_vm_create_core(pm0, "VM0");
213   sg_vm_set_ramsize(vm0, 1L * 1024 * 1024 * 1024); // 1GiB
214
215   sg_vm_start(vm0);
216   launch_communication_worker((sg_host_t)vm0, pm2);
217   sg_actor_sleep_for(0.01);
218   sg_vm_migrate(vm0, pm1);
219   sg_actor_sleep_for(0.01);
220   sg_vm_migrate(vm0, pm0);
221   sg_actor_sleep_for(5);
222   sg_vm_destroy(vm0);
223   XBT_INFO("## Test 6 (ended)");
224 }
225
226 int main(int argc, char* argv[])
227 {
228   /* Get the arguments */
229   simgrid_init(&argc, argv);
230   sg_vm_live_migration_plugin_init();
231
232   /* load the platform file */
233   simgrid_load_platform(argv[1]);
234
235   sg_actor_create("master_", sg_host_by_name("Fafard"), master_main, 0, NULL);
236
237   simgrid_run();
238   XBT_INFO("Simulation time %g", simgrid_get_clock());
239
240   return 0;
241 }