tests: Add PC CPU test
[qemu/cris-port.git] / migration / migration.c
blob2c805f11f50e71545955b3db6e931cb6aee45f8d
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 "qemu/main-loop.h"
18 #include "migration/migration.h"
19 #include "monitor/monitor.h"
20 #include "migration/qemu-file.h"
21 #include "sysemu/sysemu.h"
22 #include "block/block.h"
23 #include "qemu/sockets.h"
24 #include "migration/block.h"
25 #include "qemu/thread.h"
26 #include "qmp-commands.h"
27 #include "trace.h"
29 enum {
30 MIG_STATE_ERROR = -1,
31 MIG_STATE_NONE,
32 MIG_STATE_SETUP,
33 MIG_STATE_CANCELLING,
34 MIG_STATE_CANCELLED,
35 MIG_STATE_ACTIVE,
36 MIG_STATE_COMPLETED,
39 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
41 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
42 * data. */
43 #define BUFFER_DELAY 100
44 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
46 /* Migration XBZRLE default cache size */
47 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
49 static NotifierList migration_state_notifiers =
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
52 static bool deferred_incoming;
54 /* When we add fault tolerance, we could have several
55 migrations at once. For now we don't need to add
56 dynamic creation of migration */
58 MigrationState *migrate_get_current(void)
60 static MigrationState current_migration = {
61 .state = MIG_STATE_NONE,
62 .bandwidth_limit = MAX_THROTTLE,
63 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
64 .mbps = -1,
67 return &current_migration;
71 * Called on -incoming with a defer: uri.
72 * The migration can be started later after any parameters have been
73 * changed.
75 static void deferred_incoming_migration(Error **errp)
77 if (deferred_incoming) {
78 error_setg(errp, "Incoming migration already deferred");
80 deferred_incoming = true;
83 void qemu_start_incoming_migration(const char *uri, Error **errp)
85 const char *p;
87 if (!strcmp(uri, "defer")) {
88 deferred_incoming_migration(errp);
89 } else if (strstart(uri, "tcp:", &p)) {
90 tcp_start_incoming_migration(p, errp);
91 #ifdef CONFIG_RDMA
92 } else if (strstart(uri, "rdma:", &p)) {
93 rdma_start_incoming_migration(p, errp);
94 #endif
95 #if !defined(WIN32)
96 } else if (strstart(uri, "exec:", &p)) {
97 exec_start_incoming_migration(p, errp);
98 } else if (strstart(uri, "unix:", &p)) {
99 unix_start_incoming_migration(p, errp);
100 } else if (strstart(uri, "fd:", &p)) {
101 fd_start_incoming_migration(p, errp);
102 #endif
103 } else {
104 error_setg(errp, "unknown migration protocol: %s", uri);
108 static void process_incoming_migration_co(void *opaque)
110 QEMUFile *f = opaque;
111 Error *local_err = NULL;
112 int ret;
114 ret = qemu_loadvm_state(f);
115 qemu_fclose(f);
116 free_xbzrle_decoded_buf();
117 if (ret < 0) {
118 error_report("load of migration failed: %s", strerror(-ret));
119 exit(EXIT_FAILURE);
121 qemu_announce_self();
123 /* Make sure all file formats flush their mutable metadata */
124 bdrv_invalidate_cache_all(&local_err);
125 if (local_err) {
126 qerror_report_err(local_err);
127 error_free(local_err);
128 exit(EXIT_FAILURE);
131 if (autostart) {
132 vm_start();
133 } else {
134 runstate_set(RUN_STATE_PAUSED);
138 void process_incoming_migration(QEMUFile *f)
140 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
141 int fd = qemu_get_fd(f);
143 assert(fd != -1);
144 qemu_set_nonblock(fd);
145 qemu_coroutine_enter(co, f);
148 /* amount of nanoseconds we are willing to wait for migration to be down.
149 * the choice of nanoseconds is because it is the maximum resolution that
150 * get_clock() can achieve. It is an internal measure. All user-visible
151 * units must be in seconds */
152 static uint64_t max_downtime = 300000000;
154 uint64_t migrate_max_downtime(void)
156 return max_downtime;
159 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
161 MigrationCapabilityStatusList *head = NULL;
162 MigrationCapabilityStatusList *caps;
163 MigrationState *s = migrate_get_current();
164 int i;
166 caps = NULL; /* silence compiler warning */
167 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
168 if (head == NULL) {
169 head = g_malloc0(sizeof(*caps));
170 caps = head;
171 } else {
172 caps->next = g_malloc0(sizeof(*caps));
173 caps = caps->next;
175 caps->value =
176 g_malloc(sizeof(*caps->value));
177 caps->value->capability = i;
178 caps->value->state = s->enabled_capabilities[i];
181 return head;
184 static void get_xbzrle_cache_stats(MigrationInfo *info)
186 if (migrate_use_xbzrle()) {
187 info->has_xbzrle_cache = true;
188 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
189 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
190 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
191 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
192 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
193 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
194 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
198 MigrationInfo *qmp_query_migrate(Error **errp)
200 MigrationInfo *info = g_malloc0(sizeof(*info));
201 MigrationState *s = migrate_get_current();
203 switch (s->state) {
204 case MIG_STATE_NONE:
205 /* no migration has happened ever */
206 break;
207 case MIG_STATE_SETUP:
208 info->has_status = true;
209 info->status = g_strdup("setup");
210 info->has_total_time = false;
211 break;
212 case MIG_STATE_ACTIVE:
213 case MIG_STATE_CANCELLING:
214 info->has_status = true;
215 info->status = g_strdup("active");
216 info->has_total_time = true;
217 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
218 - s->total_time;
219 info->has_expected_downtime = true;
220 info->expected_downtime = s->expected_downtime;
221 info->has_setup_time = true;
222 info->setup_time = s->setup_time;
224 info->has_ram = true;
225 info->ram = g_malloc0(sizeof(*info->ram));
226 info->ram->transferred = ram_bytes_transferred();
227 info->ram->remaining = ram_bytes_remaining();
228 info->ram->total = ram_bytes_total();
229 info->ram->duplicate = dup_mig_pages_transferred();
230 info->ram->skipped = skipped_mig_pages_transferred();
231 info->ram->normal = norm_mig_pages_transferred();
232 info->ram->normal_bytes = norm_mig_bytes_transferred();
233 info->ram->dirty_pages_rate = s->dirty_pages_rate;
234 info->ram->mbps = s->mbps;
235 info->ram->dirty_sync_count = s->dirty_sync_count;
237 if (blk_mig_active()) {
238 info->has_disk = true;
239 info->disk = g_malloc0(sizeof(*info->disk));
240 info->disk->transferred = blk_mig_bytes_transferred();
241 info->disk->remaining = blk_mig_bytes_remaining();
242 info->disk->total = blk_mig_bytes_total();
245 get_xbzrle_cache_stats(info);
246 break;
247 case MIG_STATE_COMPLETED:
248 get_xbzrle_cache_stats(info);
250 info->has_status = true;
251 info->status = g_strdup("completed");
252 info->has_total_time = true;
253 info->total_time = s->total_time;
254 info->has_downtime = true;
255 info->downtime = s->downtime;
256 info->has_setup_time = true;
257 info->setup_time = s->setup_time;
259 info->has_ram = true;
260 info->ram = g_malloc0(sizeof(*info->ram));
261 info->ram->transferred = ram_bytes_transferred();
262 info->ram->remaining = 0;
263 info->ram->total = ram_bytes_total();
264 info->ram->duplicate = dup_mig_pages_transferred();
265 info->ram->skipped = skipped_mig_pages_transferred();
266 info->ram->normal = norm_mig_pages_transferred();
267 info->ram->normal_bytes = norm_mig_bytes_transferred();
268 info->ram->mbps = s->mbps;
269 info->ram->dirty_sync_count = s->dirty_sync_count;
270 break;
271 case MIG_STATE_ERROR:
272 info->has_status = true;
273 info->status = g_strdup("failed");
274 break;
275 case MIG_STATE_CANCELLED:
276 info->has_status = true;
277 info->status = g_strdup("cancelled");
278 break;
281 return info;
284 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
285 Error **errp)
287 MigrationState *s = migrate_get_current();
288 MigrationCapabilityStatusList *cap;
290 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
291 error_set(errp, QERR_MIGRATION_ACTIVE);
292 return;
295 for (cap = params; cap; cap = cap->next) {
296 s->enabled_capabilities[cap->value->capability] = cap->value->state;
300 /* shared migration helpers */
302 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
304 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
305 trace_migrate_set_state(new_state);
309 static void migrate_fd_cleanup(void *opaque)
311 MigrationState *s = opaque;
313 qemu_bh_delete(s->cleanup_bh);
314 s->cleanup_bh = NULL;
316 if (s->file) {
317 trace_migrate_fd_cleanup();
318 qemu_mutex_unlock_iothread();
319 qemu_thread_join(&s->thread);
320 qemu_mutex_lock_iothread();
322 qemu_fclose(s->file);
323 s->file = NULL;
326 assert(s->state != MIG_STATE_ACTIVE);
328 if (s->state != MIG_STATE_COMPLETED) {
329 qemu_savevm_state_cancel();
330 if (s->state == MIG_STATE_CANCELLING) {
331 migrate_set_state(s, MIG_STATE_CANCELLING, MIG_STATE_CANCELLED);
335 notifier_list_notify(&migration_state_notifiers, s);
338 void migrate_fd_error(MigrationState *s)
340 trace_migrate_fd_error();
341 assert(s->file == NULL);
342 s->state = MIG_STATE_ERROR;
343 trace_migrate_set_state(MIG_STATE_ERROR);
344 notifier_list_notify(&migration_state_notifiers, s);
347 static void migrate_fd_cancel(MigrationState *s)
349 int old_state ;
350 QEMUFile *f = migrate_get_current()->file;
351 trace_migrate_fd_cancel();
353 do {
354 old_state = s->state;
355 if (old_state != MIG_STATE_SETUP && old_state != MIG_STATE_ACTIVE) {
356 break;
358 migrate_set_state(s, old_state, MIG_STATE_CANCELLING);
359 } while (s->state != MIG_STATE_CANCELLING);
362 * If we're unlucky the migration code might be stuck somewhere in a
363 * send/write while the network has failed and is waiting to timeout;
364 * if we've got shutdown(2) available then we can force it to quit.
365 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
366 * called in a bh, so there is no race against this cancel.
368 if (s->state == MIG_STATE_CANCELLING && f) {
369 qemu_file_shutdown(f);
373 void add_migration_state_change_notifier(Notifier *notify)
375 notifier_list_add(&migration_state_notifiers, notify);
378 void remove_migration_state_change_notifier(Notifier *notify)
380 notifier_remove(notify);
383 bool migration_in_setup(MigrationState *s)
385 return s->state == MIG_STATE_SETUP;
388 bool migration_has_finished(MigrationState *s)
390 return s->state == MIG_STATE_COMPLETED;
393 bool migration_has_failed(MigrationState *s)
395 return (s->state == MIG_STATE_CANCELLED ||
396 s->state == MIG_STATE_ERROR);
399 static MigrationState *migrate_init(const MigrationParams *params)
401 MigrationState *s = migrate_get_current();
402 int64_t bandwidth_limit = s->bandwidth_limit;
403 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
404 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
406 memcpy(enabled_capabilities, s->enabled_capabilities,
407 sizeof(enabled_capabilities));
409 memset(s, 0, sizeof(*s));
410 s->params = *params;
411 memcpy(s->enabled_capabilities, enabled_capabilities,
412 sizeof(enabled_capabilities));
413 s->xbzrle_cache_size = xbzrle_cache_size;
415 s->bandwidth_limit = bandwidth_limit;
416 s->state = MIG_STATE_SETUP;
417 trace_migrate_set_state(MIG_STATE_SETUP);
419 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
420 return s;
423 static GSList *migration_blockers;
425 void migrate_add_blocker(Error *reason)
427 migration_blockers = g_slist_prepend(migration_blockers, reason);
430 void migrate_del_blocker(Error *reason)
432 migration_blockers = g_slist_remove(migration_blockers, reason);
435 void qmp_migrate_incoming(const char *uri, Error **errp)
437 Error *local_err = NULL;
439 if (!deferred_incoming) {
440 error_setg(errp, "'-incoming defer' is required for migrate_incoming");
441 return;
444 qemu_start_incoming_migration(uri, &local_err);
446 if (local_err) {
447 error_propagate(errp, local_err);
448 return;
451 deferred_incoming = false;
454 void qmp_migrate(const char *uri, bool has_blk, bool blk,
455 bool has_inc, bool inc, bool has_detach, bool detach,
456 Error **errp)
458 Error *local_err = NULL;
459 MigrationState *s = migrate_get_current();
460 MigrationParams params;
461 const char *p;
463 params.blk = has_blk && blk;
464 params.shared = has_inc && inc;
466 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP ||
467 s->state == MIG_STATE_CANCELLING) {
468 error_set(errp, QERR_MIGRATION_ACTIVE);
469 return;
472 if (runstate_check(RUN_STATE_INMIGRATE)) {
473 error_setg(errp, "Guest is waiting for an incoming migration");
474 return;
477 if (qemu_savevm_state_blocked(errp)) {
478 return;
481 if (migration_blockers) {
482 *errp = error_copy(migration_blockers->data);
483 return;
486 s = migrate_init(&params);
488 if (strstart(uri, "tcp:", &p)) {
489 tcp_start_outgoing_migration(s, p, &local_err);
490 #ifdef CONFIG_RDMA
491 } else if (strstart(uri, "rdma:", &p)) {
492 rdma_start_outgoing_migration(s, p, &local_err);
493 #endif
494 #if !defined(WIN32)
495 } else if (strstart(uri, "exec:", &p)) {
496 exec_start_outgoing_migration(s, p, &local_err);
497 } else if (strstart(uri, "unix:", &p)) {
498 unix_start_outgoing_migration(s, p, &local_err);
499 } else if (strstart(uri, "fd:", &p)) {
500 fd_start_outgoing_migration(s, p, &local_err);
501 #endif
502 } else {
503 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
504 s->state = MIG_STATE_ERROR;
505 return;
508 if (local_err) {
509 migrate_fd_error(s);
510 error_propagate(errp, local_err);
511 return;
515 void qmp_migrate_cancel(Error **errp)
517 migrate_fd_cancel(migrate_get_current());
520 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
522 MigrationState *s = migrate_get_current();
523 int64_t new_size;
525 /* Check for truncation */
526 if (value != (size_t)value) {
527 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
528 "exceeding address space");
529 return;
532 /* Cache should not be larger than guest ram size */
533 if (value > ram_bytes_total()) {
534 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
535 "exceeds guest ram size ");
536 return;
539 new_size = xbzrle_cache_resize(value);
540 if (new_size < 0) {
541 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
542 "is smaller than page size");
543 return;
546 s->xbzrle_cache_size = new_size;
549 int64_t qmp_query_migrate_cache_size(Error **errp)
551 return migrate_xbzrle_cache_size();
554 void qmp_migrate_set_speed(int64_t value, Error **errp)
556 MigrationState *s;
558 if (value < 0) {
559 value = 0;
561 if (value > SIZE_MAX) {
562 value = SIZE_MAX;
565 s = migrate_get_current();
566 s->bandwidth_limit = value;
567 if (s->file) {
568 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
572 void qmp_migrate_set_downtime(double value, Error **errp)
574 value *= 1e9;
575 value = MAX(0, MIN(UINT64_MAX, value));
576 max_downtime = (uint64_t)value;
579 bool migrate_rdma_pin_all(void)
581 MigrationState *s;
583 s = migrate_get_current();
585 return s->enabled_capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
588 bool migrate_auto_converge(void)
590 MigrationState *s;
592 s = migrate_get_current();
594 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
597 bool migrate_zero_blocks(void)
599 MigrationState *s;
601 s = migrate_get_current();
603 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
606 int migrate_use_xbzrle(void)
608 MigrationState *s;
610 s = migrate_get_current();
612 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
615 int64_t migrate_xbzrle_cache_size(void)
617 MigrationState *s;
619 s = migrate_get_current();
621 return s->xbzrle_cache_size;
624 /* migration thread support */
626 static void *migration_thread(void *opaque)
628 MigrationState *s = opaque;
629 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
630 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
631 int64_t initial_bytes = 0;
632 int64_t max_size = 0;
633 int64_t start_time = initial_time;
634 bool old_vm_running = false;
636 qemu_savevm_state_begin(s->file, &s->params);
638 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
639 migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);
641 while (s->state == MIG_STATE_ACTIVE) {
642 int64_t current_time;
643 uint64_t pending_size;
645 if (!qemu_file_rate_limit(s->file)) {
646 pending_size = qemu_savevm_state_pending(s->file, max_size);
647 trace_migrate_pending(pending_size, max_size);
648 if (pending_size && pending_size >= max_size) {
649 qemu_savevm_state_iterate(s->file);
650 } else {
651 int ret;
653 qemu_mutex_lock_iothread();
654 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
655 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
656 old_vm_running = runstate_is_running();
658 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
659 if (ret >= 0) {
660 qemu_file_set_rate_limit(s->file, INT64_MAX);
661 qemu_savevm_state_complete(s->file);
663 qemu_mutex_unlock_iothread();
665 if (ret < 0) {
666 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
667 break;
670 if (!qemu_file_get_error(s->file)) {
671 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
672 break;
677 if (qemu_file_get_error(s->file)) {
678 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
679 break;
681 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
682 if (current_time >= initial_time + BUFFER_DELAY) {
683 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
684 uint64_t time_spent = current_time - initial_time;
685 double bandwidth = transferred_bytes / time_spent;
686 max_size = bandwidth * migrate_max_downtime() / 1000000;
688 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
689 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
691 trace_migrate_transferred(transferred_bytes, time_spent,
692 bandwidth, max_size);
693 /* if we haven't sent anything, we don't want to recalculate
694 10000 is a small enough number for our purposes */
695 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
696 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
699 qemu_file_reset_rate_limit(s->file);
700 initial_time = current_time;
701 initial_bytes = qemu_ftell(s->file);
703 if (qemu_file_rate_limit(s->file)) {
704 /* usleep expects microseconds */
705 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
709 qemu_mutex_lock_iothread();
710 if (s->state == MIG_STATE_COMPLETED) {
711 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
712 uint64_t transferred_bytes = qemu_ftell(s->file);
713 s->total_time = end_time - s->total_time;
714 s->downtime = end_time - start_time;
715 if (s->total_time) {
716 s->mbps = (((double) transferred_bytes * 8.0) /
717 ((double) s->total_time)) / 1000;
719 runstate_set(RUN_STATE_POSTMIGRATE);
720 } else {
721 if (old_vm_running) {
722 vm_start();
725 qemu_bh_schedule(s->cleanup_bh);
726 qemu_mutex_unlock_iothread();
728 return NULL;
731 void migrate_fd_connect(MigrationState *s)
733 s->state = MIG_STATE_SETUP;
734 trace_migrate_set_state(MIG_STATE_SETUP);
736 /* This is a best 1st approximation. ns to ms */
737 s->expected_downtime = max_downtime/1000000;
738 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
740 qemu_file_set_rate_limit(s->file,
741 s->bandwidth_limit / XFER_LIMIT_RATIO);
743 /* Notify before starting migration thread */
744 notifier_list_notify(&migration_state_notifiers, s);
746 qemu_thread_create(&s->thread, "migration", migration_thread, s,
747 QEMU_THREAD_JOINABLE);