Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
02bbafde29987ef143638de961dbbb9463411354
[simgrid.git] / examples / cpp / cloud-capping / s4u-cloud-capping.cpp
1 /* Copyright (c) 2007-2021. 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/s4u.hpp"
7 #include "simgrid/plugins/live_migration.h"
8 #include "simgrid/s4u/VirtualMachine.hpp"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
11
12 static void worker(double computation_amount, bool use_bound, double bound)
13 {
14   double clock_start = simgrid::s4u::Engine::get_clock();
15
16   simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_init(computation_amount);
17
18   if (use_bound) {
19     if (bound < 1e-12) /* close enough to 0 without any floating precision surprise */
20       XBT_INFO("bound == 0 means no capping (i.e., unlimited).");
21     exec->set_bound(bound);
22   }
23   exec->start();
24   exec->wait();
25   double clock_end     = simgrid::s4u::Engine::get_clock();
26   double duration      = clock_end - clock_start;
27   double flops_per_sec = computation_amount / duration;
28
29   if (use_bound)
30     XBT_INFO("bound to %f => duration %f (%f flops/s)", bound, duration, flops_per_sec);
31   else
32     XBT_INFO("not bound => duration %f (%f flops/s)", duration, flops_per_sec);
33 }
34
35 static void worker_busy_loop(const char* name, double speed)
36 {
37   double exec_remain_prev    = 1e11;
38   simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_async(exec_remain_prev);
39   for (int i = 0; i < 10; i++) {
40     if (speed > 0) {
41       double new_bound = (speed / 10) * i;
42       XBT_INFO("set bound of VM1 to %f", new_bound);
43       static_cast<simgrid::s4u::VirtualMachine*>(simgrid::s4u::this_actor::get_host())->set_bound(new_bound);
44     }
45     simgrid::s4u::this_actor::sleep_for(100);
46     double exec_remain_now = exec->get_remaining();
47     double flops_per_sec   = exec_remain_prev - exec_remain_now;
48     XBT_INFO("%s@%s: %.0f flops/s", name, simgrid::s4u::this_actor::get_host()->get_cname(), flops_per_sec / 100);
49     exec_remain_prev = exec_remain_now;
50     simgrid::s4u::this_actor::sleep_for(1);
51   }
52   exec->wait();
53 }
54
55 static void test_dynamic_change()
56 {
57   simgrid::s4u::Host* pm0 = simgrid::s4u::Host::by_name("Fafard");
58
59   auto* vm0 = pm0->create_vm("VM0", 1);
60   auto* vm1 = pm0->create_vm("VM1", 1);
61   vm0->start();
62   vm1->start();
63
64   simgrid::s4u::Actor::create("worker0", vm0, worker_busy_loop, "Task0", -1);
65   simgrid::s4u::Actor::create("worker1", vm1, worker_busy_loop, "Task1", pm0->get_speed());
66
67   simgrid::s4u::this_actor::sleep_for(3000); // let the activities end
68   vm0->destroy();
69   vm1->destroy();
70 }
71
72 static void test_one_activity(simgrid::s4u::Host* host)
73 {
74   const double cpu_speed          = host->get_speed();
75   const double computation_amount = cpu_speed * 10;
76
77   XBT_INFO("### Test: with/without activity set_bound");
78
79   XBT_INFO("### Test: no bound for Task1@%s", host->get_cname());
80   simgrid::s4u::Actor::create("worker0", host, worker, computation_amount, false, 0);
81
82   simgrid::s4u::this_actor::sleep_for(1000);
83
84   XBT_INFO("### Test: 50%% for Task1@%s", host->get_cname());
85   simgrid::s4u::Actor::create("worker0", host, worker, computation_amount, true, cpu_speed / 2);
86
87   simgrid::s4u::this_actor::sleep_for(1000);
88
89   XBT_INFO("### Test: 33%% for Task1@%s", host->get_cname());
90   simgrid::s4u::Actor::create("worker0", host, worker, computation_amount, true, cpu_speed / 3);
91
92   simgrid::s4u::this_actor::sleep_for(1000);
93
94   XBT_INFO("### Test: zero for Task1@%s (i.e., unlimited)", host->get_cname());
95   simgrid::s4u::Actor::create("worker0", host, worker, computation_amount, true, 0);
96
97   simgrid::s4u::this_actor::sleep_for(1000);
98
99   XBT_INFO("### Test: 200%% for Task1@%s (i.e., meaningless)", host->get_cname());
100   simgrid::s4u::Actor::create("worker0", host, worker, computation_amount, true, cpu_speed * 2);
101
102   simgrid::s4u::this_actor::sleep_for(1000);
103 }
104
105 static void test_two_activities(simgrid::s4u::Host* hostA, simgrid::s4u::Host* hostB)
106 {
107   const double cpu_speed = hostA->get_speed();
108   xbt_assert(cpu_speed == hostB->get_speed());
109   const double computation_amount = cpu_speed * 10;
110   const char* hostA_name          = hostA->get_cname();
111   const char* hostB_name          = hostB->get_cname();
112
113   XBT_INFO("### Test: no bound for Task1@%s, no bound for Task2@%s", hostA_name, hostB_name);
114   simgrid::s4u::Actor::create("worker0", hostA, worker, computation_amount, false, 0);
115   simgrid::s4u::Actor::create("worker1", hostB, worker, computation_amount, false, 0);
116
117   simgrid::s4u::this_actor::sleep_for(1000);
118
119   XBT_INFO("### Test: 0 for Task1@%s, 0 for Task2@%s (i.e., unlimited)", hostA_name, hostB_name);
120   simgrid::s4u::Actor::create("worker0", hostA, worker, computation_amount, true, 0);
121   simgrid::s4u::Actor::create("worker1", hostB, worker, computation_amount, true, 0);
122
123   simgrid::s4u::this_actor::sleep_for(1000);
124
125   XBT_INFO("### Test: 50%% for Task1@%s, 50%% for Task2@%s", hostA_name, hostB_name);
126   simgrid::s4u::Actor::create("worker0", hostA, worker, computation_amount, true, cpu_speed / 2);
127   simgrid::s4u::Actor::create("worker1", hostB, worker, computation_amount, true, cpu_speed / 2);
128
129   simgrid::s4u::this_actor::sleep_for(1000);
130
131   XBT_INFO("### Test: 25%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
132   simgrid::s4u::Actor::create("worker0", hostA, worker, computation_amount, true, cpu_speed / 4);
133   simgrid::s4u::Actor::create("worker1", hostB, worker, computation_amount, true, cpu_speed / 4);
134
135   simgrid::s4u::this_actor::sleep_for(1000);
136
137   XBT_INFO("### Test: 75%% for Task1@%s, 100%% for Task2@%s", hostA_name, hostB_name);
138   simgrid::s4u::Actor::create("worker0", hostA, worker, computation_amount, true, cpu_speed * 0.75);
139   simgrid::s4u::Actor::create("worker1", hostB, worker, computation_amount, true, cpu_speed);
140
141   simgrid::s4u::this_actor::sleep_for(1000);
142
143   XBT_INFO("### Test: no bound for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
144   simgrid::s4u::Actor::create("worker0", hostA, worker, computation_amount, false, 0);
145   simgrid::s4u::Actor::create("worker1", hostB, worker, computation_amount, true, cpu_speed / 4);
146
147   simgrid::s4u::this_actor::sleep_for(1000);
148
149   XBT_INFO("### Test: 75%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
150   simgrid::s4u::Actor::create("worker0", hostA, worker, computation_amount, true, cpu_speed * 0.75);
151   simgrid::s4u::Actor::create("worker1", hostB, worker, computation_amount, true, cpu_speed / 4);
152
153   simgrid::s4u::this_actor::sleep_for(1000);
154 }
155
156 static void master_main()
157 {
158   simgrid::s4u::Host* pm0 = simgrid::s4u::Host::by_name("Fafard");
159
160   XBT_INFO("# 1. Put a single activity on a PM.");
161   test_one_activity(pm0);
162   XBT_INFO(".");
163
164   XBT_INFO("# 2. Put two activities on a PM.");
165   test_two_activities(pm0, pm0);
166   XBT_INFO(".");
167
168   auto* vm0 = pm0->create_vm("VM0", 1);
169   vm0->start();
170
171   XBT_INFO("# 3. Put a single activity on a VM.");
172   test_one_activity(vm0);
173   XBT_INFO(".");
174
175   XBT_INFO("# 4. Put two activities on a VM.");
176   test_two_activities(vm0, vm0);
177   XBT_INFO(".");
178
179   vm0->destroy();
180
181   vm0 = pm0->create_vm("VM0", 1);
182   vm0->start();
183
184   XBT_INFO("# 6. Put an activity on a PM and an activity on a VM.");
185   test_two_activities(pm0, vm0);
186   XBT_INFO(".");
187
188   vm0->destroy();
189
190   vm0 = pm0->create_vm("VM0", 1);
191   vm0->set_bound(pm0->get_speed() / 10);
192   vm0->start();
193
194   XBT_INFO("# 7. Put a single activity on the VM capped by 10%%.");
195   test_one_activity(vm0);
196   XBT_INFO(".");
197
198   XBT_INFO("# 8. Put two activities on the VM capped by 10%%.");
199   test_two_activities(vm0, vm0);
200   XBT_INFO(".");
201
202   XBT_INFO("# 9. Put an activity on a PM and an activity on the VM capped by 10%%.");
203   test_two_activities(pm0, vm0);
204   XBT_INFO(".");
205
206   vm0->destroy();
207
208   vm0 = pm0->create_vm("VM0", 1);
209   vm0->set_ramsize(1e9); // 1GB
210   vm0->start();
211
212   double cpu_speed = pm0->get_speed();
213
214   XBT_INFO("# 10. Test migration");
215   const double computation_amount = cpu_speed * 10;
216
217   XBT_INFO("# 10. (a) Put an activity on a VM without any bound.");
218   simgrid::s4u::Actor::create("worker0", vm0, worker, computation_amount, false, 0);
219   simgrid::s4u::this_actor::sleep_for(1000);
220   XBT_INFO(".");
221
222   XBT_INFO("# 10. (b) set 10%% bound to the VM, and then put an activity on the VM.");
223   vm0->set_bound(cpu_speed / 10);
224   simgrid::s4u::Actor::create("worker0", vm0, worker, computation_amount, false, 0);
225   simgrid::s4u::this_actor::sleep_for(1000);
226   XBT_INFO(".");
227
228   XBT_INFO("# 10. (c) migrate");
229   simgrid::s4u::Host* pm1 = simgrid::s4u::Host::by_name("Fafard");
230   sg_vm_migrate(vm0, pm1);
231   XBT_INFO(".");
232
233   XBT_INFO("# 10. (d) Put an activity again on the VM.");
234   simgrid::s4u::Actor::create("worker0", vm0, worker, computation_amount, false, 0);
235   simgrid::s4u::this_actor::sleep_for(1000);
236   XBT_INFO(".");
237
238   vm0->destroy();
239
240   XBT_INFO("# 11. Change a bound dynamically.");
241   test_dynamic_change();
242 }
243
244 int main(int argc, char* argv[])
245 {
246   simgrid::s4u::Engine e(&argc, argv);
247   sg_vm_live_migration_plugin_init();
248   /* load the platform file */
249   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
250
251   e.load_platform(argv[1]);
252
253   simgrid::s4u::Actor::create("master_", e.host_by_name("Fafard"), master_main);
254
255   e.run();
256   XBT_INFO("Bye (simulation time %g)", simgrid::s4u::Engine::get_clock());
257
258   return 0;
259 }