]> git.proxmox.com Git - mirror_qemu.git/blame - migration/migration.c
migration: Add the framework of multi-thread compression
[mirror_qemu.git] / migration / migration.c
CommitLineData
5bb7910a
AL
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 *
6b620ca3
PB
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.
5bb7910a
AL
14 */
15
16#include "qemu-common.h"
6a1751b7 17#include "qemu/main-loop.h"
caf71f86 18#include "migration/migration.h"
83c9089e 19#include "monitor/monitor.h"
0d82d0e8 20#include "migration/qemu-file.h"
9c17d615 21#include "sysemu/sysemu.h"
737e150e 22#include "block/block.h"
1de7afc9 23#include "qemu/sockets.h"
caf71f86 24#include "migration/block.h"
766bd176 25#include "qemu/thread.h"
791e7c82 26#include "qmp-commands.h"
c09e5bb1 27#include "trace.h"
065e2813 28
d0ae46c1 29#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
5bb7910a 30
5b4e1eb7
JQ
31/* Amount of time to allocate to each "chunk" of bandwidth-throttled
32 * data. */
33#define BUFFER_DELAY 100
34#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
35
8706d2d5
LL
36/* Default compression thread count */
37#define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
38/*0: means nocompress, 1: best speed, ... 9: best compress ratio */
39#define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
40
17ad9b35
OW
41/* Migration XBZRLE default cache size */
42#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
43
99a0db9b
GH
44static NotifierList migration_state_notifiers =
45 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
46
adde220a
DDAG
47static bool deferred_incoming;
48
17549e84
JQ
49/* When we add fault tolerance, we could have several
50 migrations at once. For now we don't need to add
51 dynamic creation of migration */
52
859bc756 53MigrationState *migrate_get_current(void)
17549e84
JQ
54{
55 static MigrationState current_migration = {
31194731 56 .state = MIGRATION_STATUS_NONE,
d0ae46c1 57 .bandwidth_limit = MAX_THROTTLE,
17ad9b35 58 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
7e114f8c 59 .mbps = -1,
8706d2d5
LL
60 .compress_thread_count = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT,
61 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL,
17549e84
JQ
62 };
63
64 return &current_migration;
65}
66
adde220a
DDAG
67/*
68 * Called on -incoming with a defer: uri.
69 * The migration can be started later after any parameters have been
70 * changed.
71 */
72static void deferred_incoming_migration(Error **errp)
73{
74 if (deferred_incoming) {
75 error_setg(errp, "Incoming migration already deferred");
76 }
77 deferred_incoming = true;
78}
79
43eaae28 80void qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 81{
34c9dd8e
AL
82 const char *p;
83
adde220a
DDAG
84 if (!strcmp(uri, "defer")) {
85 deferred_incoming_migration(errp);
86 } else if (strstart(uri, "tcp:", &p)) {
43eaae28 87 tcp_start_incoming_migration(p, errp);
2da776db 88#ifdef CONFIG_RDMA
adde220a 89 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
90 rdma_start_incoming_migration(p, errp);
91#endif
065e2813 92#if !defined(WIN32)
adde220a 93 } else if (strstart(uri, "exec:", &p)) {
43eaae28 94 exec_start_incoming_migration(p, errp);
adde220a 95 } else if (strstart(uri, "unix:", &p)) {
43eaae28 96 unix_start_incoming_migration(p, errp);
adde220a 97 } else if (strstart(uri, "fd:", &p)) {
43eaae28 98 fd_start_incoming_migration(p, errp);
065e2813 99#endif
adde220a 100 } else {
312fd5f2 101 error_setg(errp, "unknown migration protocol: %s", uri);
8ca5e801 102 }
5bb7910a
AL
103}
104
82a4da79 105static void process_incoming_migration_co(void *opaque)
511c0231 106{
82a4da79 107 QEMUFile *f = opaque;
5a8a30db 108 Error *local_err = NULL;
1c12e1f5
PB
109 int ret;
110
111 ret = qemu_loadvm_state(f);
112 qemu_fclose(f);
905f26f2 113 free_xbzrle_decoded_buf();
1c12e1f5 114 if (ret < 0) {
db80face 115 error_report("load of migration failed: %s", strerror(-ret));
4aead692 116 exit(EXIT_FAILURE);
511c0231
JQ
117 }
118 qemu_announce_self();
511c0231 119
0f15423c 120 /* Make sure all file formats flush their mutable metadata */
5a8a30db
KW
121 bdrv_invalidate_cache_all(&local_err);
122 if (local_err) {
97baf9d9 123 error_report_err(local_err);
5a8a30db
KW
124 exit(EXIT_FAILURE);
125 }
0f15423c 126
f5bbfba1 127 if (autostart) {
511c0231 128 vm_start();
f5bbfba1 129 } else {
29ed72f1 130 runstate_set(RUN_STATE_PAUSED);
f5bbfba1 131 }
511c0231
JQ
132}
133
82a4da79
PB
134void process_incoming_migration(QEMUFile *f)
135{
136 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
137 int fd = qemu_get_fd(f);
138
139 assert(fd != -1);
f9e8cacc 140 qemu_set_nonblock(fd);
82a4da79
PB
141 qemu_coroutine_enter(co, f);
142}
143
a0a3fd60
GC
144/* amount of nanoseconds we are willing to wait for migration to be down.
145 * the choice of nanoseconds is because it is the maximum resolution that
146 * get_clock() can achieve. It is an internal measure. All user-visible
147 * units must be in seconds */
f7cd55a0 148static uint64_t max_downtime = 300000000;
a0a3fd60
GC
149
150uint64_t migrate_max_downtime(void)
151{
152 return max_downtime;
153}
154
bbf6da32
OW
155MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
156{
157 MigrationCapabilityStatusList *head = NULL;
158 MigrationCapabilityStatusList *caps;
159 MigrationState *s = migrate_get_current();
160 int i;
161
387eedeb 162 caps = NULL; /* silence compiler warning */
bbf6da32
OW
163 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
164 if (head == NULL) {
165 head = g_malloc0(sizeof(*caps));
166 caps = head;
167 } else {
168 caps->next = g_malloc0(sizeof(*caps));
169 caps = caps->next;
170 }
171 caps->value =
172 g_malloc(sizeof(*caps->value));
173 caps->value->capability = i;
174 caps->value->state = s->enabled_capabilities[i];
175 }
176
177 return head;
178}
179
f36d55af
OW
180static void get_xbzrle_cache_stats(MigrationInfo *info)
181{
182 if (migrate_use_xbzrle()) {
183 info->has_xbzrle_cache = true;
184 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
185 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
186 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
187 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
188 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
8bc39233 189 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate();
f36d55af
OW
190 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
191 }
192}
193
791e7c82 194MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 195{
791e7c82 196 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
197 MigrationState *s = migrate_get_current();
198
199 switch (s->state) {
31194731 200 case MIGRATION_STATUS_NONE:
17549e84
JQ
201 /* no migration has happened ever */
202 break;
31194731 203 case MIGRATION_STATUS_SETUP:
29ae8a41 204 info->has_status = true;
ed4fbd10 205 info->has_total_time = false;
29ae8a41 206 break;
31194731
HZ
207 case MIGRATION_STATUS_ACTIVE:
208 case MIGRATION_STATUS_CANCELLING:
791e7c82 209 info->has_status = true;
7aa939af 210 info->has_total_time = true;
bc72ad67 211 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
7aa939af 212 - s->total_time;
2c52ddf1
JQ
213 info->has_expected_downtime = true;
214 info->expected_downtime = s->expected_downtime;
ed4fbd10
MH
215 info->has_setup_time = true;
216 info->setup_time = s->setup_time;
17549e84 217
791e7c82
LC
218 info->has_ram = true;
219 info->ram = g_malloc0(sizeof(*info->ram));
220 info->ram->transferred = ram_bytes_transferred();
221 info->ram->remaining = ram_bytes_remaining();
222 info->ram->total = ram_bytes_total();
004d4c10 223 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 224 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
225 info->ram->normal = norm_mig_pages_transferred();
226 info->ram->normal_bytes = norm_mig_bytes_transferred();
8d017193 227 info->ram->dirty_pages_rate = s->dirty_pages_rate;
7e114f8c 228 info->ram->mbps = s->mbps;
58570ed8 229 info->ram->dirty_sync_count = s->dirty_sync_count;
8d017193 230
17549e84 231 if (blk_mig_active()) {
791e7c82
LC
232 info->has_disk = true;
233 info->disk = g_malloc0(sizeof(*info->disk));
234 info->disk->transferred = blk_mig_bytes_transferred();
235 info->disk->remaining = blk_mig_bytes_remaining();
236 info->disk->total = blk_mig_bytes_total();
ff8d81d8 237 }
f36d55af
OW
238
239 get_xbzrle_cache_stats(info);
17549e84 240 break;
31194731 241 case MIGRATION_STATUS_COMPLETED:
f36d55af
OW
242 get_xbzrle_cache_stats(info);
243
791e7c82 244 info->has_status = true;
00c14997 245 info->has_total_time = true;
7aa939af 246 info->total_time = s->total_time;
9c5a9fcf
JQ
247 info->has_downtime = true;
248 info->downtime = s->downtime;
ed4fbd10
MH
249 info->has_setup_time = true;
250 info->setup_time = s->setup_time;
d5f8a570
JQ
251
252 info->has_ram = true;
253 info->ram = g_malloc0(sizeof(*info->ram));
254 info->ram->transferred = ram_bytes_transferred();
255 info->ram->remaining = 0;
256 info->ram->total = ram_bytes_total();
004d4c10 257 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 258 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
259 info->ram->normal = norm_mig_pages_transferred();
260 info->ram->normal_bytes = norm_mig_bytes_transferred();
7e114f8c 261 info->ram->mbps = s->mbps;
58570ed8 262 info->ram->dirty_sync_count = s->dirty_sync_count;
17549e84 263 break;
31194731 264 case MIGRATION_STATUS_FAILED:
791e7c82 265 info->has_status = true;
17549e84 266 break;
31194731 267 case MIGRATION_STATUS_CANCELLED:
791e7c82 268 info->has_status = true;
17549e84 269 break;
5bb7910a 270 }
cde63fbe 271 info->status = s->state;
791e7c82
LC
272
273 return info;
5bb7910a
AL
274}
275
00458433
OW
276void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
277 Error **errp)
278{
279 MigrationState *s = migrate_get_current();
280 MigrationCapabilityStatusList *cap;
281
31194731
HZ
282 if (s->state == MIGRATION_STATUS_ACTIVE ||
283 s->state == MIGRATION_STATUS_SETUP) {
00458433
OW
284 error_set(errp, QERR_MIGRATION_ACTIVE);
285 return;
286 }
287
288 for (cap = params; cap; cap = cap->next) {
289 s->enabled_capabilities[cap->value->capability] = cap->value->state;
290 }
291}
292
065e2813
AL
293/* shared migration helpers */
294
51cf4c1a
Z
295static void migrate_set_state(MigrationState *s, int old_state, int new_state)
296{
297 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
298 trace_migrate_set_state(new_state);
299 }
300}
301
bb1fadc4 302static void migrate_fd_cleanup(void *opaque)
065e2813 303{
bb1fadc4
PB
304 MigrationState *s = opaque;
305
306 qemu_bh_delete(s->cleanup_bh);
307 s->cleanup_bh = NULL;
308
065e2813 309 if (s->file) {
9013dca5 310 trace_migrate_fd_cleanup();
404a7c05
PB
311 qemu_mutex_unlock_iothread();
312 qemu_thread_join(&s->thread);
313 qemu_mutex_lock_iothread();
314
8706d2d5 315 migrate_compress_threads_join();
6f190a06
PB
316 qemu_fclose(s->file);
317 s->file = NULL;
065e2813
AL
318 }
319
31194731 320 assert(s->state != MIGRATION_STATUS_ACTIVE);
7a2c1721 321
31194731 322 if (s->state != MIGRATION_STATUS_COMPLETED) {
7a2c1721 323 qemu_savevm_state_cancel();
31194731
HZ
324 if (s->state == MIGRATION_STATUS_CANCELLING) {
325 migrate_set_state(s, MIGRATION_STATUS_CANCELLING,
326 MIGRATION_STATUS_CANCELLED);
51cf4c1a 327 }
7a2c1721 328 }
a3fa1d78
PB
329
330 notifier_list_notify(&migration_state_notifiers, s);
065e2813
AL
331}
332
8b6b99b3 333void migrate_fd_error(MigrationState *s)
065e2813 334{
9013dca5 335 trace_migrate_fd_error();
bb1fadc4 336 assert(s->file == NULL);
31194731
HZ
337 s->state = MIGRATION_STATUS_FAILED;
338 trace_migrate_set_state(MIGRATION_STATUS_FAILED);
bb1fadc4 339 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
340}
341
0edda1c4 342static void migrate_fd_cancel(MigrationState *s)
065e2813 343{
6f2b811a 344 int old_state ;
a26ba26e 345 QEMUFile *f = migrate_get_current()->file;
9013dca5 346 trace_migrate_fd_cancel();
065e2813 347
6f2b811a
Z
348 do {
349 old_state = s->state;
31194731
HZ
350 if (old_state != MIGRATION_STATUS_SETUP &&
351 old_state != MIGRATION_STATUS_ACTIVE) {
6f2b811a
Z
352 break;
353 }
31194731
HZ
354 migrate_set_state(s, old_state, MIGRATION_STATUS_CANCELLING);
355 } while (s->state != MIGRATION_STATUS_CANCELLING);
a26ba26e
DDAG
356
357 /*
358 * If we're unlucky the migration code might be stuck somewhere in a
359 * send/write while the network has failed and is waiting to timeout;
360 * if we've got shutdown(2) available then we can force it to quit.
361 * The outgoing qemu file gets closed in migrate_fd_cleanup that is
362 * called in a bh, so there is no race against this cancel.
363 */
31194731 364 if (s->state == MIGRATION_STATUS_CANCELLING && f) {
a26ba26e
DDAG
365 qemu_file_shutdown(f);
366 }
065e2813
AL
367}
368
99a0db9b
GH
369void add_migration_state_change_notifier(Notifier *notify)
370{
371 notifier_list_add(&migration_state_notifiers, notify);
372}
373
374void remove_migration_state_change_notifier(Notifier *notify)
375{
31552529 376 notifier_remove(notify);
99a0db9b
GH
377}
378
02edd2e7 379bool migration_in_setup(MigrationState *s)
afe2df69 380{
31194731 381 return s->state == MIGRATION_STATUS_SETUP;
afe2df69
GH
382}
383
7073693b 384bool migration_has_finished(MigrationState *s)
99a0db9b 385{
31194731 386 return s->state == MIGRATION_STATUS_COMPLETED;
99a0db9b 387}
0edda1c4 388
afe2df69
GH
389bool migration_has_failed(MigrationState *s)
390{
31194731
HZ
391 return (s->state == MIGRATION_STATUS_CANCELLED ||
392 s->state == MIGRATION_STATUS_FAILED);
afe2df69
GH
393}
394
6607ae23 395static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 396{
17549e84 397 MigrationState *s = migrate_get_current();
d0ae46c1 398 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 399 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 400 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
8706d2d5
LL
401 int compress_level = s->compress_level;
402 int compress_thread_count = s->compress_thread_count;
bbf6da32
OW
403
404 memcpy(enabled_capabilities, s->enabled_capabilities,
405 sizeof(enabled_capabilities));
0edda1c4 406
17549e84 407 memset(s, 0, sizeof(*s));
6607ae23 408 s->params = *params;
bbf6da32
OW
409 memcpy(s->enabled_capabilities, enabled_capabilities,
410 sizeof(enabled_capabilities));
17ad9b35 411 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 412
8706d2d5
LL
413 s->compress_level = compress_level;
414 s->compress_thread_count = compress_thread_count;
0edda1c4 415 s->bandwidth_limit = bandwidth_limit;
31194731
HZ
416 s->state = MIGRATION_STATUS_SETUP;
417 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
0edda1c4 418
bc72ad67 419 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0edda1c4
JQ
420 return s;
421}
cab30143 422
fa2756b7
AL
423static GSList *migration_blockers;
424
425void migrate_add_blocker(Error *reason)
426{
427 migration_blockers = g_slist_prepend(migration_blockers, reason);
428}
429
430void migrate_del_blocker(Error *reason)
431{
432 migration_blockers = g_slist_remove(migration_blockers, reason);
433}
434
bf1ae1f4
DDAG
435void qmp_migrate_incoming(const char *uri, Error **errp)
436{
437 Error *local_err = NULL;
4debb5f5 438 static bool once = true;
bf1ae1f4
DDAG
439
440 if (!deferred_incoming) {
4debb5f5 441 error_setg(errp, "For use with '-incoming defer'");
bf1ae1f4
DDAG
442 return;
443 }
4debb5f5
DDAG
444 if (!once) {
445 error_setg(errp, "The incoming migration has already been started");
446 }
bf1ae1f4
DDAG
447
448 qemu_start_incoming_migration(uri, &local_err);
449
450 if (local_err) {
451 error_propagate(errp, local_err);
452 return;
453 }
454
4debb5f5 455 once = false;
bf1ae1f4
DDAG
456}
457
e1c37d0e
LC
458void qmp_migrate(const char *uri, bool has_blk, bool blk,
459 bool has_inc, bool inc, bool has_detach, bool detach,
460 Error **errp)
cab30143 461{
be7059cd 462 Error *local_err = NULL;
17549e84 463 MigrationState *s = migrate_get_current();
6607ae23 464 MigrationParams params;
cab30143 465 const char *p;
cab30143 466
8c0426ae
PP
467 params.blk = has_blk && blk;
468 params.shared = has_inc && inc;
6607ae23 469
31194731
HZ
470 if (s->state == MIGRATION_STATUS_ACTIVE ||
471 s->state == MIGRATION_STATUS_SETUP ||
472 s->state == MIGRATION_STATUS_CANCELLING) {
e1c37d0e
LC
473 error_set(errp, QERR_MIGRATION_ACTIVE);
474 return;
cab30143
JQ
475 }
476
ca99993a
DDAG
477 if (runstate_check(RUN_STATE_INMIGRATE)) {
478 error_setg(errp, "Guest is waiting for an incoming migration");
479 return;
480 }
481
e1c37d0e
LC
482 if (qemu_savevm_state_blocked(errp)) {
483 return;
cab30143
JQ
484 }
485
fa2756b7 486 if (migration_blockers) {
e1c37d0e
LC
487 *errp = error_copy(migration_blockers->data);
488 return;
fa2756b7
AL
489 }
490
6607ae23 491 s = migrate_init(&params);
cab30143
JQ
492
493 if (strstart(uri, "tcp:", &p)) {
f37afb5a 494 tcp_start_outgoing_migration(s, p, &local_err);
2da776db 495#ifdef CONFIG_RDMA
41310c68 496 } else if (strstart(uri, "rdma:", &p)) {
2da776db
MH
497 rdma_start_outgoing_migration(s, p, &local_err);
498#endif
cab30143
JQ
499#if !defined(WIN32)
500 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 501 exec_start_outgoing_migration(s, p, &local_err);
cab30143 502 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 503 unix_start_outgoing_migration(s, p, &local_err);
cab30143 504 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 505 fd_start_outgoing_migration(s, p, &local_err);
cab30143 506#endif
99a0db9b 507 } else {
e1c37d0e 508 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
31194731 509 s->state = MIGRATION_STATUS_FAILED;
e1c37d0e 510 return;
cab30143
JQ
511 }
512
f37afb5a 513 if (local_err) {
342ab8d1 514 migrate_fd_error(s);
f37afb5a 515 error_propagate(errp, local_err);
e1c37d0e 516 return;
1299c631 517 }
cab30143
JQ
518}
519
6cdedb07 520void qmp_migrate_cancel(Error **errp)
cab30143 521{
17549e84 522 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
523}
524
9e1ba4cc
OW
525void qmp_migrate_set_cache_size(int64_t value, Error **errp)
526{
527 MigrationState *s = migrate_get_current();
c91e681a 528 int64_t new_size;
9e1ba4cc
OW
529
530 /* Check for truncation */
531 if (value != (size_t)value) {
532 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
533 "exceeding address space");
534 return;
535 }
536
a5615b14
OW
537 /* Cache should not be larger than guest ram size */
538 if (value > ram_bytes_total()) {
539 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
540 "exceeds guest ram size ");
541 return;
542 }
543
c91e681a
OW
544 new_size = xbzrle_cache_resize(value);
545 if (new_size < 0) {
546 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
547 "is smaller than page size");
548 return;
549 }
550
551 s->xbzrle_cache_size = new_size;
9e1ba4cc
OW
552}
553
554int64_t qmp_query_migrate_cache_size(Error **errp)
555{
556 return migrate_xbzrle_cache_size();
557}
558
3dc85383 559void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 560{
cab30143
JQ
561 MigrationState *s;
562
3dc85383
LC
563 if (value < 0) {
564 value = 0;
99a0db9b 565 }
442773ce
PB
566 if (value > SIZE_MAX) {
567 value = SIZE_MAX;
568 }
cab30143 569
17549e84 570 s = migrate_get_current();
3dc85383 571 s->bandwidth_limit = value;
442773ce
PB
572 if (s->file) {
573 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
574 }
cab30143
JQ
575}
576
4f0a993b 577void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 578{
4f0a993b
LC
579 value *= 1e9;
580 value = MAX(0, MIN(UINT64_MAX, value));
581 max_downtime = (uint64_t)value;
99a0db9b 582}
17ad9b35 583
bde1e2ec
CV
584bool migrate_auto_converge(void)
585{
586 MigrationState *s;
587
588 s = migrate_get_current();
589
590 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
591}
592
323004a3
PL
593bool migrate_zero_blocks(void)
594{
595 MigrationState *s;
596
597 s = migrate_get_current();
598
599 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
600}
601
8706d2d5
LL
602bool migrate_use_compression(void)
603{
604 /* Disable compression before the patch series are applied */
605 return false;
606}
607
608int migrate_compress_level(void)
609{
610 MigrationState *s;
611
612 s = migrate_get_current();
613
614 return s->compress_level;
615}
616
617int migrate_compress_threads(void)
618{
619 MigrationState *s;
620
621 s = migrate_get_current();
622
623 return s->compress_thread_count;
624}
625
17ad9b35
OW
626int migrate_use_xbzrle(void)
627{
628 MigrationState *s;
629
630 s = migrate_get_current();
631
632 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
633}
634
635int64_t migrate_xbzrle_cache_size(void)
636{
637 MigrationState *s;
638
639 s = migrate_get_current();
640
641 return s->xbzrle_cache_size;
642}
0d82d0e8
JQ
643
644/* migration thread support */
645
5f496a1b 646static void *migration_thread(void *opaque)
0d82d0e8 647{
9848a404 648 MigrationState *s = opaque;
bc72ad67
AB
649 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
650 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
be7172e2 651 int64_t initial_bytes = 0;
0d82d0e8 652 int64_t max_size = 0;
a3fa1d78
PB
653 int64_t start_time = initial_time;
654 bool old_vm_running = false;
76f5933a 655
dba433c0 656 qemu_savevm_state_begin(s->file, &s->params);
0d82d0e8 657
bc72ad67 658 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
31194731 659 migrate_set_state(s, MIGRATION_STATUS_SETUP, MIGRATION_STATUS_ACTIVE);
29ae8a41 660
31194731 661 while (s->state == MIGRATION_STATUS_ACTIVE) {
a3e879cd 662 int64_t current_time;
c369f40d 663 uint64_t pending_size;
0d82d0e8 664
a0ff044b 665 if (!qemu_file_rate_limit(s->file)) {
c369f40d 666 pending_size = qemu_savevm_state_pending(s->file, max_size);
9013dca5 667 trace_migrate_pending(pending_size, max_size);
b22ff1fb 668 if (pending_size && pending_size >= max_size) {
dba433c0 669 qemu_savevm_state_iterate(s->file);
c369f40d 670 } else {
0e1146a7
KW
671 int ret;
672
32c835ba 673 qemu_mutex_lock_iothread();
bc72ad67 674 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
c369f40d 675 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
a3fa1d78 676 old_vm_running = runstate_is_running();
0e1146a7
KW
677
678 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
679 if (ret >= 0) {
40596834 680 qemu_file_set_rate_limit(s->file, INT64_MAX);
0e1146a7
KW
681 qemu_savevm_state_complete(s->file);
682 }
32c835ba 683 qemu_mutex_unlock_iothread();
0e1146a7
KW
684
685 if (ret < 0) {
31194731
HZ
686 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
687 MIGRATION_STATUS_FAILED);
0e1146a7
KW
688 break;
689 }
690
059f896c 691 if (!qemu_file_get_error(s->file)) {
31194731
HZ
692 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
693 MIGRATION_STATUS_COMPLETED);
059f896c
PB
694 break;
695 }
c369f40d
JQ
696 }
697 }
f4410a5d 698
fd45ee2c 699 if (qemu_file_get_error(s->file)) {
31194731
HZ
700 migrate_set_state(s, MIGRATION_STATUS_ACTIVE,
701 MIGRATION_STATUS_FAILED);
fd45ee2c
PB
702 break;
703 }
bc72ad67 704 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
0d82d0e8 705 if (current_time >= initial_time + BUFFER_DELAY) {
be7172e2 706 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
77417f10 707 uint64_t time_spent = current_time - initial_time;
0d82d0e8
JQ
708 double bandwidth = transferred_bytes / time_spent;
709 max_size = bandwidth * migrate_max_downtime() / 1000000;
710
7e114f8c
MH
711 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
712 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
713
9013dca5
AK
714 trace_migrate_transferred(transferred_bytes, time_spent,
715 bandwidth, max_size);
90f8ae72
JQ
716 /* if we haven't sent anything, we don't want to recalculate
717 10000 is a small enough number for our purposes */
718 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
719 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
720 }
0d82d0e8 721
1964a397 722 qemu_file_reset_rate_limit(s->file);
0d82d0e8 723 initial_time = current_time;
be7172e2 724 initial_bytes = qemu_ftell(s->file);
0d82d0e8 725 }
a0ff044b 726 if (qemu_file_rate_limit(s->file)) {
0d82d0e8
JQ
727 /* usleep expects microseconds */
728 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
729 }
a3fa1d78
PB
730 }
731
f4410a5d 732 qemu_mutex_lock_iothread();
31194731 733 if (s->state == MIGRATION_STATUS_COMPLETED) {
bc72ad67 734 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
d6ed7312 735 uint64_t transferred_bytes = qemu_ftell(s->file);
a3fa1d78
PB
736 s->total_time = end_time - s->total_time;
737 s->downtime = end_time - start_time;
d6ed7312
PL
738 if (s->total_time) {
739 s->mbps = (((double) transferred_bytes * 8.0) /
740 ((double) s->total_time)) / 1000;
741 }
a3fa1d78
PB
742 runstate_set(RUN_STATE_POSTMIGRATE);
743 } else {
744 if (old_vm_running) {
a3fa1d78 745 vm_start();
dba433c0 746 }
0d82d0e8 747 }
bb1fadc4 748 qemu_bh_schedule(s->cleanup_bh);
dba433c0 749 qemu_mutex_unlock_iothread();
f4410a5d 750
0d82d0e8
JQ
751 return NULL;
752}
753
9848a404 754void migrate_fd_connect(MigrationState *s)
0d82d0e8 755{
31194731
HZ
756 s->state = MIGRATION_STATUS_SETUP;
757 trace_migrate_set_state(MIGRATION_STATUS_SETUP);
c09e5bb1 758
cc283e3b
JQ
759 /* This is a best 1st approximation. ns to ms */
760 s->expected_downtime = max_downtime/1000000;
bb1fadc4 761 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
0d82d0e8 762
442773ce
PB
763 qemu_file_set_rate_limit(s->file,
764 s->bandwidth_limit / XFER_LIMIT_RATIO);
765
9287ac27
SH
766 /* Notify before starting migration thread */
767 notifier_list_notify(&migration_state_notifiers, s);
768
8706d2d5 769 migrate_compress_threads_create();
4900116e 770 qemu_thread_create(&s->thread, "migration", migration_thread, s,
bb1fadc4 771 QEMU_THREAD_JOINABLE);
0d82d0e8 772}