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 "migration/migration.h"
18 #include "monitor/monitor.h"
19 #include "migration/qemu-file.h"
20 #include "sysemu/sysemu.h"
21 #include "block/block.h"
22 #include "qemu/sockets.h"
23 #include "migration/block.h"
24 #include "qemu/thread.h"
25 #include "qmp-commands.h"
28 //#define DEBUG_MIGRATION
30 #ifdef DEBUG_MIGRATION
31 #define DPRINTF(fmt, ...) \
32 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
34 #define DPRINTF(fmt, ...) \
46 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
48 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
50 #define BUFFER_DELAY 100
51 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
53 /* Migration XBZRLE default cache size */
54 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
56 static NotifierList migration_state_notifiers
=
57 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers
);
59 /* When we add fault tolerance, we could have several
60 migrations at once. For now we don't need to add
61 dynamic creation of migration */
63 MigrationState
*migrate_get_current(void)
65 static MigrationState current_migration
= {
66 .state
= MIG_STATE_SETUP
,
67 .bandwidth_limit
= MAX_THROTTLE
,
68 .xbzrle_cache_size
= DEFAULT_MIGRATE_CACHE_SIZE
,
71 return ¤t_migration
;
74 void qemu_start_incoming_migration(const char *uri
, Error
**errp
)
78 if (strstart(uri
, "tcp:", &p
))
79 tcp_start_incoming_migration(p
, errp
);
81 else if (strstart(uri
, "exec:", &p
))
82 exec_start_incoming_migration(p
, errp
);
83 else if (strstart(uri
, "unix:", &p
))
84 unix_start_incoming_migration(p
, errp
);
85 else if (strstart(uri
, "fd:", &p
))
86 fd_start_incoming_migration(p
, errp
);
89 error_setg(errp
, "unknown migration protocol: %s", uri
);
93 static void process_incoming_migration_co(void *opaque
)
98 ret
= qemu_loadvm_state(f
);
101 fprintf(stderr
, "load of migration failed\n");
104 qemu_announce_self();
105 DPRINTF("successfully loaded vm state\n");
107 bdrv_clear_incoming_migration_all();
108 /* Make sure all file formats flush their mutable metadata */
109 bdrv_invalidate_cache_all();
114 runstate_set(RUN_STATE_PAUSED
);
118 void process_incoming_migration(QEMUFile
*f
)
120 Coroutine
*co
= qemu_coroutine_create(process_incoming_migration_co
);
121 int fd
= qemu_get_fd(f
);
124 socket_set_nonblock(fd
);
125 qemu_coroutine_enter(co
, f
);
128 /* amount of nanoseconds we are willing to wait for migration to be down.
129 * the choice of nanoseconds is because it is the maximum resolution that
130 * get_clock() can achieve. It is an internal measure. All user-visible
131 * units must be in seconds */
132 static uint64_t max_downtime
= 30000000;
134 uint64_t migrate_max_downtime(void)
139 MigrationCapabilityStatusList
*qmp_query_migrate_capabilities(Error
**errp
)
141 MigrationCapabilityStatusList
*head
= NULL
;
142 MigrationCapabilityStatusList
*caps
;
143 MigrationState
*s
= migrate_get_current();
146 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
148 head
= g_malloc0(sizeof(*caps
));
151 caps
->next
= g_malloc0(sizeof(*caps
));
155 g_malloc(sizeof(*caps
->value
));
156 caps
->value
->capability
= i
;
157 caps
->value
->state
= s
->enabled_capabilities
[i
];
163 static void get_xbzrle_cache_stats(MigrationInfo
*info
)
165 if (migrate_use_xbzrle()) {
166 info
->has_xbzrle_cache
= true;
167 info
->xbzrle_cache
= g_malloc0(sizeof(*info
->xbzrle_cache
));
168 info
->xbzrle_cache
->cache_size
= migrate_xbzrle_cache_size();
169 info
->xbzrle_cache
->bytes
= xbzrle_mig_bytes_transferred();
170 info
->xbzrle_cache
->pages
= xbzrle_mig_pages_transferred();
171 info
->xbzrle_cache
->cache_miss
= xbzrle_mig_pages_cache_miss();
172 info
->xbzrle_cache
->overflow
= xbzrle_mig_pages_overflow();
176 MigrationInfo
*qmp_query_migrate(Error
**errp
)
178 MigrationInfo
*info
= g_malloc0(sizeof(*info
));
179 MigrationState
*s
= migrate_get_current();
182 case MIG_STATE_SETUP
:
183 /* no migration has happened ever */
185 case MIG_STATE_ACTIVE
:
186 info
->has_status
= true;
187 info
->status
= g_strdup("active");
188 info
->has_total_time
= true;
189 info
->total_time
= qemu_get_clock_ms(rt_clock
)
191 info
->has_expected_downtime
= true;
192 info
->expected_downtime
= s
->expected_downtime
;
194 info
->has_ram
= true;
195 info
->ram
= g_malloc0(sizeof(*info
->ram
));
196 info
->ram
->transferred
= ram_bytes_transferred();
197 info
->ram
->remaining
= ram_bytes_remaining();
198 info
->ram
->total
= ram_bytes_total();
199 info
->ram
->duplicate
= dup_mig_pages_transferred();
200 info
->ram
->skipped
= skipped_mig_pages_transferred();
201 info
->ram
->normal
= norm_mig_pages_transferred();
202 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
203 info
->ram
->dirty_pages_rate
= s
->dirty_pages_rate
;
205 if (blk_mig_active()) {
206 info
->has_disk
= true;
207 info
->disk
= g_malloc0(sizeof(*info
->disk
));
208 info
->disk
->transferred
= blk_mig_bytes_transferred();
209 info
->disk
->remaining
= blk_mig_bytes_remaining();
210 info
->disk
->total
= blk_mig_bytes_total();
213 get_xbzrle_cache_stats(info
);
215 case MIG_STATE_COMPLETED
:
216 get_xbzrle_cache_stats(info
);
218 info
->has_status
= true;
219 info
->status
= g_strdup("completed");
220 info
->total_time
= s
->total_time
;
221 info
->has_downtime
= true;
222 info
->downtime
= s
->downtime
;
224 info
->has_ram
= true;
225 info
->ram
= g_malloc0(sizeof(*info
->ram
));
226 info
->ram
->transferred
= ram_bytes_transferred();
227 info
->ram
->remaining
= 0;
228 info
->ram
->total
= ram_bytes_total();
229 info
->ram
->duplicate
= dup_mig_pages_transferred();
230 info
->ram
->skipped
= skipped_mig_pages_transferred();
231 info
->ram
->normal
= norm_mig_pages_transferred();
232 info
->ram
->normal_bytes
= norm_mig_bytes_transferred();
234 case MIG_STATE_ERROR
:
235 info
->has_status
= true;
236 info
->status
= g_strdup("failed");
238 case MIG_STATE_CANCELLED
:
239 info
->has_status
= true;
240 info
->status
= g_strdup("cancelled");
247 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList
*params
,
250 MigrationState
*s
= migrate_get_current();
251 MigrationCapabilityStatusList
*cap
;
253 if (s
->state
== MIG_STATE_ACTIVE
) {
254 error_set(errp
, QERR_MIGRATION_ACTIVE
);
258 for (cap
= params
; cap
; cap
= cap
->next
) {
259 s
->enabled_capabilities
[cap
->value
->capability
] = cap
->value
->state
;
263 /* shared migration helpers */
265 static void migrate_fd_cleanup(void *opaque
)
267 MigrationState
*s
= opaque
;
269 qemu_bh_delete(s
->cleanup_bh
);
270 s
->cleanup_bh
= NULL
;
273 DPRINTF("closing file\n");
274 qemu_mutex_unlock_iothread();
275 qemu_thread_join(&s
->thread
);
276 qemu_mutex_lock_iothread();
278 qemu_fclose(s
->file
);
282 assert(s
->state
!= MIG_STATE_ACTIVE
);
284 if (s
->state
!= MIG_STATE_COMPLETED
) {
285 qemu_savevm_state_cancel();
288 notifier_list_notify(&migration_state_notifiers
, s
);
291 static void migrate_finish_set_state(MigrationState
*s
, int new_state
)
293 if (__sync_val_compare_and_swap(&s
->state
, MIG_STATE_ACTIVE
,
294 new_state
) == new_state
) {
295 trace_migrate_set_state(new_state
);
299 void migrate_fd_error(MigrationState
*s
)
301 DPRINTF("setting error state\n");
302 assert(s
->file
== NULL
);
303 s
->state
= MIG_STATE_ERROR
;
304 trace_migrate_set_state(MIG_STATE_ERROR
);
305 notifier_list_notify(&migration_state_notifiers
, s
);
308 static void migrate_fd_cancel(MigrationState
*s
)
310 DPRINTF("cancelling migration\n");
312 migrate_finish_set_state(s
, MIG_STATE_CANCELLED
);
315 void add_migration_state_change_notifier(Notifier
*notify
)
317 notifier_list_add(&migration_state_notifiers
, notify
);
320 void remove_migration_state_change_notifier(Notifier
*notify
)
322 notifier_remove(notify
);
325 bool migration_is_active(MigrationState
*s
)
327 return s
->state
== MIG_STATE_ACTIVE
;
330 bool migration_has_finished(MigrationState
*s
)
332 return s
->state
== MIG_STATE_COMPLETED
;
335 bool migration_has_failed(MigrationState
*s
)
337 return (s
->state
== MIG_STATE_CANCELLED
||
338 s
->state
== MIG_STATE_ERROR
);
341 static MigrationState
*migrate_init(const MigrationParams
*params
)
343 MigrationState
*s
= migrate_get_current();
344 int64_t bandwidth_limit
= s
->bandwidth_limit
;
345 bool enabled_capabilities
[MIGRATION_CAPABILITY_MAX
];
346 int64_t xbzrle_cache_size
= s
->xbzrle_cache_size
;
348 memcpy(enabled_capabilities
, s
->enabled_capabilities
,
349 sizeof(enabled_capabilities
));
351 memset(s
, 0, sizeof(*s
));
352 s
->bandwidth_limit
= bandwidth_limit
;
354 memcpy(s
->enabled_capabilities
, enabled_capabilities
,
355 sizeof(enabled_capabilities
));
356 s
->xbzrle_cache_size
= xbzrle_cache_size
;
358 s
->bandwidth_limit
= bandwidth_limit
;
359 s
->state
= MIG_STATE_SETUP
;
360 trace_migrate_set_state(MIG_STATE_SETUP
);
362 s
->total_time
= qemu_get_clock_ms(rt_clock
);
366 static GSList
*migration_blockers
;
368 void migrate_add_blocker(Error
*reason
)
370 migration_blockers
= g_slist_prepend(migration_blockers
, reason
);
373 void migrate_del_blocker(Error
*reason
)
375 migration_blockers
= g_slist_remove(migration_blockers
, reason
);
378 void qmp_migrate(const char *uri
, bool has_blk
, bool blk
,
379 bool has_inc
, bool inc
, bool has_detach
, bool detach
,
382 Error
*local_err
= NULL
;
383 MigrationState
*s
= migrate_get_current();
384 MigrationParams params
;
390 if (s
->state
== MIG_STATE_ACTIVE
) {
391 error_set(errp
, QERR_MIGRATION_ACTIVE
);
395 if (qemu_savevm_state_blocked(errp
)) {
399 if (migration_blockers
) {
400 *errp
= error_copy(migration_blockers
->data
);
404 s
= migrate_init(¶ms
);
406 if (strstart(uri
, "tcp:", &p
)) {
407 tcp_start_outgoing_migration(s
, p
, &local_err
);
409 } else if (strstart(uri
, "exec:", &p
)) {
410 exec_start_outgoing_migration(s
, p
, &local_err
);
411 } else if (strstart(uri
, "unix:", &p
)) {
412 unix_start_outgoing_migration(s
, p
, &local_err
);
413 } else if (strstart(uri
, "fd:", &p
)) {
414 fd_start_outgoing_migration(s
, p
, &local_err
);
417 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "uri", "a valid migration protocol");
423 error_propagate(errp
, local_err
);
428 void qmp_migrate_cancel(Error
**errp
)
430 migrate_fd_cancel(migrate_get_current());
433 void qmp_migrate_set_cache_size(int64_t value
, Error
**errp
)
435 MigrationState
*s
= migrate_get_current();
437 /* Check for truncation */
438 if (value
!= (size_t)value
) {
439 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
440 "exceeding address space");
444 s
->xbzrle_cache_size
= xbzrle_cache_resize(value
);
447 int64_t qmp_query_migrate_cache_size(Error
**errp
)
449 return migrate_xbzrle_cache_size();
452 void qmp_migrate_set_speed(int64_t value
, Error
**errp
)
459 if (value
> SIZE_MAX
) {
463 s
= migrate_get_current();
464 s
->bandwidth_limit
= value
;
466 qemu_file_set_rate_limit(s
->file
, s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
470 void qmp_migrate_set_downtime(double value
, Error
**errp
)
473 value
= MAX(0, MIN(UINT64_MAX
, value
));
474 max_downtime
= (uint64_t)value
;
477 int migrate_use_xbzrle(void)
481 s
= migrate_get_current();
483 return s
->enabled_capabilities
[MIGRATION_CAPABILITY_XBZRLE
];
486 int64_t migrate_xbzrle_cache_size(void)
490 s
= migrate_get_current();
492 return s
->xbzrle_cache_size
;
495 /* migration thread support */
497 static void *migration_thread(void *opaque
)
499 MigrationState
*s
= opaque
;
500 int64_t initial_time
= qemu_get_clock_ms(rt_clock
);
501 int64_t sleep_time
= 0;
502 int64_t initial_bytes
= 0;
503 int64_t max_size
= 0;
504 int64_t start_time
= initial_time
;
505 bool old_vm_running
= false;
507 DPRINTF("beginning savevm\n");
508 qemu_savevm_state_begin(s
->file
, &s
->params
);
510 while (s
->state
== MIG_STATE_ACTIVE
) {
511 int64_t current_time
;
512 uint64_t pending_size
;
514 if (!qemu_file_rate_limit(s
->file
)) {
515 DPRINTF("iterate\n");
516 pending_size
= qemu_savevm_state_pending(s
->file
, max_size
);
517 DPRINTF("pending size %lu max %lu\n", pending_size
, max_size
);
518 if (pending_size
&& pending_size
>= max_size
) {
519 qemu_savevm_state_iterate(s
->file
);
521 DPRINTF("done iterating\n");
522 qemu_mutex_lock_iothread();
523 start_time
= qemu_get_clock_ms(rt_clock
);
524 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
525 old_vm_running
= runstate_is_running();
526 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE
);
527 qemu_file_set_rate_limit(s
->file
, INT_MAX
);
528 qemu_savevm_state_complete(s
->file
);
529 qemu_mutex_unlock_iothread();
530 if (!qemu_file_get_error(s
->file
)) {
531 migrate_finish_set_state(s
, MIG_STATE_COMPLETED
);
537 if (qemu_file_get_error(s
->file
)) {
538 migrate_finish_set_state(s
, MIG_STATE_ERROR
);
541 current_time
= qemu_get_clock_ms(rt_clock
);
542 if (current_time
>= initial_time
+ BUFFER_DELAY
) {
543 uint64_t transferred_bytes
= qemu_ftell(s
->file
) - initial_bytes
;
544 uint64_t time_spent
= current_time
- initial_time
- sleep_time
;
545 double bandwidth
= transferred_bytes
/ time_spent
;
546 max_size
= bandwidth
* migrate_max_downtime() / 1000000;
548 DPRINTF("transferred %" PRIu64
" time_spent %" PRIu64
549 " bandwidth %g max_size %" PRId64
"\n",
550 transferred_bytes
, time_spent
, bandwidth
, max_size
);
551 /* if we haven't sent anything, we don't want to recalculate
552 10000 is a small enough number for our purposes */
553 if (s
->dirty_bytes_rate
&& transferred_bytes
> 10000) {
554 s
->expected_downtime
= s
->dirty_bytes_rate
/ bandwidth
;
557 qemu_file_reset_rate_limit(s
->file
);
559 initial_time
= current_time
;
560 initial_bytes
= qemu_ftell(s
->file
);
562 if (qemu_file_rate_limit(s
->file
)) {
563 /* usleep expects microseconds */
564 g_usleep((initial_time
+ BUFFER_DELAY
- current_time
)*1000);
565 sleep_time
+= qemu_get_clock_ms(rt_clock
) - current_time
;
569 qemu_mutex_lock_iothread();
570 if (s
->state
== MIG_STATE_COMPLETED
) {
571 int64_t end_time
= qemu_get_clock_ms(rt_clock
);
572 s
->total_time
= end_time
- s
->total_time
;
573 s
->downtime
= end_time
- start_time
;
574 runstate_set(RUN_STATE_POSTMIGRATE
);
576 if (old_vm_running
) {
580 qemu_bh_schedule(s
->cleanup_bh
);
581 qemu_mutex_unlock_iothread();
586 void migrate_fd_connect(MigrationState
*s
)
588 s
->state
= MIG_STATE_ACTIVE
;
589 trace_migrate_set_state(MIG_STATE_ACTIVE
);
591 /* This is a best 1st approximation. ns to ms */
592 s
->expected_downtime
= max_downtime
/1000000;
593 s
->cleanup_bh
= qemu_bh_new(migrate_fd_cleanup
, s
);
595 qemu_file_set_rate_limit(s
->file
,
596 s
->bandwidth_limit
/ XFER_LIMIT_RATIO
);
598 qemu_thread_create(&s
->thread
, migration_thread
, s
,
599 QEMU_THREAD_JOINABLE
);
600 notifier_list_notify(&migration_state_notifiers
, s
);