]>
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 | 38 | enum { |
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 |
57 | static 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 | 64 | MigrationState *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 ¤t_migration; | |
74 | } | |
75 | ||
43eaae28 | 76 | void 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 | 99 | static 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 |
124 | void 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 */ | |
138 | static uint64_t max_downtime = 30000000; | |
139 | ||
140 | uint64_t migrate_max_downtime(void) | |
141 | { | |
142 | return max_downtime; | |
143 | } | |
144 | ||
bbf6da32 OW |
145 | MigrationCapabilityStatusList *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 |
169 | static 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 | 182 | MigrationInfo *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"); | |
ed4fbd10 | 194 | info->has_total_time = false; |
29ae8a41 | 195 | break; |
17549e84 | 196 | case MIG_STATE_ACTIVE: |
791e7c82 LC |
197 | info->has_status = true; |
198 | info->status = g_strdup("active"); | |
7aa939af JQ |
199 | info->has_total_time = true; |
200 | info->total_time = qemu_get_clock_ms(rt_clock) | |
201 | - s->total_time; | |
2c52ddf1 JQ |
202 | info->has_expected_downtime = true; |
203 | info->expected_downtime = s->expected_downtime; | |
ed4fbd10 MH |
204 | info->has_setup_time = true; |
205 | info->setup_time = s->setup_time; | |
17549e84 | 206 | |
791e7c82 LC |
207 | info->has_ram = true; |
208 | info->ram = g_malloc0(sizeof(*info->ram)); | |
209 | info->ram->transferred = ram_bytes_transferred(); | |
210 | info->ram->remaining = ram_bytes_remaining(); | |
211 | info->ram->total = ram_bytes_total(); | |
004d4c10 | 212 | info->ram->duplicate = dup_mig_pages_transferred(); |
f1c72795 | 213 | info->ram->skipped = skipped_mig_pages_transferred(); |
004d4c10 OW |
214 | info->ram->normal = norm_mig_pages_transferred(); |
215 | info->ram->normal_bytes = norm_mig_bytes_transferred(); | |
8d017193 | 216 | info->ram->dirty_pages_rate = s->dirty_pages_rate; |
7e114f8c | 217 | info->ram->mbps = s->mbps; |
8d017193 | 218 | |
17549e84 | 219 | if (blk_mig_active()) { |
791e7c82 LC |
220 | info->has_disk = true; |
221 | info->disk = g_malloc0(sizeof(*info->disk)); | |
222 | info->disk->transferred = blk_mig_bytes_transferred(); | |
223 | info->disk->remaining = blk_mig_bytes_remaining(); | |
224 | info->disk->total = blk_mig_bytes_total(); | |
ff8d81d8 | 225 | } |
f36d55af OW |
226 | |
227 | get_xbzrle_cache_stats(info); | |
17549e84 JQ |
228 | break; |
229 | case MIG_STATE_COMPLETED: | |
f36d55af OW |
230 | get_xbzrle_cache_stats(info); |
231 | ||
791e7c82 LC |
232 | info->has_status = true; |
233 | info->status = g_strdup("completed"); | |
7aa939af | 234 | info->total_time = s->total_time; |
9c5a9fcf JQ |
235 | info->has_downtime = true; |
236 | info->downtime = s->downtime; | |
ed4fbd10 MH |
237 | info->has_setup_time = true; |
238 | info->setup_time = s->setup_time; | |
d5f8a570 JQ |
239 | |
240 | info->has_ram = true; | |
241 | info->ram = g_malloc0(sizeof(*info->ram)); | |
242 | info->ram->transferred = ram_bytes_transferred(); | |
243 | info->ram->remaining = 0; | |
244 | info->ram->total = ram_bytes_total(); | |
004d4c10 | 245 | info->ram->duplicate = dup_mig_pages_transferred(); |
f1c72795 | 246 | info->ram->skipped = skipped_mig_pages_transferred(); |
004d4c10 OW |
247 | info->ram->normal = norm_mig_pages_transferred(); |
248 | info->ram->normal_bytes = norm_mig_bytes_transferred(); | |
7e114f8c | 249 | info->ram->mbps = s->mbps; |
17549e84 JQ |
250 | break; |
251 | case MIG_STATE_ERROR: | |
791e7c82 LC |
252 | info->has_status = true; |
253 | info->status = g_strdup("failed"); | |
17549e84 JQ |
254 | break; |
255 | case MIG_STATE_CANCELLED: | |
791e7c82 LC |
256 | info->has_status = true; |
257 | info->status = g_strdup("cancelled"); | |
17549e84 | 258 | break; |
5bb7910a | 259 | } |
791e7c82 LC |
260 | |
261 | return info; | |
5bb7910a AL |
262 | } |
263 | ||
00458433 OW |
264 | void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params, |
265 | Error **errp) | |
266 | { | |
267 | MigrationState *s = migrate_get_current(); | |
268 | MigrationCapabilityStatusList *cap; | |
269 | ||
29ae8a41 | 270 | if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) { |
00458433 OW |
271 | error_set(errp, QERR_MIGRATION_ACTIVE); |
272 | return; | |
273 | } | |
274 | ||
275 | for (cap = params; cap; cap = cap->next) { | |
276 | s->enabled_capabilities[cap->value->capability] = cap->value->state; | |
277 | } | |
278 | } | |
279 | ||
065e2813 AL |
280 | /* shared migration helpers */ |
281 | ||
bb1fadc4 | 282 | static void migrate_fd_cleanup(void *opaque) |
065e2813 | 283 | { |
bb1fadc4 PB |
284 | MigrationState *s = opaque; |
285 | ||
286 | qemu_bh_delete(s->cleanup_bh); | |
287 | s->cleanup_bh = NULL; | |
288 | ||
065e2813 | 289 | if (s->file) { |
d0f2c4c6 | 290 | DPRINTF("closing file\n"); |
404a7c05 PB |
291 | qemu_mutex_unlock_iothread(); |
292 | qemu_thread_join(&s->thread); | |
293 | qemu_mutex_lock_iothread(); | |
294 | ||
6f190a06 PB |
295 | qemu_fclose(s->file); |
296 | s->file = NULL; | |
065e2813 AL |
297 | } |
298 | ||
a3fa1d78 | 299 | assert(s->state != MIG_STATE_ACTIVE); |
7a2c1721 | 300 | |
a3fa1d78 | 301 | if (s->state != MIG_STATE_COMPLETED) { |
7a2c1721 PB |
302 | qemu_savevm_state_cancel(); |
303 | } | |
a3fa1d78 PB |
304 | |
305 | notifier_list_notify(&migration_state_notifiers, s); | |
065e2813 AL |
306 | } |
307 | ||
d58f574b | 308 | static void migrate_set_state(MigrationState *s, int old_state, int new_state) |
f4410a5d | 309 | { |
d58f574b | 310 | if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) { |
f4410a5d PB |
311 | trace_migrate_set_state(new_state); |
312 | } | |
313 | } | |
314 | ||
8b6b99b3 | 315 | void migrate_fd_error(MigrationState *s) |
065e2813 | 316 | { |
8b6b99b3 | 317 | DPRINTF("setting error state\n"); |
bb1fadc4 PB |
318 | assert(s->file == NULL); |
319 | s->state = MIG_STATE_ERROR; | |
320 | trace_migrate_set_state(MIG_STATE_ERROR); | |
321 | notifier_list_notify(&migration_state_notifiers, s); | |
458cf28e JQ |
322 | } |
323 | ||
0edda1c4 | 324 | static void migrate_fd_cancel(MigrationState *s) |
065e2813 | 325 | { |
d0f2c4c6 | 326 | DPRINTF("cancelling migration\n"); |
065e2813 | 327 | |
d58f574b | 328 | migrate_set_state(s, s->state, MIG_STATE_CANCELLED); |
065e2813 AL |
329 | } |
330 | ||
99a0db9b GH |
331 | void add_migration_state_change_notifier(Notifier *notify) |
332 | { | |
333 | notifier_list_add(&migration_state_notifiers, notify); | |
334 | } | |
335 | ||
336 | void remove_migration_state_change_notifier(Notifier *notify) | |
337 | { | |
31552529 | 338 | notifier_remove(notify); |
99a0db9b GH |
339 | } |
340 | ||
afe2df69 GH |
341 | bool migration_is_active(MigrationState *s) |
342 | { | |
343 | return s->state == MIG_STATE_ACTIVE; | |
344 | } | |
345 | ||
7073693b | 346 | bool migration_has_finished(MigrationState *s) |
99a0db9b | 347 | { |
7073693b | 348 | return s->state == MIG_STATE_COMPLETED; |
99a0db9b | 349 | } |
0edda1c4 | 350 | |
afe2df69 GH |
351 | bool migration_has_failed(MigrationState *s) |
352 | { | |
353 | return (s->state == MIG_STATE_CANCELLED || | |
354 | s->state == MIG_STATE_ERROR); | |
355 | } | |
356 | ||
6607ae23 | 357 | static MigrationState *migrate_init(const MigrationParams *params) |
0edda1c4 | 358 | { |
17549e84 | 359 | MigrationState *s = migrate_get_current(); |
d0ae46c1 | 360 | int64_t bandwidth_limit = s->bandwidth_limit; |
bbf6da32 | 361 | bool enabled_capabilities[MIGRATION_CAPABILITY_MAX]; |
17ad9b35 | 362 | int64_t xbzrle_cache_size = s->xbzrle_cache_size; |
bbf6da32 OW |
363 | |
364 | memcpy(enabled_capabilities, s->enabled_capabilities, | |
365 | sizeof(enabled_capabilities)); | |
0edda1c4 | 366 | |
17549e84 | 367 | memset(s, 0, sizeof(*s)); |
6607ae23 | 368 | s->params = *params; |
bbf6da32 OW |
369 | memcpy(s->enabled_capabilities, enabled_capabilities, |
370 | sizeof(enabled_capabilities)); | |
17ad9b35 | 371 | s->xbzrle_cache_size = xbzrle_cache_size; |
1299c631 | 372 | |
0edda1c4 | 373 | s->bandwidth_limit = bandwidth_limit; |
d5934dde | 374 | s->state = MIG_STATE_SETUP; |
c09e5bb1 | 375 | trace_migrate_set_state(MIG_STATE_SETUP); |
0edda1c4 | 376 | |
c09e5bb1 | 377 | s->total_time = qemu_get_clock_ms(rt_clock); |
0edda1c4 JQ |
378 | return s; |
379 | } | |
cab30143 | 380 | |
fa2756b7 AL |
381 | static GSList *migration_blockers; |
382 | ||
383 | void migrate_add_blocker(Error *reason) | |
384 | { | |
385 | migration_blockers = g_slist_prepend(migration_blockers, reason); | |
386 | } | |
387 | ||
388 | void migrate_del_blocker(Error *reason) | |
389 | { | |
390 | migration_blockers = g_slist_remove(migration_blockers, reason); | |
391 | } | |
392 | ||
e1c37d0e LC |
393 | void qmp_migrate(const char *uri, bool has_blk, bool blk, |
394 | bool has_inc, bool inc, bool has_detach, bool detach, | |
395 | Error **errp) | |
cab30143 | 396 | { |
be7059cd | 397 | Error *local_err = NULL; |
17549e84 | 398 | MigrationState *s = migrate_get_current(); |
6607ae23 | 399 | MigrationParams params; |
cab30143 | 400 | const char *p; |
cab30143 | 401 | |
6607ae23 IY |
402 | params.blk = blk; |
403 | params.shared = inc; | |
404 | ||
29ae8a41 | 405 | if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) { |
e1c37d0e LC |
406 | error_set(errp, QERR_MIGRATION_ACTIVE); |
407 | return; | |
cab30143 JQ |
408 | } |
409 | ||
e1c37d0e LC |
410 | if (qemu_savevm_state_blocked(errp)) { |
411 | return; | |
cab30143 JQ |
412 | } |
413 | ||
fa2756b7 | 414 | if (migration_blockers) { |
e1c37d0e LC |
415 | *errp = error_copy(migration_blockers->data); |
416 | return; | |
fa2756b7 AL |
417 | } |
418 | ||
6607ae23 | 419 | s = migrate_init(¶ms); |
cab30143 JQ |
420 | |
421 | if (strstart(uri, "tcp:", &p)) { | |
f37afb5a | 422 | tcp_start_outgoing_migration(s, p, &local_err); |
2da776db MH |
423 | #ifdef CONFIG_RDMA |
424 | } else if (strstart(uri, "x-rdma:", &p)) { | |
425 | rdma_start_outgoing_migration(s, p, &local_err); | |
426 | #endif | |
cab30143 JQ |
427 | #if !defined(WIN32) |
428 | } else if (strstart(uri, "exec:", &p)) { | |
f37afb5a | 429 | exec_start_outgoing_migration(s, p, &local_err); |
cab30143 | 430 | } else if (strstart(uri, "unix:", &p)) { |
f37afb5a | 431 | unix_start_outgoing_migration(s, p, &local_err); |
cab30143 | 432 | } else if (strstart(uri, "fd:", &p)) { |
f37afb5a | 433 | fd_start_outgoing_migration(s, p, &local_err); |
cab30143 | 434 | #endif |
99a0db9b | 435 | } else { |
e1c37d0e LC |
436 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol"); |
437 | return; | |
cab30143 JQ |
438 | } |
439 | ||
f37afb5a | 440 | if (local_err) { |
342ab8d1 | 441 | migrate_fd_error(s); |
f37afb5a | 442 | error_propagate(errp, local_err); |
e1c37d0e | 443 | return; |
1299c631 | 444 | } |
cab30143 JQ |
445 | } |
446 | ||
6cdedb07 | 447 | void qmp_migrate_cancel(Error **errp) |
cab30143 | 448 | { |
17549e84 | 449 | migrate_fd_cancel(migrate_get_current()); |
cab30143 JQ |
450 | } |
451 | ||
9e1ba4cc OW |
452 | void qmp_migrate_set_cache_size(int64_t value, Error **errp) |
453 | { | |
454 | MigrationState *s = migrate_get_current(); | |
455 | ||
456 | /* Check for truncation */ | |
457 | if (value != (size_t)value) { | |
458 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", | |
459 | "exceeding address space"); | |
460 | return; | |
461 | } | |
462 | ||
463 | s->xbzrle_cache_size = xbzrle_cache_resize(value); | |
464 | } | |
465 | ||
466 | int64_t qmp_query_migrate_cache_size(Error **errp) | |
467 | { | |
468 | return migrate_xbzrle_cache_size(); | |
469 | } | |
470 | ||
3dc85383 | 471 | void qmp_migrate_set_speed(int64_t value, Error **errp) |
cab30143 | 472 | { |
cab30143 JQ |
473 | MigrationState *s; |
474 | ||
3dc85383 LC |
475 | if (value < 0) { |
476 | value = 0; | |
99a0db9b | 477 | } |
442773ce PB |
478 | if (value > SIZE_MAX) { |
479 | value = SIZE_MAX; | |
480 | } | |
cab30143 | 481 | |
17549e84 | 482 | s = migrate_get_current(); |
3dc85383 | 483 | s->bandwidth_limit = value; |
442773ce PB |
484 | if (s->file) { |
485 | qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO); | |
486 | } | |
cab30143 JQ |
487 | } |
488 | ||
4f0a993b | 489 | void qmp_migrate_set_downtime(double value, Error **errp) |
cab30143 | 490 | { |
4f0a993b LC |
491 | value *= 1e9; |
492 | value = MAX(0, MIN(UINT64_MAX, value)); | |
493 | max_downtime = (uint64_t)value; | |
99a0db9b | 494 | } |
17ad9b35 | 495 | |
60d9222c MH |
496 | bool migrate_rdma_pin_all(void) |
497 | { | |
498 | MigrationState *s; | |
499 | ||
500 | s = migrate_get_current(); | |
501 | ||
502 | return s->enabled_capabilities[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL]; | |
503 | } | |
504 | ||
bde1e2ec CV |
505 | bool migrate_auto_converge(void) |
506 | { | |
507 | MigrationState *s; | |
508 | ||
509 | s = migrate_get_current(); | |
510 | ||
511 | return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE]; | |
512 | } | |
513 | ||
323004a3 PL |
514 | bool migrate_zero_blocks(void) |
515 | { | |
516 | MigrationState *s; | |
517 | ||
518 | s = migrate_get_current(); | |
519 | ||
520 | return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS]; | |
521 | } | |
522 | ||
17ad9b35 OW |
523 | int migrate_use_xbzrle(void) |
524 | { | |
525 | MigrationState *s; | |
526 | ||
527 | s = migrate_get_current(); | |
528 | ||
529 | return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE]; | |
530 | } | |
531 | ||
532 | int64_t migrate_xbzrle_cache_size(void) | |
533 | { | |
534 | MigrationState *s; | |
535 | ||
536 | s = migrate_get_current(); | |
537 | ||
538 | return s->xbzrle_cache_size; | |
539 | } | |
0d82d0e8 JQ |
540 | |
541 | /* migration thread support */ | |
542 | ||
5f496a1b | 543 | static void *migration_thread(void *opaque) |
0d82d0e8 | 544 | { |
9848a404 | 545 | MigrationState *s = opaque; |
0d82d0e8 | 546 | int64_t initial_time = qemu_get_clock_ms(rt_clock); |
ed4fbd10 | 547 | int64_t setup_start = qemu_get_clock_ms(host_clock); |
be7172e2 | 548 | int64_t initial_bytes = 0; |
0d82d0e8 | 549 | int64_t max_size = 0; |
a3fa1d78 PB |
550 | int64_t start_time = initial_time; |
551 | bool old_vm_running = false; | |
76f5933a | 552 | |
76f5933a | 553 | DPRINTF("beginning savevm\n"); |
dba433c0 | 554 | qemu_savevm_state_begin(s->file, &s->params); |
0d82d0e8 | 555 | |
ed4fbd10 | 556 | s->setup_time = qemu_get_clock_ms(host_clock) - setup_start; |
29ae8a41 MH |
557 | migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE); |
558 | ||
ed4fbd10 MH |
559 | DPRINTF("setup complete\n"); |
560 | ||
dba433c0 | 561 | while (s->state == MIG_STATE_ACTIVE) { |
a3e879cd | 562 | int64_t current_time; |
c369f40d | 563 | uint64_t pending_size; |
0d82d0e8 | 564 | |
a0ff044b | 565 | if (!qemu_file_rate_limit(s->file)) { |
c369f40d JQ |
566 | DPRINTF("iterate\n"); |
567 | pending_size = qemu_savevm_state_pending(s->file, max_size); | |
568 | DPRINTF("pending size %lu max %lu\n", pending_size, max_size); | |
b22ff1fb | 569 | if (pending_size && pending_size >= max_size) { |
dba433c0 | 570 | qemu_savevm_state_iterate(s->file); |
c369f40d | 571 | } else { |
0e1146a7 KW |
572 | int ret; |
573 | ||
c369f40d | 574 | DPRINTF("done iterating\n"); |
32c835ba | 575 | qemu_mutex_lock_iothread(); |
c369f40d JQ |
576 | start_time = qemu_get_clock_ms(rt_clock); |
577 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); | |
a3fa1d78 | 578 | old_vm_running = runstate_is_running(); |
0e1146a7 KW |
579 | |
580 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); | |
581 | if (ret >= 0) { | |
582 | qemu_file_set_rate_limit(s->file, INT_MAX); | |
583 | qemu_savevm_state_complete(s->file); | |
584 | } | |
32c835ba | 585 | qemu_mutex_unlock_iothread(); |
0e1146a7 KW |
586 | |
587 | if (ret < 0) { | |
d58f574b | 588 | migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR); |
0e1146a7 KW |
589 | break; |
590 | } | |
591 | ||
059f896c | 592 | if (!qemu_file_get_error(s->file)) { |
d58f574b | 593 | migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED); |
059f896c PB |
594 | break; |
595 | } | |
c369f40d JQ |
596 | } |
597 | } | |
f4410a5d | 598 | |
fd45ee2c | 599 | if (qemu_file_get_error(s->file)) { |
d58f574b | 600 | migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR); |
fd45ee2c PB |
601 | break; |
602 | } | |
a3e879cd | 603 | current_time = qemu_get_clock_ms(rt_clock); |
0d82d0e8 | 604 | if (current_time >= initial_time + BUFFER_DELAY) { |
be7172e2 | 605 | uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes; |
77417f10 | 606 | uint64_t time_spent = current_time - initial_time; |
0d82d0e8 JQ |
607 | double bandwidth = transferred_bytes / time_spent; |
608 | max_size = bandwidth * migrate_max_downtime() / 1000000; | |
609 | ||
7e114f8c MH |
610 | s->mbps = time_spent ? (((double) transferred_bytes * 8.0) / |
611 | ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1; | |
612 | ||
0d82d0e8 JQ |
613 | DPRINTF("transferred %" PRIu64 " time_spent %" PRIu64 |
614 | " bandwidth %g max_size %" PRId64 "\n", | |
615 | transferred_bytes, time_spent, bandwidth, max_size); | |
90f8ae72 JQ |
616 | /* if we haven't sent anything, we don't want to recalculate |
617 | 10000 is a small enough number for our purposes */ | |
618 | if (s->dirty_bytes_rate && transferred_bytes > 10000) { | |
619 | s->expected_downtime = s->dirty_bytes_rate / bandwidth; | |
620 | } | |
0d82d0e8 | 621 | |
1964a397 | 622 | qemu_file_reset_rate_limit(s->file); |
0d82d0e8 | 623 | initial_time = current_time; |
be7172e2 | 624 | initial_bytes = qemu_ftell(s->file); |
0d82d0e8 | 625 | } |
a0ff044b | 626 | if (qemu_file_rate_limit(s->file)) { |
0d82d0e8 JQ |
627 | /* usleep expects microseconds */ |
628 | g_usleep((initial_time + BUFFER_DELAY - current_time)*1000); | |
629 | } | |
a3fa1d78 PB |
630 | } |
631 | ||
f4410a5d | 632 | qemu_mutex_lock_iothread(); |
a3fa1d78 PB |
633 | if (s->state == MIG_STATE_COMPLETED) { |
634 | int64_t end_time = qemu_get_clock_ms(rt_clock); | |
635 | s->total_time = end_time - s->total_time; | |
636 | s->downtime = end_time - start_time; | |
637 | runstate_set(RUN_STATE_POSTMIGRATE); | |
638 | } else { | |
639 | if (old_vm_running) { | |
a3fa1d78 | 640 | vm_start(); |
dba433c0 | 641 | } |
0d82d0e8 | 642 | } |
bb1fadc4 | 643 | qemu_bh_schedule(s->cleanup_bh); |
dba433c0 | 644 | qemu_mutex_unlock_iothread(); |
f4410a5d | 645 | |
0d82d0e8 JQ |
646 | return NULL; |
647 | } | |
648 | ||
9848a404 | 649 | void migrate_fd_connect(MigrationState *s) |
0d82d0e8 | 650 | { |
29ae8a41 MH |
651 | s->state = MIG_STATE_SETUP; |
652 | trace_migrate_set_state(MIG_STATE_SETUP); | |
c09e5bb1 | 653 | |
cc283e3b JQ |
654 | /* This is a best 1st approximation. ns to ms */ |
655 | s->expected_downtime = max_downtime/1000000; | |
bb1fadc4 | 656 | s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s); |
0d82d0e8 | 657 | |
442773ce PB |
658 | qemu_file_set_rate_limit(s->file, |
659 | s->bandwidth_limit / XFER_LIMIT_RATIO); | |
660 | ||
5f496a1b | 661 | qemu_thread_create(&s->thread, migration_thread, s, |
bb1fadc4 | 662 | QEMU_THREAD_JOINABLE); |
0d3b26f5 | 663 | notifier_list_notify(&migration_state_notifiers, s); |
0d82d0e8 | 664 | } |