migration: Add dirty_pages_rate to query migrate output
[qemu/ar7.git] / migration.c
blob330c16a97f33bdf6c2c064dd67ad4b353718971f
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 "migration.h"
18 #include "monitor.h"
19 #include "buffered_file.h"
20 #include "sysemu.h"
21 #include "block.h"
22 #include "qemu_socket.h"
23 #include "block-migration.h"
24 #include "qmp-commands.h"
26 //#define DEBUG_MIGRATION
28 #ifdef DEBUG_MIGRATION
29 #define DPRINTF(fmt, ...) \
30 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
31 #else
32 #define DPRINTF(fmt, ...) \
33 do { } while (0)
34 #endif
36 enum {
37 MIG_STATE_ERROR,
38 MIG_STATE_SETUP,
39 MIG_STATE_CANCELLED,
40 MIG_STATE_ACTIVE,
41 MIG_STATE_COMPLETED,
44 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
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 /* 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 = MIG_STATE_SETUP,
60 .bandwidth_limit = MAX_THROTTLE,
61 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
64 return &current_migration;
67 int qemu_start_incoming_migration(const char *uri, Error **errp)
69 const char *p;
70 int ret;
72 if (strstart(uri, "tcp:", &p))
73 ret = tcp_start_incoming_migration(p, errp);
74 #if !defined(WIN32)
75 else if (strstart(uri, "exec:", &p))
76 ret = exec_start_incoming_migration(p);
77 else if (strstart(uri, "unix:", &p))
78 ret = unix_start_incoming_migration(p);
79 else if (strstart(uri, "fd:", &p))
80 ret = fd_start_incoming_migration(p);
81 #endif
82 else {
83 fprintf(stderr, "unknown migration protocol: %s\n", uri);
84 ret = -EPROTONOSUPPORT;
86 return ret;
89 void process_incoming_migration(QEMUFile *f)
91 if (qemu_loadvm_state(f) < 0) {
92 fprintf(stderr, "load of migration failed\n");
93 exit(0);
95 qemu_announce_self();
96 DPRINTF("successfully loaded vm state\n");
98 bdrv_clear_incoming_migration_all();
99 /* Make sure all file formats flush their mutable metadata */
100 bdrv_invalidate_cache_all();
102 if (autostart) {
103 vm_start();
104 } else {
105 runstate_set(RUN_STATE_PRELAUNCH);
109 /* amount of nanoseconds we are willing to wait for migration to be down.
110 * the choice of nanoseconds is because it is the maximum resolution that
111 * get_clock() can achieve. It is an internal measure. All user-visible
112 * units must be in seconds */
113 static uint64_t max_downtime = 30000000;
115 uint64_t migrate_max_downtime(void)
117 return max_downtime;
120 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
122 MigrationCapabilityStatusList *head = NULL;
123 MigrationCapabilityStatusList *caps;
124 MigrationState *s = migrate_get_current();
125 int i;
127 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
128 if (head == NULL) {
129 head = g_malloc0(sizeof(*caps));
130 caps = head;
131 } else {
132 caps->next = g_malloc0(sizeof(*caps));
133 caps = caps->next;
135 caps->value =
136 g_malloc(sizeof(*caps->value));
137 caps->value->capability = i;
138 caps->value->state = s->enabled_capabilities[i];
141 return head;
144 static void get_xbzrle_cache_stats(MigrationInfo *info)
146 if (migrate_use_xbzrle()) {
147 info->has_xbzrle_cache = true;
148 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
149 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
150 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
151 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
152 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
153 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
157 MigrationInfo *qmp_query_migrate(Error **errp)
159 MigrationInfo *info = g_malloc0(sizeof(*info));
160 MigrationState *s = migrate_get_current();
162 switch (s->state) {
163 case MIG_STATE_SETUP:
164 /* no migration has happened ever */
165 break;
166 case MIG_STATE_ACTIVE:
167 info->has_status = true;
168 info->status = g_strdup("active");
169 info->has_total_time = true;
170 info->total_time = qemu_get_clock_ms(rt_clock)
171 - s->total_time;
172 info->has_expected_downtime = true;
173 info->expected_downtime = s->expected_downtime;
175 info->has_ram = true;
176 info->ram = g_malloc0(sizeof(*info->ram));
177 info->ram->transferred = ram_bytes_transferred();
178 info->ram->remaining = ram_bytes_remaining();
179 info->ram->total = ram_bytes_total();
180 info->ram->duplicate = dup_mig_pages_transferred();
181 info->ram->normal = norm_mig_pages_transferred();
182 info->ram->normal_bytes = norm_mig_bytes_transferred();
183 info->ram->dirty_pages_rate = s->dirty_pages_rate;
186 if (blk_mig_active()) {
187 info->has_disk = true;
188 info->disk = g_malloc0(sizeof(*info->disk));
189 info->disk->transferred = blk_mig_bytes_transferred();
190 info->disk->remaining = blk_mig_bytes_remaining();
191 info->disk->total = blk_mig_bytes_total();
194 get_xbzrle_cache_stats(info);
195 break;
196 case MIG_STATE_COMPLETED:
197 get_xbzrle_cache_stats(info);
199 info->has_status = true;
200 info->status = g_strdup("completed");
201 info->total_time = s->total_time;
202 info->has_downtime = true;
203 info->downtime = s->downtime;
205 info->has_ram = true;
206 info->ram = g_malloc0(sizeof(*info->ram));
207 info->ram->transferred = ram_bytes_transferred();
208 info->ram->remaining = 0;
209 info->ram->total = ram_bytes_total();
210 info->ram->duplicate = dup_mig_pages_transferred();
211 info->ram->normal = norm_mig_pages_transferred();
212 info->ram->normal_bytes = norm_mig_bytes_transferred();
213 break;
214 case MIG_STATE_ERROR:
215 info->has_status = true;
216 info->status = g_strdup("failed");
217 break;
218 case MIG_STATE_CANCELLED:
219 info->has_status = true;
220 info->status = g_strdup("cancelled");
221 break;
224 return info;
227 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
228 Error **errp)
230 MigrationState *s = migrate_get_current();
231 MigrationCapabilityStatusList *cap;
233 if (s->state == MIG_STATE_ACTIVE) {
234 error_set(errp, QERR_MIGRATION_ACTIVE);
235 return;
238 for (cap = params; cap; cap = cap->next) {
239 s->enabled_capabilities[cap->value->capability] = cap->value->state;
243 /* shared migration helpers */
245 static int migrate_fd_cleanup(MigrationState *s)
247 int ret = 0;
249 if (s->fd != -1) {
250 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
253 if (s->file) {
254 DPRINTF("closing file\n");
255 ret = qemu_fclose(s->file);
256 s->file = NULL;
259 if (s->fd != -1) {
260 close(s->fd);
261 s->fd = -1;
264 return ret;
267 void migrate_fd_error(MigrationState *s)
269 DPRINTF("setting error state\n");
270 s->state = MIG_STATE_ERROR;
271 notifier_list_notify(&migration_state_notifiers, s);
272 migrate_fd_cleanup(s);
275 static void migrate_fd_completed(MigrationState *s)
277 DPRINTF("setting completed state\n");
278 if (migrate_fd_cleanup(s) < 0) {
279 s->state = MIG_STATE_ERROR;
280 } else {
281 s->state = MIG_STATE_COMPLETED;
282 runstate_set(RUN_STATE_POSTMIGRATE);
284 notifier_list_notify(&migration_state_notifiers, s);
287 static void migrate_fd_put_notify(void *opaque)
289 MigrationState *s = opaque;
291 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
292 qemu_file_put_notify(s->file);
293 if (s->file && qemu_file_get_error(s->file)) {
294 migrate_fd_error(s);
298 static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
299 size_t size)
301 MigrationState *s = opaque;
302 ssize_t ret;
304 if (s->state != MIG_STATE_ACTIVE) {
305 return -EIO;
308 do {
309 ret = s->write(s, data, size);
310 } while (ret == -1 && ((s->get_error(s)) == EINTR));
312 if (ret == -1)
313 ret = -(s->get_error(s));
315 if (ret == -EAGAIN) {
316 qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
319 return ret;
322 static void migrate_fd_put_ready(void *opaque)
324 MigrationState *s = opaque;
325 int ret;
327 if (s->state != MIG_STATE_ACTIVE) {
328 DPRINTF("put_ready returning because of non-active state\n");
329 return;
332 DPRINTF("iterate\n");
333 ret = qemu_savevm_state_iterate(s->file);
334 if (ret < 0) {
335 migrate_fd_error(s);
336 } else if (ret == 1) {
337 int old_vm_running = runstate_is_running();
338 int64_t start_time, end_time;
340 DPRINTF("done iterating\n");
341 start_time = qemu_get_clock_ms(rt_clock);
342 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
343 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
345 if (qemu_savevm_state_complete(s->file) < 0) {
346 migrate_fd_error(s);
347 } else {
348 migrate_fd_completed(s);
350 end_time = qemu_get_clock_ms(rt_clock);
351 s->total_time = end_time - s->total_time;
352 s->downtime = end_time - start_time;
353 if (s->state != MIG_STATE_COMPLETED) {
354 if (old_vm_running) {
355 vm_start();
361 static void migrate_fd_cancel(MigrationState *s)
363 if (s->state != MIG_STATE_ACTIVE)
364 return;
366 DPRINTF("cancelling migration\n");
368 s->state = MIG_STATE_CANCELLED;
369 notifier_list_notify(&migration_state_notifiers, s);
370 qemu_savevm_state_cancel(s->file);
372 migrate_fd_cleanup(s);
375 static void migrate_fd_wait_for_unfreeze(void *opaque)
377 MigrationState *s = opaque;
378 int ret;
380 DPRINTF("wait for unfreeze\n");
381 if (s->state != MIG_STATE_ACTIVE)
382 return;
384 do {
385 fd_set wfds;
387 FD_ZERO(&wfds);
388 FD_SET(s->fd, &wfds);
390 ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
391 } while (ret == -1 && (s->get_error(s)) == EINTR);
393 if (ret == -1) {
394 qemu_file_set_error(s->file, -s->get_error(s));
398 static int migrate_fd_close(void *opaque)
400 MigrationState *s = opaque;
402 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
403 return s->close(s);
406 void add_migration_state_change_notifier(Notifier *notify)
408 notifier_list_add(&migration_state_notifiers, notify);
411 void remove_migration_state_change_notifier(Notifier *notify)
413 notifier_remove(notify);
416 bool migration_is_active(MigrationState *s)
418 return s->state == MIG_STATE_ACTIVE;
421 bool migration_has_finished(MigrationState *s)
423 return s->state == MIG_STATE_COMPLETED;
426 bool migration_has_failed(MigrationState *s)
428 return (s->state == MIG_STATE_CANCELLED ||
429 s->state == MIG_STATE_ERROR);
432 void migrate_fd_connect(MigrationState *s)
434 int ret;
436 s->state = MIG_STATE_ACTIVE;
437 s->file = qemu_fopen_ops_buffered(s,
438 s->bandwidth_limit,
439 migrate_fd_put_buffer,
440 migrate_fd_put_ready,
441 migrate_fd_wait_for_unfreeze,
442 migrate_fd_close);
444 DPRINTF("beginning savevm\n");
445 ret = qemu_savevm_state_begin(s->file, &s->params);
446 if (ret < 0) {
447 DPRINTF("failed, %d\n", ret);
448 migrate_fd_error(s);
449 return;
451 migrate_fd_put_ready(s);
454 static MigrationState *migrate_init(const MigrationParams *params)
456 MigrationState *s = migrate_get_current();
457 int64_t bandwidth_limit = s->bandwidth_limit;
458 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
459 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
461 memcpy(enabled_capabilities, s->enabled_capabilities,
462 sizeof(enabled_capabilities));
464 memset(s, 0, sizeof(*s));
465 s->bandwidth_limit = bandwidth_limit;
466 s->params = *params;
467 memcpy(s->enabled_capabilities, enabled_capabilities,
468 sizeof(enabled_capabilities));
469 s->xbzrle_cache_size = xbzrle_cache_size;
471 s->bandwidth_limit = bandwidth_limit;
472 s->state = MIG_STATE_SETUP;
473 s->total_time = qemu_get_clock_ms(rt_clock);
475 return s;
478 static GSList *migration_blockers;
480 void migrate_add_blocker(Error *reason)
482 migration_blockers = g_slist_prepend(migration_blockers, reason);
485 void migrate_del_blocker(Error *reason)
487 migration_blockers = g_slist_remove(migration_blockers, reason);
490 void qmp_migrate(const char *uri, bool has_blk, bool blk,
491 bool has_inc, bool inc, bool has_detach, bool detach,
492 Error **errp)
494 MigrationState *s = migrate_get_current();
495 MigrationParams params;
496 const char *p;
497 int ret;
499 params.blk = blk;
500 params.shared = inc;
502 if (s->state == MIG_STATE_ACTIVE) {
503 error_set(errp, QERR_MIGRATION_ACTIVE);
504 return;
507 if (qemu_savevm_state_blocked(errp)) {
508 return;
511 if (migration_blockers) {
512 *errp = error_copy(migration_blockers->data);
513 return;
516 s = migrate_init(&params);
518 if (strstart(uri, "tcp:", &p)) {
519 ret = tcp_start_outgoing_migration(s, p, errp);
520 #if !defined(WIN32)
521 } else if (strstart(uri, "exec:", &p)) {
522 ret = exec_start_outgoing_migration(s, p);
523 } else if (strstart(uri, "unix:", &p)) {
524 ret = unix_start_outgoing_migration(s, p);
525 } else if (strstart(uri, "fd:", &p)) {
526 ret = fd_start_outgoing_migration(s, p);
527 #endif
528 } else {
529 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
530 return;
533 if (ret < 0) {
534 if (!error_is_set(errp)) {
535 DPRINTF("migration failed: %s\n", strerror(-ret));
536 /* FIXME: we should return meaningful errors */
537 error_set(errp, QERR_UNDEFINED_ERROR);
539 return;
542 notifier_list_notify(&migration_state_notifiers, s);
545 void qmp_migrate_cancel(Error **errp)
547 migrate_fd_cancel(migrate_get_current());
550 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
552 MigrationState *s = migrate_get_current();
554 /* Check for truncation */
555 if (value != (size_t)value) {
556 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
557 "exceeding address space");
558 return;
561 s->xbzrle_cache_size = xbzrle_cache_resize(value);
564 int64_t qmp_query_migrate_cache_size(Error **errp)
566 return migrate_xbzrle_cache_size();
569 void qmp_migrate_set_speed(int64_t value, Error **errp)
571 MigrationState *s;
573 if (value < 0) {
574 value = 0;
577 s = migrate_get_current();
578 s->bandwidth_limit = value;
579 qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
582 void qmp_migrate_set_downtime(double value, Error **errp)
584 value *= 1e9;
585 value = MAX(0, MIN(UINT64_MAX, value));
586 max_downtime = (uint64_t)value;
589 int migrate_use_xbzrle(void)
591 MigrationState *s;
593 s = migrate_get_current();
595 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
598 int64_t migrate_xbzrle_cache_size(void)
600 MigrationState *s;
602 s = migrate_get_current();
604 return s->xbzrle_cache_size;