Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
vm_migrate is now part of the live_migration plugin \o/
[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/VmLiveMigration.hpp>
12 #include <xbt/ex.hpp>
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(vm_live_migration, "S4U virtual machines live migration");
15
16 namespace simgrid {
17 namespace vm {
18
19 void MigrationRx::operator()()
20 {
21   XBT_DEBUG("mig: rx_start");
22   bool received_finalize = false;
23
24   std::string finalize_task_name =
25       std::string("__mig_stage3:") + vm_->getCname() + "(" + src_pm_->getCname() + "-" + dst_pm_->getCname() + ")";
26
27   while (not received_finalize) {
28     std::string* payload = static_cast<std::string*>(mbox->get());
29
30     if (finalize_task_name == *payload)
31       received_finalize = true;
32
33     delete payload;
34   }
35
36   // Here Stage 1, 2  and 3 have been performed.
37   // Hence complete the migration
38
39   // Copy the reference to the vm (if SRC crashes now, do_migration will free ms)
40   // This is clearly ugly but I (Adrien) need more time to do something cleaner (actually we should copy the whole ms
41   // structure at the beginning and free it at the end of each function)
42   simgrid::s4u::VirtualMachine* vm = vm_;
43   simgrid::s4u::Host* dst_pm       = dst_pm_;
44
45   // Make sure that we cannot get interrupted between the migrate and the resume to not end in an inconsistent state
46   simgrid::simix::kernelImmediate([vm, dst_pm]() {
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->pimpl_vm_->setPm(dst_pm);
53     vm->resume();
54   });
55
56   // Now the VM is running on the new host (the migration is completed) (even if the SRC crash)
57   vm->pimpl_vm_->isMigrating = false;
58   XBT_DEBUG("VM(%s) moved from PM(%s) to PM(%s)", vm_->getCname(), src_pm_->getCname(), dst_pm_->getCname());
59
60   if (TRACE_msg_vm_is_enabled()) {
61     static long long int counter = 0;
62     std::string key              = std::to_string(counter);
63     counter++;
64
65     // start link
66     container_t msg = simgrid::instr::Container::byName(vm->getName());
67     simgrid::instr::Container::getRoot()->getLink("MSG_VM_LINK")->startEvent(msg, "M", key);
68
69     // destroy existing container of this vm
70     container_t existing_container = simgrid::instr::Container::byName(vm->getName());
71     existing_container->removeFromParent();
72     delete existing_container;
73
74     // create new container on the new_host location
75     new simgrid::instr::Container(vm->getCname(), "MSG_VM", simgrid::instr::Container::byName(dst_pm_->getName()));
76
77     // end link
78     msg = simgrid::instr::Container::byName(vm->getName());
79     simgrid::instr::Container::getRoot()->getLink("MSG_VM_LINK")->endEvent(msg, "M", key);
80   }
81   // Inform the SRC that the migration has been correctly performed
82   std::string* payload = new std::string("__mig_stage4:");
83   *payload             = *payload + vm_->getCname() + "(" + src_pm_->getCname() + "-" + dst_pm_->getCname() + ")";
84
85   mbox_ctl->put(payload, 0);
86
87   XBT_DEBUG("mig: rx_done");
88 }
89
90 static sg_size_t get_updated_size(double computed, double dp_rate, double dp_cap)
91 {
92   double updated_size = computed * dp_rate;
93   XBT_DEBUG("updated_size %f dp_rate %f", updated_size, dp_rate);
94   if (updated_size > dp_cap) {
95     updated_size = dp_cap;
96   }
97
98   return static_cast<sg_size_t>(updated_size);
99 }
100
101 sg_size_t MigrationTx::sendMigrationData(sg_size_t size, int stage, int stage2_round, double mig_speed, double timeout)
102 {
103   sg_size_t sent   = size;
104   std::string* msg = new std::string("__mig_stage");
105   *msg = *msg + std::to_string(stage) + ":" + vm_->getCname() + "(" + src_pm_->getCname() + "-" + dst_pm_->getCname() +
106          ")";
107
108   double clock_sta = s4u::Engine::getClock();
109
110   s4u::Activity* comm = nullptr;
111   try {
112     if (mig_speed > 0)
113       comm = mbox->put_init(msg, size)->setRate(mig_speed)->wait(timeout);
114     else
115       comm = mbox->put_async(msg, size)->wait();
116   } catch (xbt_ex& e) {
117     if (comm) {
118       sg_size_t remaining = static_cast<sg_size_t>(comm->getRemains());
119       XBT_VERB("timeout (%lf s) in sending_migration_data, remaining %llu bytes of %llu", timeout, remaining, size);
120       sent -= remaining;
121     }
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   s_vm_params_t params;
144   vm_->getParameters(&params);
145   const sg_size_t ramsize = vm_->getRamsize();
146   const double dp_rate    = host_speed ? (params.mig_speed * params.dp_intensity) / host_speed : 1;
147   const double dp_cap     = params.dp_cap;
148   const double mig_speed  = params.mig_speed;
149   double max_downtime     = params.max_downtime;
150
151   double mig_timeout = 10000000.0;
152   bool skip_stage2   = false;
153
154   size_t remaining_size = ramsize;
155   size_t threshold      = 0.0;
156
157   /* check parameters */
158   if (ramsize == 0)
159     XBT_WARN("migrate a VM, but ramsize is zero");
160
161   if (max_downtime <= 0) {
162     XBT_WARN("use the default max_downtime value 30ms");
163     max_downtime = 0.03;
164   }
165
166   /* Stage1: send all memory pages to the destination. */
167   XBT_DEBUG("mig-stage1: remaining_size %zu", remaining_size);
168   sg_vm_start_dirty_page_tracking(vm_);
169
170   double computed_during_stage1 = 0;
171   double clock_prev_send        = s4u::Engine::getClock();
172
173   try {
174     /* At stage 1, we do not need timeout. We have to send all the memory pages even though the duration of this
175      * transfer exceeds the timeout value. */
176     XBT_VERB("Stage 1: Gonna send %llu bytes", ramsize);
177     sg_size_t sent = sendMigrationData(ramsize, 1, 0, mig_speed, -1);
178     remaining_size -= sent;
179     computed_during_stage1 = sg_vm_lookup_computed_flops(vm_);
180
181     if (sent < ramsize) {
182       XBT_VERB("mig-stage1: timeout, force moving to stage 3");
183       skip_stage2 = true;
184     } else if (sent > ramsize)
185       XBT_CRITICAL("bug");
186
187   } catch (xbt_ex& e) {
188     // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
189     // Stop the dirty page tracking an return (there is no memory space to release)
190     sg_vm_stop_dirty_page_tracking(vm_);
191     return;
192   }
193
194   double clock_post_send = s4u::Engine::getClock();
195   mig_timeout -= (clock_post_send - clock_prev_send);
196   if (mig_timeout < 0) {
197     XBT_VERB("The duration of stage 1 exceeds the timeout value, skip stage 2");
198     skip_stage2 = true;
199   }
200
201   /* estimate bandwidth */
202   double bandwidth = ramsize / (clock_post_send - clock_prev_send);
203   threshold        = bandwidth * max_downtime;
204   XBT_DEBUG("actual bandwidth %f (MB/s), threshold %zu", bandwidth / 1024 / 1024, threshold);
205
206   /* Stage2: send update pages iteratively until the size of remaining states becomes smaller than threshold value. */
207   if (not skip_stage2) {
208
209     int stage2_round = 0;
210     for (;;) {
211       sg_size_t updated_size = 0;
212       if (stage2_round == 0) {
213         /* just after stage1, nothing has been updated. But, we have to send the data updated during stage1 */
214         updated_size = get_updated_size(computed_during_stage1, dp_rate, dp_cap);
215       } else {
216         double computed = sg_vm_lookup_computed_flops(vm_);
217         updated_size    = get_updated_size(computed, dp_rate, dp_cap);
218       }
219
220       XBT_DEBUG("mig-stage 2:%d updated_size %llu computed_during_stage1 %f dp_rate %f dp_cap %f", stage2_round,
221                 updated_size, computed_during_stage1, dp_rate, dp_cap);
222
223       /* Check whether the remaining size is below the threshold value. If so, move to stage 3. */
224       remaining_size += updated_size;
225       XBT_DEBUG("mig-stage2.%d: remaining_size %zu (%s threshold %zu)", stage2_round, remaining_size,
226                 (remaining_size < threshold) ? "<" : ">", threshold);
227       if (remaining_size < threshold)
228         break;
229
230       sg_size_t sent         = 0;
231       double clock_prev_send = s4u::Engine::getClock();
232       try {
233         XBT_DEBUG("Stage 2, gonna send %llu", updated_size);
234         sent = sendMigrationData(updated_size, 2, stage2_round, mig_speed, mig_timeout);
235       } catch (xbt_ex& e) {
236         // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data
237         // code)
238         // Stop the dirty page tracking an return (there is no memory space to release)
239         sg_vm_stop_dirty_page_tracking(vm_);
240         return;
241       }
242       double clock_post_send = s4u::Engine::getClock();
243
244       if (sent == updated_size) {
245         /* timeout did not happen */
246         double bandwidth = updated_size / (clock_post_send - clock_prev_send);
247         threshold        = bandwidth * max_downtime;
248         XBT_DEBUG("actual bandwidth %f, threshold %zu", bandwidth / 1024 / 1024, threshold);
249         remaining_size -= sent;
250         stage2_round += 1;
251         mig_timeout -= (clock_post_send - clock_prev_send);
252         xbt_assert(mig_timeout > 0);
253
254       } else if (sent < updated_size) {
255         /* When timeout happens, we move to stage 3. The size of memory pages
256          * updated before timeout must be added to the remaining size. */
257         XBT_VERB("mig-stage2.%d: timeout, force moving to stage 3. sent %llu / %llu, eta %lf", stage2_round, sent,
258                  updated_size, (clock_post_send - clock_prev_send));
259         remaining_size -= sent;
260
261         double computed = sg_vm_lookup_computed_flops(vm_);
262         updated_size    = get_updated_size(computed, dp_rate, dp_cap);
263         remaining_size += updated_size;
264         break;
265       } else
266         XBT_CRITICAL("bug");
267     }
268   }
269
270   /* Stage3: stop the VM and copy the rest of states. */
271   XBT_DEBUG("mig-stage3: remaining_size %zu", remaining_size);
272   vm_->suspend();
273   sg_vm_stop_dirty_page_tracking(vm_);
274
275   try {
276     XBT_DEBUG("Stage 3: Gonna send %zu bytes", remaining_size);
277     sendMigrationData(remaining_size, 3, 0, mig_speed, -1);
278   } catch (xbt_ex& e) {
279     // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
280     // Stop the dirty page tracking an return (there is no memory space to release)
281     vm_->resume();
282     return;
283   }
284
285   // At that point the Migration is considered valid for the SRC node but remind that the DST side should relocate
286   // effectively the VM on the DST node.
287   XBT_DEBUG("mig: tx_done");
288 }
289 }
290 }
291
292 SG_BEGIN_DECL()
293 void sg_vm_migrate(simgrid::s4u::VirtualMachine* vm, simgrid::s4u::Host* dst_pm)
294 {
295   simgrid::s4u::Host* src_pm = vm->getPm();
296
297   if (src_pm->isOff())
298     THROWF(vm_error, 0, "Cannot migrate VM '%s' from host '%s', which is offline.", vm->getCname(), src_pm->getCname());
299   if (dst_pm->isOff())
300     THROWF(vm_error, 0, "Cannot migrate VM '%s' to host '%s', which is offline.", vm->getCname(), dst_pm->getCname());
301   if (vm->getState() != SURF_VM_STATE_RUNNING)
302     THROWF(vm_error, 0, "Cannot migrate VM '%s' that is not running yet.", vm->getCname());
303   if (vm->isMigrating())
304     THROWF(vm_error, 0, "Cannot migrate VM '%s' that is already migrating.", vm->getCname());
305
306   std::string rx_name =
307       std::string("__pr_mig_rx:") + vm->getCname() + "(" + src_pm->getCname() + "-" + dst_pm->getCname() + ")";
308   std::string tx_name =
309       std::string("__pr_mig_tx:") + vm->getCname() + "(" + src_pm->getCname() + "-" + dst_pm->getCname() + ")";
310
311   simgrid::s4u::ActorPtr rx =
312       simgrid::s4u::Actor::createActor(rx_name.c_str(), dst_pm, simgrid::vm::MigrationRx(vm, dst_pm));
313   simgrid::s4u::ActorPtr tx =
314       simgrid::s4u::Actor::createActor(tx_name.c_str(), src_pm, simgrid::vm::MigrationTx(vm, dst_pm));
315
316   /* wait until the migration have finished or on error has occurred */
317   XBT_DEBUG("wait for reception of the final ACK (i.e. migration has been correctly performed");
318   simgrid::s4u::MailboxPtr mbox_ctl = simgrid::s4u::Mailbox::byName(
319       std::string("__mbox_mig_ctl:") + vm->getCname() + "(" + src_pm->getCname() + "-" + dst_pm->getCname() + ")");
320   delete static_cast<std::string*>(mbox_ctl->get());
321
322   tx->join();
323   rx->join();
324
325   vm->pimpl_vm_->isMigrating = false;
326 }
327 }