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. */
7 #include "simgrid/msg.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);
64 #define DOUBLE_MAX 1e11
66 static void test_dynamic_change(void)
68 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
69 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
70 xbt_dynar_free(&hosts_dynar);
72 msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
73 msg_host_t vm1 = MSG_vm_create_core(pm0, "VM1");
77 msg_task_t task0 = MSG_task_create("Task0", DOUBLE_MAX, 0, NULL);
78 msg_task_t task1 = MSG_task_create("Task1", DOUBLE_MAX, 0, NULL);
79 MSG_process_create("worker0", worker_busy_loop_main, &task0, vm0);
80 MSG_process_create("worker1", worker_busy_loop_main, &task1, vm1);
82 double task0_remain_prev = MSG_task_get_flops_amount(task0);
83 double task1_remain_prev = MSG_task_get_flops_amount(task1);
86 const double cpu_speed = MSG_host_get_speed(pm0);
88 for (i = 0; i < 10; i++) {
89 double new_bound = (cpu_speed / 10) * i;
90 XBT_INFO("set bound of VM1 to %f", new_bound);
91 MSG_vm_set_bound(vm1, new_bound);
92 MSG_process_sleep(100);
94 double task0_remain_now = MSG_task_get_flops_amount(task0);
95 double task1_remain_now = MSG_task_get_flops_amount(task1);
97 double task0_flops_per_sec = task0_remain_prev - task0_remain_now;
98 double task1_flops_per_sec = task1_remain_prev - task1_remain_now;
100 XBT_INFO("Task0@VM0: %f flops/s", task0_flops_per_sec / 100);
101 XBT_INFO("Task1@VM1: %f flops/s", task1_flops_per_sec / 100);
103 task0_remain_prev = task0_remain_now;
104 task1_remain_prev = task1_remain_now;
107 MSG_process_sleep(2000); // let the tasks end
113 static void test_one_task(msg_host_t hostA)
115 const double cpu_speed = MSG_host_get_speed(hostA);
116 const double computation_amount = cpu_speed * 10;
117 const char *hostA_name = MSG_host_get_name(hostA);
119 XBT_INFO("### Test: with/without MSG_task_set_bound");
121 XBT_INFO("### Test: no bound for Task1@%s", hostA_name);
122 launch_worker(hostA, "worker0", computation_amount, 0, 0);
124 MSG_process_sleep(1000);
126 XBT_INFO("### Test: 50%% for Task1@%s", hostA_name);
127 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
129 MSG_process_sleep(1000);
131 XBT_INFO("### Test: 33%% for Task1@%s", hostA_name);
132 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 3);
134 MSG_process_sleep(1000);
136 XBT_INFO("### Test: zero for Task1@%s (i.e., unlimited)", hostA_name);
137 launch_worker(hostA, "worker0", computation_amount, 1, 0);
139 MSG_process_sleep(1000);
141 XBT_INFO("### Test: 200%% for Task1@%s (i.e., meaningless)", hostA_name);
142 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 2);
144 MSG_process_sleep(1000);
147 static void test_two_tasks(msg_host_t hostA, msg_host_t hostB)
149 const double cpu_speed = MSG_host_get_speed(hostA);
150 xbt_assert(cpu_speed == MSG_host_get_speed(hostB));
151 const double computation_amount = cpu_speed * 10;
152 const char *hostA_name = MSG_host_get_name(hostA);
153 const char *hostB_name = MSG_host_get_name(hostB);
155 XBT_INFO("### Test: no bound for Task1@%s, no bound for Task2@%s", hostA_name, hostB_name);
156 launch_worker(hostA, "worker0", computation_amount, 0, 0);
157 launch_worker(hostB, "worker1", computation_amount, 0, 0);
159 MSG_process_sleep(1000);
161 XBT_INFO("### Test: 0 for Task1@%s, 0 for Task2@%s (i.e., unlimited)", hostA_name, hostB_name);
162 launch_worker(hostA, "worker0", computation_amount, 1, 0);
163 launch_worker(hostB, "worker1", computation_amount, 1, 0);
165 MSG_process_sleep(1000);
167 XBT_INFO("### Test: 50%% for Task1@%s, 50%% for Task2@%s", hostA_name, hostB_name);
168 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
169 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 2);
171 MSG_process_sleep(1000);
173 XBT_INFO("### Test: 25%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
174 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 4);
175 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
177 MSG_process_sleep(1000);
179 XBT_INFO("### Test: 75%% for Task1@%s, 100%% for Task2@%s", hostA_name, hostB_name);
180 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
181 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed);
183 MSG_process_sleep(1000);
185 XBT_INFO("### Test: no bound for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
186 launch_worker(hostA, "worker0", computation_amount, 0, 0);
187 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
189 MSG_process_sleep(1000);
191 XBT_INFO("### Test: 75%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
192 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
193 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
195 MSG_process_sleep(1000);
198 static int master_main(int argc, char *argv[])
200 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
201 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
202 msg_host_t pm1 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
203 xbt_dynar_free(&hosts_dynar);
205 XBT_INFO("# 1. Put a single task on a PM. ");
209 XBT_INFO("# 2. Put two tasks on a PM.");
210 test_two_tasks(pm0, pm0);
213 msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
216 XBT_INFO("# 3. Put a single task on a VM. ");
220 XBT_INFO("# 4. Put two tasks on a VM.");
221 test_two_tasks(vm0, vm0);
226 vm0 = MSG_vm_create_core(pm0, "VM0");
229 XBT_INFO("# 6. Put a task on a PM and a task on a VM.");
230 test_two_tasks(pm0, vm0);
235 vm0 = MSG_vm_create_core(pm0, "VM0");
236 double cpu_speed = MSG_host_get_speed(pm0);
237 MSG_vm_set_bound(vm0, cpu_speed / 10);
240 XBT_INFO("# 7. Put a single task on the VM capped by 10%%.");
244 XBT_INFO("# 8. Put two tasks on the VM capped by 10%%.");
245 test_two_tasks(vm0, vm0);
248 XBT_INFO("# 9. Put a task on a PM and a task on the VM capped by 10%%.");
249 test_two_tasks(pm0, vm0);
254 vm0 = MSG_vm_create_core(pm0, "VM0");
256 s_vm_params_t params;
257 memset(¶ms, 0, sizeof(params));
258 params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
259 MSG_host_set_params(vm0, ¶ms);
262 cpu_speed = MSG_host_get_speed(pm0);
265 XBT_INFO("# 10. Test migration");
266 const double computation_amount = cpu_speed * 10;
268 XBT_INFO("# 10. (a) Put a task on a VM without any bound.");
269 launch_worker(vm0, "worker0", computation_amount, 0, 0);
270 MSG_process_sleep(1000);
273 XBT_INFO("# 10. (b) set 10%% bound to the VM, and then put a task on the VM.");
274 MSG_vm_set_bound(vm0, cpu_speed / 10);
275 launch_worker(vm0, "worker0", computation_amount, 0, 0);
276 MSG_process_sleep(1000);
279 XBT_INFO("# 10. (c) migrate");
280 MSG_vm_migrate(vm0, pm1);
283 XBT_INFO("# 10. (d) Put a task again on the VM.");
284 launch_worker(vm0, "worker0", computation_amount, 0, 0);
285 MSG_process_sleep(1000);
290 XBT_INFO("# 11. Change a bound dynamically.");
291 test_dynamic_change();
296 static void launch_master(msg_host_t host)
298 const char *pr_name = "master_";
299 char **argv = xbt_new(char *, 2);
300 argv[0] = xbt_strdup(pr_name);
303 MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
306 int main(int argc, char *argv[])
308 /* Get the arguments */
309 MSG_init(&argc, argv);
311 /* load the platform file */
312 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
314 MSG_create_environment(argv[1]);
316 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
317 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
319 xbt_dynar_free(&hosts_dynar);
321 int res = MSG_main();
322 XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
324 return !(res == MSG_OK);