Logo AND Algorithmique Numérique Distribuée

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