1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/msg.h"
7 #include "simgrid/plugins/live_migration.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11 /** @addtogroup MSG_examples
13 * - <b>cloud/bound.c</b>: Demonstrates the use of @ref MSG_task_set_bound
16 static int worker_main(int argc, char* argv[])
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");
22 double clock_sta = MSG_get_clock();
24 msg_task_t task = MSG_task_create("Task", computation_amount, 0, NULL);
26 MSG_task_set_bound(task, bound);
27 MSG_task_execute(task);
28 MSG_task_destroy(task);
30 double clock_end = MSG_get_clock();
31 double duration = clock_end - clock_sta;
32 double flops_per_sec = computation_amount / duration;
35 XBT_INFO("bound to %f => duration %f (%f flops/s)", bound, duration, flops_per_sec);
37 XBT_INFO("not bound => duration %f (%f flops/s)", duration, flops_per_sec);
42 static void launch_worker(msg_host_t host, const char* pr_name, double computation_amount, int use_bound, double bound)
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);
51 MSG_process_create_with_arguments(pr_name, worker_main, NULL, host, 4, argv);
54 static int worker_busy_loop_main(int argc, char* argv[])
56 msg_task_t* task = MSG_process_get_data(MSG_process_self());
57 MSG_task_execute(*task);
58 MSG_task_destroy(*task);
63 static void test_dynamic_change(void)
65 const double DOUBLE_MAX = 1e11;
66 msg_host_t pm0 = MSG_host_by_name("Fafard");
68 msg_vm_t vm0 = MSG_vm_create_core(pm0, "VM0");
69 msg_vm_t vm1 = MSG_vm_create_core(pm0, "VM1");
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);
78 double task0_remain_prev = MSG_task_get_flops_amount(task0);
79 double task1_remain_prev = MSG_task_get_flops_amount(task1);
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);
88 double task0_remain_now = MSG_task_get_flops_amount(task0);
89 double task1_remain_now = MSG_task_get_flops_amount(task1);
91 double task0_flops_per_sec = task0_remain_prev - task0_remain_now;
92 double task1_flops_per_sec = task1_remain_prev - task1_remain_now;
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);
97 task0_remain_prev = task0_remain_now;
98 task1_remain_prev = task1_remain_now;
101 MSG_process_sleep(2000); // let the tasks end
107 static void test_one_task(msg_host_t hostA)
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);
113 XBT_INFO("### Test: with/without MSG_task_set_bound");
115 XBT_INFO("### Test: no bound for Task1@%s", hostA_name);
116 launch_worker(hostA, "worker0", computation_amount, 0, 0);
118 MSG_process_sleep(1000);
120 XBT_INFO("### Test: 50%% for Task1@%s", hostA_name);
121 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
123 MSG_process_sleep(1000);
125 XBT_INFO("### Test: 33%% for Task1@%s", hostA_name);
126 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 3);
128 MSG_process_sleep(1000);
130 XBT_INFO("### Test: zero for Task1@%s (i.e., unlimited)", hostA_name);
131 launch_worker(hostA, "worker0", computation_amount, 1, 0);
133 MSG_process_sleep(1000);
135 XBT_INFO("### Test: 200%% for Task1@%s (i.e., meaningless)", hostA_name);
136 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 2);
138 MSG_process_sleep(1000);
141 static void test_two_tasks(msg_host_t hostA, msg_host_t hostB)
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);
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);
153 MSG_process_sleep(1000);
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);
159 MSG_process_sleep(1000);
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);
165 MSG_process_sleep(1000);
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);
171 MSG_process_sleep(1000);
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);
177 MSG_process_sleep(1000);
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);
183 MSG_process_sleep(1000);
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);
189 MSG_process_sleep(1000);
192 static int master_main(int argc, char* argv[])
194 msg_host_t pm0 = MSG_host_by_name("Fafard");
195 msg_host_t pm1 = MSG_host_by_name("Fafard");
197 XBT_INFO("# 1. Put a single task on a PM. ");
201 XBT_INFO("# 2. Put two tasks on a PM.");
202 test_two_tasks(pm0, pm0);
205 msg_vm_t vm0 = MSG_vm_create_core(pm0, "VM0");
208 XBT_INFO("# 3. Put a single task on a VM. ");
209 test_one_task((msg_host_t)vm0);
212 XBT_INFO("# 4. Put two tasks on a VM.");
213 test_two_tasks((msg_host_t)vm0, (msg_host_t)vm0);
218 vm0 = MSG_vm_create_core(pm0, "VM0");
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);
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);
232 XBT_INFO("# 7. Put a single task on the VM capped by 10%%.");
233 test_one_task((msg_host_t)vm0);
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);
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);
246 vm0 = MSG_vm_create_core(pm0, "VM0");
248 MSG_vm_set_ramsize(vm0, 1e9); // 1GB
251 cpu_speed = MSG_host_get_speed(pm0);
254 XBT_INFO("# 10. Test migration");
255 const double computation_amount = cpu_speed * 10;
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);
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);
268 XBT_INFO("# 10. (c) migrate");
269 MSG_vm_migrate(vm0, pm1);
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);
279 XBT_INFO("# 11. Change a bound dynamically.");
280 test_dynamic_change();
285 static void launch_master(msg_host_t host)
287 const char* pr_name = "master_";
288 char** argv = xbt_new(char*, 2);
289 argv[0] = xbt_strdup(pr_name);
292 MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
295 int main(int argc, char* argv[])
297 /* Get the arguments */
298 MSG_init(&argc, argv);
299 MSG_vm_live_migration_plugin_init();
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]);
304 MSG_create_environment(argv[1]);
306 msg_host_t pm0 = MSG_host_by_name("Fafard");
309 int res = MSG_main();
310 XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
312 return !(res == MSG_OK);