util/hbitmap: Add an API to reset all set bits in hbitmap
[qemu/ar7.git] / migration / migration.c
blobc6ac08a0cbc6e1e78cd20938617c36265f27c2de
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/error-report.h"
18 #include "qemu/main-loop.h"
19 #include "migration/migration.h"
20 #include "migration/qemu-file.h"
21 #include "sysemu/sysemu.h"
22 #include "block/block.h"
23 #include "qapi/qmp/qerror.h"
24 #include "qemu/sockets.h"
25 #include "migration/block.h"
26 #include "qemu/thread.h"
27 #include "qmp-commands.h"
28 #include "trace.h"
30 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
32 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
33 * data. */
34 #define BUFFER_DELAY 100
35 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
37 /* Default compression thread count */
38 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
39 /* Default decompression thread count, usually decompression is at
40 * least 4 times as fast as compression.*/
41 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
42 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
43 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
45 /* Migration XBZRLE default cache size */
46 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
48 static NotifierList migration_state_notifiers =
49 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
51 static bool deferred_incoming;
53 /* When we add fault tolerance, we could have several
54 migrations at once. For now we don't need to add
55 dynamic creation of migration */
57 /* For outgoing */
58 MigrationState *migrate_get_current(void)
60 static MigrationState current_migration = {
61 .state = MIGRATION_STATUS_NONE,
62 .bandwidth_limit = MAX_THROTTLE,
63 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
64 .mbps = -1,
65 .parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] =
66 DEFAULT_MIGRATE_COMPRESS_LEVEL,
67 .parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
68 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
69 .parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
70 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT,
73 return &current_migration;
76 /* For incoming */
77 static MigrationIncomingState *mis_current;
79 MigrationIncomingState *migration_incoming_get_current(void)
81 return mis_current;
84 MigrationIncomingState *migration_incoming_state_new(QEMUFile* f)
86 mis_current = g_malloc0(sizeof(MigrationIncomingState));
87 mis_current->file = f;
88 QLIST_INIT(&mis_current->loadvm_handlers);
90 return mis_current;
93 void migration_incoming_state_destroy(void)
95 loadvm_free_handlers(mis_current);
96 g_free(mis_current);
97 mis_current = NULL;
101 * Called on -incoming with a defer: uri.
102 * The migration can be started later after any parameters have been
103 * changed.
105 static void deferred_incoming_migration(Error **errp)
107 if (deferred_incoming) {
108 error_setg(errp, "Incoming migration already deferred");
110 deferred_incoming = true;
113 void qemu_start_incoming_migration(const char *uri, Error **errp)
115 const char *p;
117 if (!strcmp(uri, "defer")) {
118 deferred_incoming_migration(errp);
119 } else if (strstart(uri, "tcp:", &p)) {
120 tcp_start_incoming_migration(p, errp);
121 #ifdef CONFIG_RDMA
122 } else if (strstart(uri, "rdma:", &p)) {
123 rdma_start_incoming_migration(p, errp);
124 #endif
125 #if !defined(WIN32)
126 } else if (strstart(uri, "exec:", &p)) {
127 exec_start_incoming_migration(p, errp);
128 } else if (strstart(uri, "unix:", &p)) {
129 unix_start_incoming_migration(p, errp);
130 } else if (strstart(uri, "fd:", &p)) {
131 fd_start_incoming_migration(p, errp);
132 #endif
133 } else {
134 error_setg(errp, "unknown migration protocol: %s", uri);
138 static void process_incoming_migration_co(void *opaque)
140 QEMUFile *f = opaque;
141 Error *local_err = NULL;
142 int ret;
144 migration_incoming_state_new(f);
146 ret = qemu_loadvm_state(f);
148 qemu_fclose(f);
149 free_xbzrle_decoded_buf();
150 migration_incoming_state_destroy();
152 if (ret < 0) {
153 error_report("load of migration failed: %s", strerror(-ret));
154 migrate_decompress_threads_join();
155 exit(EXIT_FAILURE);
157 qemu_announce_self();
159 /* Make sure all file formats flush their mutable metadata */
160 bdrv_invalidate_cache_all(&local_err);
161 if (local_err) {
162 error_report_err(local_err);
163 migrate_decompress_threads_join();
164 exit(EXIT_FAILURE);
167 if (autostart) {
168 vm_start();
169 } else {
170 runstate_set(RUN_STATE_PAUSED);
172 migrate_decompress_threads_join();
175 void process_incoming_migration(QEMUFile *f)
177 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
178 int fd = qemu_get_fd(f);
180 assert(fd != -1);
181 migrate_decompress_threads_create();
182 qemu_set_nonblock(fd);
183 qemu_coroutine_enter(co, f);
186 /* amount of nanoseconds we are willing to wait for migration to be down.
187 * the choice of nanoseconds is because it is the maximum resolution that
188 * get_clock() can achieve. It is an internal measure. All user-visible
189 * units must be in seconds */
190 static uint64_t max_downtime = 300000000;
192 uint64_t migrate_max_downtime(void)
194 return max_downtime;
197 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
199 MigrationCapabilityStatusList *head = NULL;
200 MigrationCapabilityStatusList *caps;
201 MigrationState *s = migrate_get_current();
202 int i;
204 caps = NULL; /* silence compiler warning */
205 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
206 if (head == NULL) {
207 head = g_malloc0(sizeof(*caps));
208 caps = head;
209 } else {
210 caps->next = g_malloc0(sizeof(*caps));
211 caps = caps->next;
213 caps->value =
214 g_malloc(sizeof(*caps->value));
215 caps->value->capability = i;
216 caps->value->state = s->enabled_capabilities[i];
219 return head;
222 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
224 MigrationParameters *params;
225 MigrationState *s = migrate_get_current();
227 params = g_malloc0(sizeof(*params));
228 params->compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
229 params->compress_threads =
230 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
231 params->decompress_threads =
232 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
234 return params;
237 static void get_xbzrle_cache_stats(MigrationInfo *info)
239 if (migrate_use_xbzrle()) {
240 info->has_xbzrle_cache = true;
241 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
242 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
243 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
244 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
245 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
246 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
247 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
251 MigrationInfo *qmp_query_migrate(Error **errp)
253 MigrationInfo *info = g_malloc0(sizeof(*info));
254 MigrationState *s = migrate_get_current();
256 switch (s->state) {
257 case MIGRATION_STATUS_NONE:
258 /* no migration has happened ever */
259 break;
260 case MIGRATION_STATUS_SETUP:
261 info->has_status = true;
262 info->has_total_time = false;
263 break;
264 case MIGRATION_STATUS_ACTIVE:
265 case MIGRATION_STATUS_CANCELLING:
266 info->has_status = true;
267 info->has_total_time = true;
268 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
269 - s->total_time;
270 info->has_expected_downtime = true;
271 info->expected_downtime = s->expected_downtime;
272 info->has_setup_time = true;
273 info->setup_time = s->setup_time;
275 info->has_ram = true;
276 info->ram = g_malloc0(sizeof(*info->ram));
277 info->ram->transferred = ram_bytes_transferred();
278 info->ram->remaining = ram_bytes_remaining();
279 info->ram->total = ram_bytes_total();
280 info->ram->duplicate = dup_mig_pages_transferred();
281 info->ram->skipped = skipped_mig_pages_transferred();
282 info->ram->normal = norm_mig_pages_transferred();
283 info->ram->normal_bytes = norm_mig_bytes_transferred();
284 info->ram->dirty_pages_rate = s->dirty_pages_rate;
285 info->ram->mbps = s->mbps;
286 info->ram->dirty_sync_count = s->dirty_sync_count;
288 if (blk_mig_active()) {
289 info->has_disk = true;
290 info->disk = g_malloc0(sizeof(*info->disk));
291 info->disk->transferred = blk_mig_bytes_transferred();
292 info->disk->remaining = blk_mig_bytes_remaining();
293 info->disk->total = blk_mig_bytes_total();
296 get_xbzrle_cache_stats(info);
297 break;
298 case MIGRATION_STATUS_COMPLETED:
299 get_xbzrle_cache_stats(info);
301 info->has_status = true;
302 info->has_total_time = true;
303 info->total_time = s->total_time;
304 info->has_downtime = true;
305 info->downtime = s->downtime;
306 info->has_setup_time = true;
307 info->setup_time = s->setup_time;
309 info->has_ram = true;
310 info->ram = g_malloc0(sizeof(*info->ram));
311 info->ram->transferred = ram_bytes_transferred();
312 info->ram->remaining = 0;
313 info->ram->total = ram_bytes_total();
314 info->ram->duplicate = dup_mig_pages_transferred();
315 info->ram->skipped = skipped_mig_pages_transferred();
316 info->ram->normal = norm_mig_pages_transferred();
317 info->ram->normal_bytes = norm_mig_bytes_transferred();
318 info->ram->mbps = s->mbps;
319 info->ram->dirty_sync_count = s->dirty_sync_count;
320 break;
321 case MIGRATION_STATUS_FAILED:
322 info->has_status = true;
323 break;
324 case MIGRATION_STATUS_CANCELLED:
325 info->has_status = true;
326 break;
328 info->status = s->state;
330 return info;
333 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
334 Error **errp)
336 MigrationState *s = migrate_get_current();
337 MigrationCapabilityStatusList *cap;
339 if (s->state == MIGRATION_STATUS_ACTIVE ||
340 s->state == MIGRATION_STATUS_SETUP) {
341 error_setg(errp, QERR_MIGRATION_ACTIVE);
342 return;
345 for (cap = params; cap; cap = cap->next) {
346 s->enabled_capabilities[cap->value->capability] = cap->value->state;
350 void qmp_migrate_set_parameters(bool has_compress_level,
351 int64_t compress_level,
352 bool has_compress_threads,
353 int64_t compress_threads,
354 bool has_decompress_threads,
355 int64_t decompress_threads, Error **errp)
357 MigrationState *s = migrate_get_current();
359 if (has_compress_level && (compress_level < 0 || compress_level > 9)) {
360 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
361 "is invalid, it should be in the range of 0 to 9");
362 return;
364 if (has_compress_threads &&
365 (compress_threads < 1 || compress_threads > 255)) {
366 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
367 "compress_threads",
368 "is invalid, it should be in the range of 1 to 255");
369 return;
371 if (has_decompress_threads &&
372 (decompress_threads < 1 || decompress_threads > 255)) {
373 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
374 "decompress_threads",
375 "is invalid, it should be in the range of 1 to 255");
376 return;
379 if (has_compress_level) {
380 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
382 if (has_compress_threads) {
383 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] = compress_threads;
385 if (has_decompress_threads) {
386 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
387 decompress_threads;
391 /* shared migration helpers */
393 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
395 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
396 trace_migrate_set_state(new_state);
400 static void migrate_fd_cleanup(void *opaque)
402 MigrationState *s = opaque;
404 qemu_bh_delete(s->cleanup_bh);
405 s->cleanup_bh = NULL;
407 if (s->file) {
408 trace_migrate_fd_cleanup();
409 qemu_mutex_unlock_iothread();
410 qemu_thread_join(&s->thread);
411 qemu_mutex_lock_iothread();
413 migrate_compress_threads_join();
414 qemu_fclose(s->file);
415 s->file = NULL;
418 assert(s->state != MIGRATION_STATUS_ACTIVE);
420 if (s->state != MIGRATION_STATUS_COMPLETED) {
421 qemu_savevm_state_cancel();
422 if (s->state == MIGRATION_STATUS_CANCELLING) {
423 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
424 MIGRATION_STATUS_CANCELLED);
428 notifier_list_notify(&migration_state_notifiers, s);
431 void migrate_fd_error(MigrationState *s)
433 trace_migrate_fd_error();
434 assert(s->file == NULL);
435 s->state = MIGRATION_STATUS_FAILED;
436 trace_migrate_set_state(MIGRATION_STATUS_FAILED);
437 notifier_list_notify(&migration_state_notifiers, s);
440 static void migrate_fd_cancel(MigrationState *s)
442 int old_state ;
443 QEMUFile *f = migrate_get_current()->file;
444 trace_migrate_fd_cancel();
446 do {
447 old_state = s->state;
448 if (old_state != MIGRATION_STATUS_SETUP &&
449 old_state != MIGRATION_STATUS_ACTIVE) {
450 break;
452 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
453 } while (s->state != MIGRATION_STATUS_CANCELLING);
456 * If we're unlucky the migration code might be stuck somewhere in a
457 * send/write while the network has failed and is waiting to timeout;
458 * if we've got shutdown(2) available then we can force it to quit.
459 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
460 * called in a bh, so there is no race against this cancel.
462 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
463 qemu_file_shutdown(f);
467 void add_migration_state_change_notifier(Notifier *notify)
469 notifier_list_add(&migration_state_notifiers, notify);
472 void remove_migration_state_change_notifier(Notifier *notify)
474 notifier_remove(notify);
477 bool migration_in_setup(MigrationState *s)
479 return s->state == MIGRATION_STATUS_SETUP;
482 bool migration_has_finished(MigrationState *s)
484 return s->state == MIGRATION_STATUS_COMPLETED;
487 bool migration_has_failed(MigrationState *s)
489 return (s->state == MIGRATION_STATUS_CANCELLED ||
490 s->state == MIGRATION_STATUS_FAILED);
493 static MigrationState *migrate_init(const MigrationParams *params)
495 MigrationState *s = migrate_get_current();
496 int64_t bandwidth_limit = s->bandwidth_limit;
497 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
498 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
499 int compress_level = s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
500 int compress_thread_count =
501 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
502 int decompress_thread_count =
503 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
505 memcpy(enabled_capabilities, s->enabled_capabilities,
506 sizeof(enabled_capabilities));
508 memset(s, 0, sizeof(*s));
509 s->params = *params;
510 memcpy(s->enabled_capabilities, enabled_capabilities,
511 sizeof(enabled_capabilities));
512 s->xbzrle_cache_size = xbzrle_cache_size;
514 s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL] = compress_level;
515 s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS] =
516 compress_thread_count;
517 s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS] =
518 decompress_thread_count;
519 s->bandwidth_limit = bandwidth_limit;
520 s->state = MIGRATION_STATUS_SETUP;
521 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
523 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
524 return s;
527 static GSList *migration_blockers;
529 void migrate_add_blocker(Error *reason)
531 migration_blockers = g_slist_prepend(migration_blockers, reason);
534 void migrate_del_blocker(Error *reason)
536 migration_blockers = g_slist_remove(migration_blockers, reason);
539 void qmp_migrate_incoming(const char *uri, Error **errp)
541 Error *local_err = NULL;
542 static bool once = true;
544 if (!deferred_incoming) {
545 error_setg(errp, "For use with '-incoming defer'");
546 return;
548 if (!once) {
549 error_setg(errp, "The incoming migration has already been started");
552 qemu_start_incoming_migration(uri, &local_err);
554 if (local_err) {
555 error_propagate(errp, local_err);
556 return;
559 once = false;
562 void qmp_migrate(const char *uri, bool has_blk, bool blk,
563 bool has_inc, bool inc, bool has_detach, bool detach,
564 Error **errp)
566 Error *local_err = NULL;
567 MigrationState *s = migrate_get_current();
568 MigrationParams params;
569 const char *p;
571 params.blk = has_blk && blk;
572 params.shared = has_inc && inc;
574 if (s->state == MIGRATION_STATUS_ACTIVE ||
575 s->state == MIGRATION_STATUS_SETUP ||
576 s->state == MIGRATION_STATUS_CANCELLING) {
577 error_setg(errp, QERR_MIGRATION_ACTIVE);
578 return;
581 if (runstate_check(RUN_STATE_INMIGRATE)) {
582 error_setg(errp, "Guest is waiting for an incoming migration");
583 return;
586 if (qemu_savevm_state_blocked(errp)) {
587 return;
590 if (migration_blockers) {
591 *errp = error_copy(migration_blockers->data);
592 return;
595 s = migrate_init(&params);
597 if (strstart(uri, "tcp:", &p)) {
598 tcp_start_outgoing_migration(s, p, &local_err);
599 #ifdef CONFIG_RDMA
600 } else if (strstart(uri, "rdma:", &p)) {
601 rdma_start_outgoing_migration(s, p, &local_err);
602 #endif
603 #if !defined(WIN32)
604 } else if (strstart(uri, "exec:", &p)) {
605 exec_start_outgoing_migration(s, p, &local_err);
606 } else if (strstart(uri, "unix:", &p)) {
607 unix_start_outgoing_migration(s, p, &local_err);
608 } else if (strstart(uri, "fd:", &p)) {
609 fd_start_outgoing_migration(s, p, &local_err);
610 #endif
611 } else {
612 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
613 "a valid migration protocol");
614 s->state = MIGRATION_STATUS_FAILED;
615 return;
618 if (local_err) {
619 migrate_fd_error(s);
620 error_propagate(errp, local_err);
621 return;
625 void qmp_migrate_cancel(Error **errp)
627 migrate_fd_cancel(migrate_get_current());
630 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
632 MigrationState *s = migrate_get_current();
633 int64_t new_size;
635 /* Check for truncation */
636 if (value != (size_t)value) {
637 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
638 "exceeding address space");
639 return;
642 /* Cache should not be larger than guest ram size */
643 if (value > ram_bytes_total()) {
644 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
645 "exceeds guest ram size ");
646 return;
649 new_size = xbzrle_cache_resize(value);
650 if (new_size < 0) {
651 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
652 "is smaller than page size");
653 return;
656 s->xbzrle_cache_size = new_size;
659 int64_t qmp_query_migrate_cache_size(Error **errp)
661 return migrate_xbzrle_cache_size();
664 void qmp_migrate_set_speed(int64_t value, Error **errp)
666 MigrationState *s;
668 if (value < 0) {
669 value = 0;
671 if (value > SIZE_MAX) {
672 value = SIZE_MAX;
675 s = migrate_get_current();
676 s->bandwidth_limit = value;
677 if (s->file) {
678 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
682 void qmp_migrate_set_downtime(double value, Error **errp)
684 value *= 1e9;
685 value = MAX(0, MIN(UINT64_MAX, value));
686 max_downtime = (uint64_t)value;
689 bool migrate_auto_converge(void)
691 MigrationState *s;
693 s = migrate_get_current();
695 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
698 bool migrate_zero_blocks(void)
700 MigrationState *s;
702 s = migrate_get_current();
704 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
707 bool migrate_use_compression(void)
709 MigrationState *s;
711 s = migrate_get_current();
713 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
716 int migrate_compress_level(void)
718 MigrationState *s;
720 s = migrate_get_current();
722 return s->parameters[MIGRATION_PARAMETER_COMPRESS_LEVEL];
725 int migrate_compress_threads(void)
727 MigrationState *s;
729 s = migrate_get_current();
731 return s->parameters[MIGRATION_PARAMETER_COMPRESS_THREADS];
734 int migrate_decompress_threads(void)
736 MigrationState *s;
738 s = migrate_get_current();
740 return s->parameters[MIGRATION_PARAMETER_DECOMPRESS_THREADS];
743 int migrate_use_xbzrle(void)
745 MigrationState *s;
747 s = migrate_get_current();
749 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
752 int64_t migrate_xbzrle_cache_size(void)
754 MigrationState *s;
756 s = migrate_get_current();
758 return s->xbzrle_cache_size;
761 /* migration thread support */
763 static void *migration_thread(void *opaque)
765 MigrationState *s = opaque;
766 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
767 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
768 int64_t initial_bytes = 0;
769 int64_t max_size = 0;
770 int64_t start_time = initial_time;
771 bool old_vm_running = false;
773 qemu_savevm_state_header(s->file);
774 qemu_savevm_state_begin(s->file, &s->params);
776 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
777 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
779 while (s->state == MIGRATION_STATUS_ACTIVE) {
780 int64_t current_time;
781 uint64_t pending_size;
783 if (!qemu_file_rate_limit(s->file)) {
784 pending_size = qemu_savevm_state_pending(s->file, max_size);
785 trace_migrate_pending(pending_size, max_size);
786 if (pending_size && pending_size >= max_size) {
787 qemu_savevm_state_iterate(s->file);
788 } else {
789 int ret;
791 qemu_mutex_lock_iothread();
792 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
793 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
794 old_vm_running = runstate_is_running();
796 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
797 if (ret >= 0) {
798 qemu_file_set_rate_limit(s->file, INT64_MAX);
799 qemu_savevm_state_complete(s->file);
801 qemu_mutex_unlock_iothread();
803 if (ret < 0) {
804 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
805 MIGRATION_STATUS_FAILED);
806 break;
809 if (!qemu_file_get_error(s->file)) {
810 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
811 MIGRATION_STATUS_COMPLETED);
812 break;
817 if (qemu_file_get_error(s->file)) {
818 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
819 MIGRATION_STATUS_FAILED);
820 break;
822 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
823 if (current_time >= initial_time + BUFFER_DELAY) {
824 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
825 uint64_t time_spent = current_time - initial_time;
826 double bandwidth = transferred_bytes / time_spent;
827 max_size = bandwidth * migrate_max_downtime() / 1000000;
829 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
830 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
832 trace_migrate_transferred(transferred_bytes, time_spent,
833 bandwidth, max_size);
834 /* if we haven't sent anything, we don't want to recalculate
835 10000 is a small enough number for our purposes */
836 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
837 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
840 qemu_file_reset_rate_limit(s->file);
841 initial_time = current_time;
842 initial_bytes = qemu_ftell(s->file);
844 if (qemu_file_rate_limit(s->file)) {
845 /* usleep expects microseconds */
846 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
850 qemu_mutex_lock_iothread();
851 if (s->state == MIGRATION_STATUS_COMPLETED) {
852 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
853 uint64_t transferred_bytes = qemu_ftell(s->file);
854 s->total_time = end_time - s->total_time;
855 s->downtime = end_time - start_time;
856 if (s->total_time) {
857 s->mbps = (((double) transferred_bytes * 8.0) /
858 ((double) s->total_time)) / 1000;
860 runstate_set(RUN_STATE_POSTMIGRATE);
861 } else {
862 if (old_vm_running) {
863 vm_start();
866 qemu_bh_schedule(s->cleanup_bh);
867 qemu_mutex_unlock_iothread();
869 return NULL;
872 void migrate_fd_connect(MigrationState *s)
874 /* This is a best 1st approximation. ns to ms */
875 s->expected_downtime = max_downtime/1000000;
876 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
878 qemu_file_set_rate_limit(s->file,
879 s->bandwidth_limit / XFER_LIMIT_RATIO);
881 /* Notify before starting migration thread */
882 notifier_list_notify(&migration_state_notifiers, s);
884 migrate_compress_threads_create();
885 qemu_thread_create(&s->thread, "migration", migration_thread, s,
886 QEMU_THREAD_JOINABLE);