Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Messing up with VM
[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
20 void MigrationRx::operator()()
21 {
22   XBT_DEBUG("mig: rx_start");
23   bool received_finalize = false;
24
25   std::string finalize_task_name =
26       std::string("__mig_stage3:") + vm_->getCname() + "(" + src_pm_->getCname() + "-" + dst_pm_->getCname() + ")";
27
28   while (not received_finalize) {
29     std::string* payload = static_cast<std::string*>(mbox->get());
30
31     if (finalize_task_name == *payload)
32       received_finalize = true;
33
34     delete payload;
35   }
36
37   // Here Stage 1, 2  and 3 have been performed.
38   // Hence complete the migration
39
40   /* Update the vm location */
41   /* precopy migration makes the VM temporally paused */
42   xbt_assert(vm_->getState() == SURF_VM_STATE_SUSPENDED);
43
44   /* Update the vm location and resume it */
45   vm_->setPm(dst_pm_);
46   vm_->resume();
47
48   // Now the VM is running on the new host (the migration is completed) (even if the SRC crash)
49   vm_->getImpl()->isMigrating = false;
50   XBT_DEBUG("VM(%s) moved from PM(%s) to PM(%s)", vm_->getCname(), src_pm_->getCname(), dst_pm_->getCname());
51
52   if (TRACE_msg_vm_is_enabled()) {
53     static long long int counter = 0;
54     std::string key              = std::to_string(counter);
55     counter++;
56
57     // start link
58     container_t msg = simgrid::instr::Container::byName(vm_->getName());
59     simgrid::instr::Container::getRoot()->getLink("MSG_VM_LINK")->startEvent(msg, "M", key);
60
61     // destroy existing container of this vm
62     container_t existing_container = simgrid::instr::Container::byName(vm_->getName());
63     existing_container->removeFromParent();
64     delete existing_container;
65
66     // create new container on the new_host location
67     new simgrid::instr::Container(vm_->getCname(), "MSG_VM", simgrid::instr::Container::byName(dst_pm_->getName()));
68
69     // end link
70     msg = simgrid::instr::Container::byName(vm_->getName());
71     simgrid::instr::Container::getRoot()->getLink("MSG_VM_LINK")->endEvent(msg, "M", key);
72   }
73   // Inform the SRC that the migration has been correctly performed
74   std::string* payload = new std::string("__mig_stage4:");
75   *payload             = *payload + vm_->getCname() + "(" + src_pm_->getCname() + "-" + dst_pm_->getCname() + ")";
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   sg_size_t 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   std::string* msg = new std::string("__mig_stage");
97   *msg = *msg + std::to_string(stage) + ":" + vm_->getCname() + "(" + src_pm_->getCname() + "-" + dst_pm_->getCname() +
98          ")";
99
100   double clock_sta = s4u::Engine::getClock();
101
102   s4u::Activity* comm = nullptr;
103   try {
104     if (mig_speed > 0)
105       comm = mbox->put_init(msg, size)->setRate(mig_speed)->wait(timeout);
106     else
107       comm = mbox->put_async(msg, size)->wait();
108   } catch (xbt_ex& e) {
109     if (comm) {
110       sg_size_t remaining = static_cast<sg_size_t>(comm->getRemains());
111       XBT_VERB("timeout (%lf s) in sending_migration_data, remaining %llu bytes of %llu", timeout, remaining, size);
112       sent -= remaining;
113     }
114   }
115
116   double clock_end    = s4u::Engine::getClock();
117   double duration     = clock_end - clock_sta;
118   double actual_speed = size / duration;
119
120   if (stage == 2)
121     XBT_DEBUG("mig-stage%d.%d: sent %llu duration %f actual_speed %f (target %f)", stage, stage2_round, size, duration,
122               actual_speed, mig_speed);
123   else
124     XBT_DEBUG("mig-stage%d: sent %llu duration %f actual_speed %f (target %f)", stage, size, duration, actual_speed,
125               mig_speed);
126
127   return sent;
128 }
129
130 void MigrationTx::operator()()
131 {
132   XBT_DEBUG("mig: tx_start");
133
134   double host_speed = vm_->getPm()->getSpeed();
135   const sg_size_t ramsize = vm_->getRamsize();
136   const double dp_rate =
137       host_speed ? (sg_vm_get_migration_speed(vm_) * sg_vm_get_dirty_page_intensity(vm_)) / host_speed : 1;
138   const sg_size_t dp_cap = sg_vm_get_working_set_memory(vm_);
139   const double mig_speed = sg_vm_get_migration_speed(vm_);
140   double max_downtime    = sg_vm_get_max_downtime(vm_);
141
142   double mig_timeout = 10000000.0;
143   bool skip_stage2   = false;
144
145   size_t remaining_size = ramsize;
146   size_t threshold      = 0.0;
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   double clock_prev_send        = s4u::Engine::getClock();
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
178   } catch (xbt_ex& e) {
179     // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
180     // Stop the dirty page tracking an return (there is no memory space to release)
181     sg_vm_stop_dirty_page_tracking(vm_);
182     return;
183   }
184
185   double clock_post_send = s4u::Engine::getClock();
186   mig_timeout -= (clock_post_send - clock_prev_send);
187   if (mig_timeout < 0) {
188     XBT_VERB("The duration of stage 1 exceeds the timeout value, skip stage 2");
189     skip_stage2 = true;
190   }
191
192   /* estimate bandwidth */
193   double bandwidth = ramsize / (clock_post_send - clock_prev_send);
194   threshold        = bandwidth * max_downtime;
195   XBT_DEBUG("actual bandwidth %f (MB/s), threshold %zu", bandwidth / 1024 / 1024, threshold);
196
197   /* Stage2: send update pages iteratively until the size of remaining states becomes smaller than threshold value. */
198   if (not skip_stage2) {
199
200     int stage2_round = 0;
201     for (;;) {
202       sg_size_t updated_size = 0;
203       if (stage2_round == 0) {
204         /* just after stage1, nothing has been updated. But, we have to send the data updated during stage1 */
205         updated_size = get_updated_size(computed_during_stage1, dp_rate, dp_cap);
206       } else {
207         double computed = sg_vm_lookup_computed_flops(vm_);
208         updated_size    = get_updated_size(computed, dp_rate, dp_cap);
209       }
210
211       XBT_DEBUG("mig-stage 2:%d updated_size %llu computed_during_stage1 %f dp_rate %f dp_cap %llu", stage2_round,
212                 updated_size, computed_during_stage1, dp_rate, dp_cap);
213
214       /* Check whether the remaining size is below the threshold value. If so, move to stage 3. */
215       remaining_size += updated_size;
216       XBT_DEBUG("mig-stage2.%d: remaining_size %zu (%s threshold %zu)", stage2_round, remaining_size,
217                 (remaining_size < threshold) ? "<" : ">", threshold);
218       if (remaining_size < threshold)
219         break;
220
221       sg_size_t sent         = 0;
222       double clock_prev_send = s4u::Engine::getClock();
223       try {
224         XBT_DEBUG("Stage 2, gonna send %llu", updated_size);
225         sent = sendMigrationData(updated_size, 2, stage2_round, mig_speed, mig_timeout);
226       } catch (xbt_ex& e) {
227         // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data
228         // code)
229         // Stop the dirty page tracking an return (there is no memory space to release)
230         sg_vm_stop_dirty_page_tracking(vm_);
231         return;
232       }
233       double clock_post_send = s4u::Engine::getClock();
234
235       if (sent == updated_size) {
236         /* timeout did not happen */
237         double bandwidth = updated_size / (clock_post_send - clock_prev_send);
238         threshold        = bandwidth * max_downtime;
239         XBT_DEBUG("actual bandwidth %f, threshold %zu", bandwidth / 1024 / 1024, threshold);
240         remaining_size -= sent;
241         stage2_round += 1;
242         mig_timeout -= (clock_post_send - clock_prev_send);
243         xbt_assert(mig_timeout > 0);
244
245       } else if (sent < updated_size) {
246         /* When timeout happens, we move to stage 3. The size of memory pages
247          * updated before timeout must be added to the remaining size. */
248         XBT_VERB("mig-stage2.%d: timeout, force moving to stage 3. sent %llu / %llu, eta %lf", stage2_round, sent,
249                  updated_size, (clock_post_send - clock_prev_send));
250         remaining_size -= sent;
251
252         double computed = sg_vm_lookup_computed_flops(vm_);
253         updated_size    = get_updated_size(computed, dp_rate, dp_cap);
254         remaining_size += updated_size;
255         break;
256       } else
257         XBT_CRITICAL("bug");
258     }
259   }
260
261   /* Stage3: stop the VM and copy the rest of states. */
262   XBT_DEBUG("mig-stage3: remaining_size %zu", remaining_size);
263   vm_->suspend();
264   sg_vm_stop_dirty_page_tracking(vm_);
265
266   try {
267     XBT_DEBUG("Stage 3: Gonna send %zu bytes", remaining_size);
268     sendMigrationData(remaining_size, 3, 0, mig_speed, -1);
269   } catch (xbt_ex& e) {
270     // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
271     // Stop the dirty page tracking an return (there is no memory space to release)
272     vm_->resume();
273     return;
274   }
275
276   // At that point the Migration is considered valid for the SRC node but remind that the DST side should relocate
277   // effectively the VM on the DST node.
278   XBT_DEBUG("mig: tx_done");
279 }
280 }
281 }
282
283 SG_BEGIN_DECL()
284 simgrid::s4u::VirtualMachine* sg_vm_create_migratable(simgrid::s4u::Host* pm, const char* name, int coreAmount,
285                                                       int ramsize, int mig_netspeed, int dp_intensity)
286 {
287   simgrid::vm::VmHostExt::ensureVmExtInstalled();
288
289   /* For the moment, intensity_rate is the percentage against the migration bandwidth */
290
291   msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, coreAmount, static_cast<sg_size_t>(ramsize) * 1024 * 1024);
292   sg_vm_set_dirty_page_intensity(vm, dp_intensity / 100.0);
293   sg_vm_set_working_set_memory(vm, vm->getRamsize() * 0.9); // assume working set memory is 90% of ramsize
294   sg_vm_set_migration_speed(vm, mig_netspeed * 1024 * 1024.0);
295
296   XBT_DEBUG("migspeed : %f intensity mem : %d", mig_netspeed * 1024 * 1024.0, dp_intensity);
297
298   return vm;
299 }
300
301 int sg_vm_is_migrating(simgrid::s4u::VirtualMachine* vm)
302 {
303   return vm->isMigrating();
304 }
305
306 void sg_vm_migrate(simgrid::s4u::VirtualMachine* vm, simgrid::s4u::Host* dst_pm)
307 {
308   simgrid::s4u::Host* src_pm = vm->getPm();
309
310   if (src_pm->isOff())
311     THROWF(vm_error, 0, "Cannot migrate VM '%s' from host '%s', which is offline.", vm->getCname(), src_pm->getCname());
312   if (dst_pm->isOff())
313     THROWF(vm_error, 0, "Cannot migrate VM '%s' to host '%s', which is offline.", vm->getCname(), dst_pm->getCname());
314   if (vm->getState() != SURF_VM_STATE_RUNNING)
315     THROWF(vm_error, 0, "Cannot migrate VM '%s' that is not running yet.", vm->getCname());
316   if (vm->isMigrating())
317     THROWF(vm_error, 0, "Cannot migrate VM '%s' that is already migrating.", vm->getCname());
318
319   vm->getImpl()->isMigrating = true;
320
321   std::string rx_name =
322       std::string("__pr_mig_rx:") + vm->getCname() + "(" + src_pm->getCname() + "-" + dst_pm->getCname() + ")";
323   std::string tx_name =
324       std::string("__pr_mig_tx:") + vm->getCname() + "(" + src_pm->getCname() + "-" + dst_pm->getCname() + ")";
325
326   simgrid::s4u::ActorPtr rx =
327       simgrid::s4u::Actor::createActor(rx_name.c_str(), dst_pm, simgrid::vm::MigrationRx(vm, dst_pm));
328   simgrid::s4u::ActorPtr tx =
329       simgrid::s4u::Actor::createActor(tx_name.c_str(), src_pm, simgrid::vm::MigrationTx(vm, dst_pm));
330
331   /* wait until the migration have finished or on error has occurred */
332   XBT_DEBUG("wait for reception of the final ACK (i.e. migration has been correctly performed");
333   simgrid::s4u::MailboxPtr mbox_ctl = simgrid::s4u::Mailbox::byName(
334       std::string("__mbox_mig_ctl:") + vm->getCname() + "(" + src_pm->getCname() + "-" + dst_pm->getCname() + ")");
335   delete static_cast<std::string*>(mbox_ctl->get());
336
337   tx->join();
338   rx->join();
339
340   vm->getImpl()->isMigrating = false;
341 }
342 }