migration: small changes around rate-limiting
[qemu.git] / migration.c
blob1f58d77f76fc112ca64adc5878ac6aed23a838e9
1 /*
2 * QEMU live migration
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu-common.h"
17 #include "migration/migration.h"
18 #include "monitor/monitor.h"
19 #include "migration/qemu-file.h"
20 #include "sysemu/sysemu.h"
21 #include "block/block.h"
22 #include "qemu/sockets.h"
23 #include "migration/block.h"
24 #include "qemu/thread.h"
25 #include "qmp-commands.h"
26 #include "trace.h"
28 //#define DEBUG_MIGRATION
30 #ifdef DEBUG_MIGRATION
31 #define DPRINTF(fmt, ...) \
32 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
33 #else
34 #define DPRINTF(fmt, ...) \
35 do { } while (0)
36 #endif
38 enum {
39 MIG_STATE_ERROR,
40 MIG_STATE_SETUP,
41 MIG_STATE_CANCELLED,
42 MIG_STATE_ACTIVE,
43 MIG_STATE_COMPLETED,
46 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
48 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
49 * data. */
50 #define BUFFER_DELAY 100
51 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
53 /* Migration XBZRLE default cache size */
54 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
56 static NotifierList migration_state_notifiers =
57 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
59 /* When we add fault tolerance, we could have several
60 migrations at once. For now we don't need to add
61 dynamic creation of migration */
63 MigrationState *migrate_get_current(void)
65 static MigrationState current_migration = {
66 .state = MIG_STATE_SETUP,
67 .bandwidth_limit = MAX_THROTTLE,
68 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
71 return &current_migration;
74 void qemu_start_incoming_migration(const char *uri, Error **errp)
76 const char *p;
78 if (strstart(uri, "tcp:", &p))
79 tcp_start_incoming_migration(p, errp);
80 #if !defined(WIN32)
81 else if (strstart(uri, "exec:", &p))
82 exec_start_incoming_migration(p, errp);
83 else if (strstart(uri, "unix:", &p))
84 unix_start_incoming_migration(p, errp);
85 else if (strstart(uri, "fd:", &p))
86 fd_start_incoming_migration(p, errp);
87 #endif
88 else {
89 error_setg(errp, "unknown migration protocol: %s", uri);
93 static void process_incoming_migration_co(void *opaque)
95 QEMUFile *f = opaque;
96 int ret;
98 ret = qemu_loadvm_state(f);
99 qemu_fclose(f);
100 if (ret < 0) {
101 fprintf(stderr, "load of migration failed\n");
102 exit(0);
104 qemu_announce_self();
105 DPRINTF("successfully loaded vm state\n");
107 bdrv_clear_incoming_migration_all();
108 /* Make sure all file formats flush their mutable metadata */
109 bdrv_invalidate_cache_all();
111 if (autostart) {
112 vm_start();
113 } else {
114 runstate_set(RUN_STATE_PAUSED);
118 void process_incoming_migration(QEMUFile *f)
120 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
121 int fd = qemu_get_fd(f);
123 assert(fd != -1);
124 socket_set_nonblock(fd);
125 qemu_coroutine_enter(co, f);
128 /* amount of nanoseconds we are willing to wait for migration to be down.
129 * the choice of nanoseconds is because it is the maximum resolution that
130 * get_clock() can achieve. It is an internal measure. All user-visible
131 * units must be in seconds */
132 static uint64_t max_downtime = 30000000;
134 uint64_t migrate_max_downtime(void)
136 return max_downtime;
139 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
141 MigrationCapabilityStatusList *head = NULL;
142 MigrationCapabilityStatusList *caps;
143 MigrationState *s = migrate_get_current();
144 int i;
146 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
147 if (head == NULL) {
148 head = g_malloc0(sizeof(*caps));
149 caps = head;
150 } else {
151 caps->next = g_malloc0(sizeof(*caps));
152 caps = caps->next;
154 caps->value =
155 g_malloc(sizeof(*caps->value));
156 caps->value->capability = i;
157 caps->value->state = s->enabled_capabilities[i];
160 return head;
163 static void get_xbzrle_cache_stats(MigrationInfo *info)
165 if (migrate_use_xbzrle()) {
166 info->has_xbzrle_cache = true;
167 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
168 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
169 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
170 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
171 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
172 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
176 MigrationInfo *qmp_query_migrate(Error **errp)
178 MigrationInfo *info = g_malloc0(sizeof(*info));
179 MigrationState *s = migrate_get_current();
181 switch (s->state) {
182 case MIG_STATE_SETUP:
183 /* no migration has happened ever */
184 break;
185 case MIG_STATE_ACTIVE:
186 info->has_status = true;
187 info->status = g_strdup("active");
188 info->has_total_time = true;
189 info->total_time = qemu_get_clock_ms(rt_clock)
190 - s->total_time;
191 info->has_expected_downtime = true;
192 info->expected_downtime = s->expected_downtime;
194 info->has_ram = true;
195 info->ram = g_malloc0(sizeof(*info->ram));
196 info->ram->transferred = ram_bytes_transferred();
197 info->ram->remaining = ram_bytes_remaining();
198 info->ram->total = ram_bytes_total();
199 info->ram->duplicate = dup_mig_pages_transferred();
200 info->ram->normal = norm_mig_pages_transferred();
201 info->ram->normal_bytes = norm_mig_bytes_transferred();
202 info->ram->dirty_pages_rate = s->dirty_pages_rate;
205 if (blk_mig_active()) {
206 info->has_disk = true;
207 info->disk = g_malloc0(sizeof(*info->disk));
208 info->disk->transferred = blk_mig_bytes_transferred();
209 info->disk->remaining = blk_mig_bytes_remaining();
210 info->disk->total = blk_mig_bytes_total();
213 get_xbzrle_cache_stats(info);
214 break;
215 case MIG_STATE_COMPLETED:
216 get_xbzrle_cache_stats(info);
218 info->has_status = true;
219 info->status = g_strdup("completed");
220 info->total_time = s->total_time;
221 info->has_downtime = true;
222 info->downtime = s->downtime;
224 info->has_ram = true;
225 info->ram = g_malloc0(sizeof(*info->ram));
226 info->ram->transferred = ram_bytes_transferred();
227 info->ram->remaining = 0;
228 info->ram->total = ram_bytes_total();
229 info->ram->duplicate = dup_mig_pages_transferred();
230 info->ram->normal = norm_mig_pages_transferred();
231 info->ram->normal_bytes = norm_mig_bytes_transferred();
232 break;
233 case MIG_STATE_ERROR:
234 info->has_status = true;
235 info->status = g_strdup("failed");
236 break;
237 case MIG_STATE_CANCELLED:
238 info->has_status = true;
239 info->status = g_strdup("cancelled");
240 break;
243 return info;
246 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
247 Error **errp)
249 MigrationState *s = migrate_get_current();
250 MigrationCapabilityStatusList *cap;
252 if (s->state == MIG_STATE_ACTIVE) {
253 error_set(errp, QERR_MIGRATION_ACTIVE);
254 return;
257 for (cap = params; cap; cap = cap->next) {
258 s->enabled_capabilities[cap->value->capability] = cap->value->state;
262 /* shared migration helpers */
264 static void migrate_fd_cleanup(void *opaque)
266 MigrationState *s = opaque;
268 qemu_bh_delete(s->cleanup_bh);
269 s->cleanup_bh = NULL;
271 if (s->file) {
272 DPRINTF("closing file\n");
273 qemu_fclose(s->file);
274 s->file = NULL;
277 assert(s->migration_file == NULL);
278 assert(s->state != MIG_STATE_ACTIVE);
280 if (s->state != MIG_STATE_COMPLETED) {
281 qemu_savevm_state_cancel();
284 notifier_list_notify(&migration_state_notifiers, s);
287 static void migrate_finish_set_state(MigrationState *s, int new_state)
289 if (__sync_val_compare_and_swap(&s->state, MIG_STATE_ACTIVE,
290 new_state) == new_state) {
291 trace_migrate_set_state(new_state);
295 void migrate_fd_error(MigrationState *s)
297 DPRINTF("setting error state\n");
298 assert(s->file == NULL);
299 s->state = MIG_STATE_ERROR;
300 trace_migrate_set_state(MIG_STATE_ERROR);
301 notifier_list_notify(&migration_state_notifiers, s);
304 static void migrate_fd_cancel(MigrationState *s)
306 DPRINTF("cancelling migration\n");
308 migrate_finish_set_state(s, MIG_STATE_CANCELLED);
311 int migrate_fd_close(MigrationState *s)
313 int rc = 0;
314 if (s->migration_file != NULL) {
315 rc = qemu_fclose(s->migration_file);
316 s->migration_file = NULL;
318 return rc;
321 void add_migration_state_change_notifier(Notifier *notify)
323 notifier_list_add(&migration_state_notifiers, notify);
326 void remove_migration_state_change_notifier(Notifier *notify)
328 notifier_remove(notify);
331 bool migration_is_active(MigrationState *s)
333 return s->state == MIG_STATE_ACTIVE;
336 bool migration_has_finished(MigrationState *s)
338 return s->state == MIG_STATE_COMPLETED;
341 bool migration_has_failed(MigrationState *s)
343 return (s->state == MIG_STATE_CANCELLED ||
344 s->state == MIG_STATE_ERROR);
347 static MigrationState *migrate_init(const MigrationParams *params)
349 MigrationState *s = migrate_get_current();
350 int64_t bandwidth_limit = s->bandwidth_limit;
351 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
352 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
354 memcpy(enabled_capabilities, s->enabled_capabilities,
355 sizeof(enabled_capabilities));
357 memset(s, 0, sizeof(*s));
358 s->bandwidth_limit = bandwidth_limit;
359 s->params = *params;
360 memcpy(s->enabled_capabilities, enabled_capabilities,
361 sizeof(enabled_capabilities));
362 s->xbzrle_cache_size = xbzrle_cache_size;
364 s->bandwidth_limit = bandwidth_limit;
365 s->state = MIG_STATE_SETUP;
366 trace_migrate_set_state(MIG_STATE_SETUP);
368 s->total_time = qemu_get_clock_ms(rt_clock);
369 return s;
372 static GSList *migration_blockers;
374 void migrate_add_blocker(Error *reason)
376 migration_blockers = g_slist_prepend(migration_blockers, reason);
379 void migrate_del_blocker(Error *reason)
381 migration_blockers = g_slist_remove(migration_blockers, reason);
384 void qmp_migrate(const char *uri, bool has_blk, bool blk,
385 bool has_inc, bool inc, bool has_detach, bool detach,
386 Error **errp)
388 Error *local_err = NULL;
389 MigrationState *s = migrate_get_current();
390 MigrationParams params;
391 const char *p;
393 params.blk = blk;
394 params.shared = inc;
396 if (s->state == MIG_STATE_ACTIVE) {
397 error_set(errp, QERR_MIGRATION_ACTIVE);
398 return;
401 if (qemu_savevm_state_blocked(errp)) {
402 return;
405 if (migration_blockers) {
406 *errp = error_copy(migration_blockers->data);
407 return;
410 s = migrate_init(&params);
412 if (strstart(uri, "tcp:", &p)) {
413 tcp_start_outgoing_migration(s, p, &local_err);
414 #if !defined(WIN32)
415 } else if (strstart(uri, "exec:", &p)) {
416 exec_start_outgoing_migration(s, p, &local_err);
417 } else if (strstart(uri, "unix:", &p)) {
418 unix_start_outgoing_migration(s, p, &local_err);
419 } else if (strstart(uri, "fd:", &p)) {
420 fd_start_outgoing_migration(s, p, &local_err);
421 #endif
422 } else {
423 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
424 return;
427 if (local_err) {
428 migrate_fd_error(s);
429 error_propagate(errp, local_err);
430 return;
434 void qmp_migrate_cancel(Error **errp)
436 migrate_fd_cancel(migrate_get_current());
439 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
441 MigrationState *s = migrate_get_current();
443 /* Check for truncation */
444 if (value != (size_t)value) {
445 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
446 "exceeding address space");
447 return;
450 s->xbzrle_cache_size = xbzrle_cache_resize(value);
453 int64_t qmp_query_migrate_cache_size(Error **errp)
455 return migrate_xbzrle_cache_size();
458 void qmp_migrate_set_speed(int64_t value, Error **errp)
460 MigrationState *s;
462 if (value < 0) {
463 value = 0;
465 if (value > SIZE_MAX) {
466 value = SIZE_MAX;
469 s = migrate_get_current();
470 s->bandwidth_limit = value;
471 if (s->file) {
472 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
476 void qmp_migrate_set_downtime(double value, Error **errp)
478 value *= 1e9;
479 value = MAX(0, MIN(UINT64_MAX, value));
480 max_downtime = (uint64_t)value;
483 int migrate_use_xbzrle(void)
485 MigrationState *s;
487 s = migrate_get_current();
489 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
492 int64_t migrate_xbzrle_cache_size(void)
494 MigrationState *s;
496 s = migrate_get_current();
498 return s->xbzrle_cache_size;
501 /* migration thread support */
503 static int migration_put_buffer(void *opaque, const uint8_t *buf,
504 int64_t pos, int size)
506 MigrationState *s = opaque;
507 int ret;
509 DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos);
511 if (size <= 0) {
512 return size;
515 qemu_put_buffer(s->migration_file, buf, size);
516 ret = qemu_file_get_error(s->migration_file);
517 if (ret) {
518 return ret;
521 s->bytes_xfer += size;
522 return size;
525 static int migration_close(void *opaque)
527 MigrationState *s = opaque;
529 DPRINTF("closing\n");
531 qemu_mutex_unlock_iothread();
532 qemu_thread_join(&s->thread);
533 qemu_mutex_lock_iothread();
534 assert(s->state != MIG_STATE_ACTIVE);
536 return migrate_fd_close(s);
539 static int migration_get_fd(void *opaque)
541 MigrationState *s = opaque;
543 return qemu_get_fd(s->migration_file);
547 * The meaning of the return values is:
548 * 0: We can continue sending
549 * 1: Time to stop
550 * negative: There has been an error
552 static int migration_rate_limit(void *opaque)
554 MigrationState *s = opaque;
555 int ret;
557 ret = qemu_file_get_error(s->file);
558 if (ret) {
559 return ret;
562 if (s->bytes_xfer >= s->xfer_limit) {
563 return 1;
566 return 0;
569 static int64_t migration_set_rate_limit(void *opaque, int64_t new_rate)
571 MigrationState *s = opaque;
572 if (qemu_file_get_error(s->file)) {
573 goto out;
576 s->xfer_limit = new_rate;
578 out:
579 return s->xfer_limit;
582 static int64_t migration_get_rate_limit(void *opaque)
584 MigrationState *s = opaque;
586 return s->xfer_limit;
589 static void *migration_thread(void *opaque)
591 MigrationState *s = opaque;
592 int64_t initial_time = qemu_get_clock_ms(rt_clock);
593 int64_t sleep_time = 0;
594 int64_t initial_bytes = 0;
595 int64_t max_size = 0;
596 int64_t start_time = initial_time;
597 bool old_vm_running = false;
599 DPRINTF("beginning savevm\n");
600 qemu_savevm_state_begin(s->file, &s->params);
602 while (s->state == MIG_STATE_ACTIVE) {
603 int64_t current_time;
604 uint64_t pending_size;
606 if (!qemu_file_rate_limit(s->file)) {
607 DPRINTF("iterate\n");
608 pending_size = qemu_savevm_state_pending(s->file, max_size);
609 DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
610 if (pending_size && pending_size >= max_size) {
611 qemu_savevm_state_iterate(s->file);
612 } else {
613 DPRINTF("done iterating\n");
614 qemu_mutex_lock_iothread();
615 start_time = qemu_get_clock_ms(rt_clock);
616 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
617 old_vm_running = runstate_is_running();
618 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
619 qemu_file_set_rate_limit(s->file, INT_MAX);
620 qemu_savevm_state_complete(s->file);
621 qemu_mutex_unlock_iothread();
622 if (!qemu_file_get_error(s->file)) {
623 migrate_finish_set_state(s, MIG_STATE_COMPLETED);
624 break;
629 if (qemu_file_get_error(s->file)) {
630 migrate_finish_set_state(s, MIG_STATE_ERROR);
631 break;
633 current_time = qemu_get_clock_ms(rt_clock);
634 if (current_time >= initial_time + BUFFER_DELAY) {
635 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
636 uint64_t time_spent = current_time - initial_time - sleep_time;
637 double bandwidth = transferred_bytes / time_spent;
638 max_size = bandwidth * migrate_max_downtime() / 1000000;
640 DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
641 " bandwidth %g max_size %" PRId64 "\n",
642 transferred_bytes, time_spent, bandwidth, max_size);
643 /* if we haven't sent anything, we don't want to recalculate
644 10000 is a small enough number for our purposes */
645 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
646 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
649 s->bytes_xfer = 0;
650 sleep_time = 0;
651 initial_time = current_time;
652 initial_bytes = qemu_ftell(s->file);
654 if (qemu_file_rate_limit(s->file)) {
655 /* usleep expects microseconds */
656 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
657 sleep_time += qemu_get_clock_ms(rt_clock) - current_time;
661 qemu_mutex_lock_iothread();
662 if (s->state == MIG_STATE_COMPLETED) {
663 int64_t end_time = qemu_get_clock_ms(rt_clock);
664 s->total_time = end_time - s->total_time;
665 s->downtime = end_time - start_time;
666 runstate_set(RUN_STATE_POSTMIGRATE);
667 } else {
668 if (old_vm_running) {
669 vm_start();
672 qemu_bh_schedule(s->cleanup_bh);
673 qemu_mutex_unlock_iothread();
675 return NULL;
678 static const QEMUFileOps migration_file_ops = {
679 .get_fd = migration_get_fd,
680 .put_buffer = migration_put_buffer,
681 .close = migration_close,
682 .rate_limit = migration_rate_limit,
683 .get_rate_limit = migration_get_rate_limit,
684 .set_rate_limit = migration_set_rate_limit,
687 void migrate_fd_connect(MigrationState *s)
689 s->state = MIG_STATE_ACTIVE;
690 trace_migrate_set_state(MIG_STATE_ACTIVE);
692 s->bytes_xfer = 0;
693 /* This is a best 1st approximation. ns to ms */
694 s->expected_downtime = max_downtime/1000000;
696 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
697 s->file = qemu_fopen_ops(s, &migration_file_ops);
699 qemu_file_set_rate_limit(s->file,
700 s->bandwidth_limit / XFER_LIMIT_RATIO);
702 qemu_thread_create(&s->thread, migration_thread, s,
703 QEMU_THREAD_JOINABLE);
704 notifier_list_notify(&migration_state_notifiers, s);