Logo AND Algorithmique Numérique Distribuée

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