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