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