]> git.proxmox.com Git - mirror_qemu.git/blob - migration.c
Count used RAMBlock pages for migration_dirty_pages
[mirror_qemu.git] / migration.c
1 /*
2 * QEMU live migration
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
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.
14 */
15
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"
28
29 enum {
30 MIG_STATE_ERROR = -1,
31 MIG_STATE_NONE,
32 MIG_STATE_SETUP,
33 MIG_STATE_CANCELLING,
34 MIG_STATE_CANCELLED,
35 MIG_STATE_ACTIVE,
36 MIG_STATE_COMPLETED,
37 };
38
39 #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
40
41 /* Amount of time to allocate to each "chunk" of bandwidth-throttled
42 * data. */
43 #define BUFFER_DELAY 100
44 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
45
46 /* Migration XBZRLE default cache size */
47 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
48
49 static NotifierList migration_state_notifiers =
50 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
51
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 */
55
56 MigrationState *migrate_get_current(void)
57 {
58 static MigrationState current_migration = {
59 .state = MIG_STATE_NONE,
60 .bandwidth_limit = MAX_THROTTLE,
61 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
62 .mbps = -1,
63 };
64
65 return &current_migration;
66 }
67
68 void qemu_start_incoming_migration(const char *uri, Error **errp)
69 {
70 const char *p;
71
72 if (strstart(uri, "tcp:", &p))
73 tcp_start_incoming_migration(p, errp);
74 #ifdef CONFIG_RDMA
75 else if (strstart(uri, "rdma:", &p))
76 rdma_start_incoming_migration(p, errp);
77 #endif
78 #if !defined(WIN32)
79 else if (strstart(uri, "exec:", &p))
80 exec_start_incoming_migration(p, errp);
81 else if (strstart(uri, "unix:", &p))
82 unix_start_incoming_migration(p, errp);
83 else if (strstart(uri, "fd:", &p))
84 fd_start_incoming_migration(p, errp);
85 #endif
86 else {
87 error_setg(errp, "unknown migration protocol: %s", uri);
88 }
89 }
90
91 static void process_incoming_migration_co(void *opaque)
92 {
93 QEMUFile *f = opaque;
94 Error *local_err = NULL;
95 int ret;
96
97 ret = qemu_loadvm_state(f);
98 qemu_fclose(f);
99 free_xbzrle_decoded_buf();
100 if (ret < 0) {
101 fprintf(stderr, "load of migration failed\n");
102 exit(EXIT_FAILURE);
103 }
104 qemu_announce_self();
105
106 bdrv_clear_incoming_migration_all();
107 /* Make sure all file formats flush their mutable metadata */
108 bdrv_invalidate_cache_all(&local_err);
109 if (local_err) {
110 qerror_report_err(local_err);
111 error_free(local_err);
112 exit(EXIT_FAILURE);
113 }
114
115 if (autostart) {
116 vm_start();
117 } else {
118 runstate_set(RUN_STATE_PAUSED);
119 }
120 }
121
122 void process_incoming_migration(QEMUFile *f)
123 {
124 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
125 int fd = qemu_get_fd(f);
126
127 assert(fd != -1);
128 qemu_set_nonblock(fd);
129 qemu_coroutine_enter(co, f);
130 }
131
132 /* amount of nanoseconds we are willing to wait for migration to be down.
133 * the choice of nanoseconds is because it is the maximum resolution that
134 * get_clock() can achieve. It is an internal measure. All user-visible
135 * units must be in seconds */
136 static uint64_t max_downtime = 30000000;
137
138 uint64_t migrate_max_downtime(void)
139 {
140 return max_downtime;
141 }
142
143 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
144 {
145 MigrationCapabilityStatusList *head = NULL;
146 MigrationCapabilityStatusList *caps;
147 MigrationState *s = migrate_get_current();
148 int i;
149
150 caps = NULL; /* silence compiler warning */
151 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
152 if (head == NULL) {
153 head = g_malloc0(sizeof(*caps));
154 caps = head;
155 } else {
156 caps->next = g_malloc0(sizeof(*caps));
157 caps = caps->next;
158 }
159 caps->value =
160 g_malloc(sizeof(*caps->value));
161 caps->value->capability = i;
162 caps->value->state = s->enabled_capabilities[i];
163 }
164
165 return head;
166 }
167
168 static void get_xbzrle_cache_stats(MigrationInfo *info)
169 {
170 if (migrate_use_xbzrle()) {
171 info->has_xbzrle_cache = true;
172 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
173 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
174 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
175 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
176 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
177 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
178 }
179 }
180
181 MigrationInfo *qmp_query_migrate(Error **errp)
182 {
183 MigrationInfo *info = g_malloc0(sizeof(*info));
184 MigrationState *s = migrate_get_current();
185
186 switch (s->state) {
187 case MIG_STATE_NONE:
188 /* no migration has happened ever */
189 break;
190 case MIG_STATE_SETUP:
191 info->has_status = true;
192 info->status = g_strdup("setup");
193 info->has_total_time = false;
194 break;
195 case MIG_STATE_ACTIVE:
196 case MIG_STATE_CANCELLING:
197 info->has_status = true;
198 info->status = g_strdup("active");
199 info->has_total_time = true;
200 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
201 - s->total_time;
202 info->has_expected_downtime = true;
203 info->expected_downtime = s->expected_downtime;
204 info->has_setup_time = true;
205 info->setup_time = s->setup_time;
206
207 info->has_ram = true;
208 info->ram = g_malloc0(sizeof(*info->ram));
209 info->ram->transferred = ram_bytes_transferred();
210 info->ram->remaining = ram_bytes_remaining();
211 info->ram->total = ram_bytes_total();
212 info->ram->duplicate = dup_mig_pages_transferred();
213 info->ram->skipped = skipped_mig_pages_transferred();
214 info->ram->normal = norm_mig_pages_transferred();
215 info->ram->normal_bytes = norm_mig_bytes_transferred();
216 info->ram->dirty_pages_rate = s->dirty_pages_rate;
217 info->ram->mbps = s->mbps;
218
219 if (blk_mig_active()) {
220 info->has_disk = true;
221 info->disk = g_malloc0(sizeof(*info->disk));
222 info->disk->transferred = blk_mig_bytes_transferred();
223 info->disk->remaining = blk_mig_bytes_remaining();
224 info->disk->total = blk_mig_bytes_total();
225 }
226
227 get_xbzrle_cache_stats(info);
228 break;
229 case MIG_STATE_COMPLETED:
230 get_xbzrle_cache_stats(info);
231
232 info->has_status = true;
233 info->status = g_strdup("completed");
234 info->has_total_time = true;
235 info->total_time = s->total_time;
236 info->has_downtime = true;
237 info->downtime = s->downtime;
238 info->has_setup_time = true;
239 info->setup_time = s->setup_time;
240
241 info->has_ram = true;
242 info->ram = g_malloc0(sizeof(*info->ram));
243 info->ram->transferred = ram_bytes_transferred();
244 info->ram->remaining = 0;
245 info->ram->total = ram_bytes_total();
246 info->ram->duplicate = dup_mig_pages_transferred();
247 info->ram->skipped = skipped_mig_pages_transferred();
248 info->ram->normal = norm_mig_pages_transferred();
249 info->ram->normal_bytes = norm_mig_bytes_transferred();
250 info->ram->mbps = s->mbps;
251 break;
252 case MIG_STATE_ERROR:
253 info->has_status = true;
254 info->status = g_strdup("failed");
255 break;
256 case MIG_STATE_CANCELLED:
257 info->has_status = true;
258 info->status = g_strdup("cancelled");
259 break;
260 }
261
262 return info;
263 }
264
265 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
266 Error **errp)
267 {
268 MigrationState *s = migrate_get_current();
269 MigrationCapabilityStatusList *cap;
270
271 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
272 error_set(errp, QERR_MIGRATION_ACTIVE);
273 return;
274 }
275
276 for (cap = params; cap; cap = cap->next) {
277 s->enabled_capabilities[cap->value->capability] = cap->value->state;
278 }
279 }
280
281 /* shared migration helpers */
282
283 static void migrate_set_state(MigrationState *s, int old_state, int new_state)
284 {
285 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
286 trace_migrate_set_state(new_state);
287 }
288 }
289
290 static void migrate_fd_cleanup(void *opaque)
291 {
292 MigrationState *s = opaque;
293
294 qemu_bh_delete(s->cleanup_bh);
295 s->cleanup_bh = NULL;
296
297 if (s->file) {
298 trace_migrate_fd_cleanup();
299 qemu_mutex_unlock_iothread();
300 qemu_thread_join(&s->thread);
301 qemu_mutex_lock_iothread();
302
303 qemu_fclose(s->file);
304 s->file = NULL;
305 }
306
307 assert(s->state != MIG_STATE_ACTIVE);
308
309 if (s->state != MIG_STATE_COMPLETED) {
310 qemu_savevm_state_cancel();
311 if (s->state == MIG_STATE_CANCELLING) {
312 migrate_set_state(s, MIG_STATE_CANCELLING, MIG_STATE_CANCELLED);
313 }
314 }
315
316 notifier_list_notify(&migration_state_notifiers, s);
317 }
318
319 void migrate_fd_error(MigrationState *s)
320 {
321 trace_migrate_fd_error();
322 assert(s->file == NULL);
323 s->state = MIG_STATE_ERROR;
324 trace_migrate_set_state(MIG_STATE_ERROR);
325 notifier_list_notify(&migration_state_notifiers, s);
326 }
327
328 static void migrate_fd_cancel(MigrationState *s)
329 {
330 int old_state ;
331 trace_migrate_fd_cancel();
332
333 do {
334 old_state = s->state;
335 if (old_state != MIG_STATE_SETUP && old_state != MIG_STATE_ACTIVE) {
336 break;
337 }
338 migrate_set_state(s, old_state, MIG_STATE_CANCELLING);
339 } while (s->state != MIG_STATE_CANCELLING);
340 }
341
342 void add_migration_state_change_notifier(Notifier *notify)
343 {
344 notifier_list_add(&migration_state_notifiers, notify);
345 }
346
347 void remove_migration_state_change_notifier(Notifier *notify)
348 {
349 notifier_remove(notify);
350 }
351
352 bool migration_in_setup(MigrationState *s)
353 {
354 return s->state == MIG_STATE_SETUP;
355 }
356
357 bool migration_has_finished(MigrationState *s)
358 {
359 return s->state == MIG_STATE_COMPLETED;
360 }
361
362 bool migration_has_failed(MigrationState *s)
363 {
364 return (s->state == MIG_STATE_CANCELLED ||
365 s->state == MIG_STATE_ERROR);
366 }
367
368 static MigrationState *migrate_init(const MigrationParams *params)
369 {
370 MigrationState *s = migrate_get_current();
371 int64_t bandwidth_limit = s->bandwidth_limit;
372 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
373 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
374
375 memcpy(enabled_capabilities, s->enabled_capabilities,
376 sizeof(enabled_capabilities));
377
378 memset(s, 0, sizeof(*s));
379 s->params = *params;
380 memcpy(s->enabled_capabilities, enabled_capabilities,
381 sizeof(enabled_capabilities));
382 s->xbzrle_cache_size = xbzrle_cache_size;
383
384 s->bandwidth_limit = bandwidth_limit;
385 s->state = MIG_STATE_SETUP;
386 trace_migrate_set_state(MIG_STATE_SETUP);
387
388 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
389 return s;
390 }
391
392 static GSList *migration_blockers;
393
394 void migrate_add_blocker(Error *reason)
395 {
396 migration_blockers = g_slist_prepend(migration_blockers, reason);
397 }
398
399 void migrate_del_blocker(Error *reason)
400 {
401 migration_blockers = g_slist_remove(migration_blockers, reason);
402 }
403
404 void qmp_migrate(const char *uri, bool has_blk, bool blk,
405 bool has_inc, bool inc, bool has_detach, bool detach,
406 Error **errp)
407 {
408 Error *local_err = NULL;
409 MigrationState *s = migrate_get_current();
410 MigrationParams params;
411 const char *p;
412
413 params.blk = has_blk && blk;
414 params.shared = has_inc && inc;
415
416 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP ||
417 s->state == MIG_STATE_CANCELLING) {
418 error_set(errp, QERR_MIGRATION_ACTIVE);
419 return;
420 }
421
422 if (runstate_check(RUN_STATE_INMIGRATE)) {
423 error_setg(errp, "Guest is waiting for an incoming migration");
424 return;
425 }
426
427 if (qemu_savevm_state_blocked(errp)) {
428 return;
429 }
430
431 if (migration_blockers) {
432 *errp = error_copy(migration_blockers->data);
433 return;
434 }
435
436 s = migrate_init(&params);
437
438 if (strstart(uri, "tcp:", &p)) {
439 tcp_start_outgoing_migration(s, p, &local_err);
440 #ifdef CONFIG_RDMA
441 } else if (strstart(uri, "rdma:", &p)) {
442 rdma_start_outgoing_migration(s, p, &local_err);
443 #endif
444 #if !defined(WIN32)
445 } else if (strstart(uri, "exec:", &p)) {
446 exec_start_outgoing_migration(s, p, &local_err);
447 } else if (strstart(uri, "unix:", &p)) {
448 unix_start_outgoing_migration(s, p, &local_err);
449 } else if (strstart(uri, "fd:", &p)) {
450 fd_start_outgoing_migration(s, p, &local_err);
451 #endif
452 } else {
453 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
454 s->state = MIG_STATE_ERROR;
455 return;
456 }
457
458 if (local_err) {
459 migrate_fd_error(s);
460 error_propagate(errp, local_err);
461 return;
462 }
463 }
464
465 void qmp_migrate_cancel(Error **errp)
466 {
467 migrate_fd_cancel(migrate_get_current());
468 }
469
470 void qmp_migrate_set_cache_size(int64_t value, Error **errp)
471 {
472 MigrationState *s = migrate_get_current();
473 int64_t new_size;
474
475 /* Check for truncation */
476 if (value != (size_t)value) {
477 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
478 "exceeding address space");
479 return;
480 }
481
482 /* Cache should not be larger than guest ram size */
483 if (value > ram_bytes_total()) {
484 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
485 "exceeds guest ram size ");
486 return;
487 }
488
489 new_size = xbzrle_cache_resize(value);
490 if (new_size < 0) {
491 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
492 "is smaller than page size");
493 return;
494 }
495
496 s->xbzrle_cache_size = new_size;
497 }
498
499 int64_t qmp_query_migrate_cache_size(Error **errp)
500 {
501 return migrate_xbzrle_cache_size();
502 }
503
504 void qmp_migrate_set_speed(int64_t value, Error **errp)
505 {
506 MigrationState *s;
507
508 if (value < 0) {
509 value = 0;
510 }
511 if (value > SIZE_MAX) {
512 value = SIZE_MAX;
513 }
514
515 s = migrate_get_current();
516 s->bandwidth_limit = value;
517 if (s->file) {
518 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
519 }
520 }
521
522 void qmp_migrate_set_downtime(double value, Error **errp)
523 {
524 value *= 1e9;
525 value = MAX(0, MIN(UINT64_MAX, value));
526 max_downtime = (uint64_t)value;
527 }
528
529 bool migrate_rdma_pin_all(void)
530 {
531 MigrationState *s;
532
533 s = migrate_get_current();
534
535 return s->enabled_capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
536 }
537
538 bool migrate_auto_converge(void)
539 {
540 MigrationState *s;
541
542 s = migrate_get_current();
543
544 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
545 }
546
547 bool migrate_zero_blocks(void)
548 {
549 MigrationState *s;
550
551 s = migrate_get_current();
552
553 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
554 }
555
556 int migrate_use_xbzrle(void)
557 {
558 MigrationState *s;
559
560 s = migrate_get_current();
561
562 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
563 }
564
565 int64_t migrate_xbzrle_cache_size(void)
566 {
567 MigrationState *s;
568
569 s = migrate_get_current();
570
571 return s->xbzrle_cache_size;
572 }
573
574 /* migration thread support */
575
576 static void *migration_thread(void *opaque)
577 {
578 MigrationState *s = opaque;
579 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
580 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
581 int64_t initial_bytes = 0;
582 int64_t max_size = 0;
583 int64_t start_time = initial_time;
584 bool old_vm_running = false;
585
586 qemu_savevm_state_begin(s->file, &s->params);
587
588 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
589 migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);
590
591 while (s->state == MIG_STATE_ACTIVE) {
592 int64_t current_time;
593 uint64_t pending_size;
594
595 if (!qemu_file_rate_limit(s->file)) {
596 pending_size = qemu_savevm_state_pending(s->file, max_size);
597 trace_migrate_pending(pending_size, max_size);
598 if (pending_size && pending_size >= max_size) {
599 qemu_savevm_state_iterate(s->file);
600 } else {
601 int ret;
602
603 qemu_mutex_lock_iothread();
604 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
605 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
606 old_vm_running = runstate_is_running();
607
608 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
609 if (ret >= 0) {
610 qemu_file_set_rate_limit(s->file, INT64_MAX);
611 qemu_savevm_state_complete(s->file);
612 }
613 qemu_mutex_unlock_iothread();
614
615 if (ret < 0) {
616 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
617 break;
618 }
619
620 if (!qemu_file_get_error(s->file)) {
621 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
622 break;
623 }
624 }
625 }
626
627 if (qemu_file_get_error(s->file)) {
628 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
629 break;
630 }
631 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
632 if (current_time >= initial_time + BUFFER_DELAY) {
633 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
634 uint64_t time_spent = current_time - initial_time;
635 double bandwidth = transferred_bytes / time_spent;
636 max_size = bandwidth * migrate_max_downtime() / 1000000;
637
638 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
639 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
640
641 trace_migrate_transferred(transferred_bytes, time_spent,
642 bandwidth, max_size);
643 /* if we haven't sent anything, we don't want to recalculate
644 10000 is a small enough number for our purposes */
645 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
646 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
647 }
648
649 qemu_file_reset_rate_limit(s->file);
650 initial_time = current_time;
651 initial_bytes = qemu_ftell(s->file);
652 }
653 if (qemu_file_rate_limit(s->file)) {
654 /* usleep expects microseconds */
655 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
656 }
657 }
658
659 qemu_mutex_lock_iothread();
660 if (s->state == MIG_STATE_COMPLETED) {
661 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
662 s->total_time = end_time - s->total_time;
663 s->downtime = end_time - start_time;
664 runstate_set(RUN_STATE_POSTMIGRATE);
665 } else {
666 if (old_vm_running) {
667 vm_start();
668 }
669 }
670 qemu_bh_schedule(s->cleanup_bh);
671 qemu_mutex_unlock_iothread();
672
673 return NULL;
674 }
675
676 void migrate_fd_connect(MigrationState *s)
677 {
678 s->state = MIG_STATE_SETUP;
679 trace_migrate_set_state(MIG_STATE_SETUP);
680
681 /* This is a best 1st approximation. ns to ms */
682 s->expected_downtime = max_downtime/1000000;
683 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
684
685 qemu_file_set_rate_limit(s->file,
686 s->bandwidth_limit / XFER_LIMIT_RATIO);
687
688 /* Notify before starting migration thread */
689 notifier_list_notify(&migration_state_notifiers, s);
690
691 qemu_thread_create(&s->thread, "migration", migration_thread, s,
692 QEMU_THREAD_JOINABLE);
693 }