Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace #define with constexpr declarations.
[simgrid.git] / teshsuite / msg / cloud-capping / cloud-capping.c
1 /* Copyright (c) 2007-2019. 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 #include "simgrid/plugins/live_migration.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  *
13  * - <b>cloud/bound.c</b>: Demonstrates the use of @ref MSG_task_set_bound
14  */
15
16 static int worker_main(int argc, char* argv[])
17 {
18   double computation_amount = xbt_str_parse_double(argv[1], "Invalid computation amount: %s");
19   int use_bound             = xbt_str_parse_int(argv[2], "Second parameter (use_bound) should be 0 or 1 but is: %s");
20   double bound              = xbt_str_parse_double(argv[3], "Invalid bound: %s");
21
22   double clock_sta = MSG_get_clock();
23
24   msg_task_t task = MSG_task_create("Task", computation_amount, 0, NULL);
25   if (use_bound)
26     MSG_task_set_bound(task, bound);
27   MSG_task_execute(task);
28   MSG_task_destroy(task);
29
30   double clock_end     = MSG_get_clock();
31   double duration      = clock_end - clock_sta;
32   double flops_per_sec = computation_amount / duration;
33
34   if (use_bound)
35     XBT_INFO("bound to %f => duration %f (%f flops/s)", bound, duration, flops_per_sec);
36   else
37     XBT_INFO("not bound => duration %f (%f flops/s)", duration, flops_per_sec);
38
39   return 0;
40 }
41
42 static void launch_worker(msg_host_t host, const char* pr_name, double computation_amount, int use_bound, double bound)
43 {
44   char** argv = xbt_new(char*, 5);
45   argv[0]     = xbt_strdup(pr_name);
46   argv[1]     = bprintf("%f", computation_amount);
47   argv[2]     = bprintf("%d", use_bound);
48   argv[3]     = bprintf("%f", bound);
49   argv[4]     = NULL;
50
51   MSG_process_create_with_arguments(pr_name, worker_main, NULL, host, 4, argv);
52 }
53
54 static int worker_busy_loop_main(int argc, char* argv[])
55 {
56   msg_task_t* task = MSG_process_get_data(MSG_process_self());
57   MSG_task_execute(*task);
58   MSG_task_destroy(*task);
59
60   return 0;
61 }
62
63 static void test_dynamic_change(void)
64 {
65   const double DOUBLE_MAX = 1e11;
66   msg_host_t pm0 = MSG_host_by_name("Fafard");
67
68   msg_vm_t vm0 = MSG_vm_create_core(pm0, "VM0");
69   msg_vm_t vm1 = MSG_vm_create_core(pm0, "VM1");
70   MSG_vm_start(vm0);
71   MSG_vm_start(vm1);
72
73   msg_task_t task0 = MSG_task_create("Task0", DOUBLE_MAX, 0, NULL);
74   msg_task_t task1 = MSG_task_create("Task1", DOUBLE_MAX, 0, NULL);
75   MSG_process_create("worker0", worker_busy_loop_main, &task0, (msg_host_t)vm0);
76   MSG_process_create("worker1", worker_busy_loop_main, &task1, (msg_host_t)vm1);
77
78   double task0_remain_prev = MSG_task_get_flops_amount(task0);
79   double task1_remain_prev = MSG_task_get_flops_amount(task1);
80
81   const double cpu_speed = MSG_host_get_speed(pm0);
82   for (int i = 0; i < 10; i++) {
83     double new_bound = (cpu_speed / 10) * i;
84     XBT_INFO("set bound of VM1 to %f", new_bound);
85     MSG_vm_set_bound(vm1, new_bound);
86     MSG_process_sleep(100);
87
88     double task0_remain_now = MSG_task_get_flops_amount(task0);
89     double task1_remain_now = MSG_task_get_flops_amount(task1);
90
91     double task0_flops_per_sec = task0_remain_prev - task0_remain_now;
92     double task1_flops_per_sec = task1_remain_prev - task1_remain_now;
93
94     XBT_INFO("Task0@VM0: %f flops/s", task0_flops_per_sec / 100);
95     XBT_INFO("Task1@VM1: %f flops/s", task1_flops_per_sec / 100);
96
97     task0_remain_prev = task0_remain_now;
98     task1_remain_prev = task1_remain_now;
99   }
100
101   MSG_process_sleep(2000); // let the tasks end
102
103   MSG_vm_destroy(vm0);
104   MSG_vm_destroy(vm1);
105 }
106
107 static void test_one_task(msg_host_t hostA)
108 {
109   const double cpu_speed          = MSG_host_get_speed(hostA);
110   const double computation_amount = cpu_speed * 10;
111   const char* hostA_name          = MSG_host_get_name(hostA);
112
113   XBT_INFO("### Test: with/without MSG_task_set_bound");
114
115   XBT_INFO("### Test: no bound for Task1@%s", hostA_name);
116   launch_worker(hostA, "worker0", computation_amount, 0, 0);
117
118   MSG_process_sleep(1000);
119
120   XBT_INFO("### Test: 50%% for Task1@%s", hostA_name);
121   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
122
123   MSG_process_sleep(1000);
124
125   XBT_INFO("### Test: 33%% for Task1@%s", hostA_name);
126   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 3);
127
128   MSG_process_sleep(1000);
129
130   XBT_INFO("### Test: zero for Task1@%s (i.e., unlimited)", hostA_name);
131   launch_worker(hostA, "worker0", computation_amount, 1, 0);
132
133   MSG_process_sleep(1000);
134
135   XBT_INFO("### Test: 200%% for Task1@%s (i.e., meaningless)", hostA_name);
136   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 2);
137
138   MSG_process_sleep(1000);
139 }
140
141 static void test_two_tasks(msg_host_t hostA, msg_host_t hostB)
142 {
143   const double cpu_speed = MSG_host_get_speed(hostA);
144   xbt_assert(cpu_speed == MSG_host_get_speed(hostB));
145   const double computation_amount = cpu_speed * 10;
146   const char* hostA_name          = MSG_host_get_name(hostA);
147   const char* hostB_name          = MSG_host_get_name(hostB);
148
149   XBT_INFO("### Test: no bound for Task1@%s, no bound for Task2@%s", hostA_name, hostB_name);
150   launch_worker(hostA, "worker0", computation_amount, 0, 0);
151   launch_worker(hostB, "worker1", computation_amount, 0, 0);
152
153   MSG_process_sleep(1000);
154
155   XBT_INFO("### Test: 0 for Task1@%s, 0 for Task2@%s (i.e., unlimited)", hostA_name, hostB_name);
156   launch_worker(hostA, "worker0", computation_amount, 1, 0);
157   launch_worker(hostB, "worker1", computation_amount, 1, 0);
158
159   MSG_process_sleep(1000);
160
161   XBT_INFO("### Test: 50%% for Task1@%s, 50%% for Task2@%s", hostA_name, hostB_name);
162   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
163   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 2);
164
165   MSG_process_sleep(1000);
166
167   XBT_INFO("### Test: 25%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
168   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 4);
169   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
170
171   MSG_process_sleep(1000);
172
173   XBT_INFO("### Test: 75%% for Task1@%s, 100%% for Task2@%s", hostA_name, hostB_name);
174   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
175   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed);
176
177   MSG_process_sleep(1000);
178
179   XBT_INFO("### Test: no bound for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
180   launch_worker(hostA, "worker0", computation_amount, 0, 0);
181   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
182
183   MSG_process_sleep(1000);
184
185   XBT_INFO("### Test: 75%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
186   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
187   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
188
189   MSG_process_sleep(1000);
190 }
191
192 static int master_main(int argc, char* argv[])
193 {
194   msg_host_t pm0 = MSG_host_by_name("Fafard");
195   msg_host_t pm1 = MSG_host_by_name("Fafard");
196
197   XBT_INFO("# 1. Put a single task on a PM. ");
198   test_one_task(pm0);
199   XBT_INFO(" ");
200
201   XBT_INFO("# 2. Put two tasks on a PM.");
202   test_two_tasks(pm0, pm0);
203   XBT_INFO(" ");
204
205   msg_vm_t vm0 = MSG_vm_create_core(pm0, "VM0");
206   MSG_vm_start(vm0);
207
208   XBT_INFO("# 3. Put a single task on a VM. ");
209   test_one_task((msg_host_t)vm0);
210   XBT_INFO(" ");
211
212   XBT_INFO("# 4. Put two tasks on a VM.");
213   test_two_tasks((msg_host_t)vm0, (msg_host_t)vm0);
214   XBT_INFO(" ");
215
216   MSG_vm_destroy(vm0);
217
218   vm0 = MSG_vm_create_core(pm0, "VM0");
219   MSG_vm_start(vm0);
220
221   XBT_INFO("# 6. Put a task on a PM and a task on a VM.");
222   test_two_tasks(pm0, (msg_host_t)vm0);
223   XBT_INFO(" ");
224
225   MSG_vm_destroy(vm0);
226
227   vm0              = MSG_vm_create_core(pm0, "VM0");
228   double cpu_speed = MSG_host_get_speed(pm0);
229   MSG_vm_set_bound(vm0, cpu_speed / 10);
230   MSG_vm_start(vm0);
231
232   XBT_INFO("# 7. Put a single task on the VM capped by 10%%.");
233   test_one_task((msg_host_t)vm0);
234   XBT_INFO(" ");
235
236   XBT_INFO("# 8. Put two tasks on the VM capped by 10%%.");
237   test_two_tasks((msg_host_t)vm0, (msg_host_t)vm0);
238   XBT_INFO(" ");
239
240   XBT_INFO("# 9. Put a task on a PM and a task on the VM capped by 10%%.");
241   test_two_tasks(pm0, (msg_host_t)vm0);
242   XBT_INFO(" ");
243
244   MSG_vm_destroy(vm0);
245
246   vm0 = MSG_vm_create_core(pm0, "VM0");
247
248   MSG_vm_set_ramsize(vm0, 1e9); // 1GB
249   MSG_vm_start(vm0);
250
251   cpu_speed = MSG_host_get_speed(pm0);
252   MSG_vm_start(vm0);
253
254   XBT_INFO("# 10. Test migration");
255   const double computation_amount = cpu_speed * 10;
256
257   XBT_INFO("# 10. (a) Put a task on a VM without any bound.");
258   launch_worker((msg_host_t)vm0, "worker0", computation_amount, 0, 0);
259   MSG_process_sleep(1000);
260   XBT_INFO(" ");
261
262   XBT_INFO("# 10. (b) set 10%% bound to the VM, and then put a task on the VM.");
263   MSG_vm_set_bound(vm0, cpu_speed / 10);
264   launch_worker((msg_host_t)vm0, "worker0", computation_amount, 0, 0);
265   MSG_process_sleep(1000);
266   XBT_INFO(" ");
267
268   XBT_INFO("# 10. (c) migrate");
269   MSG_vm_migrate(vm0, pm1);
270   XBT_INFO(" ");
271
272   XBT_INFO("# 10. (d) Put a task again on the VM.");
273   launch_worker((msg_host_t)vm0, "worker0", computation_amount, 0, 0);
274   MSG_process_sleep(1000);
275   XBT_INFO(" ");
276
277   MSG_vm_destroy(vm0);
278
279   XBT_INFO("# 11. Change a bound dynamically.");
280   test_dynamic_change();
281
282   return 0;
283 }
284
285 static void launch_master(msg_host_t host)
286 {
287   const char* pr_name = "master_";
288   char** argv         = xbt_new(char*, 2);
289   argv[0]             = xbt_strdup(pr_name);
290   argv[1]             = NULL;
291
292   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
293 }
294
295 int main(int argc, char* argv[])
296 {
297   /* Get the arguments */
298   MSG_init(&argc, argv);
299   MSG_vm_live_migration_plugin_init();
300
301   /* load the platform file */
302   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
303
304   MSG_create_environment(argv[1]);
305
306   msg_host_t pm0 = MSG_host_by_name("Fafard");
307   launch_master(pm0);
308
309   int res = MSG_main();
310   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
311
312   return !(res == MSG_OK);
313 }