1 /* Copyright (c) 2007-2020. 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/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"
13 #include "xbt/asserts.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(cloud_capping, "Messages specific for this example");
19 static void worker_main(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
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");
26 double clock_sta = simgrid_get_clock();
28 sg_exec_t exec = sg_actor_exec_init(computation_amount);
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);
36 double clock_end = simgrid_get_clock();
37 double duration = clock_end - clock_sta;
38 double flops_per_sec = computation_amount / duration;
41 XBT_INFO("bound to %f => duration %f (%f flops/s)", bound, duration, flops_per_sec);
43 XBT_INFO("not bound => duration %f (%f flops/s)", duration, flops_per_sec);
46 static void launch_worker(sg_host_t host, const char* pr_name, double computation_amount, int use_bound, double bound)
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};
53 sg_actor_t actor = sg_actor_init(pr_name, host);
54 sg_actor_start(actor, worker_main, 4, argv);
61 static void worker_busy_loop(int argc, char* argv[])
64 double speed = xbt_str_parse_double(argv[2], "Invalid speed value");
65 double exec_remain_prev = 1e11;
67 sg_exec_t exec = sg_actor_exec_async(exec_remain_prev);
68 for (int i = 0; i < 10; i++) {
70 double new_bound = (speed / 10) * i;
71 XBT_INFO("set bound of VM1 to %f", new_bound);
72 sg_vm_set_bound(((sg_vm_t)sg_actor_get_host(sg_actor_self())), new_bound);
74 sg_actor_sleep_for(100);
75 double exec_remain_now = sg_exec_get_remaining(exec);
76 double flops_per_sec = exec_remain_prev - exec_remain_now;
77 XBT_INFO("%s@%s: %.0f flops/s", name, sg_host_get_name(sg_actor_get_host(sg_actor_self())), flops_per_sec / 100);
78 exec_remain_prev = exec_remain_now;
79 sg_actor_sleep_for(1);
85 static void test_dynamic_change()
87 sg_host_t pm0 = sg_host_by_name("Fafard");
89 sg_vm_t vm0 = sg_vm_create_core(pm0, "VM0");
90 sg_vm_t vm1 = sg_vm_create_core(pm0, "VM1");
95 const char* w0_argv[] = {"worker0", "Task0", "-1.0", NULL};
96 sg_actor_t w0 = sg_actor_init("worker0", (sg_host_t)vm0);
97 sg_actor_start(w0, worker_busy_loop, w0_argc, w0_argv);
100 char* speed = bprintf("%f", sg_host_speed(pm0));
101 const char* w1_argv[] = {"worker1", "Task1", speed, NULL};
103 sg_actor_t w1 = sg_actor_init("worker1", (sg_host_t)vm1);
104 sg_actor_start(w1, worker_busy_loop, w1_argc, w1_argv);
106 sg_actor_sleep_for(3000); // let the tasks end
113 static void test_one_task(sg_host_t hostA)
115 const double cpu_speed = sg_host_speed(hostA);
116 const double computation_amount = cpu_speed * 10;
117 const char* hostA_name = sg_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 sg_actor_sleep_for(1000);
126 XBT_INFO("### Test: 50%% for Task1@%s", hostA_name);
127 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
129 sg_actor_sleep_for(1000);
131 XBT_INFO("### Test: 33%% for Task1@%s", hostA_name);
132 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 3);
134 sg_actor_sleep_for(1000);
136 XBT_INFO("### Test: zero for Task1@%s (i.e., unlimited)", hostA_name);
137 launch_worker(hostA, "worker0", computation_amount, 1, 0);
139 sg_actor_sleep_for(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 sg_actor_sleep_for(1000);
147 static void test_two_tasks(sg_host_t hostA, sg_host_t hostB)
149 const double cpu_speed = sg_host_speed(hostA);
150 xbt_assert(cpu_speed == sg_host_speed(hostB));
151 const double computation_amount = cpu_speed * 10;
152 const char* hostA_name = sg_host_get_name(hostA);
153 const char* hostB_name = sg_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 sg_actor_sleep_for(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 sg_actor_sleep_for(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 sg_actor_sleep_for(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 sg_actor_sleep_for(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 sg_actor_sleep_for(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 sg_actor_sleep_for(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 sg_actor_sleep_for(1000);
198 static void master_main(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
200 sg_host_t pm0 = sg_host_by_name("Fafard");
201 sg_host_t pm1 = sg_host_by_name("Fafard");
203 XBT_INFO("# 1. Put a single task on a PM. ");
207 XBT_INFO("# 2. Put two tasks on a PM.");
208 test_two_tasks(pm0, pm0);
211 sg_vm_t vm0 = sg_vm_create_core(pm0, "VM0");
214 XBT_INFO("# 3. Put a single task on a VM. ");
215 test_one_task((sg_host_t)vm0);
218 XBT_INFO("# 4. Put two tasks on a VM.");
219 test_two_tasks((sg_host_t)vm0, (sg_host_t)vm0);
224 vm0 = sg_vm_create_core(pm0, "VM0");
227 XBT_INFO("# 6. Put a task on a PM and a task on a VM.");
228 test_two_tasks(pm0, (sg_host_t)vm0);
233 vm0 = sg_vm_create_core(pm0, "VM0");
234 double cpu_speed = sg_host_speed(pm0);
235 sg_vm_set_bound(vm0, cpu_speed / 10);
238 XBT_INFO("# 7. Put a single task on the VM capped by 10%%.");
239 test_one_task((sg_host_t)vm0);
242 XBT_INFO("# 8. Put two tasks on the VM capped by 10%%.");
243 test_two_tasks((sg_host_t)vm0, (sg_host_t)vm0);
246 XBT_INFO("# 9. Put a task on a PM and a task on the VM capped by 10%%.");
247 test_two_tasks(pm0, (sg_host_t)vm0);
252 vm0 = sg_vm_create_core(pm0, "VM0");
254 sg_vm_set_ramsize(vm0, 1e9); // 1GB
257 cpu_speed = sg_host_speed(pm0);
260 XBT_INFO("# 10. Test migration");
261 const double computation_amount = cpu_speed * 10;
263 XBT_INFO("# 10. (a) Put a task on a VM without any bound.");
264 launch_worker((sg_host_t)vm0, "worker0", computation_amount, 0, 0);
265 sg_actor_sleep_for(1000);
268 XBT_INFO("# 10. (b) set 10%% bound to the VM, and then put a task on the VM.");
269 sg_vm_set_bound(vm0, cpu_speed / 10);
270 launch_worker((sg_host_t)vm0, "worker0", computation_amount, 0, 0);
271 sg_actor_sleep_for(1000);
274 XBT_INFO("# 10. (c) migrate");
275 sg_vm_migrate(vm0, pm1);
278 XBT_INFO("# 10. (d) Put a task again on the VM.");
279 launch_worker((sg_host_t)vm0, "worker0", computation_amount, 0, 0);
280 sg_actor_sleep_for(1000);
285 XBT_INFO("# 11. Change a bound dynamically.");
286 test_dynamic_change();
289 int main(int argc, char* argv[])
291 /* Get the arguments */
292 simgrid_init(&argc, argv);
293 sg_vm_live_migration_plugin_init();
295 /* load the platform file */
296 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
298 simgrid_load_platform(argv[1]);
300 sg_actor_t actor = sg_actor_init("master_", sg_host_by_name("Fafard"));
301 sg_actor_start(actor, master_main, 0, NULL);
304 XBT_INFO("Bye (simulation time %g)", simgrid_get_clock());