]>
Commit | Line | Data |
---|---|---|
56e93d26 JQ |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
76cc7b58 JQ |
5 | * Copyright (c) 2011-2015 Red Hat Inc |
6 | * | |
7 | * Authors: | |
8 | * Juan Quintela <quintela@redhat.com> | |
56e93d26 JQ |
9 | * |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
26 | * THE SOFTWARE. | |
27 | */ | |
28 | #include <stdint.h> | |
56e93d26 | 29 | #include <zlib.h> |
56e93d26 JQ |
30 | #include "qemu/bitops.h" |
31 | #include "qemu/bitmap.h" | |
7205c9ec JQ |
32 | #include "qemu/timer.h" |
33 | #include "qemu/main-loop.h" | |
56e93d26 JQ |
34 | #include "migration/migration.h" |
35 | #include "exec/address-spaces.h" | |
36 | #include "migration/page_cache.h" | |
56e93d26 | 37 | #include "qemu/error-report.h" |
56e93d26 | 38 | #include "trace.h" |
56e93d26 | 39 | #include "exec/ram_addr.h" |
56e93d26 JQ |
40 | #include "qemu/rcu_queue.h" |
41 | ||
42 | #ifdef DEBUG_MIGRATION_RAM | |
43 | #define DPRINTF(fmt, ...) \ | |
44 | do { fprintf(stdout, "migration_ram: " fmt, ## __VA_ARGS__); } while (0) | |
45 | #else | |
46 | #define DPRINTF(fmt, ...) \ | |
47 | do { } while (0) | |
48 | #endif | |
49 | ||
50 | static bool mig_throttle_on; | |
51 | static int dirty_rate_high_cnt; | |
52 | static void check_guest_throttling(void); | |
53 | ||
54 | static uint64_t bitmap_sync_count; | |
55 | ||
56 | /***********************************************************/ | |
57 | /* ram save/restore */ | |
58 | ||
59 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ | |
60 | #define RAM_SAVE_FLAG_COMPRESS 0x02 | |
61 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 | |
62 | #define RAM_SAVE_FLAG_PAGE 0x08 | |
63 | #define RAM_SAVE_FLAG_EOS 0x10 | |
64 | #define RAM_SAVE_FLAG_CONTINUE 0x20 | |
65 | #define RAM_SAVE_FLAG_XBZRLE 0x40 | |
66 | /* 0x80 is reserved in migration.h start with 0x100 next */ | |
67 | #define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100 | |
68 | ||
69 | static const uint8_t ZERO_TARGET_PAGE[TARGET_PAGE_SIZE]; | |
70 | ||
71 | static inline bool is_zero_range(uint8_t *p, uint64_t size) | |
72 | { | |
73 | return buffer_find_nonzero_offset(p, size) == size; | |
74 | } | |
75 | ||
76 | /* struct contains XBZRLE cache and a static page | |
77 | used by the compression */ | |
78 | static struct { | |
79 | /* buffer used for XBZRLE encoding */ | |
80 | uint8_t *encoded_buf; | |
81 | /* buffer for storing page content */ | |
82 | uint8_t *current_buf; | |
83 | /* Cache for XBZRLE, Protected by lock. */ | |
84 | PageCache *cache; | |
85 | QemuMutex lock; | |
86 | } XBZRLE; | |
87 | ||
88 | /* buffer used for XBZRLE decoding */ | |
89 | static uint8_t *xbzrle_decoded_buf; | |
90 | ||
91 | static void XBZRLE_cache_lock(void) | |
92 | { | |
93 | if (migrate_use_xbzrle()) | |
94 | qemu_mutex_lock(&XBZRLE.lock); | |
95 | } | |
96 | ||
97 | static void XBZRLE_cache_unlock(void) | |
98 | { | |
99 | if (migrate_use_xbzrle()) | |
100 | qemu_mutex_unlock(&XBZRLE.lock); | |
101 | } | |
102 | ||
103 | /* | |
104 | * called from qmp_migrate_set_cache_size in main thread, possibly while | |
105 | * a migration is in progress. | |
106 | * A running migration maybe using the cache and might finish during this | |
107 | * call, hence changes to the cache are protected by XBZRLE.lock(). | |
108 | */ | |
109 | int64_t xbzrle_cache_resize(int64_t new_size) | |
110 | { | |
111 | PageCache *new_cache; | |
112 | int64_t ret; | |
113 | ||
114 | if (new_size < TARGET_PAGE_SIZE) { | |
115 | return -1; | |
116 | } | |
117 | ||
118 | XBZRLE_cache_lock(); | |
119 | ||
120 | if (XBZRLE.cache != NULL) { | |
121 | if (pow2floor(new_size) == migrate_xbzrle_cache_size()) { | |
122 | goto out_new_size; | |
123 | } | |
124 | new_cache = cache_init(new_size / TARGET_PAGE_SIZE, | |
125 | TARGET_PAGE_SIZE); | |
126 | if (!new_cache) { | |
127 | error_report("Error creating cache"); | |
128 | ret = -1; | |
129 | goto out; | |
130 | } | |
131 | ||
132 | cache_fini(XBZRLE.cache); | |
133 | XBZRLE.cache = new_cache; | |
134 | } | |
135 | ||
136 | out_new_size: | |
137 | ret = pow2floor(new_size); | |
138 | out: | |
139 | XBZRLE_cache_unlock(); | |
140 | return ret; | |
141 | } | |
142 | ||
143 | /* accounting for migration statistics */ | |
144 | typedef struct AccountingInfo { | |
145 | uint64_t dup_pages; | |
146 | uint64_t skipped_pages; | |
147 | uint64_t norm_pages; | |
148 | uint64_t iterations; | |
149 | uint64_t xbzrle_bytes; | |
150 | uint64_t xbzrle_pages; | |
151 | uint64_t xbzrle_cache_miss; | |
152 | double xbzrle_cache_miss_rate; | |
153 | uint64_t xbzrle_overflows; | |
154 | } AccountingInfo; | |
155 | ||
156 | static AccountingInfo acct_info; | |
157 | ||
158 | static void acct_clear(void) | |
159 | { | |
160 | memset(&acct_info, 0, sizeof(acct_info)); | |
161 | } | |
162 | ||
163 | uint64_t dup_mig_bytes_transferred(void) | |
164 | { | |
165 | return acct_info.dup_pages * TARGET_PAGE_SIZE; | |
166 | } | |
167 | ||
168 | uint64_t dup_mig_pages_transferred(void) | |
169 | { | |
170 | return acct_info.dup_pages; | |
171 | } | |
172 | ||
173 | uint64_t skipped_mig_bytes_transferred(void) | |
174 | { | |
175 | return acct_info.skipped_pages * TARGET_PAGE_SIZE; | |
176 | } | |
177 | ||
178 | uint64_t skipped_mig_pages_transferred(void) | |
179 | { | |
180 | return acct_info.skipped_pages; | |
181 | } | |
182 | ||
183 | uint64_t norm_mig_bytes_transferred(void) | |
184 | { | |
185 | return acct_info.norm_pages * TARGET_PAGE_SIZE; | |
186 | } | |
187 | ||
188 | uint64_t norm_mig_pages_transferred(void) | |
189 | { | |
190 | return acct_info.norm_pages; | |
191 | } | |
192 | ||
193 | uint64_t xbzrle_mig_bytes_transferred(void) | |
194 | { | |
195 | return acct_info.xbzrle_bytes; | |
196 | } | |
197 | ||
198 | uint64_t xbzrle_mig_pages_transferred(void) | |
199 | { | |
200 | return acct_info.xbzrle_pages; | |
201 | } | |
202 | ||
203 | uint64_t xbzrle_mig_pages_cache_miss(void) | |
204 | { | |
205 | return acct_info.xbzrle_cache_miss; | |
206 | } | |
207 | ||
208 | double xbzrle_mig_cache_miss_rate(void) | |
209 | { | |
210 | return acct_info.xbzrle_cache_miss_rate; | |
211 | } | |
212 | ||
213 | uint64_t xbzrle_mig_pages_overflow(void) | |
214 | { | |
215 | return acct_info.xbzrle_overflows; | |
216 | } | |
217 | ||
218 | /* This is the last block that we have visited serching for dirty pages | |
219 | */ | |
220 | static RAMBlock *last_seen_block; | |
221 | /* This is the last block from where we have sent data */ | |
222 | static RAMBlock *last_sent_block; | |
223 | static ram_addr_t last_offset; | |
224 | static unsigned long *migration_bitmap; | |
dd631697 | 225 | static QemuMutex migration_bitmap_mutex; |
56e93d26 JQ |
226 | static uint64_t migration_dirty_pages; |
227 | static uint32_t last_version; | |
228 | static bool ram_bulk_stage; | |
229 | ||
b8fb8cb7 DDAG |
230 | /* used by the search for pages to send */ |
231 | struct PageSearchStatus { | |
232 | /* Current block being searched */ | |
233 | RAMBlock *block; | |
234 | /* Current offset to search from */ | |
235 | ram_addr_t offset; | |
236 | /* Set once we wrap around */ | |
237 | bool complete_round; | |
238 | }; | |
239 | typedef struct PageSearchStatus PageSearchStatus; | |
240 | ||
56e93d26 JQ |
241 | struct CompressParam { |
242 | bool start; | |
243 | bool done; | |
244 | QEMUFile *file; | |
245 | QemuMutex mutex; | |
246 | QemuCond cond; | |
247 | RAMBlock *block; | |
248 | ram_addr_t offset; | |
249 | }; | |
250 | typedef struct CompressParam CompressParam; | |
251 | ||
252 | struct DecompressParam { | |
253 | bool start; | |
254 | QemuMutex mutex; | |
255 | QemuCond cond; | |
256 | void *des; | |
257 | uint8 *compbuf; | |
258 | int len; | |
259 | }; | |
260 | typedef struct DecompressParam DecompressParam; | |
261 | ||
262 | static CompressParam *comp_param; | |
263 | static QemuThread *compress_threads; | |
264 | /* comp_done_cond is used to wake up the migration thread when | |
265 | * one of the compression threads has finished the compression. | |
266 | * comp_done_lock is used to co-work with comp_done_cond. | |
267 | */ | |
268 | static QemuMutex *comp_done_lock; | |
269 | static QemuCond *comp_done_cond; | |
270 | /* The empty QEMUFileOps will be used by file in CompressParam */ | |
271 | static const QEMUFileOps empty_ops = { }; | |
272 | ||
273 | static bool compression_switch; | |
274 | static bool quit_comp_thread; | |
275 | static bool quit_decomp_thread; | |
276 | static DecompressParam *decomp_param; | |
277 | static QemuThread *decompress_threads; | |
278 | static uint8_t *compressed_data_buf; | |
279 | ||
280 | static int do_compress_ram_page(CompressParam *param); | |
281 | ||
282 | static void *do_data_compress(void *opaque) | |
283 | { | |
284 | CompressParam *param = opaque; | |
285 | ||
286 | while (!quit_comp_thread) { | |
287 | qemu_mutex_lock(¶m->mutex); | |
288 | /* Re-check the quit_comp_thread in case of | |
289 | * terminate_compression_threads is called just before | |
290 | * qemu_mutex_lock(¶m->mutex) and after | |
291 | * while(!quit_comp_thread), re-check it here can make | |
292 | * sure the compression thread terminate as expected. | |
293 | */ | |
294 | while (!param->start && !quit_comp_thread) { | |
295 | qemu_cond_wait(¶m->cond, ¶m->mutex); | |
296 | } | |
297 | if (!quit_comp_thread) { | |
298 | do_compress_ram_page(param); | |
299 | } | |
300 | param->start = false; | |
301 | qemu_mutex_unlock(¶m->mutex); | |
302 | ||
303 | qemu_mutex_lock(comp_done_lock); | |
304 | param->done = true; | |
305 | qemu_cond_signal(comp_done_cond); | |
306 | qemu_mutex_unlock(comp_done_lock); | |
307 | } | |
308 | ||
309 | return NULL; | |
310 | } | |
311 | ||
312 | static inline void terminate_compression_threads(void) | |
313 | { | |
314 | int idx, thread_count; | |
315 | ||
316 | thread_count = migrate_compress_threads(); | |
317 | quit_comp_thread = true; | |
318 | for (idx = 0; idx < thread_count; idx++) { | |
319 | qemu_mutex_lock(&comp_param[idx].mutex); | |
320 | qemu_cond_signal(&comp_param[idx].cond); | |
321 | qemu_mutex_unlock(&comp_param[idx].mutex); | |
322 | } | |
323 | } | |
324 | ||
325 | void migrate_compress_threads_join(void) | |
326 | { | |
327 | int i, thread_count; | |
328 | ||
329 | if (!migrate_use_compression()) { | |
330 | return; | |
331 | } | |
332 | terminate_compression_threads(); | |
333 | thread_count = migrate_compress_threads(); | |
334 | for (i = 0; i < thread_count; i++) { | |
335 | qemu_thread_join(compress_threads + i); | |
336 | qemu_fclose(comp_param[i].file); | |
337 | qemu_mutex_destroy(&comp_param[i].mutex); | |
338 | qemu_cond_destroy(&comp_param[i].cond); | |
339 | } | |
340 | qemu_mutex_destroy(comp_done_lock); | |
341 | qemu_cond_destroy(comp_done_cond); | |
342 | g_free(compress_threads); | |
343 | g_free(comp_param); | |
344 | g_free(comp_done_cond); | |
345 | g_free(comp_done_lock); | |
346 | compress_threads = NULL; | |
347 | comp_param = NULL; | |
348 | comp_done_cond = NULL; | |
349 | comp_done_lock = NULL; | |
350 | } | |
351 | ||
352 | void migrate_compress_threads_create(void) | |
353 | { | |
354 | int i, thread_count; | |
355 | ||
356 | if (!migrate_use_compression()) { | |
357 | return; | |
358 | } | |
359 | quit_comp_thread = false; | |
360 | compression_switch = true; | |
361 | thread_count = migrate_compress_threads(); | |
362 | compress_threads = g_new0(QemuThread, thread_count); | |
363 | comp_param = g_new0(CompressParam, thread_count); | |
364 | comp_done_cond = g_new0(QemuCond, 1); | |
365 | comp_done_lock = g_new0(QemuMutex, 1); | |
366 | qemu_cond_init(comp_done_cond); | |
367 | qemu_mutex_init(comp_done_lock); | |
368 | for (i = 0; i < thread_count; i++) { | |
369 | /* com_param[i].file is just used as a dummy buffer to save data, set | |
370 | * it's ops to empty. | |
371 | */ | |
372 | comp_param[i].file = qemu_fopen_ops(NULL, &empty_ops); | |
373 | comp_param[i].done = true; | |
374 | qemu_mutex_init(&comp_param[i].mutex); | |
375 | qemu_cond_init(&comp_param[i].cond); | |
376 | qemu_thread_create(compress_threads + i, "compress", | |
377 | do_data_compress, comp_param + i, | |
378 | QEMU_THREAD_JOINABLE); | |
379 | } | |
380 | } | |
381 | ||
382 | /** | |
383 | * save_page_header: Write page header to wire | |
384 | * | |
385 | * If this is the 1st block, it also writes the block identification | |
386 | * | |
387 | * Returns: Number of bytes written | |
388 | * | |
389 | * @f: QEMUFile where to send the data | |
390 | * @block: block that contains the page we want to send | |
391 | * @offset: offset inside the block for the page | |
392 | * in the lower bits, it contains flags | |
393 | */ | |
394 | static size_t save_page_header(QEMUFile *f, RAMBlock *block, ram_addr_t offset) | |
395 | { | |
9f5f380b | 396 | size_t size, len; |
56e93d26 JQ |
397 | |
398 | qemu_put_be64(f, offset); | |
399 | size = 8; | |
400 | ||
401 | if (!(offset & RAM_SAVE_FLAG_CONTINUE)) { | |
9f5f380b LL |
402 | len = strlen(block->idstr); |
403 | qemu_put_byte(f, len); | |
404 | qemu_put_buffer(f, (uint8_t *)block->idstr, len); | |
405 | size += 1 + len; | |
56e93d26 JQ |
406 | } |
407 | return size; | |
408 | } | |
409 | ||
410 | /* Update the xbzrle cache to reflect a page that's been sent as all 0. | |
411 | * The important thing is that a stale (not-yet-0'd) page be replaced | |
412 | * by the new data. | |
413 | * As a bonus, if the page wasn't in the cache it gets added so that | |
414 | * when a small write is made into the 0'd page it gets XBZRLE sent | |
415 | */ | |
416 | static void xbzrle_cache_zero_page(ram_addr_t current_addr) | |
417 | { | |
418 | if (ram_bulk_stage || !migrate_use_xbzrle()) { | |
419 | return; | |
420 | } | |
421 | ||
422 | /* We don't care if this fails to allocate a new cache page | |
423 | * as long as it updated an old one */ | |
424 | cache_insert(XBZRLE.cache, current_addr, ZERO_TARGET_PAGE, | |
425 | bitmap_sync_count); | |
426 | } | |
427 | ||
428 | #define ENCODING_FLAG_XBZRLE 0x1 | |
429 | ||
430 | /** | |
431 | * save_xbzrle_page: compress and send current page | |
432 | * | |
433 | * Returns: 1 means that we wrote the page | |
434 | * 0 means that page is identical to the one already sent | |
435 | * -1 means that xbzrle would be longer than normal | |
436 | * | |
437 | * @f: QEMUFile where to send the data | |
438 | * @current_data: | |
439 | * @current_addr: | |
440 | * @block: block that contains the page we want to send | |
441 | * @offset: offset inside the block for the page | |
442 | * @last_stage: if we are at the completion stage | |
443 | * @bytes_transferred: increase it with the number of transferred bytes | |
444 | */ | |
445 | static int save_xbzrle_page(QEMUFile *f, uint8_t **current_data, | |
446 | ram_addr_t current_addr, RAMBlock *block, | |
447 | ram_addr_t offset, bool last_stage, | |
448 | uint64_t *bytes_transferred) | |
449 | { | |
450 | int encoded_len = 0, bytes_xbzrle; | |
451 | uint8_t *prev_cached_page; | |
452 | ||
453 | if (!cache_is_cached(XBZRLE.cache, current_addr, bitmap_sync_count)) { | |
454 | acct_info.xbzrle_cache_miss++; | |
455 | if (!last_stage) { | |
456 | if (cache_insert(XBZRLE.cache, current_addr, *current_data, | |
457 | bitmap_sync_count) == -1) { | |
458 | return -1; | |
459 | } else { | |
460 | /* update *current_data when the page has been | |
461 | inserted into cache */ | |
462 | *current_data = get_cached_data(XBZRLE.cache, current_addr); | |
463 | } | |
464 | } | |
465 | return -1; | |
466 | } | |
467 | ||
468 | prev_cached_page = get_cached_data(XBZRLE.cache, current_addr); | |
469 | ||
470 | /* save current buffer into memory */ | |
471 | memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE); | |
472 | ||
473 | /* XBZRLE encoding (if there is no overflow) */ | |
474 | encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf, | |
475 | TARGET_PAGE_SIZE, XBZRLE.encoded_buf, | |
476 | TARGET_PAGE_SIZE); | |
477 | if (encoded_len == 0) { | |
478 | DPRINTF("Skipping unmodified page\n"); | |
479 | return 0; | |
480 | } else if (encoded_len == -1) { | |
481 | DPRINTF("Overflow\n"); | |
482 | acct_info.xbzrle_overflows++; | |
483 | /* update data in the cache */ | |
484 | if (!last_stage) { | |
485 | memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE); | |
486 | *current_data = prev_cached_page; | |
487 | } | |
488 | return -1; | |
489 | } | |
490 | ||
491 | /* we need to update the data in the cache, in order to get the same data */ | |
492 | if (!last_stage) { | |
493 | memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE); | |
494 | } | |
495 | ||
496 | /* Send XBZRLE based compressed page */ | |
497 | bytes_xbzrle = save_page_header(f, block, offset | RAM_SAVE_FLAG_XBZRLE); | |
498 | qemu_put_byte(f, ENCODING_FLAG_XBZRLE); | |
499 | qemu_put_be16(f, encoded_len); | |
500 | qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len); | |
501 | bytes_xbzrle += encoded_len + 1 + 2; | |
502 | acct_info.xbzrle_pages++; | |
503 | acct_info.xbzrle_bytes += bytes_xbzrle; | |
504 | *bytes_transferred += bytes_xbzrle; | |
505 | ||
506 | return 1; | |
507 | } | |
508 | ||
2ff64038 | 509 | /* Called with rcu_read_lock() to protect migration_bitmap */ |
56e93d26 | 510 | static inline |
2f68e399 | 511 | ram_addr_t migration_bitmap_find_and_reset_dirty(RAMBlock *rb, |
56e93d26 JQ |
512 | ram_addr_t start) |
513 | { | |
2f68e399 | 514 | unsigned long base = rb->offset >> TARGET_PAGE_BITS; |
56e93d26 | 515 | unsigned long nr = base + (start >> TARGET_PAGE_BITS); |
2f68e399 DDAG |
516 | uint64_t rb_size = rb->used_length; |
517 | unsigned long size = base + (rb_size >> TARGET_PAGE_BITS); | |
2ff64038 | 518 | unsigned long *bitmap; |
56e93d26 JQ |
519 | |
520 | unsigned long next; | |
521 | ||
2ff64038 | 522 | bitmap = atomic_rcu_read(&migration_bitmap); |
56e93d26 JQ |
523 | if (ram_bulk_stage && nr > base) { |
524 | next = nr + 1; | |
525 | } else { | |
2ff64038 | 526 | next = find_next_bit(bitmap, size, nr); |
56e93d26 JQ |
527 | } |
528 | ||
529 | if (next < size) { | |
2ff64038 | 530 | clear_bit(next, bitmap); |
56e93d26 JQ |
531 | migration_dirty_pages--; |
532 | } | |
533 | return (next - base) << TARGET_PAGE_BITS; | |
534 | } | |
535 | ||
2ff64038 | 536 | /* Called with rcu_read_lock() to protect migration_bitmap */ |
56e93d26 JQ |
537 | static void migration_bitmap_sync_range(ram_addr_t start, ram_addr_t length) |
538 | { | |
2ff64038 LZ |
539 | unsigned long *bitmap; |
540 | bitmap = atomic_rcu_read(&migration_bitmap); | |
56e93d26 | 541 | migration_dirty_pages += |
2ff64038 | 542 | cpu_physical_memory_sync_dirty_bitmap(bitmap, start, length); |
56e93d26 JQ |
543 | } |
544 | ||
56e93d26 JQ |
545 | /* Fix me: there are too many global variables used in migration process. */ |
546 | static int64_t start_time; | |
547 | static int64_t bytes_xfer_prev; | |
548 | static int64_t num_dirty_pages_period; | |
549 | static uint64_t xbzrle_cache_miss_prev; | |
550 | static uint64_t iterations_prev; | |
551 | ||
552 | static void migration_bitmap_sync_init(void) | |
553 | { | |
554 | start_time = 0; | |
555 | bytes_xfer_prev = 0; | |
556 | num_dirty_pages_period = 0; | |
557 | xbzrle_cache_miss_prev = 0; | |
558 | iterations_prev = 0; | |
559 | } | |
560 | ||
561 | /* Called with iothread lock held, to protect ram_list.dirty_memory[] */ | |
562 | static void migration_bitmap_sync(void) | |
563 | { | |
564 | RAMBlock *block; | |
565 | uint64_t num_dirty_pages_init = migration_dirty_pages; | |
566 | MigrationState *s = migrate_get_current(); | |
567 | int64_t end_time; | |
568 | int64_t bytes_xfer_now; | |
569 | ||
570 | bitmap_sync_count++; | |
571 | ||
572 | if (!bytes_xfer_prev) { | |
573 | bytes_xfer_prev = ram_bytes_transferred(); | |
574 | } | |
575 | ||
576 | if (!start_time) { | |
577 | start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); | |
578 | } | |
579 | ||
580 | trace_migration_bitmap_sync_start(); | |
581 | address_space_sync_dirty_bitmap(&address_space_memory); | |
582 | ||
dd631697 | 583 | qemu_mutex_lock(&migration_bitmap_mutex); |
56e93d26 JQ |
584 | rcu_read_lock(); |
585 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { | |
2f68e399 | 586 | migration_bitmap_sync_range(block->offset, block->used_length); |
56e93d26 JQ |
587 | } |
588 | rcu_read_unlock(); | |
dd631697 | 589 | qemu_mutex_unlock(&migration_bitmap_mutex); |
56e93d26 JQ |
590 | |
591 | trace_migration_bitmap_sync_end(migration_dirty_pages | |
592 | - num_dirty_pages_init); | |
593 | num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init; | |
594 | end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); | |
595 | ||
596 | /* more than 1 second = 1000 millisecons */ | |
597 | if (end_time > start_time + 1000) { | |
598 | if (migrate_auto_converge()) { | |
599 | /* The following detection logic can be refined later. For now: | |
600 | Check to see if the dirtied bytes is 50% more than the approx. | |
601 | amount of bytes that just got transferred since the last time we | |
602 | were in this routine. If that happens >N times (for now N==4) | |
603 | we turn on the throttle down logic */ | |
604 | bytes_xfer_now = ram_bytes_transferred(); | |
605 | if (s->dirty_pages_rate && | |
606 | (num_dirty_pages_period * TARGET_PAGE_SIZE > | |
607 | (bytes_xfer_now - bytes_xfer_prev)/2) && | |
608 | (dirty_rate_high_cnt++ > 4)) { | |
609 | trace_migration_throttle(); | |
610 | mig_throttle_on = true; | |
611 | dirty_rate_high_cnt = 0; | |
612 | } | |
613 | bytes_xfer_prev = bytes_xfer_now; | |
614 | } else { | |
615 | mig_throttle_on = false; | |
616 | } | |
617 | if (migrate_use_xbzrle()) { | |
618 | if (iterations_prev != acct_info.iterations) { | |
619 | acct_info.xbzrle_cache_miss_rate = | |
620 | (double)(acct_info.xbzrle_cache_miss - | |
621 | xbzrle_cache_miss_prev) / | |
622 | (acct_info.iterations - iterations_prev); | |
623 | } | |
624 | iterations_prev = acct_info.iterations; | |
625 | xbzrle_cache_miss_prev = acct_info.xbzrle_cache_miss; | |
626 | } | |
627 | s->dirty_pages_rate = num_dirty_pages_period * 1000 | |
628 | / (end_time - start_time); | |
629 | s->dirty_bytes_rate = s->dirty_pages_rate * TARGET_PAGE_SIZE; | |
630 | start_time = end_time; | |
631 | num_dirty_pages_period = 0; | |
632 | } | |
633 | s->dirty_sync_count = bitmap_sync_count; | |
634 | } | |
635 | ||
636 | /** | |
637 | * save_zero_page: Send the zero page to the stream | |
638 | * | |
639 | * Returns: Number of pages written. | |
640 | * | |
641 | * @f: QEMUFile where to send the data | |
642 | * @block: block that contains the page we want to send | |
643 | * @offset: offset inside the block for the page | |
644 | * @p: pointer to the page | |
645 | * @bytes_transferred: increase it with the number of transferred bytes | |
646 | */ | |
647 | static int save_zero_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset, | |
648 | uint8_t *p, uint64_t *bytes_transferred) | |
649 | { | |
650 | int pages = -1; | |
651 | ||
652 | if (is_zero_range(p, TARGET_PAGE_SIZE)) { | |
653 | acct_info.dup_pages++; | |
654 | *bytes_transferred += save_page_header(f, block, | |
655 | offset | RAM_SAVE_FLAG_COMPRESS); | |
656 | qemu_put_byte(f, 0); | |
657 | *bytes_transferred += 1; | |
658 | pages = 1; | |
659 | } | |
660 | ||
661 | return pages; | |
662 | } | |
663 | ||
664 | /** | |
665 | * ram_save_page: Send the given page to the stream | |
666 | * | |
667 | * Returns: Number of pages written. | |
668 | * | |
669 | * @f: QEMUFile where to send the data | |
670 | * @block: block that contains the page we want to send | |
671 | * @offset: offset inside the block for the page | |
672 | * @last_stage: if we are at the completion stage | |
673 | * @bytes_transferred: increase it with the number of transferred bytes | |
674 | */ | |
675 | static int ram_save_page(QEMUFile *f, RAMBlock* block, ram_addr_t offset, | |
676 | bool last_stage, uint64_t *bytes_transferred) | |
677 | { | |
678 | int pages = -1; | |
679 | uint64_t bytes_xmit; | |
680 | ram_addr_t current_addr; | |
56e93d26 JQ |
681 | uint8_t *p; |
682 | int ret; | |
683 | bool send_async = true; | |
684 | ||
2f68e399 | 685 | p = block->host + offset; |
56e93d26 JQ |
686 | |
687 | /* In doubt sent page as normal */ | |
688 | bytes_xmit = 0; | |
689 | ret = ram_control_save_page(f, block->offset, | |
690 | offset, TARGET_PAGE_SIZE, &bytes_xmit); | |
691 | if (bytes_xmit) { | |
692 | *bytes_transferred += bytes_xmit; | |
693 | pages = 1; | |
694 | } | |
695 | ||
696 | XBZRLE_cache_lock(); | |
697 | ||
698 | current_addr = block->offset + offset; | |
699 | ||
700 | if (block == last_sent_block) { | |
701 | offset |= RAM_SAVE_FLAG_CONTINUE; | |
702 | } | |
703 | if (ret != RAM_SAVE_CONTROL_NOT_SUPP) { | |
704 | if (ret != RAM_SAVE_CONTROL_DELAYED) { | |
705 | if (bytes_xmit > 0) { | |
706 | acct_info.norm_pages++; | |
707 | } else if (bytes_xmit == 0) { | |
708 | acct_info.dup_pages++; | |
709 | } | |
710 | } | |
711 | } else { | |
712 | pages = save_zero_page(f, block, offset, p, bytes_transferred); | |
713 | if (pages > 0) { | |
714 | /* Must let xbzrle know, otherwise a previous (now 0'd) cached | |
715 | * page would be stale | |
716 | */ | |
717 | xbzrle_cache_zero_page(current_addr); | |
718 | } else if (!ram_bulk_stage && migrate_use_xbzrle()) { | |
719 | pages = save_xbzrle_page(f, &p, current_addr, block, | |
720 | offset, last_stage, bytes_transferred); | |
721 | if (!last_stage) { | |
722 | /* Can't send this cached data async, since the cache page | |
723 | * might get updated before it gets to the wire | |
724 | */ | |
725 | send_async = false; | |
726 | } | |
727 | } | |
728 | } | |
729 | ||
730 | /* XBZRLE overflow or normal page */ | |
731 | if (pages == -1) { | |
732 | *bytes_transferred += save_page_header(f, block, | |
733 | offset | RAM_SAVE_FLAG_PAGE); | |
734 | if (send_async) { | |
735 | qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE); | |
736 | } else { | |
737 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); | |
738 | } | |
739 | *bytes_transferred += TARGET_PAGE_SIZE; | |
740 | pages = 1; | |
741 | acct_info.norm_pages++; | |
742 | } | |
743 | ||
744 | XBZRLE_cache_unlock(); | |
745 | ||
746 | return pages; | |
747 | } | |
748 | ||
749 | static int do_compress_ram_page(CompressParam *param) | |
750 | { | |
751 | int bytes_sent, blen; | |
752 | uint8_t *p; | |
753 | RAMBlock *block = param->block; | |
754 | ram_addr_t offset = param->offset; | |
755 | ||
2f68e399 | 756 | p = block->host + (offset & TARGET_PAGE_MASK); |
56e93d26 JQ |
757 | |
758 | bytes_sent = save_page_header(param->file, block, offset | | |
759 | RAM_SAVE_FLAG_COMPRESS_PAGE); | |
760 | blen = qemu_put_compression_data(param->file, p, TARGET_PAGE_SIZE, | |
761 | migrate_compress_level()); | |
762 | bytes_sent += blen; | |
763 | ||
764 | return bytes_sent; | |
765 | } | |
766 | ||
767 | static inline void start_compression(CompressParam *param) | |
768 | { | |
769 | param->done = false; | |
770 | qemu_mutex_lock(¶m->mutex); | |
771 | param->start = true; | |
772 | qemu_cond_signal(¶m->cond); | |
773 | qemu_mutex_unlock(¶m->mutex); | |
774 | } | |
775 | ||
776 | static inline void start_decompression(DecompressParam *param) | |
777 | { | |
778 | qemu_mutex_lock(¶m->mutex); | |
779 | param->start = true; | |
780 | qemu_cond_signal(¶m->cond); | |
781 | qemu_mutex_unlock(¶m->mutex); | |
782 | } | |
783 | ||
784 | static uint64_t bytes_transferred; | |
785 | ||
786 | static void flush_compressed_data(QEMUFile *f) | |
787 | { | |
788 | int idx, len, thread_count; | |
789 | ||
790 | if (!migrate_use_compression()) { | |
791 | return; | |
792 | } | |
793 | thread_count = migrate_compress_threads(); | |
794 | for (idx = 0; idx < thread_count; idx++) { | |
795 | if (!comp_param[idx].done) { | |
796 | qemu_mutex_lock(comp_done_lock); | |
797 | while (!comp_param[idx].done && !quit_comp_thread) { | |
798 | qemu_cond_wait(comp_done_cond, comp_done_lock); | |
799 | } | |
800 | qemu_mutex_unlock(comp_done_lock); | |
801 | } | |
802 | if (!quit_comp_thread) { | |
803 | len = qemu_put_qemu_file(f, comp_param[idx].file); | |
804 | bytes_transferred += len; | |
805 | } | |
806 | } | |
807 | } | |
808 | ||
809 | static inline void set_compress_params(CompressParam *param, RAMBlock *block, | |
810 | ram_addr_t offset) | |
811 | { | |
812 | param->block = block; | |
813 | param->offset = offset; | |
814 | } | |
815 | ||
816 | static int compress_page_with_multi_thread(QEMUFile *f, RAMBlock *block, | |
817 | ram_addr_t offset, | |
818 | uint64_t *bytes_transferred) | |
819 | { | |
820 | int idx, thread_count, bytes_xmit = -1, pages = -1; | |
821 | ||
822 | thread_count = migrate_compress_threads(); | |
823 | qemu_mutex_lock(comp_done_lock); | |
824 | while (true) { | |
825 | for (idx = 0; idx < thread_count; idx++) { | |
826 | if (comp_param[idx].done) { | |
827 | bytes_xmit = qemu_put_qemu_file(f, comp_param[idx].file); | |
828 | set_compress_params(&comp_param[idx], block, offset); | |
829 | start_compression(&comp_param[idx]); | |
830 | pages = 1; | |
831 | acct_info.norm_pages++; | |
832 | *bytes_transferred += bytes_xmit; | |
833 | break; | |
834 | } | |
835 | } | |
836 | if (pages > 0) { | |
837 | break; | |
838 | } else { | |
839 | qemu_cond_wait(comp_done_cond, comp_done_lock); | |
840 | } | |
841 | } | |
842 | qemu_mutex_unlock(comp_done_lock); | |
843 | ||
844 | return pages; | |
845 | } | |
846 | ||
847 | /** | |
848 | * ram_save_compressed_page: compress the given page and send it to the stream | |
849 | * | |
850 | * Returns: Number of pages written. | |
851 | * | |
852 | * @f: QEMUFile where to send the data | |
853 | * @block: block that contains the page we want to send | |
854 | * @offset: offset inside the block for the page | |
855 | * @last_stage: if we are at the completion stage | |
856 | * @bytes_transferred: increase it with the number of transferred bytes | |
857 | */ | |
858 | static int ram_save_compressed_page(QEMUFile *f, RAMBlock *block, | |
859 | ram_addr_t offset, bool last_stage, | |
860 | uint64_t *bytes_transferred) | |
861 | { | |
862 | int pages = -1; | |
863 | uint64_t bytes_xmit; | |
56e93d26 JQ |
864 | uint8_t *p; |
865 | int ret; | |
866 | ||
2f68e399 | 867 | p = block->host + offset; |
56e93d26 JQ |
868 | |
869 | bytes_xmit = 0; | |
870 | ret = ram_control_save_page(f, block->offset, | |
871 | offset, TARGET_PAGE_SIZE, &bytes_xmit); | |
872 | if (bytes_xmit) { | |
873 | *bytes_transferred += bytes_xmit; | |
874 | pages = 1; | |
875 | } | |
876 | if (block == last_sent_block) { | |
877 | offset |= RAM_SAVE_FLAG_CONTINUE; | |
878 | } | |
879 | if (ret != RAM_SAVE_CONTROL_NOT_SUPP) { | |
880 | if (ret != RAM_SAVE_CONTROL_DELAYED) { | |
881 | if (bytes_xmit > 0) { | |
882 | acct_info.norm_pages++; | |
883 | } else if (bytes_xmit == 0) { | |
884 | acct_info.dup_pages++; | |
885 | } | |
886 | } | |
887 | } else { | |
888 | /* When starting the process of a new block, the first page of | |
889 | * the block should be sent out before other pages in the same | |
890 | * block, and all the pages in last block should have been sent | |
891 | * out, keeping this order is important, because the 'cont' flag | |
892 | * is used to avoid resending the block name. | |
893 | */ | |
894 | if (block != last_sent_block) { | |
895 | flush_compressed_data(f); | |
896 | pages = save_zero_page(f, block, offset, p, bytes_transferred); | |
897 | if (pages == -1) { | |
898 | set_compress_params(&comp_param[0], block, offset); | |
899 | /* Use the qemu thread to compress the data to make sure the | |
900 | * first page is sent out before other pages | |
901 | */ | |
902 | bytes_xmit = do_compress_ram_page(&comp_param[0]); | |
903 | acct_info.norm_pages++; | |
904 | qemu_put_qemu_file(f, comp_param[0].file); | |
905 | *bytes_transferred += bytes_xmit; | |
906 | pages = 1; | |
907 | } | |
908 | } else { | |
909 | pages = save_zero_page(f, block, offset, p, bytes_transferred); | |
910 | if (pages == -1) { | |
911 | pages = compress_page_with_multi_thread(f, block, offset, | |
912 | bytes_transferred); | |
913 | } | |
914 | } | |
915 | } | |
916 | ||
917 | return pages; | |
918 | } | |
919 | ||
920 | /** | |
921 | * ram_find_and_save_block: Finds a dirty page and sends it to f | |
922 | * | |
923 | * Called within an RCU critical section. | |
924 | * | |
925 | * Returns: The number of pages written | |
926 | * 0 means no dirty pages | |
927 | * | |
928 | * @f: QEMUFile where to send the data | |
929 | * @last_stage: if we are at the completion stage | |
930 | * @bytes_transferred: increase it with the number of transferred bytes | |
931 | */ | |
932 | ||
933 | static int ram_find_and_save_block(QEMUFile *f, bool last_stage, | |
934 | uint64_t *bytes_transferred) | |
935 | { | |
b8fb8cb7 | 936 | PageSearchStatus pss; |
56e93d26 | 937 | int pages = 0; |
56e93d26 | 938 | |
b8fb8cb7 DDAG |
939 | pss.block = last_seen_block; |
940 | pss.offset = last_offset; | |
941 | pss.complete_round = false; | |
942 | ||
943 | if (!pss.block) { | |
944 | pss.block = QLIST_FIRST_RCU(&ram_list.blocks); | |
945 | } | |
56e93d26 JQ |
946 | |
947 | while (true) { | |
b8fb8cb7 DDAG |
948 | pss.offset = migration_bitmap_find_and_reset_dirty(pss.block, |
949 | pss.offset); | |
950 | if (pss.complete_round && pss.block == last_seen_block && | |
951 | pss.offset >= last_offset) { | |
56e93d26 JQ |
952 | break; |
953 | } | |
b8fb8cb7 DDAG |
954 | if (pss.offset >= pss.block->used_length) { |
955 | pss.offset = 0; | |
956 | pss.block = QLIST_NEXT_RCU(pss.block, next); | |
957 | if (!pss.block) { | |
958 | pss.block = QLIST_FIRST_RCU(&ram_list.blocks); | |
959 | pss.complete_round = true; | |
56e93d26 JQ |
960 | ram_bulk_stage = false; |
961 | if (migrate_use_xbzrle()) { | |
962 | /* If xbzrle is on, stop using the data compression at this | |
963 | * point. In theory, xbzrle can do better than compression. | |
964 | */ | |
965 | flush_compressed_data(f); | |
966 | compression_switch = false; | |
967 | } | |
968 | } | |
969 | } else { | |
970 | if (compression_switch && migrate_use_compression()) { | |
b8fb8cb7 DDAG |
971 | pages = ram_save_compressed_page(f, pss.block, pss.offset, |
972 | last_stage, | |
56e93d26 JQ |
973 | bytes_transferred); |
974 | } else { | |
b8fb8cb7 | 975 | pages = ram_save_page(f, pss.block, pss.offset, last_stage, |
56e93d26 JQ |
976 | bytes_transferred); |
977 | } | |
978 | ||
979 | /* if page is unmodified, continue to the next */ | |
980 | if (pages > 0) { | |
b8fb8cb7 | 981 | last_sent_block = pss.block; |
56e93d26 JQ |
982 | break; |
983 | } | |
984 | } | |
985 | } | |
986 | ||
b8fb8cb7 DDAG |
987 | last_seen_block = pss.block; |
988 | last_offset = pss.offset; | |
56e93d26 JQ |
989 | |
990 | return pages; | |
991 | } | |
992 | ||
993 | void acct_update_position(QEMUFile *f, size_t size, bool zero) | |
994 | { | |
995 | uint64_t pages = size / TARGET_PAGE_SIZE; | |
996 | if (zero) { | |
997 | acct_info.dup_pages += pages; | |
998 | } else { | |
999 | acct_info.norm_pages += pages; | |
1000 | bytes_transferred += size; | |
1001 | qemu_update_position(f, size); | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | static ram_addr_t ram_save_remaining(void) | |
1006 | { | |
1007 | return migration_dirty_pages; | |
1008 | } | |
1009 | ||
1010 | uint64_t ram_bytes_remaining(void) | |
1011 | { | |
1012 | return ram_save_remaining() * TARGET_PAGE_SIZE; | |
1013 | } | |
1014 | ||
1015 | uint64_t ram_bytes_transferred(void) | |
1016 | { | |
1017 | return bytes_transferred; | |
1018 | } | |
1019 | ||
1020 | uint64_t ram_bytes_total(void) | |
1021 | { | |
1022 | RAMBlock *block; | |
1023 | uint64_t total = 0; | |
1024 | ||
1025 | rcu_read_lock(); | |
1026 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) | |
1027 | total += block->used_length; | |
1028 | rcu_read_unlock(); | |
1029 | return total; | |
1030 | } | |
1031 | ||
1032 | void free_xbzrle_decoded_buf(void) | |
1033 | { | |
1034 | g_free(xbzrle_decoded_buf); | |
1035 | xbzrle_decoded_buf = NULL; | |
1036 | } | |
1037 | ||
1038 | static void migration_end(void) | |
1039 | { | |
2ff64038 LZ |
1040 | /* caller have hold iothread lock or is in a bh, so there is |
1041 | * no writing race against this migration_bitmap | |
1042 | */ | |
1043 | unsigned long *bitmap = migration_bitmap; | |
1044 | atomic_rcu_set(&migration_bitmap, NULL); | |
1045 | if (bitmap) { | |
56e93d26 | 1046 | memory_global_dirty_log_stop(); |
2ff64038 LZ |
1047 | synchronize_rcu(); |
1048 | g_free(bitmap); | |
56e93d26 JQ |
1049 | } |
1050 | ||
1051 | XBZRLE_cache_lock(); | |
1052 | if (XBZRLE.cache) { | |
1053 | cache_fini(XBZRLE.cache); | |
1054 | g_free(XBZRLE.encoded_buf); | |
1055 | g_free(XBZRLE.current_buf); | |
1056 | XBZRLE.cache = NULL; | |
1057 | XBZRLE.encoded_buf = NULL; | |
1058 | XBZRLE.current_buf = NULL; | |
1059 | } | |
1060 | XBZRLE_cache_unlock(); | |
1061 | } | |
1062 | ||
1063 | static void ram_migration_cancel(void *opaque) | |
1064 | { | |
1065 | migration_end(); | |
1066 | } | |
1067 | ||
1068 | static void reset_ram_globals(void) | |
1069 | { | |
1070 | last_seen_block = NULL; | |
1071 | last_sent_block = NULL; | |
1072 | last_offset = 0; | |
1073 | last_version = ram_list.version; | |
1074 | ram_bulk_stage = true; | |
1075 | } | |
1076 | ||
1077 | #define MAX_WAIT 50 /* ms, half buffered_file limit */ | |
1078 | ||
dd631697 LZ |
1079 | void migration_bitmap_extend(ram_addr_t old, ram_addr_t new) |
1080 | { | |
1081 | /* called in qemu main thread, so there is | |
1082 | * no writing race against this migration_bitmap | |
1083 | */ | |
1084 | if (migration_bitmap) { | |
1085 | unsigned long *old_bitmap = migration_bitmap, *bitmap; | |
1086 | bitmap = bitmap_new(new); | |
1087 | ||
1088 | /* prevent migration_bitmap content from being set bit | |
1089 | * by migration_bitmap_sync_range() at the same time. | |
1090 | * it is safe to migration if migration_bitmap is cleared bit | |
1091 | * at the same time. | |
1092 | */ | |
1093 | qemu_mutex_lock(&migration_bitmap_mutex); | |
1094 | bitmap_copy(bitmap, old_bitmap, old); | |
1095 | bitmap_set(bitmap, old, new - old); | |
1096 | atomic_rcu_set(&migration_bitmap, bitmap); | |
1097 | qemu_mutex_unlock(&migration_bitmap_mutex); | |
1098 | migration_dirty_pages += new - old; | |
1099 | synchronize_rcu(); | |
1100 | g_free(old_bitmap); | |
1101 | } | |
1102 | } | |
56e93d26 JQ |
1103 | |
1104 | /* Each of ram_save_setup, ram_save_iterate and ram_save_complete has | |
1105 | * long-running RCU critical section. When rcu-reclaims in the code | |
1106 | * start to become numerous it will be necessary to reduce the | |
1107 | * granularity of these critical sections. | |
1108 | */ | |
1109 | ||
1110 | static int ram_save_setup(QEMUFile *f, void *opaque) | |
1111 | { | |
1112 | RAMBlock *block; | |
1113 | int64_t ram_bitmap_pages; /* Size of bitmap in pages, including gaps */ | |
1114 | ||
1115 | mig_throttle_on = false; | |
1116 | dirty_rate_high_cnt = 0; | |
1117 | bitmap_sync_count = 0; | |
1118 | migration_bitmap_sync_init(); | |
dd631697 | 1119 | qemu_mutex_init(&migration_bitmap_mutex); |
56e93d26 JQ |
1120 | |
1121 | if (migrate_use_xbzrle()) { | |
1122 | XBZRLE_cache_lock(); | |
1123 | XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() / | |
1124 | TARGET_PAGE_SIZE, | |
1125 | TARGET_PAGE_SIZE); | |
1126 | if (!XBZRLE.cache) { | |
1127 | XBZRLE_cache_unlock(); | |
1128 | error_report("Error creating cache"); | |
1129 | return -1; | |
1130 | } | |
1131 | XBZRLE_cache_unlock(); | |
1132 | ||
1133 | /* We prefer not to abort if there is no memory */ | |
1134 | XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE); | |
1135 | if (!XBZRLE.encoded_buf) { | |
1136 | error_report("Error allocating encoded_buf"); | |
1137 | return -1; | |
1138 | } | |
1139 | ||
1140 | XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE); | |
1141 | if (!XBZRLE.current_buf) { | |
1142 | error_report("Error allocating current_buf"); | |
1143 | g_free(XBZRLE.encoded_buf); | |
1144 | XBZRLE.encoded_buf = NULL; | |
1145 | return -1; | |
1146 | } | |
1147 | ||
1148 | acct_clear(); | |
1149 | } | |
1150 | ||
1151 | /* iothread lock needed for ram_list.dirty_memory[] */ | |
1152 | qemu_mutex_lock_iothread(); | |
1153 | qemu_mutex_lock_ramlist(); | |
1154 | rcu_read_lock(); | |
1155 | bytes_transferred = 0; | |
1156 | reset_ram_globals(); | |
1157 | ||
1158 | ram_bitmap_pages = last_ram_offset() >> TARGET_PAGE_BITS; | |
1159 | migration_bitmap = bitmap_new(ram_bitmap_pages); | |
1160 | bitmap_set(migration_bitmap, 0, ram_bitmap_pages); | |
1161 | ||
1162 | /* | |
1163 | * Count the total number of pages used by ram blocks not including any | |
1164 | * gaps due to alignment or unplugs. | |
1165 | */ | |
1166 | migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS; | |
1167 | ||
1168 | memory_global_dirty_log_start(); | |
1169 | migration_bitmap_sync(); | |
1170 | qemu_mutex_unlock_ramlist(); | |
1171 | qemu_mutex_unlock_iothread(); | |
1172 | ||
1173 | qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE); | |
1174 | ||
1175 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { | |
1176 | qemu_put_byte(f, strlen(block->idstr)); | |
1177 | qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); | |
1178 | qemu_put_be64(f, block->used_length); | |
1179 | } | |
1180 | ||
1181 | rcu_read_unlock(); | |
1182 | ||
1183 | ram_control_before_iterate(f, RAM_CONTROL_SETUP); | |
1184 | ram_control_after_iterate(f, RAM_CONTROL_SETUP); | |
1185 | ||
1186 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); | |
1187 | ||
1188 | return 0; | |
1189 | } | |
1190 | ||
1191 | static int ram_save_iterate(QEMUFile *f, void *opaque) | |
1192 | { | |
1193 | int ret; | |
1194 | int i; | |
1195 | int64_t t0; | |
1196 | int pages_sent = 0; | |
1197 | ||
1198 | rcu_read_lock(); | |
1199 | if (ram_list.version != last_version) { | |
1200 | reset_ram_globals(); | |
1201 | } | |
1202 | ||
1203 | /* Read version before ram_list.blocks */ | |
1204 | smp_rmb(); | |
1205 | ||
1206 | ram_control_before_iterate(f, RAM_CONTROL_ROUND); | |
1207 | ||
1208 | t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); | |
1209 | i = 0; | |
1210 | while ((ret = qemu_file_rate_limit(f)) == 0) { | |
1211 | int pages; | |
1212 | ||
1213 | pages = ram_find_and_save_block(f, false, &bytes_transferred); | |
1214 | /* no more pages to sent */ | |
1215 | if (pages == 0) { | |
1216 | break; | |
1217 | } | |
1218 | pages_sent += pages; | |
1219 | acct_info.iterations++; | |
1220 | check_guest_throttling(); | |
1221 | /* we want to check in the 1st loop, just in case it was the 1st time | |
1222 | and we had to sync the dirty bitmap. | |
1223 | qemu_get_clock_ns() is a bit expensive, so we only check each some | |
1224 | iterations | |
1225 | */ | |
1226 | if ((i & 63) == 0) { | |
1227 | uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000; | |
1228 | if (t1 > MAX_WAIT) { | |
1229 | DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n", | |
1230 | t1, i); | |
1231 | break; | |
1232 | } | |
1233 | } | |
1234 | i++; | |
1235 | } | |
1236 | flush_compressed_data(f); | |
1237 | rcu_read_unlock(); | |
1238 | ||
1239 | /* | |
1240 | * Must occur before EOS (or any QEMUFile operation) | |
1241 | * because of RDMA protocol. | |
1242 | */ | |
1243 | ram_control_after_iterate(f, RAM_CONTROL_ROUND); | |
1244 | ||
1245 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); | |
1246 | bytes_transferred += 8; | |
1247 | ||
1248 | ret = qemu_file_get_error(f); | |
1249 | if (ret < 0) { | |
1250 | return ret; | |
1251 | } | |
1252 | ||
1253 | return pages_sent; | |
1254 | } | |
1255 | ||
1256 | /* Called with iothread lock */ | |
1257 | static int ram_save_complete(QEMUFile *f, void *opaque) | |
1258 | { | |
1259 | rcu_read_lock(); | |
1260 | ||
1261 | migration_bitmap_sync(); | |
1262 | ||
1263 | ram_control_before_iterate(f, RAM_CONTROL_FINISH); | |
1264 | ||
1265 | /* try transferring iterative blocks of memory */ | |
1266 | ||
1267 | /* flush all remaining blocks regardless of rate limiting */ | |
1268 | while (true) { | |
1269 | int pages; | |
1270 | ||
1271 | pages = ram_find_and_save_block(f, true, &bytes_transferred); | |
1272 | /* no more blocks to sent */ | |
1273 | if (pages == 0) { | |
1274 | break; | |
1275 | } | |
1276 | } | |
1277 | ||
1278 | flush_compressed_data(f); | |
1279 | ram_control_after_iterate(f, RAM_CONTROL_FINISH); | |
56e93d26 JQ |
1280 | |
1281 | rcu_read_unlock(); | |
d09a6fde PB |
1282 | |
1283 | migration_end(); | |
56e93d26 JQ |
1284 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
1285 | ||
1286 | return 0; | |
1287 | } | |
1288 | ||
1289 | static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size) | |
1290 | { | |
1291 | uint64_t remaining_size; | |
1292 | ||
1293 | remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE; | |
1294 | ||
1295 | if (remaining_size < max_size) { | |
1296 | qemu_mutex_lock_iothread(); | |
1297 | rcu_read_lock(); | |
1298 | migration_bitmap_sync(); | |
1299 | rcu_read_unlock(); | |
1300 | qemu_mutex_unlock_iothread(); | |
1301 | remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE; | |
1302 | } | |
1303 | return remaining_size; | |
1304 | } | |
1305 | ||
1306 | static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) | |
1307 | { | |
1308 | unsigned int xh_len; | |
1309 | int xh_flags; | |
1310 | ||
1311 | if (!xbzrle_decoded_buf) { | |
1312 | xbzrle_decoded_buf = g_malloc(TARGET_PAGE_SIZE); | |
1313 | } | |
1314 | ||
1315 | /* extract RLE header */ | |
1316 | xh_flags = qemu_get_byte(f); | |
1317 | xh_len = qemu_get_be16(f); | |
1318 | ||
1319 | if (xh_flags != ENCODING_FLAG_XBZRLE) { | |
1320 | error_report("Failed to load XBZRLE page - wrong compression!"); | |
1321 | return -1; | |
1322 | } | |
1323 | ||
1324 | if (xh_len > TARGET_PAGE_SIZE) { | |
1325 | error_report("Failed to load XBZRLE page - len overflow!"); | |
1326 | return -1; | |
1327 | } | |
1328 | /* load data and decode */ | |
1329 | qemu_get_buffer(f, xbzrle_decoded_buf, xh_len); | |
1330 | ||
1331 | /* decode RLE */ | |
1332 | if (xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host, | |
1333 | TARGET_PAGE_SIZE) == -1) { | |
1334 | error_report("Failed to load XBZRLE page - decode error!"); | |
1335 | return -1; | |
1336 | } | |
1337 | ||
1338 | return 0; | |
1339 | } | |
1340 | ||
1341 | /* Must be called from within a rcu critical section. | |
1342 | * Returns a pointer from within the RCU-protected ram_list. | |
1343 | */ | |
1344 | static inline void *host_from_stream_offset(QEMUFile *f, | |
1345 | ram_addr_t offset, | |
1346 | int flags) | |
1347 | { | |
1348 | static RAMBlock *block = NULL; | |
1349 | char id[256]; | |
1350 | uint8_t len; | |
1351 | ||
1352 | if (flags & RAM_SAVE_FLAG_CONTINUE) { | |
1353 | if (!block || block->max_length <= offset) { | |
1354 | error_report("Ack, bad migration stream!"); | |
1355 | return NULL; | |
1356 | } | |
1357 | ||
2f68e399 | 1358 | return block->host + offset; |
56e93d26 JQ |
1359 | } |
1360 | ||
1361 | len = qemu_get_byte(f); | |
1362 | qemu_get_buffer(f, (uint8_t *)id, len); | |
1363 | id[len] = 0; | |
1364 | ||
1365 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { | |
1366 | if (!strncmp(id, block->idstr, sizeof(id)) && | |
1367 | block->max_length > offset) { | |
2f68e399 | 1368 | return block->host + offset; |
56e93d26 JQ |
1369 | } |
1370 | } | |
1371 | ||
1372 | error_report("Can't find block %s!", id); | |
1373 | return NULL; | |
1374 | } | |
1375 | ||
1376 | /* | |
1377 | * If a page (or a whole RDMA chunk) has been | |
1378 | * determined to be zero, then zap it. | |
1379 | */ | |
1380 | void ram_handle_compressed(void *host, uint8_t ch, uint64_t size) | |
1381 | { | |
1382 | if (ch != 0 || !is_zero_range(host, size)) { | |
1383 | memset(host, ch, size); | |
1384 | } | |
1385 | } | |
1386 | ||
1387 | static void *do_data_decompress(void *opaque) | |
1388 | { | |
1389 | DecompressParam *param = opaque; | |
1390 | unsigned long pagesize; | |
1391 | ||
1392 | while (!quit_decomp_thread) { | |
1393 | qemu_mutex_lock(¶m->mutex); | |
1394 | while (!param->start && !quit_decomp_thread) { | |
1395 | qemu_cond_wait(¶m->cond, ¶m->mutex); | |
1396 | pagesize = TARGET_PAGE_SIZE; | |
1397 | if (!quit_decomp_thread) { | |
1398 | /* uncompress() will return failed in some case, especially | |
1399 | * when the page is dirted when doing the compression, it's | |
1400 | * not a problem because the dirty page will be retransferred | |
1401 | * and uncompress() won't break the data in other pages. | |
1402 | */ | |
1403 | uncompress((Bytef *)param->des, &pagesize, | |
1404 | (const Bytef *)param->compbuf, param->len); | |
1405 | } | |
1406 | param->start = false; | |
1407 | } | |
1408 | qemu_mutex_unlock(¶m->mutex); | |
1409 | } | |
1410 | ||
1411 | return NULL; | |
1412 | } | |
1413 | ||
1414 | void migrate_decompress_threads_create(void) | |
1415 | { | |
1416 | int i, thread_count; | |
1417 | ||
1418 | thread_count = migrate_decompress_threads(); | |
1419 | decompress_threads = g_new0(QemuThread, thread_count); | |
1420 | decomp_param = g_new0(DecompressParam, thread_count); | |
1421 | compressed_data_buf = g_malloc0(compressBound(TARGET_PAGE_SIZE)); | |
1422 | quit_decomp_thread = false; | |
1423 | for (i = 0; i < thread_count; i++) { | |
1424 | qemu_mutex_init(&decomp_param[i].mutex); | |
1425 | qemu_cond_init(&decomp_param[i].cond); | |
1426 | decomp_param[i].compbuf = g_malloc0(compressBound(TARGET_PAGE_SIZE)); | |
1427 | qemu_thread_create(decompress_threads + i, "decompress", | |
1428 | do_data_decompress, decomp_param + i, | |
1429 | QEMU_THREAD_JOINABLE); | |
1430 | } | |
1431 | } | |
1432 | ||
1433 | void migrate_decompress_threads_join(void) | |
1434 | { | |
1435 | int i, thread_count; | |
1436 | ||
1437 | quit_decomp_thread = true; | |
1438 | thread_count = migrate_decompress_threads(); | |
1439 | for (i = 0; i < thread_count; i++) { | |
1440 | qemu_mutex_lock(&decomp_param[i].mutex); | |
1441 | qemu_cond_signal(&decomp_param[i].cond); | |
1442 | qemu_mutex_unlock(&decomp_param[i].mutex); | |
1443 | } | |
1444 | for (i = 0; i < thread_count; i++) { | |
1445 | qemu_thread_join(decompress_threads + i); | |
1446 | qemu_mutex_destroy(&decomp_param[i].mutex); | |
1447 | qemu_cond_destroy(&decomp_param[i].cond); | |
1448 | g_free(decomp_param[i].compbuf); | |
1449 | } | |
1450 | g_free(decompress_threads); | |
1451 | g_free(decomp_param); | |
1452 | g_free(compressed_data_buf); | |
1453 | decompress_threads = NULL; | |
1454 | decomp_param = NULL; | |
1455 | compressed_data_buf = NULL; | |
1456 | } | |
1457 | ||
1458 | static void decompress_data_with_multi_threads(uint8_t *compbuf, | |
1459 | void *host, int len) | |
1460 | { | |
1461 | int idx, thread_count; | |
1462 | ||
1463 | thread_count = migrate_decompress_threads(); | |
1464 | while (true) { | |
1465 | for (idx = 0; idx < thread_count; idx++) { | |
1466 | if (!decomp_param[idx].start) { | |
1467 | memcpy(decomp_param[idx].compbuf, compbuf, len); | |
1468 | decomp_param[idx].des = host; | |
1469 | decomp_param[idx].len = len; | |
1470 | start_decompression(&decomp_param[idx]); | |
1471 | break; | |
1472 | } | |
1473 | } | |
1474 | if (idx < thread_count) { | |
1475 | break; | |
1476 | } | |
1477 | } | |
1478 | } | |
1479 | ||
1480 | static int ram_load(QEMUFile *f, void *opaque, int version_id) | |
1481 | { | |
1482 | int flags = 0, ret = 0; | |
1483 | static uint64_t seq_iter; | |
1484 | int len = 0; | |
1485 | ||
1486 | seq_iter++; | |
1487 | ||
1488 | if (version_id != 4) { | |
1489 | ret = -EINVAL; | |
1490 | } | |
1491 | ||
1492 | /* This RCU critical section can be very long running. | |
1493 | * When RCU reclaims in the code start to become numerous, | |
1494 | * it will be necessary to reduce the granularity of this | |
1495 | * critical section. | |
1496 | */ | |
1497 | rcu_read_lock(); | |
1498 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { | |
1499 | ram_addr_t addr, total_ram_bytes; | |
1500 | void *host; | |
1501 | uint8_t ch; | |
1502 | ||
1503 | addr = qemu_get_be64(f); | |
1504 | flags = addr & ~TARGET_PAGE_MASK; | |
1505 | addr &= TARGET_PAGE_MASK; | |
1506 | ||
1507 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { | |
1508 | case RAM_SAVE_FLAG_MEM_SIZE: | |
1509 | /* Synchronize RAM block list */ | |
1510 | total_ram_bytes = addr; | |
1511 | while (!ret && total_ram_bytes) { | |
1512 | RAMBlock *block; | |
56e93d26 JQ |
1513 | char id[256]; |
1514 | ram_addr_t length; | |
1515 | ||
1516 | len = qemu_get_byte(f); | |
1517 | qemu_get_buffer(f, (uint8_t *)id, len); | |
1518 | id[len] = 0; | |
1519 | length = qemu_get_be64(f); | |
1520 | ||
1521 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { | |
1522 | if (!strncmp(id, block->idstr, sizeof(id))) { | |
1523 | if (length != block->used_length) { | |
1524 | Error *local_err = NULL; | |
1525 | ||
1526 | ret = qemu_ram_resize(block->offset, length, &local_err); | |
1527 | if (local_err) { | |
1528 | error_report_err(local_err); | |
1529 | } | |
1530 | } | |
632e3a5c DDAG |
1531 | ram_control_load_hook(f, RAM_CONTROL_BLOCK_REG, |
1532 | block->idstr); | |
56e93d26 JQ |
1533 | break; |
1534 | } | |
1535 | } | |
1536 | ||
1537 | if (!block) { | |
1538 | error_report("Unknown ramblock \"%s\", cannot " | |
1539 | "accept migration", id); | |
1540 | ret = -EINVAL; | |
1541 | } | |
1542 | ||
1543 | total_ram_bytes -= length; | |
1544 | } | |
1545 | break; | |
1546 | case RAM_SAVE_FLAG_COMPRESS: | |
1547 | host = host_from_stream_offset(f, addr, flags); | |
1548 | if (!host) { | |
1549 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); | |
1550 | ret = -EINVAL; | |
1551 | break; | |
1552 | } | |
1553 | ch = qemu_get_byte(f); | |
1554 | ram_handle_compressed(host, ch, TARGET_PAGE_SIZE); | |
1555 | break; | |
1556 | case RAM_SAVE_FLAG_PAGE: | |
1557 | host = host_from_stream_offset(f, addr, flags); | |
1558 | if (!host) { | |
1559 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); | |
1560 | ret = -EINVAL; | |
1561 | break; | |
1562 | } | |
1563 | qemu_get_buffer(f, host, TARGET_PAGE_SIZE); | |
1564 | break; | |
1565 | case RAM_SAVE_FLAG_COMPRESS_PAGE: | |
1566 | host = host_from_stream_offset(f, addr, flags); | |
1567 | if (!host) { | |
1568 | error_report("Invalid RAM offset " RAM_ADDR_FMT, addr); | |
1569 | ret = -EINVAL; | |
1570 | break; | |
1571 | } | |
1572 | ||
1573 | len = qemu_get_be32(f); | |
1574 | if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) { | |
1575 | error_report("Invalid compressed data length: %d", len); | |
1576 | ret = -EINVAL; | |
1577 | break; | |
1578 | } | |
1579 | qemu_get_buffer(f, compressed_data_buf, len); | |
1580 | decompress_data_with_multi_threads(compressed_data_buf, host, len); | |
1581 | break; | |
1582 | case RAM_SAVE_FLAG_XBZRLE: | |
1583 | host = host_from_stream_offset(f, addr, flags); | |
1584 | if (!host) { | |
1585 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); | |
1586 | ret = -EINVAL; | |
1587 | break; | |
1588 | } | |
1589 | if (load_xbzrle(f, addr, host) < 0) { | |
1590 | error_report("Failed to decompress XBZRLE page at " | |
1591 | RAM_ADDR_FMT, addr); | |
1592 | ret = -EINVAL; | |
1593 | break; | |
1594 | } | |
1595 | break; | |
1596 | case RAM_SAVE_FLAG_EOS: | |
1597 | /* normal exit */ | |
1598 | break; | |
1599 | default: | |
1600 | if (flags & RAM_SAVE_FLAG_HOOK) { | |
632e3a5c | 1601 | ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL); |
56e93d26 JQ |
1602 | } else { |
1603 | error_report("Unknown combination of migration flags: %#x", | |
1604 | flags); | |
1605 | ret = -EINVAL; | |
1606 | } | |
1607 | } | |
1608 | if (!ret) { | |
1609 | ret = qemu_file_get_error(f); | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | rcu_read_unlock(); | |
1614 | DPRINTF("Completed load of VM with exit code %d seq iteration " | |
1615 | "%" PRIu64 "\n", ret, seq_iter); | |
1616 | return ret; | |
1617 | } | |
1618 | ||
1619 | static SaveVMHandlers savevm_ram_handlers = { | |
1620 | .save_live_setup = ram_save_setup, | |
1621 | .save_live_iterate = ram_save_iterate, | |
1622 | .save_live_complete = ram_save_complete, | |
1623 | .save_live_pending = ram_save_pending, | |
1624 | .load_state = ram_load, | |
1625 | .cancel = ram_migration_cancel, | |
1626 | }; | |
1627 | ||
1628 | void ram_mig_init(void) | |
1629 | { | |
1630 | qemu_mutex_init(&XBZRLE.lock); | |
1631 | register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL); | |
1632 | } | |
1633 | /* Stub function that's gets run on the vcpu when its brought out of the | |
1634 | VM to run inside qemu via async_run_on_cpu()*/ | |
1635 | ||
1636 | static void mig_sleep_cpu(void *opq) | |
1637 | { | |
1638 | qemu_mutex_unlock_iothread(); | |
1639 | g_usleep(30*1000); | |
1640 | qemu_mutex_lock_iothread(); | |
1641 | } | |
1642 | ||
1643 | /* To reduce the dirty rate explicitly disallow the VCPUs from spending | |
1644 | much time in the VM. The migration thread will try to catchup. | |
1645 | Workload will experience a performance drop. | |
1646 | */ | |
1647 | static void mig_throttle_guest_down(void) | |
1648 | { | |
1649 | CPUState *cpu; | |
1650 | ||
1651 | qemu_mutex_lock_iothread(); | |
1652 | CPU_FOREACH(cpu) { | |
1653 | async_run_on_cpu(cpu, mig_sleep_cpu, NULL); | |
1654 | } | |
1655 | qemu_mutex_unlock_iothread(); | |
1656 | } | |
1657 | ||
1658 | static void check_guest_throttling(void) | |
1659 | { | |
1660 | static int64_t t0; | |
1661 | int64_t t1; | |
1662 | ||
1663 | if (!mig_throttle_on) { | |
1664 | return; | |
1665 | } | |
1666 | ||
1667 | if (!t0) { | |
1668 | t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); | |
1669 | return; | |
1670 | } | |
1671 | ||
1672 | t1 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); | |
1673 | ||
1674 | /* If it has been more than 40 ms since the last time the guest | |
1675 | * was throttled then do it again. | |
1676 | */ | |
1677 | if (40 < (t1-t0)/1000000) { | |
1678 | mig_throttle_guest_down(); | |
1679 | t0 = t1; | |
1680 | } | |
1681 | } |