usb: usb-hid QOMify
[qemu/ar7.git] / migration / migration.c
blob732d229708f722a57533c35085a7a6ebcd55be79
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 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
31 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
32 * data. */
33 #define BUFFER_DELAY 100
34 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
36 /* Default compression thread count */
37 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
38 /* Default decompression thread count, usually decompression is at
39 * least 4 times as fast as compression.*/
40 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
41 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
42 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
44 /* Migration XBZRLE default cache size */
45 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
47 static NotifierList migration_state_notifiers =
48 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
50 static bool deferred_incoming;
52 /* When we add fault tolerance, we could have several
53 migrations at once. For now we don't need to add
54 dynamic creation of migration */
56 MigrationState *migrate_get_current(void)
58 static MigrationState current_migration = {
59 .state = MIGRATION_STATUS_NONE,
60 .bandwidth_limit = MAX_THROTTLE,
61 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
62 .mbps = -1,
63 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
64 DEFAULT_MIGRATE_COMPRESS_LEVEL,
65 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
66 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
67 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
68 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
71 return &current_migration;
75 * Called on -incoming with a defer: uri.
76 * The migration can be started later after any parameters have been
77 * changed.
79 static void deferred_incoming_migration(Error **errp)
81 if (deferred_incoming) {
82 error_setg(errp, "Incoming migration already deferred");
84 deferred_incoming = true;
87 void qemu_start_incoming_migration(const char *uri, Error **errp)
89 const char *p;
91 if (!strcmp(uri, "defer")) {
92 deferred_incoming_migration(errp);
93 } else if (strstart(uri, "tcp:", &p)) {
94 tcp_start_incoming_migration(p, errp);
95 #ifdef CONFIG_RDMA
96 } else if (strstart(uri, "rdma:", &p)) {
97 rdma_start_incoming_migration(p, errp);
98 #endif
99 #if !defined(WIN32)
100 } else if (strstart(uri, "exec:", &p)) {
101 exec_start_incoming_migration(p, errp);
102 } else if (strstart(uri, "unix:", &p)) {
103 unix_start_incoming_migration(p, errp);
104 } else if (strstart(uri, "fd:", &p)) {
105 fd_start_incoming_migration(p, errp);
106 #endif
107 } else {
108 error_setg(errp, "unknown migration protocol: %s", uri);
112 static void process_incoming_migration_co(void *opaque)
114 QEMUFile *f = opaque;
115 Error *local_err = NULL;
116 int ret;
118 ret = qemu_loadvm_state(f);
119 qemu_fclose(f);
120 free_xbzrle_decoded_buf();
121 if (ret < 0) {
122 error_report("load of migration failed: %s", strerror(-ret));
123 migrate_decompress_threads_join();
124 exit(EXIT_FAILURE);
126 qemu_announce_self();
128 /* Make sure all file formats flush their mutable metadata */
129 bdrv_invalidate_cache_all(&local_err);
130 if (local_err) {
131 error_report_err(local_err);
132 migrate_decompress_threads_join();
133 exit(EXIT_FAILURE);
136 if (autostart) {
137 vm_start();
138 } else {
139 runstate_set(RUN_STATE_PAUSED);
141 migrate_decompress_threads_join();
144 void process_incoming_migration(QEMUFile *f)
146 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
147 int fd = qemu_get_fd(f);
149 assert(fd != -1);
150 migrate_decompress_threads_create();
151 qemu_set_nonblock(fd);
152 qemu_coroutine_enter(co, f);
155 /* amount of nanoseconds we are willing to wait for migration to be down.
156 * the choice of nanoseconds is because it is the maximum resolution that
157 * get_clock() can achieve. It is an internal measure. All user-visible
158 * units must be in seconds */
159 static uint64_t max_downtime = 300000000;
161 uint64_t migrate_max_downtime(void)
163 return max_downtime;
166 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
168 MigrationCapabilityStatusList *head = NULL;
169 MigrationCapabilityStatusList *caps;
170 MigrationState *s = migrate_get_current();
171 int i;
173 caps = NULL; /* silence compiler warning */
174 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
175 if (head == NULL) {
176 head = g_malloc0(sizeof(*caps));
177 caps = head;
178 } else {
179 caps->next = g_malloc0(sizeof(*caps));
180 caps = caps->next;
182 caps->value =
183 g_malloc(sizeof(*caps->value));
184 caps->value->capability = i;
185 caps->value->state = s->enabled_capabilities[i];
188 return head;
191 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
193 MigrationParameters *params;
194 MigrationState *s = migrate_get_current();
196 params = g_malloc0(sizeof(*params));
197 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
198 params->compress_threads =
199 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
200 params->decompress_threads =
201 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
203 return params;
206 static void get_xbzrle_cache_stats(MigrationInfo *info)
208 if (migrate_use_xbzrle()) {
209 info->has_xbzrle_cache = true;
210 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
211 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
212 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
213 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
214 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
215 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
216 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
220 MigrationInfo *qmp_query_migrate(Error **errp)
222 MigrationInfo *info = g_malloc0(sizeof(*info));
223 MigrationState *s = migrate_get_current();
225 switch (s->state) {
226 case MIGRATION_STATUS_NONE:
227 /* no migration has happened ever */
228 break;
229 case MIGRATION_STATUS_SETUP:
230 info->has_status = true;
231 info->has_total_time = false;
232 break;
233 case MIGRATION_STATUS_ACTIVE:
234 case MIGRATION_STATUS_CANCELLING:
235 info->has_status = true;
236 info->has_total_time = true;
237 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
238 - s->total_time;
239 info->has_expected_downtime = true;
240 info->expected_downtime = s->expected_downtime;
241 info->has_setup_time = true;
242 info->setup_time = s->setup_time;
244 info->has_ram = true;
245 info->ram = g_malloc0(sizeof(*info->ram));
246 info->ram->transferred = ram_bytes_transferred();
247 info->ram->remaining = ram_bytes_remaining();
248 info->ram->total = ram_bytes_total();
249 info->ram->duplicate = dup_mig_pages_transferred();
250 info->ram->skipped = skipped_mig_pages_transferred();
251 info->ram->normal = norm_mig_pages_transferred();
252 info->ram->normal_bytes = norm_mig_bytes_transferred();
253 info->ram->dirty_pages_rate = s->dirty_pages_rate;
254 info->ram->mbps = s->mbps;
255 info->ram->dirty_sync_count = s->dirty_sync_count;
257 if (blk_mig_active()) {
258 info->has_disk = true;
259 info->disk = g_malloc0(sizeof(*info->disk));
260 info->disk->transferred = blk_mig_bytes_transferred();
261 info->disk->remaining = blk_mig_bytes_remaining();
262 info->disk->total = blk_mig_bytes_total();
265 get_xbzrle_cache_stats(info);
266 break;
267 case MIGRATION_STATUS_COMPLETED:
268 get_xbzrle_cache_stats(info);
270 info->has_status = true;
271 info->has_total_time = true;
272 info->total_time = s->total_time;
273 info->has_downtime = true;
274 info->downtime = s->downtime;
275 info->has_setup_time = true;
276 info->setup_time = s->setup_time;
278 info->has_ram = true;
279 info->ram = g_malloc0(sizeof(*info->ram));
280 info->ram->transferred = ram_bytes_transferred();
281 info->ram->remaining = 0;
282 info->ram->total = ram_bytes_total();
283 info->ram->duplicate = dup_mig_pages_transferred();
284 info->ram->skipped = skipped_mig_pages_transferred();
285 info->ram->normal = norm_mig_pages_transferred();
286 info->ram->normal_bytes = norm_mig_bytes_transferred();
287 info->ram->mbps = s->mbps;
288 info->ram->dirty_sync_count = s->dirty_sync_count;
289 break;
290 case MIGRATION_STATUS_FAILED:
291 info->has_status = true;
292 break;
293 case MIGRATION_STATUS_CANCELLED:
294 info->has_status = true;
295 break;
297 info->status = s->state;
299 return info;
302 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
303 Error **errp)
305 MigrationState *s = migrate_get_current();
306 MigrationCapabilityStatusList *cap;
308 if (s->state == MIGRATION_STATUS_ACTIVE ||
309 s->state == MIGRATION_STATUS_SETUP) {
310 error_set(errp, QERR_MIGRATION_ACTIVE);
311 return;
314 for (cap = params; cap; cap = cap->next) {
315 s->enabled_capabilities[cap->value->capability] = cap->value->state;
319 void qmp_migrate_set_parameters(bool has_compress_level,
320 int64_t compress_level,
321 bool has_compress_threads,
322 int64_t compress_threads,
323 bool has_decompress_threads,
324 int64_t decompress_threads, Error **errp)
326 MigrationState *s = migrate_get_current();
328 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
329 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
330 "is invalid, it should be in the range of 0 to 9");
331 return;
333 if (has_compress_threads &&
334 (compress_threads < 1 || compress_threads > 255)) {
335 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
336 "compress_threads",
337 "is invalid, it should be in the range of 1 to 255");
338 return;
340 if (has_decompress_threads &&
341 (decompress_threads < 1 || decompress_threads > 255)) {
342 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
343 "decompress_threads",
344 "is invalid, it should be in the range of 1 to 255");
345 return;
348 if (has_compress_level) {
349 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
351 if (has_compress_threads) {
352 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
354 if (has_decompress_threads) {
355 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
356 decompress_threads;
360 /* shared migration helpers */
362 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
364 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
365 trace_migrate_set_state(new_state);
369 static void migrate_fd_cleanup(void *opaque)
371 MigrationState *s = opaque;
373 qemu_bh_delete(s->cleanup_bh);
374 s->cleanup_bh = NULL;
376 if (s->file) {
377 trace_migrate_fd_cleanup();
378 qemu_mutex_unlock_iothread();
379 qemu_thread_join(&s->thread);
380 qemu_mutex_lock_iothread();
382 migrate_compress_threads_join();
383 qemu_fclose(s->file);
384 s->file = NULL;
387 assert(s->state != MIGRATION_STATUS_ACTIVE);
389 if (s->state != MIGRATION_STATUS_COMPLETED) {
390 qemu_savevm_state_cancel();
391 if (s->state == MIGRATION_STATUS_CANCELLING) {
392 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
393 MIGRATION_STATUS_CANCELLED);
397 notifier_list_notify(&migration_state_notifiers, s);
400 void migrate_fd_error(MigrationState *s)
402 trace_migrate_fd_error();
403 assert(s->file == NULL);
404 s->state = MIGRATION_STATUS_FAILED;
405 trace_migrate_set_state(MIGRATION_STATUS_FAILED);
406 notifier_list_notify(&migration_state_notifiers, s);
409 static void migrate_fd_cancel(MigrationState *s)
411 int old_state ;
412 QEMUFile *f = migrate_get_current()->file;
413 trace_migrate_fd_cancel();
415 do {
416 old_state = s->state;
417 if (old_state != MIGRATION_STATUS_SETUP &&
418 old_state != MIGRATION_STATUS_ACTIVE) {
419 break;
421 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
422 } while (s->state != MIGRATION_STATUS_CANCELLING);
425 * If we're unlucky the migration code might be stuck somewhere in a
426 * send/write while the network has failed and is waiting to timeout;
427 * if we've got shutdown(2) available then we can force it to quit.
428 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
429 * called in a bh, so there is no race against this cancel.
431 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
432 qemu_file_shutdown(f);
436 void add_migration_state_change_notifier(Notifier *notify)
438 notifier_list_add(&migration_state_notifiers, notify);
441 void remove_migration_state_change_notifier(Notifier *notify)
443 notifier_remove(notify);
446 bool migration_in_setup(MigrationState *s)
448 return s->state == MIGRATION_STATUS_SETUP;
451 bool migration_has_finished(MigrationState *s)
453 return s->state == MIGRATION_STATUS_COMPLETED;
456 bool migration_has_failed(MigrationState *s)
458 return (s->state == MIGRATION_STATUS_CANCELLED ||
459 s->state == MIGRATION_STATUS_FAILED);
462 static MigrationState *migrate_init(const MigrationParams *params)
464 MigrationState *s = migrate_get_current();
465 int64_t bandwidth_limit = s->bandwidth_limit;
466 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
467 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
468 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
469 int compress_thread_count =
470 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
471 int decompress_thread_count =
472 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
474 memcpy(enabled_capabilities, s->enabled_capabilities,
475 sizeof(enabled_capabilities));
477 memset(s, 0, sizeof(*s));
478 s->params = *params;
479 memcpy(s->enabled_capabilities, enabled_capabilities,
480 sizeof(enabled_capabilities));
481 s->xbzrle_cache_size = xbzrle_cache_size;
483 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
484 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
485 compress_thread_count;
486 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
487 decompress_thread_count;
488 s->bandwidth_limit = bandwidth_limit;
489 s->state = MIGRATION_STATUS_SETUP;
490 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
492 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
493 return s;
496 static GSList *migration_blockers;
498 void migrate_add_blocker(Error *reason)
500 migration_blockers = g_slist_prepend(migration_blockers, reason);
503 void migrate_del_blocker(Error *reason)
505 migration_blockers = g_slist_remove(migration_blockers, reason);
508 void qmp_migrate_incoming(const char *uri, Error **errp)
510 Error *local_err = NULL;
511 static bool once = true;
513 if (!deferred_incoming) {
514 error_setg(errp, "For use with '-incoming defer'");
515 return;
517 if (!once) {
518 error_setg(errp, "The incoming migration has already been started");
521 qemu_start_incoming_migration(uri, &local_err);
523 if (local_err) {
524 error_propagate(errp, local_err);
525 return;
528 once = false;
531 void qmp_migrate(const char *uri, bool has_blk, bool blk,
532 bool has_inc, bool inc, bool has_detach, bool detach,
533 Error **errp)
535 Error *local_err = NULL;
536 MigrationState *s = migrate_get_current();
537 MigrationParams params;
538 const char *p;
540 params.blk = has_blk && blk;
541 params.shared = has_inc && inc;
543 if (s->state == MIGRATION_STATUS_ACTIVE ||
544 s->state == MIGRATION_STATUS_SETUP ||
545 s->state == MIGRATION_STATUS_CANCELLING) {
546 error_set(errp, QERR_MIGRATION_ACTIVE);
547 return;
550 if (runstate_check(RUN_STATE_INMIGRATE)) {
551 error_setg(errp, "Guest is waiting for an incoming migration");
552 return;
555 if (qemu_savevm_state_blocked(errp)) {
556 return;
559 if (migration_blockers) {
560 *errp = error_copy(migration_blockers->data);
561 return;
564 s = migrate_init(&params);
566 if (strstart(uri, "tcp:", &p)) {
567 tcp_start_outgoing_migration(s, p, &local_err);
568 #ifdef CONFIG_RDMA
569 } else if (strstart(uri, "rdma:", &p)) {
570 rdma_start_outgoing_migration(s, p, &local_err);
571 #endif
572 #if !defined(WIN32)
573 } else if (strstart(uri, "exec:", &p)) {
574 exec_start_outgoing_migration(s, p, &local_err);
575 } else if (strstart(uri, "unix:", &p)) {
576 unix_start_outgoing_migration(s, p, &local_err);
577 } else if (strstart(uri, "fd:", &p)) {
578 fd_start_outgoing_migration(s, p, &local_err);
579 #endif
580 } else {
581 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
582 s->state = MIGRATION_STATUS_FAILED;
583 return;
586 if (local_err) {
587 migrate_fd_error(s);
588 error_propagate(errp, local_err);
589 return;
593 void qmp_migrate_cancel(Error **errp)
595 migrate_fd_cancel(migrate_get_current());
598 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
600 MigrationState *s = migrate_get_current();
601 int64_t new_size;
603 /* Check for truncation */
604 if (value != (size_t)value) {
605 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
606 "exceeding address space");
607 return;
610 /* Cache should not be larger than guest ram size */
611 if (value > ram_bytes_total()) {
612 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
613 "exceeds guest ram size ");
614 return;
617 new_size = xbzrle_cache_resize(value);
618 if (new_size < 0) {
619 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
620 "is smaller than page size");
621 return;
624 s->xbzrle_cache_size = new_size;
627 int64_t qmp_query_migrate_cache_size(Error **errp)
629 return migrate_xbzrle_cache_size();
632 void qmp_migrate_set_speed(int64_t value, Error **errp)
634 MigrationState *s;
636 if (value < 0) {
637 value = 0;
639 if (value > SIZE_MAX) {
640 value = SIZE_MAX;
643 s = migrate_get_current();
644 s->bandwidth_limit = value;
645 if (s->file) {
646 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
650 void qmp_migrate_set_downtime(double value, Error **errp)
652 value *= 1e9;
653 value = MAX(0, MIN(UINT64_MAX, value));
654 max_downtime = (uint64_t)value;
657 bool migrate_auto_converge(void)
659 MigrationState *s;
661 s = migrate_get_current();
663 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
666 bool migrate_zero_blocks(void)
668 MigrationState *s;
670 s = migrate_get_current();
672 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
675 bool migrate_use_compression(void)
677 MigrationState *s;
679 s = migrate_get_current();
681 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
684 int migrate_compress_level(void)
686 MigrationState *s;
688 s = migrate_get_current();
690 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
693 int migrate_compress_threads(void)
695 MigrationState *s;
697 s = migrate_get_current();
699 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
702 int migrate_decompress_threads(void)
704 MigrationState *s;
706 s = migrate_get_current();
708 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
711 int migrate_use_xbzrle(void)
713 MigrationState *s;
715 s = migrate_get_current();
717 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
720 int64_t migrate_xbzrle_cache_size(void)
722 MigrationState *s;
724 s = migrate_get_current();
726 return s->xbzrle_cache_size;
729 /* migration thread support */
731 static void *migration_thread(void *opaque)
733 MigrationState *s = opaque;
734 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
735 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
736 int64_t initial_bytes = 0;
737 int64_t max_size = 0;
738 int64_t start_time = initial_time;
739 bool old_vm_running = false;
741 qemu_savevm_state_begin(s->file, &s->params);
743 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
744 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
746 while (s->state == MIGRATION_STATUS_ACTIVE) {
747 int64_t current_time;
748 uint64_t pending_size;
750 if (!qemu_file_rate_limit(s->file)) {
751 pending_size = qemu_savevm_state_pending(s->file, max_size);
752 trace_migrate_pending(pending_size, max_size);
753 if (pending_size && pending_size >= max_size) {
754 qemu_savevm_state_iterate(s->file);
755 } else {
756 int ret;
758 qemu_mutex_lock_iothread();
759 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
760 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
761 old_vm_running = runstate_is_running();
763 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
764 if (ret >= 0) {
765 qemu_file_set_rate_limit(s->file, INT64_MAX);
766 qemu_savevm_state_complete(s->file);
768 qemu_mutex_unlock_iothread();
770 if (ret < 0) {
771 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
772 MIGRATION_STATUS_FAILED);
773 break;
776 if (!qemu_file_get_error(s->file)) {
777 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
778 MIGRATION_STATUS_COMPLETED);
779 break;
784 if (qemu_file_get_error(s->file)) {
785 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
786 MIGRATION_STATUS_FAILED);
787 break;
789 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
790 if (current_time >= initial_time + BUFFER_DELAY) {
791 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
792 uint64_t time_spent = current_time - initial_time;
793 double bandwidth = transferred_bytes / time_spent;
794 max_size = bandwidth * migrate_max_downtime() / 1000000;
796 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
797 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
799 trace_migrate_transferred(transferred_bytes, time_spent,
800 bandwidth, max_size);
801 /* if we haven't sent anything, we don't want to recalculate
802 10000 is a small enough number for our purposes */
803 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
804 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
807 qemu_file_reset_rate_limit(s->file);
808 initial_time = current_time;
809 initial_bytes = qemu_ftell(s->file);
811 if (qemu_file_rate_limit(s->file)) {
812 /* usleep expects microseconds */
813 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
817 qemu_mutex_lock_iothread();
818 if (s->state == MIGRATION_STATUS_COMPLETED) {
819 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
820 uint64_t transferred_bytes = qemu_ftell(s->file);
821 s->total_time = end_time - s->total_time;
822 s->downtime = end_time - start_time;
823 if (s->total_time) {
824 s->mbps = (((double) transferred_bytes * 8.0) /
825 ((double) s->total_time)) / 1000;
827 runstate_set(RUN_STATE_POSTMIGRATE);
828 } else {
829 if (old_vm_running) {
830 vm_start();
833 qemu_bh_schedule(s->cleanup_bh);
834 qemu_mutex_unlock_iothread();
836 return NULL;
839 void migrate_fd_connect(MigrationState *s)
841 s->state = MIGRATION_STATUS_SETUP;
842 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
844 /* This is a best 1st approximation. ns to ms */
845 s->expected_downtime = max_downtime/1000000;
846 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
848 qemu_file_set_rate_limit(s->file,
849 s->bandwidth_limit / XFER_LIMIT_RATIO);
851 /* Notify before starting migration thread */
852 notifier_list_notify(&migration_state_notifiers, s);
854 migrate_compress_threads_create();
855 qemu_thread_create(&s->thread, "migration", migration_thread, s,
856 QEMU_THREAD_JOINABLE);