4 * Copyright IBM, Corp. 2008
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"
26 #include "migration/block.h"
27 #include "qemu/thread.h"
28 #include "qmp-commands.h"
30 #include "qapi/util.h"
31 #include "qapi-event.h"
33 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
35 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
37 #define BUFFER_DELAY 100
38 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
40 /* Default compression thread count */
41 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
42 /* Default decompression thread count, usually decompression is at
43 * least 4 times as fast as compression.*/
44 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
45 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
46 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
48 /* Migration XBZRLE default cache size */
49 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
51 static NotifierList migration_state_notifiers
=
52 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
54 static bool deferred_incoming
;
56 /* When we add fault tolerance, we could have several
57 migrations at once. For now we don't need to add
58 dynamic creation of migration */
61 MigrationState
*migrate_get_current(void)
63 static MigrationState current_migration
= {
64 .state
= MIGRATION_STATUS_NONE
,
65 .bandwidth_limit
= MAX_THROTTLE
,
66 .xbzrle_cache_size
= DEFAULT_MIGRATE_CACHE_SIZE
,
68 .parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
] =
69 DEFAULT_MIGRATE_COMPRESS_LEVEL
,
70 .parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
] =
71 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT
,
72 .parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
] =
73 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT
,
76 return ¤t_migration
;
80 static MigrationIncomingState
*mis_current
;
82 MigrationIncomingState
*migration_incoming_get_current(void)
87 MigrationIncomingState
*migration_incoming_state_new(QEMUFile
* f
)
89 mis_current
= g_malloc0(sizeof(MigrationIncomingState
));
90 mis_current
->file
= f
;
91 QLIST_INIT(&mis_current
->loadvm_handlers
);
96 void migration_incoming_state_destroy(void)
98 loadvm_free_handlers(mis_current
);
107 uint8_t runstate
[100];
112 static GlobalState global_state
;
114 int global_state_store(void)
116 if (!runstate_store((char *)global_state
.runstate
,
117 sizeof(global_state
.runstate
))) {
118 error_report("runstate name too big: %s", global_state
.runstate
);
119 trace_migrate_state_too_big();
125 static bool global_state_received(void)
127 return global_state
.received
;
130 static RunState
global_state_get_runstate(void)
132 return global_state
.state
;
135 void global_state_set_optional(void)
137 global_state
.optional
= true;
140 static bool global_state_needed(void *opaque
)
142 GlobalState
*s
= opaque
;
143 char *runstate
= (char *)s
->runstate
;
145 /* If it is not optional, it is mandatory */
147 if (s
->optional
== false) {
151 /* If state is running or paused, it is not needed */
153 if (strcmp(runstate
, "running") == 0 ||
154 strcmp(runstate
, "paused") == 0) {
158 /* for any other state it is needed */
162 static int global_state_post_load(void *opaque
, int version_id
)
164 GlobalState
*s
= opaque
;
165 Error
*local_err
= NULL
;
167 char *runstate
= (char *)s
->runstate
;
170 trace_migrate_global_state_post_load(runstate
);
172 r
= qapi_enum_parse(RunState_lookup
, runstate
, RUN_STATE_MAX
,
177 error_report_err(local_err
);
186 static void global_state_pre_save(void *opaque
)
188 GlobalState
*s
= opaque
;
190 trace_migrate_global_state_pre_save((char *)s
->runstate
);
191 s
->size
= strlen((char *)s
->runstate
) + 1;
194 static const VMStateDescription vmstate_globalstate
= {
195 .name
= "globalstate",
197 .minimum_version_id
= 1,
198 .post_load
= global_state_post_load
,
199 .pre_save
= global_state_pre_save
,
200 .needed
= global_state_needed
,
201 .fields
= (VMStateField
[]) {
202 VMSTATE_UINT32(size
, GlobalState
),
203 VMSTATE_BUFFER(runstate
, GlobalState
),
204 VMSTATE_END_OF_LIST()
208 void register_global_state(void)
210 /* We would use it independently that we receive it */
211 strcpy((char *)&global_state
.runstate
, "");
212 global_state
.received
= false;
213 vmstate_register(NULL
, 0, &vmstate_globalstate
, &global_state
);
216 static void migrate_generate_event(int new_state
)
218 if (migrate_use_events()) {
219 qapi_event_send_migration(new_state
, &error_abort
);
224 * Called on -incoming with a defer: uri.
225 * The migration can be started later after any parameters have been
228 static void deferred_incoming_migration(Error
**errp
)
230 if (deferred_incoming
) {
231 error_setg(errp
, "Incoming migration already deferred");
233 deferred_incoming
= true;
236 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
240 qapi_event_send_migration(MIGRATION_STATUS_SETUP
, &error_abort
);
241 if (!strcmp(uri
, "defer")) {
242 deferred_incoming_migration(errp
);
243 } else if (strstart(uri
, "tcp:", &p
)) {
244 tcp_start_incoming_migration(p
, errp
);
246 } else if (strstart(uri
, "rdma:", &p
)) {
247 rdma_start_incoming_migration(p
, errp
);
250 } else if (strstart(uri
, "exec:", &p
)) {
251 exec_start_incoming_migration(p
, errp
);
252 } else if (strstart(uri
, "unix:", &p
)) {
253 unix_start_incoming_migration(p
, errp
);
254 } else if (strstart(uri
, "fd:", &p
)) {
255 fd_start_incoming_migration(p
, errp
);
258 error_setg(errp
, "unknown migration protocol: %s", uri
);
262 static void process_incoming_migration_co(void *opaque
)
264 QEMUFile
*f
= opaque
;
265 Error
*local_err
= NULL
;
268 migration_incoming_state_new(f
);
269 migrate_generate_event(MIGRATION_STATUS_ACTIVE
);
270 ret
= qemu_loadvm_state(f
);
273 free_xbzrle_decoded_buf();
274 migration_incoming_state_destroy();
277 migrate_generate_event(MIGRATION_STATUS_FAILED
);
278 error_report("load of migration failed: %s", strerror(-ret
));
279 migrate_decompress_threads_join();
282 migrate_generate_event(MIGRATION_STATUS_COMPLETED
);
283 qemu_announce_self();
285 /* Make sure all file formats flush their mutable metadata */
286 bdrv_invalidate_cache_all(&local_err
);
288 error_report_err(local_err
);
289 migrate_decompress_threads_join();
293 /* If global state section was not received or we are in running
294 state, we need to obey autostart. Any other state is set with
297 if (!global_state_received() ||
298 global_state_get_runstate() == RUN_STATE_RUNNING
) {
302 runstate_set(RUN_STATE_PAUSED
);
305 runstate_set(global_state_get_runstate());
307 migrate_decompress_threads_join();
310 void process_incoming_migration(QEMUFile
*f
)
312 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
);
313 int fd
= qemu_get_fd(f
);
316 migrate_decompress_threads_create();
317 qemu_set_nonblock(fd
);
318 qemu_coroutine_enter(co
, f
);
321 /* amount of nanoseconds we are willing to wait for migration to be down.
322 * the choice of nanoseconds is because it is the maximum resolution that
323 * get_clock() can achieve. It is an internal measure. All user-visible
324 * units must be in seconds */
325 static uint64_t max_downtime
= 300000000;
327 uint64_t migrate_max_downtime(void)
332 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
334 MigrationCapabilityStatusList
*head
= NULL
;
335 MigrationCapabilityStatusList
*caps
;
336 MigrationState
*s
= migrate_get_current();
339 caps
= NULL
; /* silence compiler warning */
340 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
342 head
= g_malloc0(sizeof(*caps
));
345 caps
->next
= g_malloc0(sizeof(*caps
));
349 g_malloc(sizeof(*caps
->value
));
350 caps
->value
->capability
= i
;
351 caps
->value
->state
= s
->enabled_capabilities
[i
];
357 MigrationParameters
*qmp_query_migrate_parameters(Error
**errp
)
359 MigrationParameters
*params
;
360 MigrationState
*s
= migrate_get_current();
362 params
= g_malloc0(sizeof(*params
));
363 params
->compress_level
= s
->parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
];
364 params
->compress_threads
=
365 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
];
366 params
->decompress_threads
=
367 s
->parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
];
372 static void get_xbzrle_cache_stats(MigrationInfo
*info
)
374 if (migrate_use_xbzrle()) {
375 info
->has_xbzrle_cache
= true;
376 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
377 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
378 info
->xbzrle_cache
->bytes
= xbzrle_mig_bytes_transferred();
379 info
->xbzrle_cache
->pages
= xbzrle_mig_pages_transferred();
380 info
->xbzrle_cache
->cache_miss
= xbzrle_mig_pages_cache_miss();
381 info
->xbzrle_cache
->cache_miss_rate
= xbzrle_mig_cache_miss_rate();
382 info
->xbzrle_cache
->overflow
= xbzrle_mig_pages_overflow();
386 MigrationInfo
*qmp_query_migrate(Error
**errp
)
388 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
389 MigrationState
*s
= migrate_get_current();
392 case MIGRATION_STATUS_NONE
:
393 /* no migration has happened ever */
395 case MIGRATION_STATUS_SETUP
:
396 info
->has_status
= true;
397 info
->has_total_time
= false;
399 case MIGRATION_STATUS_ACTIVE
:
400 case MIGRATION_STATUS_CANCELLING
:
401 info
->has_status
= true;
402 info
->has_total_time
= true;
403 info
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
)
405 info
->has_expected_downtime
= true;
406 info
->expected_downtime
= s
->expected_downtime
;
407 info
->has_setup_time
= true;
408 info
->setup_time
= s
->setup_time
;
410 info
->has_ram
= true;
411 info
->ram
= g_malloc0(sizeof(*info
->ram
));
412 info
->ram
->transferred
= ram_bytes_transferred();
413 info
->ram
->remaining
= ram_bytes_remaining();
414 info
->ram
->total
= ram_bytes_total();
415 info
->ram
->duplicate
= dup_mig_pages_transferred();
416 info
->ram
->skipped
= skipped_mig_pages_transferred();
417 info
->ram
->normal
= norm_mig_pages_transferred();
418 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
419 info
->ram
->dirty_pages_rate
= s
->dirty_pages_rate
;
420 info
->ram
->mbps
= s
->mbps
;
421 info
->ram
->dirty_sync_count
= s
->dirty_sync_count
;
423 if (blk_mig_active()) {
424 info
->has_disk
= true;
425 info
->disk
= g_malloc0(sizeof(*info
->disk
));
426 info
->disk
->transferred
= blk_mig_bytes_transferred();
427 info
->disk
->remaining
= blk_mig_bytes_remaining();
428 info
->disk
->total
= blk_mig_bytes_total();
431 get_xbzrle_cache_stats(info
);
433 case MIGRATION_STATUS_COMPLETED
:
434 get_xbzrle_cache_stats(info
);
436 info
->has_status
= true;
437 info
->has_total_time
= true;
438 info
->total_time
= s
->total_time
;
439 info
->has_downtime
= true;
440 info
->downtime
= s
->downtime
;
441 info
->has_setup_time
= true;
442 info
->setup_time
= s
->setup_time
;
444 info
->has_ram
= true;
445 info
->ram
= g_malloc0(sizeof(*info
->ram
));
446 info
->ram
->transferred
= ram_bytes_transferred();
447 info
->ram
->remaining
= 0;
448 info
->ram
->total
= ram_bytes_total();
449 info
->ram
->duplicate
= dup_mig_pages_transferred();
450 info
->ram
->skipped
= skipped_mig_pages_transferred();
451 info
->ram
->normal
= norm_mig_pages_transferred();
452 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
453 info
->ram
->mbps
= s
->mbps
;
454 info
->ram
->dirty_sync_count
= s
->dirty_sync_count
;
456 case MIGRATION_STATUS_FAILED
:
457 info
->has_status
= true;
459 case MIGRATION_STATUS_CANCELLED
:
460 info
->has_status
= true;
463 info
->status
= s
->state
;
468 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
471 MigrationState
*s
= migrate_get_current();
472 MigrationCapabilityStatusList
*cap
;
474 if (s
->state
== MIGRATION_STATUS_ACTIVE
||
475 s
->state
== MIGRATION_STATUS_SETUP
) {
476 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
480 for (cap
= params
; cap
; cap
= cap
->next
) {
481 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
485 void qmp_migrate_set_parameters(bool has_compress_level
,
486 int64_t compress_level
,
487 bool has_compress_threads
,
488 int64_t compress_threads
,
489 bool has_decompress_threads
,
490 int64_t decompress_threads
, Error
**errp
)
492 MigrationState
*s
= migrate_get_current();
494 if (has_compress_level
&& (compress_level
< 0 || compress_level
> 9)) {
495 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "compress_level",
496 "is invalid, it should be in the range of 0 to 9");
499 if (has_compress_threads
&&
500 (compress_threads
< 1 || compress_threads
> 255)) {
501 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
503 "is invalid, it should be in the range of 1 to 255");
506 if (has_decompress_threads
&&
507 (decompress_threads
< 1 || decompress_threads
> 255)) {
508 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
509 "decompress_threads",
510 "is invalid, it should be in the range of 1 to 255");
514 if (has_compress_level
) {
515 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
] = compress_level
;
517 if (has_compress_threads
) {
518 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
] = compress_threads
;
520 if (has_decompress_threads
) {
521 s
->parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
] =
526 /* shared migration helpers */
528 static void migrate_set_state(MigrationState
*s
, int old_state
, int new_state
)
530 if (atomic_cmpxchg(&s
->state
, old_state
, new_state
) == old_state
) {
531 trace_migrate_set_state(new_state
);
532 migrate_generate_event(new_state
);
536 static void migrate_fd_cleanup(void *opaque
)
538 MigrationState
*s
= opaque
;
540 qemu_bh_delete(s
->cleanup_bh
);
541 s
->cleanup_bh
= NULL
;
544 trace_migrate_fd_cleanup();
545 qemu_mutex_unlock_iothread();
546 qemu_thread_join(&s
->thread
);
547 qemu_mutex_lock_iothread();
549 migrate_compress_threads_join();
550 qemu_fclose(s
->file
);
554 assert(s
->state
!= MIGRATION_STATUS_ACTIVE
);
556 if (s
->state
!= MIGRATION_STATUS_COMPLETED
) {
557 qemu_savevm_state_cancel();
558 if (s
->state
== MIGRATION_STATUS_CANCELLING
) {
559 migrate_set_state(s
, MIGRATION_STATUS_CANCELLING
,
560 MIGRATION_STATUS_CANCELLED
);
564 notifier_list_notify(&migration_state_notifiers
, s
);
567 void migrate_fd_error(MigrationState
*s
)
569 trace_migrate_fd_error();
570 assert(s
->file
== NULL
);
571 migrate_set_state(s
, MIGRATION_STATUS_SETUP
, MIGRATION_STATUS_FAILED
);
572 notifier_list_notify(&migration_state_notifiers
, s
);
575 static void migrate_fd_cancel(MigrationState
*s
)
578 QEMUFile
*f
= migrate_get_current()->file
;
579 trace_migrate_fd_cancel();
582 old_state
= s
->state
;
583 if (old_state
!= MIGRATION_STATUS_SETUP
&&
584 old_state
!= MIGRATION_STATUS_ACTIVE
) {
587 migrate_set_state(s
, old_state
, MIGRATION_STATUS_CANCELLING
);
588 } while (s
->state
!= MIGRATION_STATUS_CANCELLING
);
591 * If we're unlucky the migration code might be stuck somewhere in a
592 * send/write while the network has failed and is waiting to timeout;
593 * if we've got shutdown(2) available then we can force it to quit.
594 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
595 * called in a bh, so there is no race against this cancel.
597 if (s
->state
== MIGRATION_STATUS_CANCELLING
&& f
) {
598 qemu_file_shutdown(f
);
602 void add_migration_state_change_notifier(Notifier
*notify
)
604 notifier_list_add(&migration_state_notifiers
, notify
);
607 void remove_migration_state_change_notifier(Notifier
*notify
)
609 notifier_remove(notify
);
612 bool migration_in_setup(MigrationState
*s
)
614 return s
->state
== MIGRATION_STATUS_SETUP
;
617 bool migration_has_finished(MigrationState
*s
)
619 return s
->state
== MIGRATION_STATUS_COMPLETED
;
622 bool migration_has_failed(MigrationState
*s
)
624 return (s
->state
== MIGRATION_STATUS_CANCELLED
||
625 s
->state
== MIGRATION_STATUS_FAILED
);
628 static MigrationState
*migrate_init(const MigrationParams
*params
)
630 MigrationState
*s
= migrate_get_current();
631 int64_t bandwidth_limit
= s
->bandwidth_limit
;
632 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
633 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
634 int compress_level
= s
->parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
];
635 int compress_thread_count
=
636 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
];
637 int decompress_thread_count
=
638 s
->parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
];
640 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
641 sizeof(enabled_capabilities
));
643 memset(s
, 0, sizeof(*s
));
645 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
646 sizeof(enabled_capabilities
));
647 s
->xbzrle_cache_size
= xbzrle_cache_size
;
649 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
] = compress_level
;
650 s
->parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
] =
651 compress_thread_count
;
652 s
->parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
] =
653 decompress_thread_count
;
654 s
->bandwidth_limit
= bandwidth_limit
;
655 migrate_set_state(s
, MIGRATION_STATUS_NONE
, MIGRATION_STATUS_SETUP
);
657 s
->total_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
661 static GSList
*migration_blockers
;
663 void migrate_add_blocker(Error
*reason
)
665 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
668 void migrate_del_blocker(Error
*reason
)
670 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
673 void qmp_migrate_incoming(const char *uri
, Error
**errp
)
675 Error
*local_err
= NULL
;
676 static bool once
= true;
678 if (!deferred_incoming
) {
679 error_setg(errp
, "For use with '-incoming defer'");
683 error_setg(errp
, "The incoming migration has already been started");
686 qemu_start_incoming_migration(uri
, &local_err
);
689 error_propagate(errp
, local_err
);
696 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
697 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
700 Error
*local_err
= NULL
;
701 MigrationState
*s
= migrate_get_current();
702 MigrationParams params
;
705 params
.blk
= has_blk
&& blk
;
706 params
.shared
= has_inc
&& inc
;
708 if (s
->state
== MIGRATION_STATUS_ACTIVE
||
709 s
->state
== MIGRATION_STATUS_SETUP
||
710 s
->state
== MIGRATION_STATUS_CANCELLING
) {
711 error_setg(errp
, QERR_MIGRATION_ACTIVE
);
714 if (runstate_check(RUN_STATE_INMIGRATE
)) {
715 error_setg(errp
, "Guest is waiting for an incoming migration");
719 if (qemu_savevm_state_blocked(errp
)) {
723 if (migration_blockers
) {
724 *errp
= error_copy(migration_blockers
->data
);
728 /* We are starting a new migration, so we want to start in a clean
729 state. This change is only needed if previous migration
730 failed/was cancelled. We don't use migrate_set_state() because
731 we are setting the initial state, not changing it. */
732 s
->state
= MIGRATION_STATUS_NONE
;
734 s
= migrate_init(¶ms
);
736 if (strstart(uri
, "tcp:", &p
)) {
737 tcp_start_outgoing_migration(s
, p
, &local_err
);
739 } else if (strstart(uri
, "rdma:", &p
)) {
740 rdma_start_outgoing_migration(s
, p
, &local_err
);
743 } else if (strstart(uri
, "exec:", &p
)) {
744 exec_start_outgoing_migration(s
, p
, &local_err
);
745 } else if (strstart(uri
, "unix:", &p
)) {
746 unix_start_outgoing_migration(s
, p
, &local_err
);
747 } else if (strstart(uri
, "fd:", &p
)) {
748 fd_start_outgoing_migration(s
, p
, &local_err
);
751 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri",
752 "a valid migration protocol");
753 migrate_set_state(s
, MIGRATION_STATUS_SETUP
, MIGRATION_STATUS_FAILED
);
759 error_propagate(errp
, local_err
);
764 void qmp_migrate_cancel(Error
**errp
)
766 migrate_fd_cancel(migrate_get_current());
769 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
771 MigrationState
*s
= migrate_get_current();
774 /* Check for truncation */
775 if (value
!= (size_t)value
) {
776 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
777 "exceeding address space");
781 /* Cache should not be larger than guest ram size */
782 if (value
> ram_bytes_total()) {
783 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
784 "exceeds guest ram size ");
788 new_size
= xbzrle_cache_resize(value
);
790 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
791 "is smaller than page size");
795 s
->xbzrle_cache_size
= new_size
;
798 int64_t qmp_query_migrate_cache_size(Error
**errp
)
800 return migrate_xbzrle_cache_size();
803 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
810 if (value
> SIZE_MAX
) {
814 s
= migrate_get_current();
815 s
->bandwidth_limit
= value
;
817 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
821 void qmp_migrate_set_downtime(double value
, Error
**errp
)
824 value
= MAX(0, MIN(UINT64_MAX
, value
));
825 max_downtime
= (uint64_t)value
;
828 bool migrate_auto_converge(void)
832 s
= migrate_get_current();
834 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_AUTO_CONVERGE
];
837 bool migrate_zero_blocks(void)
841 s
= migrate_get_current();
843 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_ZERO_BLOCKS
];
846 bool migrate_use_compression(void)
850 s
= migrate_get_current();
852 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_COMPRESS
];
855 int migrate_compress_level(void)
859 s
= migrate_get_current();
861 return s
->parameters
[MIGRATION_PARAMETER_COMPRESS_LEVEL
];
864 int migrate_compress_threads(void)
868 s
= migrate_get_current();
870 return s
->parameters
[MIGRATION_PARAMETER_COMPRESS_THREADS
];
873 int migrate_decompress_threads(void)
877 s
= migrate_get_current();
879 return s
->parameters
[MIGRATION_PARAMETER_DECOMPRESS_THREADS
];
882 bool migrate_use_events(void)
886 s
= migrate_get_current();
888 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_EVENTS
];
891 int migrate_use_xbzrle(void)
895 s
= migrate_get_current();
897 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
900 int64_t migrate_xbzrle_cache_size(void)
904 s
= migrate_get_current();
906 return s
->xbzrle_cache_size
;
909 /* migration thread support */
911 static void *migration_thread(void *opaque
)
913 MigrationState
*s
= opaque
;
914 int64_t initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
915 int64_t setup_start
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
916 int64_t initial_bytes
= 0;
917 int64_t max_size
= 0;
918 int64_t start_time
= initial_time
;
919 bool old_vm_running
= false;
921 rcu_register_thread();
923 qemu_savevm_state_header(s
->file
);
924 qemu_savevm_state_begin(s
->file
, &s
->params
);
926 s
->setup_time
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) - setup_start
;
927 migrate_set_state(s
, MIGRATION_STATUS_SETUP
, MIGRATION_STATUS_ACTIVE
);
929 while (s
->state
== MIGRATION_STATUS_ACTIVE
) {
930 int64_t current_time
;
931 uint64_t pending_size
;
933 if (!qemu_file_rate_limit(s
->file
)) {
934 pending_size
= qemu_savevm_state_pending(s
->file
, max_size
);
935 trace_migrate_pending(pending_size
, max_size
);
936 if (pending_size
&& pending_size
>= max_size
) {
937 qemu_savevm_state_iterate(s
->file
);
941 qemu_mutex_lock_iothread();
942 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
943 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
944 old_vm_running
= runstate_is_running();
946 ret
= global_state_store();
948 ret
= vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
950 qemu_file_set_rate_limit(s
->file
, INT64_MAX
);
951 qemu_savevm_state_complete(s
->file
);
954 qemu_mutex_unlock_iothread();
957 migrate_set_state(s
, MIGRATION_STATUS_ACTIVE
,
958 MIGRATION_STATUS_FAILED
);
962 if (!qemu_file_get_error(s
->file
)) {
963 migrate_set_state(s
, MIGRATION_STATUS_ACTIVE
,
964 MIGRATION_STATUS_COMPLETED
);
970 if (qemu_file_get_error(s
->file
)) {
971 migrate_set_state(s
, MIGRATION_STATUS_ACTIVE
,
972 MIGRATION_STATUS_FAILED
);
975 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
976 if (current_time
>= initial_time
+ BUFFER_DELAY
) {
977 uint64_t transferred_bytes
= qemu_ftell(s
->file
) - initial_bytes
;
978 uint64_t time_spent
= current_time
- initial_time
;
979 double bandwidth
= transferred_bytes
/ time_spent
;
980 max_size
= bandwidth
* migrate_max_downtime() / 1000000;
982 s
->mbps
= time_spent
? (((double) transferred_bytes
* 8.0) /
983 ((double) time_spent
/ 1000.0)) / 1000.0 / 1000.0 : -1;
985 trace_migrate_transferred(transferred_bytes
, time_spent
,
986 bandwidth
, max_size
);
987 /* if we haven't sent anything, we don't want to recalculate
988 10000 is a small enough number for our purposes */
989 if (s
->dirty_bytes_rate
&& transferred_bytes
> 10000) {
990 s
->expected_downtime
= s
->dirty_bytes_rate
/ bandwidth
;
993 qemu_file_reset_rate_limit(s
->file
);
994 initial_time
= current_time
;
995 initial_bytes
= qemu_ftell(s
->file
);
997 if (qemu_file_rate_limit(s
->file
)) {
998 /* usleep expects microseconds */
999 g_usleep((initial_time
+ BUFFER_DELAY
- current_time
)*1000);
1003 qemu_mutex_lock_iothread();
1004 if (s
->state
== MIGRATION_STATUS_COMPLETED
) {
1005 int64_t end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1006 uint64_t transferred_bytes
= qemu_ftell(s
->file
);
1007 s
->total_time
= end_time
- s
->total_time
;
1008 s
->downtime
= end_time
- start_time
;
1009 if (s
->total_time
) {
1010 s
->mbps
= (((double) transferred_bytes
* 8.0) /
1011 ((double) s
->total_time
)) / 1000;
1013 runstate_set(RUN_STATE_POSTMIGRATE
);
1015 if (old_vm_running
) {
1019 qemu_bh_schedule(s
->cleanup_bh
);
1020 qemu_mutex_unlock_iothread();
1022 rcu_unregister_thread();
1026 void migrate_fd_connect(MigrationState
*s
)
1028 /* This is a best 1st approximation. ns to ms */
1029 s
->expected_downtime
= max_downtime
/1000000;
1030 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup
, s
);
1032 qemu_file_set_rate_limit(s
->file
,
1033 s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
1035 /* Notify before starting migration thread */
1036 notifier_list_notify(&migration_state_notifiers
, s
);
1038 migrate_compress_threads_create();
1039 qemu_thread_create(&s
->thread
, "migration", migration_thread
, s
,
1040 QEMU_THREAD_JOINABLE
);