Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / plugins / vm / VmLiveMigration.cpp
1 /* Copyright (c) 2013-2023. 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/Exception.hpp>
7 #include <simgrid/plugins/live_migration.h>
8
9 #include "src/instr/instr_private.hpp"
10 #include "src/kernel/resource/VirtualMachineImpl.hpp"
11 #include "src/plugins/vm/VmLiveMigration.hpp"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(vm_live_migration, s4u, "S4U virtual machines live migration");
14
15 namespace simgrid::plugin::vm {
16 xbt::Extension<s4u::Host, VmMigrationExt> VmMigrationExt::EXTENSION_ID;
17
18 void VmMigrationExt::ensureVmMigrationExtInstalled()
19 {
20   if (not EXTENSION_ID.valid())
21     EXTENSION_ID = s4u::Host::extension_create<VmMigrationExt>();
22 }
23
24 void MigrationRx::operator()()
25 {
26   XBT_DEBUG("mig: rx_start");
27   bool received_finalize = false;
28
29   std::string finalize_task_name =
30       "__mig_stage3:" + vm_->get_name() + "(" + src_pm_->get_name() + "-" + dst_pm_->get_name() + ")";
31
32   while (not received_finalize) {
33     auto payload = mbox->get_unique<std::string>();
34
35     if (finalize_task_name == *payload)
36       received_finalize = true;
37   }
38
39   // Here Stage 1, 2  and 3 have been performed.
40   // Hence complete the migration
41
42   /* Update the vm location */
43   /* precopy migration makes the VM temporally paused */
44   xbt_assert(vm_->get_state() == s4u::VirtualMachine::State::SUSPENDED);
45
46   /* Update the vm location and resume it */
47   vm_->set_pm(dst_pm_);
48   vm_->resume();
49
50   // Now the VM is running on the new host (the migration is completed) (even if the SRC crash)
51   vm_->get_vm_impl()->end_migration();
52   XBT_DEBUG("VM(%s) moved from PM(%s) to PM(%s)", vm_->get_cname(), src_pm_->get_cname(), dst_pm_->get_cname());
53
54   if (TRACE_vm_is_enabled()) {
55     static long long int counter = 0;
56     std::string key              = std::to_string(counter);
57     counter++;
58
59     // start link
60     auto* msg = instr::Container::by_name(vm_->get_name());
61     instr::Container::get_root()->get_link("VM_LINK")->start_event(msg, "M", key);
62
63     // destroy existing container of this vm
64     instr::Container::by_name(vm_->get_name())->remove_from_parent();
65
66     // create new container on the new_host location
67     new instr::Container(vm_->get_name(), "VM", instr::Container::by_name(dst_pm_->get_name()));
68
69     // end link
70     msg = instr::Container::by_name(vm_->get_name());
71     instr::Container::get_root()->get_link("VM_LINK")->end_event(msg, "M", key);
72   }
73   // Inform the SRC that the migration has been correctly performed
74   auto* payload = new std::string("__mig_stage4:");
75   *payload      = *payload + vm_->get_name() + "(" + src_pm_->get_name() + "-" + dst_pm_->get_name() + ")";
76
77   mbox_ctl->put(payload, 0);
78
79   XBT_DEBUG("mig: rx_done");
80 }
81
82 static sg_size_t get_updated_size(double computed, double dp_rate, sg_size_t dp_cap)
83 {
84   auto updated_size = static_cast<sg_size_t>(computed * dp_rate);
85   XBT_DEBUG("updated_size %llu dp_rate %f", updated_size, dp_rate);
86   if (updated_size > dp_cap) {
87     updated_size = dp_cap;
88   }
89
90   return updated_size;
91 }
92
93 sg_size_t MigrationTx::sendMigrationData(sg_size_t size, int stage, int stage2_round, double mig_speed, double timeout)
94 {
95   sg_size_t sent   = size;
96   auto msg = std::make_unique<std::string>(xbt::string_printf("__mig_stage%d:%s(%s-%s)", stage, vm_->get_cname(),
97                                                               src_pm_->get_cname(), dst_pm_->get_cname()));
98   double clock_sta = s4u::Engine::get_clock();
99
100   s4u::CommPtr comm = mbox->put_init(msg.get(), size);
101   if (mig_speed > 0)
102     comm->set_rate(mig_speed);
103   try {
104     comm->wait_for(timeout);
105     msg.release();
106   } catch (const Exception&) {
107     auto remaining = static_cast<sg_size_t>(comm->get_remaining());
108     XBT_VERB("timeout (%lf s) in sending_migration_data, remaining %llu bytes of %llu", timeout, remaining, size);
109     sent -= remaining;
110   }
111
112   double clock_end    = s4u::Engine::get_clock();
113   double duration     = clock_end - clock_sta;
114   double actual_speed = static_cast<double>(size) / duration;
115
116   if (stage == 2)
117     XBT_DEBUG("mig-stage%d.%d: sent %llu duration %f actual_speed %f (target %f)", stage, stage2_round, size, duration,
118               actual_speed, mig_speed);
119   else
120     XBT_DEBUG("mig-stage%d: sent %llu duration %f actual_speed %f (target %f)", stage, size, duration, actual_speed,
121               mig_speed);
122
123   return sent;
124 }
125
126 void MigrationTx::operator()()
127 {
128   XBT_DEBUG("mig: tx_start");
129
130   double host_speed       = vm_->get_pm()->get_speed();
131   const sg_size_t ramsize = vm_->get_ramsize();
132   const double dp_rate =
133       host_speed != 0.0 ? (sg_vm_get_migration_speed(vm_) * sg_vm_get_dirty_page_intensity(vm_)) / host_speed : 1;
134   const sg_size_t dp_cap = sg_vm_get_working_set_memory(vm_);
135   const double mig_speed = sg_vm_get_migration_speed(vm_);
136   double max_downtime    = sg_vm_get_max_downtime(vm_);
137
138   double mig_timeout = 10000000.0;
139   bool skip_stage2   = false;
140
141   size_t remaining_size = ramsize;
142
143   double clock_prev_send;
144   double clock_post_send;
145   double bandwidth;
146   size_t threshold;
147
148   /* check parameters */
149   if (ramsize == 0)
150     XBT_WARN("migrate a VM, but ramsize is zero");
151
152   if (max_downtime <= 0) {
153     XBT_WARN("use the default max_downtime value 30ms");
154     max_downtime = 0.03;
155   }
156
157   /* Stage1: send all memory pages to the destination. */
158   XBT_DEBUG("mig-stage1: remaining_size %zu", remaining_size);
159   sg_vm_start_dirty_page_tracking(vm_);
160
161   double computed_during_stage1 = 0;
162   clock_prev_send               = s4u::Engine::get_clock();
163
164   try {
165     /* At stage 1, we do not need timeout. We have to send all the memory pages even though the duration of this
166      * transfer exceeds the timeout value. */
167     XBT_VERB("Stage 1: Gonna send %llu bytes", ramsize);
168     sg_size_t sent = sendMigrationData(ramsize, 1, 0, mig_speed, -1);
169     remaining_size -= sent;
170     computed_during_stage1 = sg_vm_lookup_computed_flops(vm_);
171
172     if (sent < ramsize) {
173       XBT_VERB("mig-stage1: timeout, force moving to stage 3");
174       skip_stage2 = true;
175     } else if (sent > ramsize)
176       XBT_CRITICAL("bug");
177   } catch (const Exception&) {
178     // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
179     // Stop the dirty page tracking and return (there is no memory space to release)
180     sg_vm_stop_dirty_page_tracking(vm_);
181     return;
182   }
183
184   clock_post_send = s4u::Engine::get_clock();
185   mig_timeout -= (clock_post_send - clock_prev_send);
186   if (mig_timeout < 0) {
187     XBT_VERB("The duration of stage 1 exceeds the timeout value, skip stage 2");
188     skip_stage2 = true;
189   }
190
191   /* estimate bandwidth */
192   bandwidth = ramsize / (clock_post_send - clock_prev_send);
193   threshold = bandwidth * max_downtime;
194   XBT_DEBUG("actual bandwidth %f (MB/s), threshold %zu", bandwidth / 1024 / 1024, threshold);
195
196   /* Stage2: send update pages iteratively until the size of remaining states becomes smaller than threshold value. */
197   if (not skip_stage2) {
198     int stage2_round = 0;
199     /* just after stage1, nothing has been updated. But, we have to send the data updated during stage1 */
200     sg_size_t updated_size = get_updated_size(computed_during_stage1, dp_rate, dp_cap);
201     remaining_size += updated_size;
202     XBT_DEBUG("mig-stage2.%d: remaining_size %zu (%s threshold %zu)", stage2_round, remaining_size,
203               (remaining_size < threshold) ? "<" : ">", threshold);
204
205     /* When the remaining size is below the threshold value, move to stage 3. */
206     while (threshold < remaining_size) {
207       XBT_DEBUG("mig-stage 2:%d updated_size %llu computed_during_stage1 %f dp_rate %f dp_cap %llu", stage2_round,
208                 updated_size, computed_during_stage1, dp_rate, dp_cap);
209
210       sg_size_t sent  = 0;
211       clock_prev_send = s4u::Engine::get_clock();
212       try {
213         XBT_DEBUG("Stage 2, gonna send %llu", updated_size);
214         sent = sendMigrationData(updated_size, 2, stage2_round, mig_speed, mig_timeout);
215       } catch (const Exception&) {
216         // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data
217         // code)
218         // Stop the dirty page tracking and return (there is no memory space to release)
219         sg_vm_stop_dirty_page_tracking(vm_);
220         return;
221       }
222
223       remaining_size -= sent;
224       double computed = sg_vm_lookup_computed_flops(vm_);
225
226       clock_post_send = s4u::Engine::get_clock();
227
228       if (sent == updated_size) {
229         bandwidth = updated_size / (clock_post_send - clock_prev_send);
230         threshold = bandwidth * max_downtime;
231         XBT_DEBUG("actual bandwidth %f, threshold %zu", bandwidth / 1024 / 1024, threshold);
232         stage2_round += 1;
233         mig_timeout -= (clock_post_send - clock_prev_send);
234         xbt_assert(mig_timeout > 0);
235         XBT_DEBUG("mig-stage2.%d: remaining_size %zu (%s threshold %zu)", stage2_round, remaining_size,
236                   (remaining_size < threshold) ? "<" : ">", threshold);
237         updated_size = get_updated_size(computed, dp_rate, dp_cap);
238         remaining_size += updated_size;
239       } else {
240         /* When timeout happens, we move to stage 3. The size of memory pages
241          * updated before timeout must be added to the remaining size. */
242         XBT_VERB("mig-stage2.%d: timeout, force moving to stage 3. sent %llu / %llu, eta %lf", stage2_round, sent,
243                  updated_size, (clock_post_send - clock_prev_send));
244         updated_size    = get_updated_size(computed, dp_rate, dp_cap);
245         remaining_size += updated_size;
246         break;
247       }
248     }
249   }
250
251   /* Stage3: stop the VM and copy the rest of states. */
252   XBT_DEBUG("mig-stage3: remaining_size %zu", remaining_size);
253   vm_->suspend();
254   sg_vm_stop_dirty_page_tracking(vm_);
255
256   try {
257     XBT_DEBUG("Stage 3: Gonna send %zu bytes", remaining_size);
258     sendMigrationData(remaining_size, 3, 0, mig_speed, -1);
259   } catch (const Exception&) {
260     // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
261     // Stop the dirty page tracking and return (there is no memory space to release)
262     vm_->resume();
263     return;
264   }
265
266   // At that point the Migration is considered valid for the SRC node but remind that the DST side should relocate
267   // effectively the VM on the DST node.
268   XBT_DEBUG("mig: tx_done");
269 }
270 } // namespace simgrid::plugin::vm
271
272 using simgrid::plugin::vm::VmMigrationExt;
273
274 static void onVirtualMachineShutdown(simgrid::s4u::VirtualMachine const& vm)
275 {
276   if (vm.get_vm_impl()->is_migrating()) {
277     vm.extension<VmMigrationExt>()->rx_->kill();
278     vm.extension<VmMigrationExt>()->tx_->kill();
279     vm.extension<VmMigrationExt>()->issuer_->kill();
280     vm.get_vm_impl()->end_migration();
281   }
282 }
283
284 void sg_vm_live_migration_plugin_init()
285 {
286   sg_vm_dirty_page_tracking_init();
287   VmMigrationExt::ensureVmMigrationExtInstalled();
288   simgrid::s4u::VirtualMachine::on_shutdown_cb(&onVirtualMachineShutdown);
289 }
290
291 simgrid::s4u::VirtualMachine* sg_vm_create_migratable(simgrid::s4u::Host* pm, const char* name, int coreAmount,
292                                                       int ramsize, int mig_netspeed, int dp_intensity)
293 {
294   simgrid::s4u::VmHostExt::ensureVmExtInstalled();
295
296   /* For the moment, intensity_rate is the percentage against the migration bandwidth */
297
298   auto* vm = pm->create_vm(name, coreAmount, static_cast<sg_size_t>(ramsize) * 1024 * 1024);
299   sg_vm_set_dirty_page_intensity(vm, dp_intensity / 100.0);
300   sg_vm_set_working_set_memory(vm, vm->get_ramsize() * 0.9); // assume working set memory is 90% of ramsize
301   sg_vm_set_migration_speed(vm, mig_netspeed * 1024 * 1024.0);
302
303   XBT_DEBUG("migspeed : %f intensity mem : %d", mig_netspeed * 1024 * 1024.0, dp_intensity);
304
305   return vm;
306 }
307
308 int sg_vm_is_migrating(const simgrid::s4u::VirtualMachine* vm)
309 {
310   return vm->get_vm_impl()->is_migrating();
311 }
312
313 void sg_vm_migrate(simgrid::s4u::VirtualMachine* vm, simgrid::s4u::Host* dst_pm)
314 {
315   simgrid::s4u::Host* src_pm = vm->get_pm();
316
317   if (not src_pm->is_on())
318     throw simgrid::VmFailureException(
319         XBT_THROW_POINT, simgrid::xbt::string_printf("Cannot migrate VM '%s' from host '%s', which is offline.",
320                                                      vm->get_cname(), src_pm->get_cname()));
321   if (not dst_pm->is_on())
322     throw simgrid::VmFailureException(
323         XBT_THROW_POINT, simgrid::xbt::string_printf("Cannot migrate VM '%s' to host '%s', which is offline.",
324                                                      vm->get_cname(), dst_pm->get_cname()));
325   if (vm->get_state() != simgrid::s4u::VirtualMachine::State::RUNNING)
326     throw simgrid::VmFailureException(
327         XBT_THROW_POINT,
328         simgrid::xbt::string_printf("Cannot migrate VM '%s' that is not running yet.", vm->get_cname()));
329   if (vm->get_vm_impl()->is_migrating())
330     throw simgrid::VmFailureException(
331         XBT_THROW_POINT,
332         simgrid::xbt::string_printf("Cannot migrate VM '%s' that is already migrating.", vm->get_cname()));
333
334   vm->start_migration();
335
336   std::string rx_name = "__pr_mig_rx:" + vm->get_name() + "(" + src_pm->get_name() + "-" + dst_pm->get_name() + ")";
337   std::string tx_name = "__pr_mig_tx:" + vm->get_name() + "(" + src_pm->get_name() + "-" + dst_pm->get_name() + ")";
338
339   simgrid::s4u::ActorPtr rx =
340       simgrid::s4u::Actor::create(rx_name.c_str(), dst_pm, simgrid::plugin::vm::MigrationRx(vm, dst_pm));
341   simgrid::s4u::ActorPtr tx =
342       simgrid::s4u::Actor::create(tx_name.c_str(), src_pm, simgrid::plugin::vm::MigrationTx(vm, dst_pm));
343
344   vm->extension_set<VmMigrationExt>(new VmMigrationExt(simgrid::s4u::Actor::self(), rx, tx));
345
346   /* wait until the migration have finished or on error has occurred */
347   XBT_DEBUG("wait for reception of the final ACK (i.e. migration has been correctly performed");
348   simgrid::s4u::Mailbox* mbox_ctl = simgrid::s4u::Mailbox::by_name("__mbox_mig_ctl:" + vm->get_name() + "(" +
349                                                                    src_pm->get_name() + "-" + dst_pm->get_name() + ")");
350   mbox_ctl->get_unique<std::string>();
351   tx->join();
352   rx->join();
353
354   vm->end_migration();
355 }