Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename the plugins from the command line, and document it
[simgrid.git] / src / plugins / vm / VmLiveMigration.cpp
1 /* Copyright (c) 2013-2018. 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 "src/plugins/vm/VmLiveMigration.hpp"
7 #include "src/instr/instr_private.hpp"
8 #include "src/plugins/vm/VirtualMachineImpl.hpp"
9 #include "src/plugins/vm/VmHostExt.hpp"
10 #include "xbt/ex.hpp"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(vm_live_migration, "S4U virtual machines live migration");
13
14 namespace simgrid {
15 namespace vm {
16 simgrid::xbt::Extension<s4u::Host, VmMigrationExt> VmMigrationExt::EXTENSION_ID;
17
18 void VmMigrationExt::ensureVmMigrationExtInstalled()
19 {
20   if (not EXTENSION_ID.valid())
21     EXTENSION_ID = simgrid::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       std::string("__mig_stage3:") + vm_->get_cname() + "(" + src_pm_->get_cname() + "-" + dst_pm_->get_cname() + ")";
31
32   while (not received_finalize) {
33     std::string* payload = static_cast<std::string*>(mbox->get());
34
35     if (finalize_task_name == *payload)
36       received_finalize = true;
37
38     delete payload;
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_->getState() == SURF_VM_STATE_SUSPENDED);
47
48   /* Update the vm location and resume it */
49   vm_->setPm(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_->getImpl()->isMigrating = false;
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     container_t msg = simgrid::instr::Container::by_name(vm_->get_name());
63     simgrid::instr::Container::get_root()->get_link("VM_LINK")->start_event(msg, "M", key);
64
65     // destroy existing container of this vm
66     simgrid::instr::Container::by_name(vm_->get_name())->remove_from_parent();
67
68     // create new container on the new_host location
69     new simgrid::instr::Container(vm_->get_cname(), "VM", simgrid::instr::Container::by_name(dst_pm_->get_name()));
70
71     // end link
72     msg = simgrid::instr::Container::by_name(vm_->get_name());
73     simgrid::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   std::string* 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   sg_size_t 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   std::string* msg = new std::string("__mig_stage");
99   *msg             = *msg + std::to_string(stage) + ":" + vm_->get_cname() + "(" + src_pm_->get_cname() + "-" +
100          dst_pm_->get_cname() + ")";
101
102   double clock_sta = s4u::Engine::get_clock();
103
104   s4u::Activity* comm = nullptr;
105   try {
106     if (mig_speed > 0)
107       comm = mbox->put_init(msg, size)->set_rate(mig_speed)->wait(timeout);
108     else
109       comm = mbox->put_async(msg, size)->wait();
110   } catch (xbt_ex& e) {
111     if (comm) {
112       sg_size_t remaining = static_cast<sg_size_t>(comm->get_remaining());
113       XBT_VERB("timeout (%lf s) in sending_migration_data, remaining %llu bytes of %llu", timeout, remaining, size);
114       sent -= remaining;
115     }
116     delete msg;
117   }
118
119   double clock_end    = s4u::Engine::get_clock();
120   double duration     = clock_end - clock_sta;
121   double actual_speed = size / duration;
122
123   if (stage == 2)
124     XBT_DEBUG("mig-stage%d.%d: sent %llu duration %f actual_speed %f (target %f)", stage, stage2_round, size, duration,
125               actual_speed, mig_speed);
126   else
127     XBT_DEBUG("mig-stage%d: sent %llu duration %f actual_speed %f (target %f)", stage, size, duration, actual_speed,
128               mig_speed);
129
130   return sent;
131 }
132
133 void MigrationTx::operator()()
134 {
135   XBT_DEBUG("mig: tx_start");
136
137   double host_speed = vm_->getPm()->getSpeed();
138   const sg_size_t ramsize = vm_->getRamsize();
139   const double dp_rate =
140       host_speed ? (sg_vm_get_migration_speed(vm_) * sg_vm_get_dirty_page_intensity(vm_)) / host_speed : 1;
141   const sg_size_t dp_cap = sg_vm_get_working_set_memory(vm_);
142   const double mig_speed = sg_vm_get_migration_speed(vm_);
143   double max_downtime    = sg_vm_get_max_downtime(vm_);
144
145   double mig_timeout = 10000000.0;
146   bool skip_stage2   = false;
147
148   size_t remaining_size = ramsize;
149   size_t threshold      = 0.0;
150
151   /* check parameters */
152   if (ramsize == 0)
153     XBT_WARN("migrate a VM, but ramsize is zero");
154
155   if (max_downtime <= 0) {
156     XBT_WARN("use the default max_downtime value 30ms");
157     max_downtime = 0.03;
158   }
159
160   /* Stage1: send all memory pages to the destination. */
161   XBT_DEBUG("mig-stage1: remaining_size %zu", remaining_size);
162   sg_vm_start_dirty_page_tracking(vm_);
163
164   double computed_during_stage1 = 0;
165   double clock_prev_send        = s4u::Engine::get_clock();
166
167   try {
168     /* At stage 1, we do not need timeout. We have to send all the memory pages even though the duration of this
169      * transfer exceeds the timeout value. */
170     XBT_VERB("Stage 1: Gonna send %llu bytes", ramsize);
171     sg_size_t sent = sendMigrationData(ramsize, 1, 0, mig_speed, -1);
172     remaining_size -= sent;
173     computed_during_stage1 = sg_vm_lookup_computed_flops(vm_);
174
175     if (sent < ramsize) {
176       XBT_VERB("mig-stage1: timeout, force moving to stage 3");
177       skip_stage2 = true;
178     } else if (sent > ramsize)
179       XBT_CRITICAL("bug");
180
181   } catch (xbt_ex& e) {
182     // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
183     // Stop the dirty page tracking an return (there is no memory space to release)
184     sg_vm_stop_dirty_page_tracking(vm_);
185     return;
186   }
187
188   double clock_post_send = s4u::Engine::get_clock();
189   mig_timeout -= (clock_post_send - clock_prev_send);
190   if (mig_timeout < 0) {
191     XBT_VERB("The duration of stage 1 exceeds the timeout value, skip stage 2");
192     skip_stage2 = true;
193   }
194
195   /* estimate bandwidth */
196   double bandwidth = ramsize / (clock_post_send - clock_prev_send);
197   threshold        = bandwidth * max_downtime;
198   XBT_DEBUG("actual bandwidth %f (MB/s), threshold %zu", bandwidth / 1024 / 1024, threshold);
199
200   /* Stage2: send update pages iteratively until the size of remaining states becomes smaller than threshold value. */
201   if (not skip_stage2) {
202
203     int stage2_round = 0;
204     for (;;) {
205       sg_size_t updated_size = 0;
206       if (stage2_round == 0) {
207         /* just after stage1, nothing has been updated. But, we have to send the data updated during stage1 */
208         updated_size = get_updated_size(computed_during_stage1, dp_rate, dp_cap);
209       } else {
210         double computed = sg_vm_lookup_computed_flops(vm_);
211         updated_size    = get_updated_size(computed, dp_rate, dp_cap);
212       }
213
214       XBT_DEBUG("mig-stage 2:%d updated_size %llu computed_during_stage1 %f dp_rate %f dp_cap %llu", stage2_round,
215                 updated_size, computed_during_stage1, dp_rate, dp_cap);
216
217       /* Check whether the remaining size is below the threshold value. If so, move to stage 3. */
218       remaining_size += updated_size;
219       XBT_DEBUG("mig-stage2.%d: remaining_size %zu (%s threshold %zu)", stage2_round, remaining_size,
220                 (remaining_size < threshold) ? "<" : ">", threshold);
221       if (remaining_size < threshold)
222         break;
223
224       sg_size_t sent         = 0;
225       double clock_prev_send = s4u::Engine::get_clock();
226       try {
227         XBT_DEBUG("Stage 2, gonna send %llu", updated_size);
228         sent = sendMigrationData(updated_size, 2, stage2_round, mig_speed, mig_timeout);
229       } catch (xbt_ex& e) {
230         // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data
231         // code)
232         // Stop the dirty page tracking an return (there is no memory space to release)
233         sg_vm_stop_dirty_page_tracking(vm_);
234         return;
235       }
236       double clock_post_send = s4u::Engine::get_clock();
237
238       if (sent == updated_size) {
239         /* timeout did not happen */
240         double bandwidth = updated_size / (clock_post_send - clock_prev_send);
241         threshold        = bandwidth * max_downtime;
242         XBT_DEBUG("actual bandwidth %f, threshold %zu", bandwidth / 1024 / 1024, threshold);
243         remaining_size -= sent;
244         stage2_round += 1;
245         mig_timeout -= (clock_post_send - clock_prev_send);
246         xbt_assert(mig_timeout > 0);
247
248       } else if (sent < updated_size) {
249         /* When timeout happens, we move to stage 3. The size of memory pages
250          * updated before timeout must be added to the remaining size. */
251         XBT_VERB("mig-stage2.%d: timeout, force moving to stage 3. sent %llu / %llu, eta %lf", stage2_round, sent,
252                  updated_size, (clock_post_send - clock_prev_send));
253         remaining_size -= sent;
254
255         double computed = sg_vm_lookup_computed_flops(vm_);
256         updated_size    = get_updated_size(computed, dp_rate, dp_cap);
257         remaining_size += updated_size;
258         break;
259       } else
260         XBT_CRITICAL("bug");
261     }
262   }
263
264   /* Stage3: stop the VM and copy the rest of states. */
265   XBT_DEBUG("mig-stage3: remaining_size %zu", remaining_size);
266   vm_->suspend();
267   sg_vm_stop_dirty_page_tracking(vm_);
268
269   try {
270     XBT_DEBUG("Stage 3: Gonna send %zu bytes", remaining_size);
271     sendMigrationData(remaining_size, 3, 0, mig_speed, -1);
272   } catch (xbt_ex& e) {
273     // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
274     // Stop the dirty page tracking an return (there is no memory space to release)
275     vm_->resume();
276     return;
277   }
278
279   // At that point the Migration is considered valid for the SRC node but remind that the DST side should relocate
280   // effectively the VM on the DST node.
281   XBT_DEBUG("mig: tx_done");
282 }
283 }
284 }
285
286 static void onVirtualMachineShutdown(simgrid::s4u::VirtualMachine& vm)
287 {
288   if (vm.getImpl()->isMigrating) {
289     vm.extension<simgrid::vm::VmMigrationExt>()->rx_->kill();
290     vm.extension<simgrid::vm::VmMigrationExt>()->tx_->kill();
291     vm.extension<simgrid::vm::VmMigrationExt>()->issuer_->kill();
292     vm.getImpl()->isMigrating = false;
293   }
294 }
295
296 void sg_vm_live_migration_plugin_init()
297 {
298   sg_vm_dirty_page_tracking_init();
299   simgrid::vm::VmMigrationExt::ensureVmMigrationExtInstalled();
300   simgrid::s4u::VirtualMachine::on_shutdown.connect(&onVirtualMachineShutdown);
301 }
302
303 /* Deprecated. Please use MSG_vm_create_migratable() instead */
304 msg_vm_t MSG_vm_create(msg_host_t ind_pm, const char* name, int coreAmount, int ramsize, int mig_netspeed,
305                        int dp_intensity)
306 {
307   return sg_vm_create_migratable(ind_pm, name, coreAmount, ramsize, mig_netspeed, dp_intensity);
308 }
309
310 simgrid::s4u::VirtualMachine* sg_vm_create_migratable(simgrid::s4u::Host* pm, const char* name, int coreAmount,
311                                                       int ramsize, int mig_netspeed, int dp_intensity)
312 {
313   simgrid::vm::VmHostExt::ensureVmExtInstalled();
314
315   /* For the moment, intensity_rate is the percentage against the migration bandwidth */
316
317   sg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, coreAmount, static_cast<sg_size_t>(ramsize) * 1024 * 1024);
318   sg_vm_set_dirty_page_intensity(vm, dp_intensity / 100.0);
319   sg_vm_set_working_set_memory(vm, vm->getRamsize() * 0.9); // assume working set memory is 90% of ramsize
320   sg_vm_set_migration_speed(vm, mig_netspeed * 1024 * 1024.0);
321
322   XBT_DEBUG("migspeed : %f intensity mem : %d", mig_netspeed * 1024 * 1024.0, dp_intensity);
323
324   return vm;
325 }
326
327 int sg_vm_is_migrating(simgrid::s4u::VirtualMachine* vm)
328 {
329   return vm->getImpl()->isMigrating;
330 }
331
332 void sg_vm_migrate(simgrid::s4u::VirtualMachine* vm, simgrid::s4u::Host* dst_pm)
333 {
334   simgrid::s4u::Host* src_pm = vm->getPm();
335
336   if (src_pm->is_off())
337     THROWF(vm_error, 0, "Cannot migrate VM '%s' from host '%s', which is offline.", vm->get_cname(),
338            src_pm->get_cname());
339   if (dst_pm->is_off())
340     THROWF(vm_error, 0, "Cannot migrate VM '%s' to host '%s', which is offline.", vm->get_cname(), dst_pm->get_cname());
341   if (vm->getState() != SURF_VM_STATE_RUNNING)
342     THROWF(vm_error, 0, "Cannot migrate VM '%s' that is not running yet.", vm->get_cname());
343   if (vm->getImpl()->isMigrating)
344     THROWF(vm_error, 0, "Cannot migrate VM '%s' that is already migrating.", vm->get_cname());
345
346   vm->getImpl()->isMigrating = true;
347
348   std::string rx_name =
349       std::string("__pr_mig_rx:") + vm->get_cname() + "(" + src_pm->get_cname() + "-" + dst_pm->get_cname() + ")";
350   std::string tx_name =
351       std::string("__pr_mig_tx:") + vm->get_cname() + "(" + src_pm->get_cname() + "-" + dst_pm->get_cname() + ")";
352
353   simgrid::s4u::ActorPtr rx =
354       simgrid::s4u::Actor::create(rx_name.c_str(), dst_pm, simgrid::vm::MigrationRx(vm, dst_pm));
355   simgrid::s4u::ActorPtr tx =
356       simgrid::s4u::Actor::create(tx_name.c_str(), src_pm, simgrid::vm::MigrationTx(vm, dst_pm));
357
358   vm->extension_set<simgrid::vm::VmMigrationExt>(new simgrid::vm::VmMigrationExt(simgrid::s4u::Actor::self(), rx, tx));
359
360   /* wait until the migration have finished or on error has occurred */
361   XBT_DEBUG("wait for reception of the final ACK (i.e. migration has been correctly performed");
362   simgrid::s4u::MailboxPtr mbox_ctl = simgrid::s4u::Mailbox::by_name(
363       std::string("__mbox_mig_ctl:") + vm->get_cname() + "(" + src_pm->get_cname() + "-" + dst_pm->get_cname() + ")");
364   delete static_cast<std::string*>(mbox_ctl->get());
365   tx->join();
366   rx->join();
367
368   vm->getImpl()->isMigrating = false;
369 }