1 /* Copyright (c) 2007-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "simgrid/msg.h" /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
17 /** @addtogroup MSG_examples
19 * - <b>priority/priority.c</b>: Demonstrates the use of @ref
20 * MSG_task_set_bound to change the computation priority of a
25 static int worker_main(int argc, char *argv[])
27 double computation_amount = atof(argv[1]);
28 int use_bound = atoi(argv[2]);
29 double bound = atof(argv[3]);
32 double clock_sta = MSG_get_clock();
34 msg_task_t task = MSG_task_create("Task", computation_amount, 0, NULL);
36 MSG_task_set_bound(task, bound);
37 MSG_task_execute(task);
38 MSG_task_destroy(task);
40 double clock_end = MSG_get_clock();
41 double duration = clock_end - clock_sta;
42 double flops_per_sec = computation_amount / duration;
45 XBT_INFO("bound to %f => duration %f (%f flops/s)", bound, duration, flops_per_sec);
47 XBT_INFO("not bound => duration %f (%f flops/s)", duration, flops_per_sec);
53 static void launch_worker(msg_host_t host, const char *pr_name, double computation_amount, int use_bound, double bound)
55 char **argv = xbt_new(char *, 5);
56 argv[0] = xbt_strdup(pr_name);
57 argv[1] = bprintf("%f", computation_amount);
58 argv[2] = bprintf("%d", use_bound);
59 argv[3] = bprintf("%f", bound);
62 MSG_process_create_with_arguments(pr_name, worker_main, NULL, host, 4, argv);
67 static int worker_busy_loop_main(int argc, char *argv[])
69 msg_task_t *task = MSG_process_get_data(MSG_process_self());
71 MSG_task_execute(*task);
77 #define DOUBLE_MAX 1e11
79 static void test_dynamic_change(void)
81 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
82 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
84 msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
85 msg_host_t vm1 = MSG_vm_create_core(pm0, "VM1");
89 msg_task_t task0 = MSG_task_create("Task0", DOUBLE_MAX, 0, NULL);
90 msg_task_t task1 = MSG_task_create("Task1", DOUBLE_MAX, 0, NULL);
91 msg_process_t pr0 = MSG_process_create("worker0", worker_busy_loop_main, &task0, vm0);
92 msg_process_t pr1 = MSG_process_create("worker1", worker_busy_loop_main, &task1, vm1);
95 double task0_remain_prev = MSG_task_get_flops_amount(task0);
96 double task1_remain_prev = MSG_task_get_flops_amount(task1);
99 const double cpu_speed = MSG_get_host_speed(pm0);
101 for (i = 0; i < 10; i++) {
102 double new_bound = (cpu_speed / 10) * i;
103 XBT_INFO("set bound of VM1 to %f", new_bound);
104 MSG_vm_set_bound(vm1, new_bound);
105 MSG_process_sleep(100);
107 double task0_remain_now = MSG_task_get_flops_amount(task0);
108 double task1_remain_now = MSG_task_get_flops_amount(task1);
110 double task0_flops_per_sec = task0_remain_prev - task0_remain_now;
111 double task1_flops_per_sec = task1_remain_prev - task1_remain_now;
113 XBT_INFO("Task0@VM0: %f flops/s", task0_flops_per_sec / 100);
114 XBT_INFO("Task1@VM1: %f flops/s", task1_flops_per_sec / 100);
116 task0_remain_prev = task0_remain_now;
117 task1_remain_prev = task1_remain_now;
121 MSG_process_kill(pr0);
122 MSG_process_kill(pr1);
130 static void test_one_task(msg_host_t hostA)
132 const double cpu_speed = MSG_get_host_speed(hostA);
133 const double computation_amount = cpu_speed * 10;
134 const char *hostA_name = MSG_host_get_name(hostA);
136 XBT_INFO("### Test: with/without MSG_task_set_bound");
139 /* Easy-to-understand code (without calling MSG_task_set_bound) */
141 double clock_sta = MSG_get_clock();
143 msg_task_t task = MSG_task_create("Task", computation_amount, 0, NULL);
144 MSG_task_execute(task);
145 MSG_task_destroy(task);
147 double clock_end = MSG_get_clock();
148 double duration = clock_end - clock_sta;
149 double flops_per_sec = computation_amount / duration;
151 XBT_INFO("not bound => duration %f (%f flops/s)", duration, flops_per_sec);
154 /* Easy-to-understand code (with calling MSG_task_set_bound) */
156 double clock_sta = MSG_get_clock();
158 msg_task_t task = MSG_task_create("Task", computation_amount, 0, NULL);
159 MSG_task_set_bound(task, cpu_speed / 2);
160 MSG_task_execute(task);
161 MSG_task_destroy(task);
163 double clock_end = MSG_get_clock();
164 double duration = clock_end - clock_sta;
165 double flops_per_sec = computation_amount / duration;
167 XBT_INFO("bound to 0.5 => duration %f (%f flops/s)", duration, flops_per_sec);
172 XBT_INFO("### Test: no bound for Task1@%s", hostA_name);
173 launch_worker(hostA, "worker0", computation_amount, 0, 0);
176 MSG_process_sleep(1000);
179 XBT_INFO("### Test: 50%% for Task1@%s", hostA_name);
180 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
183 MSG_process_sleep(1000);
186 XBT_INFO("### Test: 33%% for Task1@%s", hostA_name);
187 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 3);
190 MSG_process_sleep(1000);
193 XBT_INFO("### Test: zero for Task1@%s (i.e., unlimited)", hostA_name);
194 launch_worker(hostA, "worker0", computation_amount, 1, 0);
197 MSG_process_sleep(1000);
200 XBT_INFO("### Test: 200%% for Task1@%s (i.e., meaningless)", hostA_name);
201 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 2);
204 MSG_process_sleep(1000);
208 static void test_two_tasks(msg_host_t hostA, msg_host_t hostB)
210 const double cpu_speed = MSG_get_host_speed(hostA);
211 xbt_assert(cpu_speed == MSG_get_host_speed(hostB));
212 const double computation_amount = cpu_speed * 10;
213 const char *hostA_name = MSG_host_get_name(hostA);
214 const char *hostB_name = MSG_host_get_name(hostB);
217 XBT_INFO("### Test: no bound for Task1@%s, no bound for Task2@%s", hostA_name, hostB_name);
218 launch_worker(hostA, "worker0", computation_amount, 0, 0);
219 launch_worker(hostB, "worker1", computation_amount, 0, 0);
222 MSG_process_sleep(1000);
225 XBT_INFO("### Test: 0 for Task1@%s, 0 for Task2@%s (i.e., unlimited)", hostA_name, hostB_name);
226 launch_worker(hostA, "worker0", computation_amount, 1, 0);
227 launch_worker(hostB, "worker1", computation_amount, 1, 0);
230 MSG_process_sleep(1000);
233 XBT_INFO("### Test: 50%% for Task1@%s, 50%% for Task2@%s", hostA_name, hostB_name);
234 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
235 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 2);
238 MSG_process_sleep(1000);
241 XBT_INFO("### Test: 25%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
242 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 4);
243 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
246 MSG_process_sleep(1000);
249 XBT_INFO("### Test: 75%% for Task1@%s, 100%% for Task2@%s", hostA_name, hostB_name);
250 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
251 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed);
254 MSG_process_sleep(1000);
257 XBT_INFO("### Test: no bound for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
258 launch_worker(hostA, "worker0", computation_amount, 0, 0);
259 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
262 MSG_process_sleep(1000);
265 XBT_INFO("### Test: 75%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
266 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
267 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
270 MSG_process_sleep(1000);
273 static int master_main(int argc, char *argv[])
275 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
276 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
277 msg_host_t pm1 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
281 XBT_INFO("# 1. Put a single task on a PM. ");
286 XBT_INFO("# 2. Put two tasks on a PM.");
287 test_two_tasks(pm0, pm0);
293 msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
296 XBT_INFO("# 3. Put a single task on a VM. ");
300 XBT_INFO("# 4. Put two tasks on a VM.");
301 test_two_tasks(vm0, vm0);
310 msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
313 XBT_INFO("# 6. Put a task on a PM and a task on a VM.");
314 test_two_tasks(pm0, vm0);
323 msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
324 const double cpu_speed = MSG_get_host_speed(pm0);
325 MSG_vm_set_bound(vm0, cpu_speed / 10);
328 XBT_INFO("# 7. Put a single task on the VM capped by 10%%.");
332 XBT_INFO("# 8. Put two tasks on the VM capped by 10%%.");
333 test_two_tasks(vm0, vm0);
336 XBT_INFO("# 9. Put a task on a PM and a task on the VM capped by 10%%.");
337 test_two_tasks(pm0, vm0);
345 msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
347 s_vm_params_t params;
348 memset(¶ms, 0, sizeof(params));
349 params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
350 MSG_host_set_params(vm0, ¶ms);
353 const double cpu_speed = MSG_get_host_speed(pm0);
356 XBT_INFO("# 10. Test migration");
357 const double computation_amount = cpu_speed * 10;
359 XBT_INFO("# 10. (a) Put a task on a VM without any bound.");
360 launch_worker(vm0, "worker0", computation_amount, 0, 0);
361 MSG_process_sleep(1000);
364 XBT_INFO("# 10. (b) set 10%% bound to the VM, and then put a task on the VM.");
365 MSG_vm_set_bound(vm0, cpu_speed / 10);
366 launch_worker(vm0, "worker0", computation_amount, 0, 0);
367 MSG_process_sleep(1000);
370 XBT_INFO("# 10. (c) migrate");
371 MSG_vm_migrate(vm0, pm1);
374 XBT_INFO("# 10. (d) Put a task again on the VM.");
375 launch_worker(vm0, "worker0", computation_amount, 0, 0);
376 MSG_process_sleep(1000);
383 XBT_INFO("# 11. Change a bound dynamically.");
384 test_dynamic_change();
389 static void launch_master(msg_host_t host)
391 const char *pr_name = "master_";
392 char **argv = xbt_new(char *, 2);
393 argv[0] = xbt_strdup(pr_name);
396 MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
399 int main(int argc, char *argv[])
401 /* Get the arguments */
402 MSG_init(&argc, argv);
404 /* load the platform file */
406 printf("Usage: %s example/msg/cloud/simple_plat.xml\n", argv[0]);
410 MSG_create_environment(argv[1]);
412 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
413 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
416 int res = MSG_main();
417 XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
420 return !(res == MSG_OK);