]> git.proxmox.com Git - mirror_qemu.git/blame - migration.c
rdma: introduce MIG_STATE_NONE and change MIG_STATE_SETUP state transition
[mirror_qemu.git] / 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"
caf71f86 17#include "migration/migration.h"
83c9089e 18#include "monitor/monitor.h"
0d82d0e8 19#include "migration/qemu-file.h"
9c17d615 20#include "sysemu/sysemu.h"
737e150e 21#include "block/block.h"
1de7afc9 22#include "qemu/sockets.h"
caf71f86 23#include "migration/block.h"
766bd176 24#include "qemu/thread.h"
791e7c82 25#include "qmp-commands.h"
c09e5bb1 26#include "trace.h"
065e2813
AL
27
28//#define DEBUG_MIGRATION
29
30#ifdef DEBUG_MIGRATION
d0f2c4c6 31#define DPRINTF(fmt, ...) \
065e2813
AL
32 do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
33#else
d0f2c4c6 34#define DPRINTF(fmt, ...) \
065e2813
AL
35 do { } while (0)
36#endif
5bb7910a 37
7dc688ed 38enum {
29ae8a41
MH
39 MIG_STATE_ERROR = -1,
40 MIG_STATE_NONE,
7dc688ed
JQ
41 MIG_STATE_SETUP,
42 MIG_STATE_CANCELLED,
43 MIG_STATE_ACTIVE,
44 MIG_STATE_COMPLETED,
45};
5bb7910a 46
d0ae46c1 47#define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
5bb7910a 48
5b4e1eb7
JQ
49/* Amount of time to allocate to each "chunk" of bandwidth-throttled
50 * data. */
51#define BUFFER_DELAY 100
52#define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
53
17ad9b35
OW
54/* Migration XBZRLE default cache size */
55#define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
56
99a0db9b
GH
57static NotifierList migration_state_notifiers =
58 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
59
17549e84
JQ
60/* When we add fault tolerance, we could have several
61 migrations at once. For now we don't need to add
62 dynamic creation of migration */
63
859bc756 64MigrationState *migrate_get_current(void)
17549e84
JQ
65{
66 static MigrationState current_migration = {
29ae8a41 67 .state = MIG_STATE_NONE,
d0ae46c1 68 .bandwidth_limit = MAX_THROTTLE,
17ad9b35 69 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
7e114f8c 70 .mbps = -1,
17549e84
JQ
71 };
72
73 return &current_migration;
74}
75
43eaae28 76void qemu_start_incoming_migration(const char *uri, Error **errp)
5bb7910a 77{
34c9dd8e
AL
78 const char *p;
79
80 if (strstart(uri, "tcp:", &p))
43eaae28 81 tcp_start_incoming_migration(p, errp);
2da776db
MH
82#ifdef CONFIG_RDMA
83 else if (strstart(uri, "x-rdma:", &p))
84 rdma_start_incoming_migration(p, errp);
85#endif
065e2813
AL
86#if !defined(WIN32)
87 else if (strstart(uri, "exec:", &p))
43eaae28 88 exec_start_incoming_migration(p, errp);
4951f65b 89 else if (strstart(uri, "unix:", &p))
43eaae28 90 unix_start_incoming_migration(p, errp);
5ac1fad3 91 else if (strstart(uri, "fd:", &p))
43eaae28 92 fd_start_incoming_migration(p, errp);
065e2813 93#endif
8ca5e801 94 else {
312fd5f2 95 error_setg(errp, "unknown migration protocol: %s", uri);
8ca5e801 96 }
5bb7910a
AL
97}
98
82a4da79 99static void process_incoming_migration_co(void *opaque)
511c0231 100{
82a4da79 101 QEMUFile *f = opaque;
1c12e1f5
PB
102 int ret;
103
104 ret = qemu_loadvm_state(f);
105 qemu_fclose(f);
106 if (ret < 0) {
511c0231 107 fprintf(stderr, "load of migration failed\n");
4aead692 108 exit(EXIT_FAILURE);
511c0231
JQ
109 }
110 qemu_announce_self();
111 DPRINTF("successfully loaded vm state\n");
112
901862cb 113 bdrv_clear_incoming_migration_all();
0f15423c
AL
114 /* Make sure all file formats flush their mutable metadata */
115 bdrv_invalidate_cache_all();
116
f5bbfba1 117 if (autostart) {
511c0231 118 vm_start();
f5bbfba1 119 } else {
29ed72f1 120 runstate_set(RUN_STATE_PAUSED);
f5bbfba1 121 }
511c0231
JQ
122}
123
82a4da79
PB
124void process_incoming_migration(QEMUFile *f)
125{
126 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
127 int fd = qemu_get_fd(f);
128
129 assert(fd != -1);
f9e8cacc 130 qemu_set_nonblock(fd);
82a4da79
PB
131 qemu_coroutine_enter(co, f);
132}
133
a0a3fd60
GC
134/* amount of nanoseconds we are willing to wait for migration to be down.
135 * the choice of nanoseconds is because it is the maximum resolution that
136 * get_clock() can achieve. It is an internal measure. All user-visible
137 * units must be in seconds */
138static uint64_t max_downtime = 30000000;
139
140uint64_t migrate_max_downtime(void)
141{
142 return max_downtime;
143}
144
bbf6da32
OW
145MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
146{
147 MigrationCapabilityStatusList *head = NULL;
148 MigrationCapabilityStatusList *caps;
149 MigrationState *s = migrate_get_current();
150 int i;
151
152 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
153 if (head == NULL) {
154 head = g_malloc0(sizeof(*caps));
155 caps = head;
156 } else {
157 caps->next = g_malloc0(sizeof(*caps));
158 caps = caps->next;
159 }
160 caps->value =
161 g_malloc(sizeof(*caps->value));
162 caps->value->capability = i;
163 caps->value->state = s->enabled_capabilities[i];
164 }
165
166 return head;
167}
168
f36d55af
OW
169static void get_xbzrle_cache_stats(MigrationInfo *info)
170{
171 if (migrate_use_xbzrle()) {
172 info->has_xbzrle_cache = true;
173 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
174 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
175 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
176 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
177 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
178 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
179 }
180}
181
791e7c82 182MigrationInfo *qmp_query_migrate(Error **errp)
5bb7910a 183{
791e7c82 184 MigrationInfo *info = g_malloc0(sizeof(*info));
17549e84
JQ
185 MigrationState *s = migrate_get_current();
186
187 switch (s->state) {
29ae8a41 188 case MIG_STATE_NONE:
17549e84
JQ
189 /* no migration has happened ever */
190 break;
29ae8a41
MH
191 case MIG_STATE_SETUP:
192 info->has_status = true;
193 info->status = g_strdup("setup");
194 break;
17549e84 195 case MIG_STATE_ACTIVE:
791e7c82
LC
196 info->has_status = true;
197 info->status = g_strdup("active");
7aa939af
JQ
198 info->has_total_time = true;
199 info->total_time = qemu_get_clock_ms(rt_clock)
200 - s->total_time;
2c52ddf1
JQ
201 info->has_expected_downtime = true;
202 info->expected_downtime = s->expected_downtime;
17549e84 203
791e7c82
LC
204 info->has_ram = true;
205 info->ram = g_malloc0(sizeof(*info->ram));
206 info->ram->transferred = ram_bytes_transferred();
207 info->ram->remaining = ram_bytes_remaining();
208 info->ram->total = ram_bytes_total();
004d4c10 209 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 210 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
211 info->ram->normal = norm_mig_pages_transferred();
212 info->ram->normal_bytes = norm_mig_bytes_transferred();
8d017193 213 info->ram->dirty_pages_rate = s->dirty_pages_rate;
7e114f8c 214 info->ram->mbps = s->mbps;
8d017193 215
17549e84 216 if (blk_mig_active()) {
791e7c82
LC
217 info->has_disk = true;
218 info->disk = g_malloc0(sizeof(*info->disk));
219 info->disk->transferred = blk_mig_bytes_transferred();
220 info->disk->remaining = blk_mig_bytes_remaining();
221 info->disk->total = blk_mig_bytes_total();
ff8d81d8 222 }
f36d55af
OW
223
224 get_xbzrle_cache_stats(info);
17549e84
JQ
225 break;
226 case MIG_STATE_COMPLETED:
f36d55af
OW
227 get_xbzrle_cache_stats(info);
228
791e7c82
LC
229 info->has_status = true;
230 info->status = g_strdup("completed");
7aa939af 231 info->total_time = s->total_time;
9c5a9fcf
JQ
232 info->has_downtime = true;
233 info->downtime = s->downtime;
d5f8a570
JQ
234
235 info->has_ram = true;
236 info->ram = g_malloc0(sizeof(*info->ram));
237 info->ram->transferred = ram_bytes_transferred();
238 info->ram->remaining = 0;
239 info->ram->total = ram_bytes_total();
004d4c10 240 info->ram->duplicate = dup_mig_pages_transferred();
f1c72795 241 info->ram->skipped = skipped_mig_pages_transferred();
004d4c10
OW
242 info->ram->normal = norm_mig_pages_transferred();
243 info->ram->normal_bytes = norm_mig_bytes_transferred();
7e114f8c 244 info->ram->mbps = s->mbps;
17549e84
JQ
245 break;
246 case MIG_STATE_ERROR:
791e7c82
LC
247 info->has_status = true;
248 info->status = g_strdup("failed");
17549e84
JQ
249 break;
250 case MIG_STATE_CANCELLED:
791e7c82
LC
251 info->has_status = true;
252 info->status = g_strdup("cancelled");
17549e84 253 break;
5bb7910a 254 }
791e7c82
LC
255
256 return info;
5bb7910a
AL
257}
258
00458433
OW
259void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
260 Error **errp)
261{
262 MigrationState *s = migrate_get_current();
263 MigrationCapabilityStatusList *cap;
264
29ae8a41 265 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
00458433
OW
266 error_set(errp, QERR_MIGRATION_ACTIVE);
267 return;
268 }
269
270 for (cap = params; cap; cap = cap->next) {
271 s->enabled_capabilities[cap->value->capability] = cap->value->state;
272 }
273}
274
065e2813
AL
275/* shared migration helpers */
276
bb1fadc4 277static void migrate_fd_cleanup(void *opaque)
065e2813 278{
bb1fadc4
PB
279 MigrationState *s = opaque;
280
281 qemu_bh_delete(s->cleanup_bh);
282 s->cleanup_bh = NULL;
283
065e2813 284 if (s->file) {
d0f2c4c6 285 DPRINTF("closing file\n");
404a7c05
PB
286 qemu_mutex_unlock_iothread();
287 qemu_thread_join(&s->thread);
288 qemu_mutex_lock_iothread();
289
6f190a06
PB
290 qemu_fclose(s->file);
291 s->file = NULL;
065e2813
AL
292 }
293
a3fa1d78 294 assert(s->state != MIG_STATE_ACTIVE);
7a2c1721 295
a3fa1d78 296 if (s->state != MIG_STATE_COMPLETED) {
7a2c1721
PB
297 qemu_savevm_state_cancel();
298 }
a3fa1d78
PB
299
300 notifier_list_notify(&migration_state_notifiers, s);
065e2813
AL
301}
302
d58f574b 303static void migrate_set_state(MigrationState *s, int old_state, int new_state)
f4410a5d 304{
d58f574b 305 if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
f4410a5d
PB
306 trace_migrate_set_state(new_state);
307 }
308}
309
8b6b99b3 310void migrate_fd_error(MigrationState *s)
065e2813 311{
8b6b99b3 312 DPRINTF("setting error state\n");
bb1fadc4
PB
313 assert(s->file == NULL);
314 s->state = MIG_STATE_ERROR;
315 trace_migrate_set_state(MIG_STATE_ERROR);
316 notifier_list_notify(&migration_state_notifiers, s);
458cf28e
JQ
317}
318
0edda1c4 319static void migrate_fd_cancel(MigrationState *s)
065e2813 320{
d0f2c4c6 321 DPRINTF("cancelling migration\n");
065e2813 322
d58f574b 323 migrate_set_state(s, s->state, MIG_STATE_CANCELLED);
065e2813
AL
324}
325
99a0db9b
GH
326void add_migration_state_change_notifier(Notifier *notify)
327{
328 notifier_list_add(&migration_state_notifiers, notify);
329}
330
331void remove_migration_state_change_notifier(Notifier *notify)
332{
31552529 333 notifier_remove(notify);
99a0db9b
GH
334}
335
afe2df69
GH
336bool migration_is_active(MigrationState *s)
337{
338 return s->state == MIG_STATE_ACTIVE;
339}
340
7073693b 341bool migration_has_finished(MigrationState *s)
99a0db9b 342{
7073693b 343 return s->state == MIG_STATE_COMPLETED;
99a0db9b 344}
0edda1c4 345
afe2df69
GH
346bool migration_has_failed(MigrationState *s)
347{
348 return (s->state == MIG_STATE_CANCELLED ||
349 s->state == MIG_STATE_ERROR);
350}
351
6607ae23 352static MigrationState *migrate_init(const MigrationParams *params)
0edda1c4 353{
17549e84 354 MigrationState *s = migrate_get_current();
d0ae46c1 355 int64_t bandwidth_limit = s->bandwidth_limit;
bbf6da32 356 bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
17ad9b35 357 int64_t xbzrle_cache_size = s->xbzrle_cache_size;
bbf6da32
OW
358
359 memcpy(enabled_capabilities, s->enabled_capabilities,
360 sizeof(enabled_capabilities));
0edda1c4 361
17549e84 362 memset(s, 0, sizeof(*s));
6607ae23 363 s->params = *params;
bbf6da32
OW
364 memcpy(s->enabled_capabilities, enabled_capabilities,
365 sizeof(enabled_capabilities));
17ad9b35 366 s->xbzrle_cache_size = xbzrle_cache_size;
1299c631 367
0edda1c4 368 s->bandwidth_limit = bandwidth_limit;
d5934dde 369 s->state = MIG_STATE_SETUP;
c09e5bb1 370 trace_migrate_set_state(MIG_STATE_SETUP);
0edda1c4 371
c09e5bb1 372 s->total_time = qemu_get_clock_ms(rt_clock);
0edda1c4
JQ
373 return s;
374}
cab30143 375
fa2756b7
AL
376static GSList *migration_blockers;
377
378void migrate_add_blocker(Error *reason)
379{
380 migration_blockers = g_slist_prepend(migration_blockers, reason);
381}
382
383void migrate_del_blocker(Error *reason)
384{
385 migration_blockers = g_slist_remove(migration_blockers, reason);
386}
387
e1c37d0e
LC
388void qmp_migrate(const char *uri, bool has_blk, bool blk,
389 bool has_inc, bool inc, bool has_detach, bool detach,
390 Error **errp)
cab30143 391{
be7059cd 392 Error *local_err = NULL;
17549e84 393 MigrationState *s = migrate_get_current();
6607ae23 394 MigrationParams params;
cab30143 395 const char *p;
cab30143 396
6607ae23
IY
397 params.blk = blk;
398 params.shared = inc;
399
29ae8a41 400 if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
e1c37d0e
LC
401 error_set(errp, QERR_MIGRATION_ACTIVE);
402 return;
cab30143
JQ
403 }
404
e1c37d0e
LC
405 if (qemu_savevm_state_blocked(errp)) {
406 return;
cab30143
JQ
407 }
408
fa2756b7 409 if (migration_blockers) {
e1c37d0e
LC
410 *errp = error_copy(migration_blockers->data);
411 return;
fa2756b7
AL
412 }
413
6607ae23 414 s = migrate_init(&params);
cab30143
JQ
415
416 if (strstart(uri, "tcp:", &p)) {
f37afb5a 417 tcp_start_outgoing_migration(s, p, &local_err);
2da776db
MH
418#ifdef CONFIG_RDMA
419 } else if (strstart(uri, "x-rdma:", &p)) {
420 rdma_start_outgoing_migration(s, p, &local_err);
421#endif
cab30143
JQ
422#if !defined(WIN32)
423 } else if (strstart(uri, "exec:", &p)) {
f37afb5a 424 exec_start_outgoing_migration(s, p, &local_err);
cab30143 425 } else if (strstart(uri, "unix:", &p)) {
f37afb5a 426 unix_start_outgoing_migration(s, p, &local_err);
cab30143 427 } else if (strstart(uri, "fd:", &p)) {
f37afb5a 428 fd_start_outgoing_migration(s, p, &local_err);
cab30143 429#endif
99a0db9b 430 } else {
e1c37d0e
LC
431 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
432 return;
cab30143
JQ
433 }
434
f37afb5a 435 if (local_err) {
342ab8d1 436 migrate_fd_error(s);
f37afb5a 437 error_propagate(errp, local_err);
e1c37d0e 438 return;
1299c631 439 }
cab30143
JQ
440}
441
6cdedb07 442void qmp_migrate_cancel(Error **errp)
cab30143 443{
17549e84 444 migrate_fd_cancel(migrate_get_current());
cab30143
JQ
445}
446
9e1ba4cc
OW
447void qmp_migrate_set_cache_size(int64_t value, Error **errp)
448{
449 MigrationState *s = migrate_get_current();
450
451 /* Check for truncation */
452 if (value != (size_t)value) {
453 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
454 "exceeding address space");
455 return;
456 }
457
458 s->xbzrle_cache_size = xbzrle_cache_resize(value);
459}
460
461int64_t qmp_query_migrate_cache_size(Error **errp)
462{
463 return migrate_xbzrle_cache_size();
464}
465
3dc85383 466void qmp_migrate_set_speed(int64_t value, Error **errp)
cab30143 467{
cab30143
JQ
468 MigrationState *s;
469
3dc85383
LC
470 if (value < 0) {
471 value = 0;
99a0db9b 472 }
442773ce
PB
473 if (value > SIZE_MAX) {
474 value = SIZE_MAX;
475 }
cab30143 476
17549e84 477 s = migrate_get_current();
3dc85383 478 s->bandwidth_limit = value;
442773ce
PB
479 if (s->file) {
480 qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
481 }
cab30143
JQ
482}
483
4f0a993b 484void qmp_migrate_set_downtime(double value, Error **errp)
cab30143 485{
4f0a993b
LC
486 value *= 1e9;
487 value = MAX(0, MIN(UINT64_MAX, value));
488 max_downtime = (uint64_t)value;
99a0db9b 489}
17ad9b35 490
60d9222c
MH
491bool migrate_rdma_pin_all(void)
492{
493 MigrationState *s;
494
495 s = migrate_get_current();
496
497 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL];
498}
499
bde1e2ec
CV
500bool migrate_auto_converge(void)
501{
502 MigrationState *s;
503
504 s = migrate_get_current();
505
506 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
507}
508
323004a3
PL
509bool migrate_zero_blocks(void)
510{
511 MigrationState *s;
512
513 s = migrate_get_current();
514
515 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
516}
517
17ad9b35
OW
518int migrate_use_xbzrle(void)
519{
520 MigrationState *s;
521
522 s = migrate_get_current();
523
524 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
525}
526
527int64_t migrate_xbzrle_cache_size(void)
528{
529 MigrationState *s;
530
531 s = migrate_get_current();
532
533 return s->xbzrle_cache_size;
534}
0d82d0e8
JQ
535
536/* migration thread support */
537
5f496a1b 538static void *migration_thread(void *opaque)
0d82d0e8 539{
9848a404 540 MigrationState *s = opaque;
0d82d0e8 541 int64_t initial_time = qemu_get_clock_ms(rt_clock);
be7172e2 542 int64_t initial_bytes = 0;
0d82d0e8 543 int64_t max_size = 0;
a3fa1d78
PB
544 int64_t start_time = initial_time;
545 bool old_vm_running = false;
76f5933a 546
76f5933a 547 DPRINTF("beginning savevm\n");
dba433c0 548 qemu_savevm_state_begin(s->file, &s->params);
0d82d0e8 549
29ae8a41
MH
550 migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);
551
dba433c0 552 while (s->state == MIG_STATE_ACTIVE) {
a3e879cd 553 int64_t current_time;
c369f40d 554 uint64_t pending_size;
0d82d0e8 555
a0ff044b 556 if (!qemu_file_rate_limit(s->file)) {
c369f40d
JQ
557 DPRINTF("iterate\n");
558 pending_size = qemu_savevm_state_pending(s->file, max_size);
559 DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
b22ff1fb 560 if (pending_size && pending_size >= max_size) {
dba433c0 561 qemu_savevm_state_iterate(s->file);
c369f40d 562 } else {
0e1146a7
KW
563 int ret;
564
c369f40d 565 DPRINTF("done iterating\n");
32c835ba 566 qemu_mutex_lock_iothread();
c369f40d
JQ
567 start_time = qemu_get_clock_ms(rt_clock);
568 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
a3fa1d78 569 old_vm_running = runstate_is_running();
0e1146a7
KW
570
571 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
572 if (ret >= 0) {
573 qemu_file_set_rate_limit(s->file, INT_MAX);
574 qemu_savevm_state_complete(s->file);
575 }
32c835ba 576 qemu_mutex_unlock_iothread();
0e1146a7
KW
577
578 if (ret < 0) {
d58f574b 579 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
0e1146a7
KW
580 break;
581 }
582
059f896c 583 if (!qemu_file_get_error(s->file)) {
d58f574b 584 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
059f896c
PB
585 break;
586 }
c369f40d
JQ
587 }
588 }
f4410a5d 589
fd45ee2c 590 if (qemu_file_get_error(s->file)) {
d58f574b 591 migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
fd45ee2c
PB
592 break;
593 }
a3e879cd 594 current_time = qemu_get_clock_ms(rt_clock);
0d82d0e8 595 if (current_time >= initial_time + BUFFER_DELAY) {
be7172e2 596 uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
77417f10 597 uint64_t time_spent = current_time - initial_time;
0d82d0e8
JQ
598 double bandwidth = transferred_bytes / time_spent;
599 max_size = bandwidth * migrate_max_downtime() / 1000000;
600
7e114f8c
MH
601 s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
602 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
603
0d82d0e8
JQ
604 DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64
605 " bandwidth %g max_size %" PRId64 "\n",
606 transferred_bytes, time_spent, bandwidth, max_size);
90f8ae72
JQ
607 /* if we haven't sent anything, we don't want to recalculate
608 10000 is a small enough number for our purposes */
609 if (s->dirty_bytes_rate && transferred_bytes > 10000) {
610 s->expected_downtime = s->dirty_bytes_rate / bandwidth;
611 }
0d82d0e8 612
1964a397 613 qemu_file_reset_rate_limit(s->file);
0d82d0e8 614 initial_time = current_time;
be7172e2 615 initial_bytes = qemu_ftell(s->file);
0d82d0e8 616 }
a0ff044b 617 if (qemu_file_rate_limit(s->file)) {
0d82d0e8
JQ
618 /* usleep expects microseconds */
619 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
620 }
a3fa1d78
PB
621 }
622
f4410a5d 623 qemu_mutex_lock_iothread();
a3fa1d78
PB
624 if (s->state == MIG_STATE_COMPLETED) {
625 int64_t end_time = qemu_get_clock_ms(rt_clock);
626 s->total_time = end_time - s->total_time;
627 s->downtime = end_time - start_time;
628 runstate_set(RUN_STATE_POSTMIGRATE);
629 } else {
630 if (old_vm_running) {
a3fa1d78 631 vm_start();
dba433c0 632 }
0d82d0e8 633 }
bb1fadc4 634 qemu_bh_schedule(s->cleanup_bh);
dba433c0 635 qemu_mutex_unlock_iothread();
f4410a5d 636
0d82d0e8
JQ
637 return NULL;
638}
639
9848a404 640void migrate_fd_connect(MigrationState *s)
0d82d0e8 641{
29ae8a41
MH
642 s->state = MIG_STATE_SETUP;
643 trace_migrate_set_state(MIG_STATE_SETUP);
c09e5bb1 644
cc283e3b
JQ
645 /* This is a best 1st approximation. ns to ms */
646 s->expected_downtime = max_downtime/1000000;
bb1fadc4 647 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
0d82d0e8 648
442773ce
PB
649 qemu_file_set_rate_limit(s->file,
650 s->bandwidth_limit / XFER_LIMIT_RATIO);
651
5f496a1b 652 qemu_thread_create(&s->thread, migration_thread, s,
bb1fadc4 653 QEMU_THREAD_JOINABLE);
0d3b26f5 654 notifier_list_notify(&migration_state_notifiers, s);
0d82d0e8 655}