Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8e37ece3d737794d09864487c3cf43e55b7bf107
[simgrid.git] / examples / c / cloud-capping / cloud-capping.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/exec.h"
9 #include "simgrid/host.h"
10 #include "simgrid/plugins/live_migration.h"
11 #include "simgrid/vm.h"
12
13 #include "xbt/asserts.h"
14 #include "xbt/log.h"
15 #include "xbt/str.h"
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(cloud_capping, "Messages specific for this example");
18
19 static void worker_main(int argc, char* argv[])
20 {
21   xbt_assert(argc == 4);
22   double computation_amount = xbt_str_parse_double(argv[1], "Invalid computation amount: %s");
23   int use_bound             = xbt_str_parse_int(argv[2], "Second parameter (use_bound) should be 0 or 1 but is: %s");
24   double bound              = xbt_str_parse_double(argv[3], "Invalid bound: %s");
25
26   double clock_sta = simgrid_get_clock();
27
28   sg_exec_t exec = sg_actor_exec_init(computation_amount);
29   if (use_bound) {
30     if (bound < 1e-12) /* close enough to 0 without any floating precision surprise */
31       XBT_INFO("bound == 0 means no capping (i.e., unlimited).");
32     sg_exec_set_bound(exec, bound);
33   }
34   sg_exec_wait(exec);
35
36   double clock_end     = simgrid_get_clock();
37   double duration      = clock_end - clock_sta;
38   double flops_per_sec = computation_amount / duration;
39
40   if (use_bound)
41     XBT_INFO("bound to %f => duration %f (%f flops/s)", bound, duration, flops_per_sec);
42   else
43     XBT_INFO("not bound => duration %f (%f flops/s)", duration, flops_per_sec);
44 }
45
46 static void launch_worker(sg_host_t host, const char* pr_name, double computation_amount, int use_bound, double bound)
47 {
48   char* argv1        = bprintf("%f", computation_amount);
49   char* argv2        = bprintf("%d", use_bound);
50   char* argv3        = bprintf("%f", bound);
51   const char* argv[] = {pr_name, argv1, argv2, argv3, NULL};
52
53   sg_actor_t actor = sg_actor_init(pr_name, host);
54   sg_actor_start(actor, worker_main, 4, argv);
55
56   free(argv1);
57   free(argv2);
58   free(argv3);
59 }
60
61 static void worker_busy_loop(int argc, char* argv[])
62 {
63   xbt_assert(argc > 2);
64   char* name              = argv[1];
65   double speed            = xbt_str_parse_double(argv[2], "Invalid speed value");
66   double exec_remain_prev = 1e11;
67
68   sg_exec_t exec = sg_actor_exec_async(exec_remain_prev);
69   for (int i = 0; i < 10; i++) {
70     if (speed > 0) {
71       double new_bound = (speed / 10) * i;
72       XBT_INFO("set bound of VM1 to %f", new_bound);
73       sg_vm_set_bound(((sg_vm_t)sg_actor_get_host(sg_actor_self())), new_bound);
74     }
75     sg_actor_sleep_for(100);
76     double exec_remain_now = sg_exec_get_remaining(exec);
77     double flops_per_sec   = exec_remain_prev - exec_remain_now;
78     XBT_INFO("%s@%s: %.0f flops/s", name, sg_host_get_name(sg_actor_get_host(sg_actor_self())), flops_per_sec / 100);
79     exec_remain_prev = exec_remain_now;
80     sg_actor_sleep_for(1);
81   }
82
83   sg_exec_wait(exec);
84 }
85
86 static void test_dynamic_change()
87 {
88   sg_host_t pm0 = sg_host_by_name("Fafard");
89
90   sg_vm_t vm0 = sg_vm_create_core(pm0, "VM0");
91   sg_vm_t vm1 = sg_vm_create_core(pm0, "VM1");
92   sg_vm_start(vm0);
93   sg_vm_start(vm1);
94
95   int w0_argc           = 3;
96   const char* w0_argv[] = {"worker0", "Task0", "-1.0", NULL};
97   sg_actor_t w0         = sg_actor_init("worker0", (sg_host_t)vm0);
98   sg_actor_start(w0, worker_busy_loop, w0_argc, w0_argv);
99
100   int w1_argc           = 3;
101   char* speed           = bprintf("%f", sg_host_speed(pm0));
102   const char* w1_argv[] = {"worker1", "Task1", speed, NULL};
103
104   sg_actor_t w1 = sg_actor_init("worker1", (sg_host_t)vm1);
105   sg_actor_start(w1, worker_busy_loop, w1_argc, w1_argv);
106
107   sg_actor_sleep_for(3000); // let the tasks end
108
109   sg_vm_destroy(vm0);
110   sg_vm_destroy(vm1);
111   free(speed);
112 }
113
114 static void test_one_task(sg_host_t hostA)
115 {
116   const double cpu_speed          = sg_host_speed(hostA);
117   const double computation_amount = cpu_speed * 10;
118   const char* hostA_name          = sg_host_get_name(hostA);
119
120   XBT_INFO("### Test: with/without sg_exec_set_bound");
121
122   XBT_INFO("### Test: no bound for Task1@%s", hostA_name);
123   launch_worker(hostA, "worker0", computation_amount, 0, 0);
124
125   sg_actor_sleep_for(1000);
126
127   XBT_INFO("### Test: 50%% for Task1@%s", hostA_name);
128   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
129
130   sg_actor_sleep_for(1000);
131
132   XBT_INFO("### Test: 33%% for Task1@%s", hostA_name);
133   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 3);
134
135   sg_actor_sleep_for(1000);
136
137   XBT_INFO("### Test: zero for Task1@%s (i.e., unlimited)", hostA_name);
138   launch_worker(hostA, "worker0", computation_amount, 1, 0);
139
140   sg_actor_sleep_for(1000);
141
142   XBT_INFO("### Test: 200%% for Task1@%s (i.e., meaningless)", hostA_name);
143   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 2);
144
145   sg_actor_sleep_for(1000);
146 }
147
148 static void test_two_tasks(sg_host_t hostA, sg_host_t hostB)
149 {
150   const double cpu_speed = sg_host_speed(hostA);
151   xbt_assert(cpu_speed == sg_host_speed(hostB));
152   const double computation_amount = cpu_speed * 10;
153   const char* hostA_name          = sg_host_get_name(hostA);
154   const char* hostB_name          = sg_host_get_name(hostB);
155
156   XBT_INFO("### Test: no bound for Task1@%s, no bound for Task2@%s", hostA_name, hostB_name);
157   launch_worker(hostA, "worker0", computation_amount, 0, 0);
158   launch_worker(hostB, "worker1", computation_amount, 0, 0);
159
160   sg_actor_sleep_for(1000);
161
162   XBT_INFO("### Test: 0 for Task1@%s, 0 for Task2@%s (i.e., unlimited)", hostA_name, hostB_name);
163   launch_worker(hostA, "worker0", computation_amount, 1, 0);
164   launch_worker(hostB, "worker1", computation_amount, 1, 0);
165
166   sg_actor_sleep_for(1000);
167
168   XBT_INFO("### Test: 50%% for Task1@%s, 50%% for Task2@%s", hostA_name, hostB_name);
169   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
170   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 2);
171
172   sg_actor_sleep_for(1000);
173
174   XBT_INFO("### Test: 25%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
175   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 4);
176   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
177
178   sg_actor_sleep_for(1000);
179
180   XBT_INFO("### Test: 75%% for Task1@%s, 100%% for Task2@%s", hostA_name, hostB_name);
181   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
182   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed);
183
184   sg_actor_sleep_for(1000);
185
186   XBT_INFO("### Test: no bound for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
187   launch_worker(hostA, "worker0", computation_amount, 0, 0);
188   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
189
190   sg_actor_sleep_for(1000);
191
192   XBT_INFO("### Test: 75%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
193   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
194   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
195
196   sg_actor_sleep_for(1000);
197 }
198
199 static void master_main(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
200 {
201   sg_host_t pm0 = sg_host_by_name("Fafard");
202   sg_host_t pm1 = sg_host_by_name("Fafard");
203
204   XBT_INFO("# 1. Put a single task on a PM. ");
205   test_one_task(pm0);
206   XBT_INFO(" ");
207
208   XBT_INFO("# 2. Put two tasks on a PM.");
209   test_two_tasks(pm0, pm0);
210   XBT_INFO(" ");
211
212   sg_vm_t vm0 = sg_vm_create_core(pm0, "VM0");
213   sg_vm_start(vm0);
214
215   XBT_INFO("# 3. Put a single task on a VM. ");
216   test_one_task((sg_host_t)vm0);
217   XBT_INFO(" ");
218
219   XBT_INFO("# 4. Put two tasks on a VM.");
220   test_two_tasks((sg_host_t)vm0, (sg_host_t)vm0);
221   XBT_INFO(" ");
222
223   sg_vm_destroy(vm0);
224
225   vm0 = sg_vm_create_core(pm0, "VM0");
226   sg_vm_start(vm0);
227
228   XBT_INFO("# 6. Put a task on a PM and a task on a VM.");
229   test_two_tasks(pm0, (sg_host_t)vm0);
230   XBT_INFO(" ");
231
232   sg_vm_destroy(vm0);
233
234   vm0              = sg_vm_create_core(pm0, "VM0");
235   double cpu_speed = sg_host_speed(pm0);
236   sg_vm_set_bound(vm0, cpu_speed / 10);
237   sg_vm_start(vm0);
238
239   XBT_INFO("# 7. Put a single task on the VM capped by 10%%.");
240   test_one_task((sg_host_t)vm0);
241   XBT_INFO(" ");
242
243   XBT_INFO("# 8. Put two tasks on the VM capped by 10%%.");
244   test_two_tasks((sg_host_t)vm0, (sg_host_t)vm0);
245   XBT_INFO(" ");
246
247   XBT_INFO("# 9. Put a task on a PM and a task on the VM capped by 10%%.");
248   test_two_tasks(pm0, (sg_host_t)vm0);
249   XBT_INFO(" ");
250
251   sg_vm_destroy(vm0);
252
253   vm0 = sg_vm_create_core(pm0, "VM0");
254
255   sg_vm_set_ramsize(vm0, 1e9); // 1GB
256   sg_vm_start(vm0);
257
258   cpu_speed = sg_host_speed(pm0);
259   sg_vm_start(vm0);
260
261   XBT_INFO("# 10. Test migration");
262   const double computation_amount = cpu_speed * 10;
263
264   XBT_INFO("# 10. (a) Put a task on a VM without any bound.");
265   launch_worker((sg_host_t)vm0, "worker0", computation_amount, 0, 0);
266   sg_actor_sleep_for(1000);
267   XBT_INFO(" ");
268
269   XBT_INFO("# 10. (b) set 10%% bound to the VM, and then put a task on the VM.");
270   sg_vm_set_bound(vm0, cpu_speed / 10);
271   launch_worker((sg_host_t)vm0, "worker0", computation_amount, 0, 0);
272   sg_actor_sleep_for(1000);
273   XBT_INFO(" ");
274
275   XBT_INFO("# 10. (c) migrate");
276   sg_vm_migrate(vm0, pm1);
277   XBT_INFO(" ");
278
279   XBT_INFO("# 10. (d) Put a task again on the VM.");
280   launch_worker((sg_host_t)vm0, "worker0", computation_amount, 0, 0);
281   sg_actor_sleep_for(1000);
282   XBT_INFO(" ");
283
284   sg_vm_destroy(vm0);
285
286   XBT_INFO("# 11. Change a bound dynamically.");
287   test_dynamic_change();
288 }
289
290 int main(int argc, char* argv[])
291 {
292   /* Get the arguments */
293   simgrid_init(&argc, argv);
294   sg_vm_live_migration_plugin_init();
295
296   /* load the platform file */
297   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
298
299   simgrid_load_platform(argv[1]);
300
301   sg_actor_t actor = sg_actor_init("master_", sg_host_by_name("Fafard"));
302   sg_actor_start(actor, master_main, 0, NULL);
303
304   simgrid_run();
305   XBT_INFO("Bye (simulation time %g)", simgrid_get_clock());
306
307   return 0;
308 }