1 /* Copyright (c) 2013-2019. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/plugins/vm/VmLiveMigration.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "src/instr/instr_private.hpp"
9 #include "src/plugins/vm/VirtualMachineImpl.hpp"
10 #include "src/plugins/vm/VmHostExt.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(vm_live_migration, s4u, "S4U virtual machines live migration");
16 xbt::Extension<s4u::Host, VmMigrationExt> VmMigrationExt::EXTENSION_ID;
18 void VmMigrationExt::ensureVmMigrationExtInstalled()
20 if (not EXTENSION_ID.valid())
21 EXTENSION_ID = s4u::Host::extension_create<VmMigrationExt>();
24 void MigrationRx::operator()()
26 XBT_DEBUG("mig: rx_start");
27 bool received_finalize = false;
29 std::string finalize_task_name =
30 std::string("__mig_stage3:") + vm_->get_cname() + "(" + src_pm_->get_cname() + "-" + dst_pm_->get_cname() + ")";
32 while (not received_finalize) {
33 const std::string* payload = static_cast<std::string*>(mbox->get());
35 if (finalize_task_name == *payload)
36 received_finalize = true;
41 // Here Stage 1, 2 and 3 have been performed.
42 // Hence complete the migration
44 /* Update the vm location */
45 /* precopy migration makes the VM temporally paused */
46 xbt_assert(vm_->get_state() == s4u::VirtualMachine::state::SUSPENDED);
48 /* Update the vm location and resume it */
52 // Now the VM is running on the new host (the migration is completed) (even if the SRC crash)
53 vm_->get_impl()->is_migrating_ = false;
54 XBT_DEBUG("VM(%s) moved from PM(%s) to PM(%s)", vm_->get_cname(), src_pm_->get_cname(), dst_pm_->get_cname());
56 if (TRACE_vm_is_enabled()) {
57 static long long int counter = 0;
58 std::string key = std::to_string(counter);
62 container_t msg = instr::Container::by_name(vm_->get_name());
63 instr::Container::get_root()->get_link("VM_LINK")->start_event(msg, "M", key);
65 // destroy existing container of this vm
66 instr::Container::by_name(vm_->get_name())->remove_from_parent();
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()));
72 msg = instr::Container::by_name(vm_->get_name());
73 instr::Container::get_root()->get_link("VM_LINK")->end_event(msg, "M", key);
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() + ")";
79 mbox_ctl->put(payload, 0);
81 XBT_DEBUG("mig: rx_done");
84 static sg_size_t get_updated_size(double computed, double dp_rate, sg_size_t dp_cap)
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;
95 sg_size_t MigrationTx::sendMigrationData(sg_size_t size, int stage, int stage2_round, double mig_speed, double timeout)
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() + ")";
102 double clock_sta = s4u::Engine::get_clock();
104 s4u::Activity* comm = nullptr;
107 comm = mbox->put_init(msg, size)->set_rate(mig_speed)->wait_for(timeout);
109 comm = mbox->put_async(msg, size)->wait_for(timeout);
110 } catch (const Exception&) {
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);
119 double clock_end = s4u::Engine::get_clock();
120 double duration = clock_end - clock_sta;
121 double actual_speed = size / duration;
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);
127 XBT_DEBUG("mig-stage%d: sent %llu duration %f actual_speed %f (target %f)", stage, size, duration, actual_speed,
133 void MigrationTx::operator()()
135 XBT_DEBUG("mig: tx_start");
137 double host_speed = vm_->get_pm()->get_speed();
138 const sg_size_t ramsize = vm_->get_ramsize();
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_);
145 double mig_timeout = 10000000.0;
146 bool skip_stage2 = false;
148 size_t remaining_size = ramsize;
150 double clock_prev_send;
151 double clock_post_send;
155 /* check parameters */
157 XBT_WARN("migrate a VM, but ramsize is zero");
159 if (max_downtime <= 0) {
160 XBT_WARN("use the default max_downtime value 30ms");
164 /* Stage1: send all memory pages to the destination. */
165 XBT_DEBUG("mig-stage1: remaining_size %zu", remaining_size);
166 sg_vm_start_dirty_page_tracking(vm_);
168 double computed_during_stage1 = 0;
169 clock_prev_send = s4u::Engine::get_clock();
172 /* At stage 1, we do not need timeout. We have to send all the memory pages even though the duration of this
173 * transfer exceeds the timeout value. */
174 XBT_VERB("Stage 1: Gonna send %llu bytes", ramsize);
175 sg_size_t sent = sendMigrationData(ramsize, 1, 0, mig_speed, -1);
176 remaining_size -= sent;
177 computed_during_stage1 = sg_vm_lookup_computed_flops(vm_);
179 if (sent < ramsize) {
180 XBT_VERB("mig-stage1: timeout, force moving to stage 3");
182 } else if (sent > ramsize)
184 } catch (const Exception&) {
185 // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
186 // Stop the dirty page tracking an return (there is no memory space to release)
187 sg_vm_stop_dirty_page_tracking(vm_);
191 clock_post_send = s4u::Engine::get_clock();
192 mig_timeout -= (clock_post_send - clock_prev_send);
193 if (mig_timeout < 0) {
194 XBT_VERB("The duration of stage 1 exceeds the timeout value, skip stage 2");
198 /* estimate bandwidth */
199 bandwidth = ramsize / (clock_post_send - clock_prev_send);
200 threshold = bandwidth * max_downtime;
201 XBT_DEBUG("actual bandwidth %f (MB/s), threshold %zu", bandwidth / 1024 / 1024, threshold);
203 /* Stage2: send update pages iteratively until the size of remaining states becomes smaller than threshold value. */
204 if (not skip_stage2) {
205 int stage2_round = 0;
206 /* just after stage1, nothing has been updated. But, we have to send the data updated during stage1 */
207 sg_size_t updated_size = get_updated_size(computed_during_stage1, dp_rate, dp_cap);
208 remaining_size += updated_size;
209 XBT_DEBUG("mig-stage2.%d: remaining_size %zu (%s threshold %zu)", stage2_round, remaining_size,
210 (remaining_size < threshold) ? "<" : ">", threshold);
212 /* When the remaining size is below the threshold value, move to stage 3. */
213 while (threshold < remaining_size) {
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);
218 clock_prev_send = s4u::Engine::get_clock();
220 XBT_DEBUG("Stage 2, gonna send %llu", updated_size);
221 sent = sendMigrationData(updated_size, 2, stage2_round, mig_speed, mig_timeout);
222 } catch (const Exception&) {
223 // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data
225 // Stop the dirty page tracking an return (there is no memory space to release)
226 sg_vm_stop_dirty_page_tracking(vm_);
230 remaining_size -= sent;
231 double computed = sg_vm_lookup_computed_flops(vm_);
233 clock_post_send = s4u::Engine::get_clock();
235 if (sent == updated_size) {
236 bandwidth = updated_size / (clock_post_send - clock_prev_send);
237 threshold = bandwidth * max_downtime;
238 XBT_DEBUG("actual bandwidth %f, threshold %zu", bandwidth / 1024 / 1024, threshold);
240 mig_timeout -= (clock_post_send - clock_prev_send);
241 xbt_assert(mig_timeout > 0);
242 XBT_DEBUG("mig-stage2.%d: remaining_size %zu (%s threshold %zu)", stage2_round, remaining_size,
243 (remaining_size < threshold) ? "<" : ">", threshold);
244 updated_size = get_updated_size(computed, dp_rate, dp_cap);
245 remaining_size += updated_size;
247 /* When timeout happens, we move to stage 3. The size of memory pages
248 * updated before timeout must be added to the remaining size. */
249 XBT_VERB("mig-stage2.%d: timeout, force moving to stage 3. sent %llu / %llu, eta %lf", stage2_round, sent,
250 updated_size, (clock_post_send - clock_prev_send));
251 updated_size = get_updated_size(computed, dp_rate, dp_cap);
252 remaining_size += updated_size;
258 /* Stage3: stop the VM and copy the rest of states. */
259 XBT_DEBUG("mig-stage3: remaining_size %zu", remaining_size);
261 sg_vm_stop_dirty_page_tracking(vm_);
264 XBT_DEBUG("Stage 3: Gonna send %zu bytes", remaining_size);
265 sendMigrationData(remaining_size, 3, 0, mig_speed, -1);
266 } catch (const Exception&) {
267 // hostfailure (if you want to know whether this is the SRC or the DST check directly in send_migration_data code)
268 // Stop the dirty page tracking an return (there is no memory space to release)
273 // At that point the Migration is considered valid for the SRC node but remind that the DST side should relocate
274 // effectively the VM on the DST node.
275 XBT_DEBUG("mig: tx_done");
278 } // namespace simgrid
280 static void onVirtualMachineShutdown(simgrid::s4u::VirtualMachine const& vm)
282 if (vm.get_impl()->is_migrating_) {
283 vm.extension<simgrid::vm::VmMigrationExt>()->rx_->kill();
284 vm.extension<simgrid::vm::VmMigrationExt>()->tx_->kill();
285 vm.extension<simgrid::vm::VmMigrationExt>()->issuer_->kill();
286 vm.get_impl()->is_migrating_ = false;
290 void sg_vm_live_migration_plugin_init()
292 sg_vm_dirty_page_tracking_init();
293 simgrid::vm::VmMigrationExt::ensureVmMigrationExtInstalled();
294 simgrid::s4u::VirtualMachine::on_shutdown.connect(&onVirtualMachineShutdown);
297 simgrid::s4u::VirtualMachine* sg_vm_create_migratable(simgrid::s4u::Host* pm, const char* name, int coreAmount,
298 int ramsize, int mig_netspeed, int dp_intensity)
300 simgrid::vm::VmHostExt::ensureVmExtInstalled();
302 /* For the moment, intensity_rate is the percentage against the migration bandwidth */
304 sg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, coreAmount, static_cast<sg_size_t>(ramsize) * 1024 * 1024);
305 sg_vm_set_dirty_page_intensity(vm, dp_intensity / 100.0);
306 sg_vm_set_working_set_memory(vm, vm->get_ramsize() * 0.9); // assume working set memory is 90% of ramsize
307 sg_vm_set_migration_speed(vm, mig_netspeed * 1024 * 1024.0);
309 XBT_DEBUG("migspeed : %f intensity mem : %d", mig_netspeed * 1024 * 1024.0, dp_intensity);
314 int sg_vm_is_migrating(simgrid::s4u::VirtualMachine* vm)
316 return vm->get_impl()->is_migrating_;
319 void sg_vm_migrate(simgrid::s4u::VirtualMachine* vm, simgrid::s4u::Host* dst_pm)
321 simgrid::s4u::Host* src_pm = vm->get_pm();
323 if (not src_pm->is_on())
324 throw simgrid::VmFailureException(
325 XBT_THROW_POINT, simgrid::xbt::string_printf("Cannot migrate VM '%s' from host '%s', which is offline.",
326 vm->get_cname(), src_pm->get_cname()));
327 if (not dst_pm->is_on())
328 throw simgrid::VmFailureException(
329 XBT_THROW_POINT, simgrid::xbt::string_printf("Cannot migrate VM '%s' to host '%s', which is offline.",
330 vm->get_cname(), dst_pm->get_cname()));
331 if (vm->get_state() != simgrid::s4u::VirtualMachine::state::RUNNING)
332 throw simgrid::VmFailureException(
334 simgrid::xbt::string_printf("Cannot migrate VM '%s' that is not running yet.", vm->get_cname()));
335 if (vm->get_impl()->is_migrating_)
336 throw simgrid::VmFailureException(
338 simgrid::xbt::string_printf("Cannot migrate VM '%s' that is already migrating.", vm->get_cname()));
340 vm->get_impl()->is_migrating_ = true;
341 simgrid::s4u::VirtualMachine::on_migration_start(*vm);
343 std::string rx_name =
344 std::string("__pr_mig_rx:") + vm->get_cname() + "(" + src_pm->get_cname() + "-" + dst_pm->get_cname() + ")";
345 std::string tx_name =
346 std::string("__pr_mig_tx:") + vm->get_cname() + "(" + src_pm->get_cname() + "-" + dst_pm->get_cname() + ")";
348 simgrid::s4u::ActorPtr rx =
349 simgrid::s4u::Actor::create(rx_name.c_str(), dst_pm, simgrid::vm::MigrationRx(vm, dst_pm));
350 simgrid::s4u::ActorPtr tx =
351 simgrid::s4u::Actor::create(tx_name.c_str(), src_pm, simgrid::vm::MigrationTx(vm, dst_pm));
353 vm->extension_set<simgrid::vm::VmMigrationExt>(new simgrid::vm::VmMigrationExt(simgrid::s4u::Actor::self(), rx, tx));
355 /* wait until the migration have finished or on error has occurred */
356 XBT_DEBUG("wait for reception of the final ACK (i.e. migration has been correctly performed");
357 simgrid::s4u::Mailbox* mbox_ctl = simgrid::s4u::Mailbox::by_name(
358 std::string("__mbox_mig_ctl:") + vm->get_cname() + "(" + src_pm->get_cname() + "-" + dst_pm->get_cname() + ")");
359 delete static_cast<std::string*>(mbox_ctl->get());
363 vm->get_impl()->is_migrating_ = false;
364 simgrid::s4u::VirtualMachine::on_migration_end(*vm);