]>
Commit | Line | Data |
---|---|---|
ad96090a BS |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | #include <stdint.h> | |
25 | #include <stdarg.h> | |
b2e0a138 | 26 | #include <stdlib.h> |
ad96090a | 27 | #ifndef _WIN32 |
1c47cb16 | 28 | #include <sys/types.h> |
ad96090a BS |
29 | #include <sys/mman.h> |
30 | #endif | |
31 | #include "config.h" | |
83c9089e | 32 | #include "monitor/monitor.h" |
9c17d615 | 33 | #include "sysemu/sysemu.h" |
1de7afc9 PB |
34 | #include "qemu/bitops.h" |
35 | #include "qemu/bitmap.h" | |
9c17d615 | 36 | #include "sysemu/arch_init.h" |
ad96090a | 37 | #include "audio/audio.h" |
0d09e41a | 38 | #include "hw/i386/pc.h" |
a2cb15b0 | 39 | #include "hw/pci/pci.h" |
0d09e41a | 40 | #include "hw/audio/audio.h" |
9c17d615 | 41 | #include "sysemu/kvm.h" |
caf71f86 | 42 | #include "migration/migration.h" |
0d09e41a | 43 | #include "hw/i386/smbios.h" |
022c62cb | 44 | #include "exec/address-spaces.h" |
0d09e41a | 45 | #include "hw/audio/pcspk.h" |
caf71f86 | 46 | #include "migration/page_cache.h" |
1de7afc9 | 47 | #include "qemu/config-file.h" |
d97326ee | 48 | #include "qemu/error-report.h" |
99afc91d | 49 | #include "qmp-commands.h" |
3c12193d | 50 | #include "trace.h" |
0d6d3c87 | 51 | #include "exec/cpu-all.h" |
12291ec1 | 52 | #include "exec/ram_addr.h" |
0445259b | 53 | #include "hw/acpi/acpi.h" |
aa8dc044 | 54 | #include "qemu/host-utils.h" |
0dc3f44a | 55 | #include "qemu/rcu_queue.h" |
ad96090a | 56 | |
3a697f69 OW |
57 | #ifdef DEBUG_ARCH_INIT |
58 | #define DPRINTF(fmt, ...) \ | |
59 | do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0) | |
60 | #else | |
61 | #define DPRINTF(fmt, ...) \ | |
62 | do { } while (0) | |
63 | #endif | |
64 | ||
ad96090a BS |
65 | #ifdef TARGET_SPARC |
66 | int graphic_width = 1024; | |
67 | int graphic_height = 768; | |
68 | int graphic_depth = 8; | |
69 | #else | |
70 | int graphic_width = 800; | |
71 | int graphic_height = 600; | |
f1ff0e89 | 72 | int graphic_depth = 32; |
ad96090a BS |
73 | #endif |
74 | ||
ad96090a BS |
75 | |
76 | #if defined(TARGET_ALPHA) | |
77 | #define QEMU_ARCH QEMU_ARCH_ALPHA | |
78 | #elif defined(TARGET_ARM) | |
79 | #define QEMU_ARCH QEMU_ARCH_ARM | |
80 | #elif defined(TARGET_CRIS) | |
81 | #define QEMU_ARCH QEMU_ARCH_CRIS | |
82 | #elif defined(TARGET_I386) | |
83 | #define QEMU_ARCH QEMU_ARCH_I386 | |
84 | #elif defined(TARGET_M68K) | |
85 | #define QEMU_ARCH QEMU_ARCH_M68K | |
81ea0e13 MW |
86 | #elif defined(TARGET_LM32) |
87 | #define QEMU_ARCH QEMU_ARCH_LM32 | |
ad96090a BS |
88 | #elif defined(TARGET_MICROBLAZE) |
89 | #define QEMU_ARCH QEMU_ARCH_MICROBLAZE | |
90 | #elif defined(TARGET_MIPS) | |
91 | #define QEMU_ARCH QEMU_ARCH_MIPS | |
d15a9c23 AG |
92 | #elif defined(TARGET_MOXIE) |
93 | #define QEMU_ARCH QEMU_ARCH_MOXIE | |
e67db06e JL |
94 | #elif defined(TARGET_OPENRISC) |
95 | #define QEMU_ARCH QEMU_ARCH_OPENRISC | |
ad96090a BS |
96 | #elif defined(TARGET_PPC) |
97 | #define QEMU_ARCH QEMU_ARCH_PPC | |
98 | #elif defined(TARGET_S390X) | |
99 | #define QEMU_ARCH QEMU_ARCH_S390X | |
100 | #elif defined(TARGET_SH4) | |
101 | #define QEMU_ARCH QEMU_ARCH_SH4 | |
102 | #elif defined(TARGET_SPARC) | |
103 | #define QEMU_ARCH QEMU_ARCH_SPARC | |
2328826b MF |
104 | #elif defined(TARGET_XTENSA) |
105 | #define QEMU_ARCH QEMU_ARCH_XTENSA | |
4f23a1e6 GX |
106 | #elif defined(TARGET_UNICORE32) |
107 | #define QEMU_ARCH QEMU_ARCH_UNICORE32 | |
48e06fe0 BK |
108 | #elif defined(TARGET_TRICORE) |
109 | #define QEMU_ARCH QEMU_ARCH_TRICORE | |
ad96090a BS |
110 | #endif |
111 | ||
112 | const uint32_t arch_type = QEMU_ARCH; | |
7ca1dfad CV |
113 | static bool mig_throttle_on; |
114 | static int dirty_rate_high_cnt; | |
115 | static void check_guest_throttling(void); | |
ad96090a | 116 | |
71411d35 C |
117 | static uint64_t bitmap_sync_count; |
118 | ||
ad96090a BS |
119 | /***********************************************************/ |
120 | /* ram save/restore */ | |
121 | ||
d20878d2 YT |
122 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
123 | #define RAM_SAVE_FLAG_COMPRESS 0x02 | |
124 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 | |
125 | #define RAM_SAVE_FLAG_PAGE 0x08 | |
126 | #define RAM_SAVE_FLAG_EOS 0x10 | |
127 | #define RAM_SAVE_FLAG_CONTINUE 0x20 | |
17ad9b35 | 128 | #define RAM_SAVE_FLAG_XBZRLE 0x40 |
0033b8b4 | 129 | /* 0x80 is reserved in migration.h start with 0x100 next */ |
ad96090a | 130 | |
756557de EH |
131 | static struct defconfig_file { |
132 | const char *filename; | |
f29a5614 EH |
133 | /* Indicates it is an user config file (disabled by -no-user-config) */ |
134 | bool userconfig; | |
756557de | 135 | } default_config_files[] = { |
f29a5614 | 136 | { CONFIG_QEMU_CONFDIR "/qemu.conf", true }, |
2e59915d | 137 | { CONFIG_QEMU_CONFDIR "/target-" TARGET_NAME ".conf", true }, |
756557de EH |
138 | { NULL }, /* end of list */ |
139 | }; | |
140 | ||
6d3cb1f9 | 141 | static const uint8_t ZERO_TARGET_PAGE[TARGET_PAGE_SIZE]; |
756557de | 142 | |
f29a5614 | 143 | int qemu_read_default_config_files(bool userconfig) |
b5a8fe5e EH |
144 | { |
145 | int ret; | |
756557de | 146 | struct defconfig_file *f; |
b5a8fe5e | 147 | |
756557de | 148 | for (f = default_config_files; f->filename; f++) { |
f29a5614 EH |
149 | if (!userconfig && f->userconfig) { |
150 | continue; | |
151 | } | |
756557de EH |
152 | ret = qemu_read_config_file(f->filename); |
153 | if (ret < 0 && ret != -ENOENT) { | |
154 | return ret; | |
155 | } | |
b5a8fe5e | 156 | } |
4d8b3c63 | 157 | |
b5a8fe5e EH |
158 | return 0; |
159 | } | |
160 | ||
dc3c26a4 | 161 | static inline bool is_zero_range(uint8_t *p, uint64_t size) |
ad96090a | 162 | { |
dc3c26a4 | 163 | return buffer_find_nonzero_offset(p, size) == size; |
ad96090a BS |
164 | } |
165 | ||
17ad9b35 OW |
166 | /* struct contains XBZRLE cache and a static page |
167 | used by the compression */ | |
168 | static struct { | |
169 | /* buffer used for XBZRLE encoding */ | |
170 | uint8_t *encoded_buf; | |
171 | /* buffer for storing page content */ | |
172 | uint8_t *current_buf; | |
fd8cec93 | 173 | /* Cache for XBZRLE, Protected by lock. */ |
17ad9b35 | 174 | PageCache *cache; |
fd8cec93 | 175 | QemuMutex lock; |
d97326ee DDAG |
176 | } XBZRLE; |
177 | ||
905f26f2 GA |
178 | /* buffer used for XBZRLE decoding */ |
179 | static uint8_t *xbzrle_decoded_buf; | |
9e1ba4cc | 180 | |
fd8cec93 GA |
181 | static void XBZRLE_cache_lock(void) |
182 | { | |
183 | if (migrate_use_xbzrle()) | |
184 | qemu_mutex_lock(&XBZRLE.lock); | |
185 | } | |
186 | ||
187 | static void XBZRLE_cache_unlock(void) | |
188 | { | |
189 | if (migrate_use_xbzrle()) | |
190 | qemu_mutex_unlock(&XBZRLE.lock); | |
191 | } | |
192 | ||
d97326ee DDAG |
193 | /* |
194 | * called from qmp_migrate_set_cache_size in main thread, possibly while | |
195 | * a migration is in progress. | |
196 | * A running migration maybe using the cache and might finish during this | |
197 | * call, hence changes to the cache are protected by XBZRLE.lock(). | |
198 | */ | |
9e1ba4cc OW |
199 | int64_t xbzrle_cache_resize(int64_t new_size) |
200 | { | |
d97326ee DDAG |
201 | PageCache *new_cache; |
202 | int64_t ret; | |
fd8cec93 | 203 | |
c91e681a OW |
204 | if (new_size < TARGET_PAGE_SIZE) { |
205 | return -1; | |
206 | } | |
207 | ||
d97326ee DDAG |
208 | XBZRLE_cache_lock(); |
209 | ||
9e1ba4cc | 210 | if (XBZRLE.cache != NULL) { |
fd8cec93 | 211 | if (pow2floor(new_size) == migrate_xbzrle_cache_size()) { |
d97326ee | 212 | goto out_new_size; |
fd8cec93 GA |
213 | } |
214 | new_cache = cache_init(new_size / TARGET_PAGE_SIZE, | |
215 | TARGET_PAGE_SIZE); | |
216 | if (!new_cache) { | |
d97326ee DDAG |
217 | error_report("Error creating cache"); |
218 | ret = -1; | |
219 | goto out; | |
fd8cec93 | 220 | } |
fd8cec93 | 221 | |
d97326ee DDAG |
222 | cache_fini(XBZRLE.cache); |
223 | XBZRLE.cache = new_cache; | |
9e1ba4cc | 224 | } |
fd8cec93 | 225 | |
d97326ee DDAG |
226 | out_new_size: |
227 | ret = pow2floor(new_size); | |
228 | out: | |
229 | XBZRLE_cache_unlock(); | |
230 | return ret; | |
9e1ba4cc OW |
231 | } |
232 | ||
004d4c10 OW |
233 | /* accounting for migration statistics */ |
234 | typedef struct AccountingInfo { | |
235 | uint64_t dup_pages; | |
f1c72795 | 236 | uint64_t skipped_pages; |
004d4c10 OW |
237 | uint64_t norm_pages; |
238 | uint64_t iterations; | |
f36d55af OW |
239 | uint64_t xbzrle_bytes; |
240 | uint64_t xbzrle_pages; | |
241 | uint64_t xbzrle_cache_miss; | |
8bc39233 | 242 | double xbzrle_cache_miss_rate; |
f36d55af | 243 | uint64_t xbzrle_overflows; |
004d4c10 OW |
244 | } AccountingInfo; |
245 | ||
246 | static AccountingInfo acct_info; | |
247 | ||
248 | static void acct_clear(void) | |
249 | { | |
250 | memset(&acct_info, 0, sizeof(acct_info)); | |
251 | } | |
252 | ||
253 | uint64_t dup_mig_bytes_transferred(void) | |
254 | { | |
255 | return acct_info.dup_pages * TARGET_PAGE_SIZE; | |
256 | } | |
257 | ||
258 | uint64_t dup_mig_pages_transferred(void) | |
259 | { | |
260 | return acct_info.dup_pages; | |
261 | } | |
262 | ||
f1c72795 PL |
263 | uint64_t skipped_mig_bytes_transferred(void) |
264 | { | |
265 | return acct_info.skipped_pages * TARGET_PAGE_SIZE; | |
266 | } | |
267 | ||
268 | uint64_t skipped_mig_pages_transferred(void) | |
269 | { | |
270 | return acct_info.skipped_pages; | |
271 | } | |
272 | ||
004d4c10 OW |
273 | uint64_t norm_mig_bytes_transferred(void) |
274 | { | |
275 | return acct_info.norm_pages * TARGET_PAGE_SIZE; | |
276 | } | |
277 | ||
278 | uint64_t norm_mig_pages_transferred(void) | |
279 | { | |
280 | return acct_info.norm_pages; | |
281 | } | |
282 | ||
f36d55af OW |
283 | uint64_t xbzrle_mig_bytes_transferred(void) |
284 | { | |
285 | return acct_info.xbzrle_bytes; | |
286 | } | |
287 | ||
288 | uint64_t xbzrle_mig_pages_transferred(void) | |
289 | { | |
290 | return acct_info.xbzrle_pages; | |
291 | } | |
292 | ||
293 | uint64_t xbzrle_mig_pages_cache_miss(void) | |
294 | { | |
295 | return acct_info.xbzrle_cache_miss; | |
296 | } | |
297 | ||
8bc39233 C |
298 | double xbzrle_mig_cache_miss_rate(void) |
299 | { | |
300 | return acct_info.xbzrle_cache_miss_rate; | |
301 | } | |
302 | ||
f36d55af OW |
303 | uint64_t xbzrle_mig_pages_overflow(void) |
304 | { | |
305 | return acct_info.xbzrle_overflows; | |
306 | } | |
307 | ||
3f7d7b09 JQ |
308 | static size_t save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset, |
309 | int cont, int flag) | |
0c51f43d | 310 | { |
3f7d7b09 JQ |
311 | size_t size; |
312 | ||
313 | qemu_put_be64(f, offset | cont | flag); | |
314 | size = 8; | |
0c51f43d | 315 | |
3f7d7b09 JQ |
316 | if (!cont) { |
317 | qemu_put_byte(f, strlen(block->idstr)); | |
318 | qemu_put_buffer(f, (uint8_t *)block->idstr, | |
319 | strlen(block->idstr)); | |
320 | size += 1 + strlen(block->idstr); | |
321 | } | |
322 | return size; | |
0c51f43d OW |
323 | } |
324 | ||
6d3cb1f9 DDAG |
325 | /* This is the last block that we have visited serching for dirty pages |
326 | */ | |
327 | static RAMBlock *last_seen_block; | |
328 | /* This is the last block from where we have sent data */ | |
329 | static RAMBlock *last_sent_block; | |
330 | static ram_addr_t last_offset; | |
331 | static unsigned long *migration_bitmap; | |
332 | static uint64_t migration_dirty_pages; | |
333 | static uint32_t last_version; | |
334 | static bool ram_bulk_stage; | |
335 | ||
336 | /* Update the xbzrle cache to reflect a page that's been sent as all 0. | |
337 | * The important thing is that a stale (not-yet-0'd) page be replaced | |
338 | * by the new data. | |
339 | * As a bonus, if the page wasn't in the cache it gets added so that | |
340 | * when a small write is made into the 0'd page it gets XBZRLE sent | |
341 | */ | |
342 | static void xbzrle_cache_zero_page(ram_addr_t current_addr) | |
343 | { | |
344 | if (ram_bulk_stage || !migrate_use_xbzrle()) { | |
345 | return; | |
346 | } | |
347 | ||
348 | /* We don't care if this fails to allocate a new cache page | |
349 | * as long as it updated an old one */ | |
27af7d6e C |
350 | cache_insert(XBZRLE.cache, current_addr, ZERO_TARGET_PAGE, |
351 | bitmap_sync_count); | |
6d3cb1f9 DDAG |
352 | } |
353 | ||
17ad9b35 OW |
354 | #define ENCODING_FLAG_XBZRLE 0x1 |
355 | ||
1534ee93 | 356 | static int save_xbzrle_page(QEMUFile *f, uint8_t **current_data, |
17ad9b35 | 357 | ram_addr_t current_addr, RAMBlock *block, |
dd051c72 | 358 | ram_addr_t offset, int cont, bool last_stage) |
17ad9b35 OW |
359 | { |
360 | int encoded_len = 0, bytes_sent = -1; | |
361 | uint8_t *prev_cached_page; | |
362 | ||
27af7d6e | 363 | if (!cache_is_cached(XBZRLE.cache, current_addr, bitmap_sync_count)) { |
1534ee93 | 364 | acct_info.xbzrle_cache_miss++; |
dd051c72 | 365 | if (!last_stage) { |
27af7d6e C |
366 | if (cache_insert(XBZRLE.cache, current_addr, *current_data, |
367 | bitmap_sync_count) == -1) { | |
89db9987 | 368 | return -1; |
1534ee93 C |
369 | } else { |
370 | /* update *current_data when the page has been | |
371 | inserted into cache */ | |
372 | *current_data = get_cached_data(XBZRLE.cache, current_addr); | |
89db9987 | 373 | } |
dd051c72 | 374 | } |
17ad9b35 OW |
375 | return -1; |
376 | } | |
377 | ||
378 | prev_cached_page = get_cached_data(XBZRLE.cache, current_addr); | |
379 | ||
380 | /* save current buffer into memory */ | |
1534ee93 | 381 | memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE); |
17ad9b35 OW |
382 | |
383 | /* XBZRLE encoding (if there is no overflow) */ | |
384 | encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf, | |
385 | TARGET_PAGE_SIZE, XBZRLE.encoded_buf, | |
386 | TARGET_PAGE_SIZE); | |
387 | if (encoded_len == 0) { | |
388 | DPRINTF("Skipping unmodified page\n"); | |
389 | return 0; | |
390 | } else if (encoded_len == -1) { | |
391 | DPRINTF("Overflow\n"); | |
f36d55af | 392 | acct_info.xbzrle_overflows++; |
17ad9b35 | 393 | /* update data in the cache */ |
1534ee93 C |
394 | if (!last_stage) { |
395 | memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE); | |
396 | *current_data = prev_cached_page; | |
397 | } | |
17ad9b35 OW |
398 | return -1; |
399 | } | |
400 | ||
401 | /* we need to update the data in the cache, in order to get the same data */ | |
dd051c72 JQ |
402 | if (!last_stage) { |
403 | memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE); | |
404 | } | |
17ad9b35 OW |
405 | |
406 | /* Send XBZRLE based compressed page */ | |
3f7d7b09 | 407 | bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE); |
17ad9b35 OW |
408 | qemu_put_byte(f, ENCODING_FLAG_XBZRLE); |
409 | qemu_put_be16(f, encoded_len); | |
410 | qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len); | |
3f7d7b09 | 411 | bytes_sent += encoded_len + 1 + 2; |
f36d55af OW |
412 | acct_info.xbzrle_pages++; |
413 | acct_info.xbzrle_bytes += bytes_sent; | |
17ad9b35 OW |
414 | |
415 | return bytes_sent; | |
416 | } | |
417 | ||
4c8ae0f6 JQ |
418 | static inline |
419 | ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr, | |
420 | ram_addr_t start) | |
69268cde | 421 | { |
4c8ae0f6 JQ |
422 | unsigned long base = mr->ram_addr >> TARGET_PAGE_BITS; |
423 | unsigned long nr = base + (start >> TARGET_PAGE_BITS); | |
0851c9f7 MT |
424 | uint64_t mr_size = TARGET_PAGE_ALIGN(memory_region_size(mr)); |
425 | unsigned long size = base + (mr_size >> TARGET_PAGE_BITS); | |
c6bf8e0e | 426 | |
70c8652b PL |
427 | unsigned long next; |
428 | ||
429 | if (ram_bulk_stage && nr > base) { | |
430 | next = nr + 1; | |
431 | } else { | |
432 | next = find_next_bit(migration_bitmap, size, nr); | |
433 | } | |
69268cde | 434 | |
4c8ae0f6 JQ |
435 | if (next < size) { |
436 | clear_bit(next, migration_bitmap); | |
c6bf8e0e | 437 | migration_dirty_pages--; |
69268cde | 438 | } |
4c8ae0f6 | 439 | return (next - base) << TARGET_PAGE_BITS; |
69268cde JQ |
440 | } |
441 | ||
791fa2a2 | 442 | static inline bool migration_bitmap_set_dirty(ram_addr_t addr) |
e44d26c8 | 443 | { |
c6bf8e0e | 444 | bool ret; |
791fa2a2 | 445 | int nr = addr >> TARGET_PAGE_BITS; |
e44d26c8 | 446 | |
c6bf8e0e JQ |
447 | ret = test_and_set_bit(nr, migration_bitmap); |
448 | ||
449 | if (!ret) { | |
450 | migration_dirty_pages++; | |
e44d26c8 | 451 | } |
c6bf8e0e | 452 | return ret; |
e44d26c8 JQ |
453 | } |
454 | ||
791fa2a2 JQ |
455 | static void migration_bitmap_sync_range(ram_addr_t start, ram_addr_t length) |
456 | { | |
457 | ram_addr_t addr; | |
aa8dc044 JQ |
458 | unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS); |
459 | ||
460 | /* start address is aligned at the start of a word? */ | |
461 | if (((page * BITS_PER_LONG) << TARGET_PAGE_BITS) == start) { | |
462 | int k; | |
463 | int nr = BITS_TO_LONGS(length >> TARGET_PAGE_BITS); | |
464 | unsigned long *src = ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION]; | |
465 | ||
466 | for (k = page; k < page + nr; k++) { | |
467 | if (src[k]) { | |
468 | unsigned long new_dirty; | |
469 | new_dirty = ~migration_bitmap[k]; | |
470 | migration_bitmap[k] |= src[k]; | |
471 | new_dirty &= src[k]; | |
472 | migration_dirty_pages += ctpopl(new_dirty); | |
473 | src[k] = 0; | |
474 | } | |
475 | } | |
476 | } else { | |
477 | for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) { | |
478 | if (cpu_physical_memory_get_dirty(start + addr, | |
479 | TARGET_PAGE_SIZE, | |
480 | DIRTY_MEMORY_MIGRATION)) { | |
481 | cpu_physical_memory_reset_dirty(start + addr, | |
482 | TARGET_PAGE_SIZE, | |
483 | DIRTY_MEMORY_MIGRATION); | |
484 | migration_bitmap_set_dirty(start + addr); | |
485 | } | |
791fa2a2 JQ |
486 | } |
487 | } | |
488 | } | |
489 | ||
490 | ||
6c1b663c C |
491 | /* Fix me: there are too many global variables used in migration process. */ |
492 | static int64_t start_time; | |
493 | static int64_t bytes_xfer_prev; | |
494 | static int64_t num_dirty_pages_period; | |
495 | ||
496 | static void migration_bitmap_sync_init(void) | |
497 | { | |
498 | start_time = 0; | |
499 | bytes_xfer_prev = 0; | |
500 | num_dirty_pages_period = 0; | |
501 | } | |
32c835ba | 502 | |
ae3a7047 | 503 | /* Called with iothread lock held, to protect ram_list.dirty_memory[] */ |
dd2df737 JQ |
504 | static void migration_bitmap_sync(void) |
505 | { | |
c6bf8e0e | 506 | RAMBlock *block; |
c6bf8e0e | 507 | uint64_t num_dirty_pages_init = migration_dirty_pages; |
8d017193 | 508 | MigrationState *s = migrate_get_current(); |
8d017193 | 509 | int64_t end_time; |
7ca1dfad | 510 | int64_t bytes_xfer_now; |
8bc39233 C |
511 | static uint64_t xbzrle_cache_miss_prev; |
512 | static uint64_t iterations_prev; | |
7ca1dfad | 513 | |
71411d35 C |
514 | bitmap_sync_count++; |
515 | ||
7ca1dfad CV |
516 | if (!bytes_xfer_prev) { |
517 | bytes_xfer_prev = ram_bytes_transferred(); | |
518 | } | |
8d017193 JQ |
519 | |
520 | if (!start_time) { | |
bc72ad67 | 521 | start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
8d017193 | 522 | } |
3c12193d JQ |
523 | |
524 | trace_migration_bitmap_sync_start(); | |
1d671369 | 525 | address_space_sync_dirty_bitmap(&address_space_memory); |
c6bf8e0e | 526 | |
0dc3f44a MD |
527 | rcu_read_lock(); |
528 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { | |
9b8424d5 | 529 | migration_bitmap_sync_range(block->mr->ram_addr, block->used_length); |
c6bf8e0e | 530 | } |
0dc3f44a MD |
531 | rcu_read_unlock(); |
532 | ||
c6bf8e0e | 533 | trace_migration_bitmap_sync_end(migration_dirty_pages |
3c12193d | 534 | - num_dirty_pages_init); |
8d017193 | 535 | num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init; |
bc72ad67 | 536 | end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
8d017193 JQ |
537 | |
538 | /* more than 1 second = 1000 millisecons */ | |
539 | if (end_time > start_time + 1000) { | |
7ca1dfad CV |
540 | if (migrate_auto_converge()) { |
541 | /* The following detection logic can be refined later. For now: | |
542 | Check to see if the dirtied bytes is 50% more than the approx. | |
543 | amount of bytes that just got transferred since the last time we | |
544 | were in this routine. If that happens >N times (for now N==4) | |
545 | we turn on the throttle down logic */ | |
546 | bytes_xfer_now = ram_bytes_transferred(); | |
547 | if (s->dirty_pages_rate && | |
548 | (num_dirty_pages_period * TARGET_PAGE_SIZE > | |
549 | (bytes_xfer_now - bytes_xfer_prev)/2) && | |
550 | (dirty_rate_high_cnt++ > 4)) { | |
551 | trace_migration_throttle(); | |
552 | mig_throttle_on = true; | |
553 | dirty_rate_high_cnt = 0; | |
554 | } | |
555 | bytes_xfer_prev = bytes_xfer_now; | |
556 | } else { | |
557 | mig_throttle_on = false; | |
558 | } | |
8bc39233 C |
559 | if (migrate_use_xbzrle()) { |
560 | if (iterations_prev != 0) { | |
561 | acct_info.xbzrle_cache_miss_rate = | |
562 | (double)(acct_info.xbzrle_cache_miss - | |
563 | xbzrle_cache_miss_prev) / | |
564 | (acct_info.iterations - iterations_prev); | |
565 | } | |
566 | iterations_prev = acct_info.iterations; | |
567 | xbzrle_cache_miss_prev = acct_info.xbzrle_cache_miss; | |
568 | } | |
8d017193 JQ |
569 | s->dirty_pages_rate = num_dirty_pages_period * 1000 |
570 | / (end_time - start_time); | |
90f8ae72 | 571 | s->dirty_bytes_rate = s->dirty_pages_rate * TARGET_PAGE_SIZE; |
8d017193 JQ |
572 | start_time = end_time; |
573 | num_dirty_pages_period = 0; | |
58570ed8 | 574 | s->dirty_sync_count = bitmap_sync_count; |
8d017193 | 575 | } |
dd2df737 JQ |
576 | } |
577 | ||
87cf878b | 578 | /** |
14bcfdc7 DDAG |
579 | * ram_save_page: Send the given page to the stream |
580 | * | |
87cf878b JQ |
581 | * Returns: Number of pages written. |
582 | * | |
583 | * @f: QEMUFile where to send the data | |
584 | * @block: block that contains the page we want to send | |
585 | * @offset: offset inside the block for the page | |
586 | * @last_stage: if we are at the completion stage | |
587 | * @bytes_transferred: increase it with the number of transferred bytes | |
14bcfdc7 DDAG |
588 | */ |
589 | static int ram_save_page(QEMUFile *f, RAMBlock* block, ram_addr_t offset, | |
87cf878b | 590 | bool last_stage, uint64_t *bytes_transferred) |
14bcfdc7 | 591 | { |
87cf878b | 592 | int pages = -1; |
6e1dea46 | 593 | uint64_t bytes_xmit; |
14bcfdc7 DDAG |
594 | int cont; |
595 | ram_addr_t current_addr; | |
596 | MemoryRegion *mr = block->mr; | |
597 | uint8_t *p; | |
598 | int ret; | |
599 | bool send_async = true; | |
600 | ||
601 | cont = (block == last_sent_block) ? RAM_SAVE_FLAG_CONTINUE : 0; | |
602 | ||
603 | p = memory_region_get_ram_ptr(mr) + offset; | |
604 | ||
605 | /* In doubt sent page as normal */ | |
6e1dea46 | 606 | bytes_xmit = 0; |
14bcfdc7 | 607 | ret = ram_control_save_page(f, block->offset, |
6e1dea46 JQ |
608 | offset, TARGET_PAGE_SIZE, &bytes_xmit); |
609 | if (bytes_xmit) { | |
87cf878b JQ |
610 | *bytes_transferred += bytes_xmit; |
611 | pages = 1; | |
6e1dea46 | 612 | } |
14bcfdc7 DDAG |
613 | |
614 | XBZRLE_cache_lock(); | |
615 | ||
616 | current_addr = block->offset + offset; | |
617 | if (ret != RAM_SAVE_CONTROL_NOT_SUPP) { | |
618 | if (ret != RAM_SAVE_CONTROL_DELAYED) { | |
6e1dea46 | 619 | if (bytes_xmit > 0) { |
14bcfdc7 | 620 | acct_info.norm_pages++; |
6e1dea46 | 621 | } else if (bytes_xmit == 0) { |
14bcfdc7 DDAG |
622 | acct_info.dup_pages++; |
623 | } | |
624 | } | |
625 | } else if (is_zero_range(p, TARGET_PAGE_SIZE)) { | |
626 | acct_info.dup_pages++; | |
87cf878b JQ |
627 | *bytes_transferred += save_block_hdr(f, block, offset, cont, |
628 | RAM_SAVE_FLAG_COMPRESS); | |
14bcfdc7 | 629 | qemu_put_byte(f, 0); |
87cf878b JQ |
630 | *bytes_transferred += 1; |
631 | pages = 1; | |
14bcfdc7 DDAG |
632 | /* Must let xbzrle know, otherwise a previous (now 0'd) cached |
633 | * page would be stale | |
634 | */ | |
635 | xbzrle_cache_zero_page(current_addr); | |
636 | } else if (!ram_bulk_stage && migrate_use_xbzrle()) { | |
87cf878b JQ |
637 | int bytes_sent; |
638 | ||
14bcfdc7 DDAG |
639 | bytes_sent = save_xbzrle_page(f, &p, current_addr, block, |
640 | offset, cont, last_stage); | |
87cf878b JQ |
641 | |
642 | if (bytes_sent > 0) { | |
643 | *bytes_transferred += bytes_sent; | |
644 | pages = 1; | |
645 | } else if (bytes_sent == 0) { | |
646 | pages = 0; | |
647 | } else { | |
648 | pages = -1; | |
649 | } | |
14bcfdc7 DDAG |
650 | if (!last_stage) { |
651 | /* Can't send this cached data async, since the cache page | |
652 | * might get updated before it gets to the wire | |
653 | */ | |
654 | send_async = false; | |
655 | } | |
656 | } | |
657 | ||
658 | /* XBZRLE overflow or normal page */ | |
87cf878b JQ |
659 | if (pages == -1) { |
660 | *bytes_transferred += save_block_hdr(f, block, offset, cont, | |
661 | RAM_SAVE_FLAG_PAGE); | |
14bcfdc7 DDAG |
662 | if (send_async) { |
663 | qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE); | |
664 | } else { | |
665 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); | |
666 | } | |
87cf878b JQ |
667 | *bytes_transferred += TARGET_PAGE_SIZE; |
668 | pages = 1; | |
14bcfdc7 DDAG |
669 | acct_info.norm_pages++; |
670 | } | |
671 | ||
672 | XBZRLE_cache_unlock(); | |
673 | ||
87cf878b | 674 | return pages; |
14bcfdc7 DDAG |
675 | } |
676 | ||
0fcd8d31 JQ |
677 | /** |
678 | * ram_find_and_save_block: Finds a dirty page and sends it to f | |
6c779f22 | 679 | * |
0dc3f44a MD |
680 | * Called within an RCU critical section. |
681 | * | |
0fcd8d31 | 682 | * Returns: The number of pages written |
b823ceaa | 683 | * 0 means no dirty pages |
0fcd8d31 JQ |
684 | * |
685 | * @f: QEMUFile where to send the data | |
686 | * @last_stage: if we are at the completion stage | |
687 | * @bytes_transferred: increase it with the number of transferred bytes | |
6c779f22 OW |
688 | */ |
689 | ||
0fcd8d31 JQ |
690 | static int ram_find_and_save_block(QEMUFile *f, bool last_stage, |
691 | uint64_t *bytes_transferred) | |
ad96090a | 692 | { |
b23a9a5c | 693 | RAMBlock *block = last_seen_block; |
e44359c3 | 694 | ram_addr_t offset = last_offset; |
4c8ae0f6 | 695 | bool complete_round = false; |
87cf878b | 696 | int pages = 0; |
71c510e2 | 697 | MemoryRegion *mr; |
ad96090a | 698 | |
e44359c3 | 699 | if (!block) |
0dc3f44a | 700 | block = QLIST_FIRST_RCU(&ram_list.blocks); |
e44359c3 | 701 | |
4c8ae0f6 | 702 | while (true) { |
71c510e2 | 703 | mr = block->mr; |
4c8ae0f6 JQ |
704 | offset = migration_bitmap_find_and_reset_dirty(mr, offset); |
705 | if (complete_round && block == last_seen_block && | |
706 | offset >= last_offset) { | |
707 | break; | |
708 | } | |
9b8424d5 | 709 | if (offset >= block->used_length) { |
4c8ae0f6 | 710 | offset = 0; |
0dc3f44a | 711 | block = QLIST_NEXT_RCU(block, next); |
4c8ae0f6 | 712 | if (!block) { |
0dc3f44a | 713 | block = QLIST_FIRST_RCU(&ram_list.blocks); |
4c8ae0f6 | 714 | complete_round = true; |
78d07ae7 | 715 | ram_bulk_stage = false; |
4c8ae0f6 JQ |
716 | } |
717 | } else { | |
87cf878b JQ |
718 | pages = ram_save_page(f, block, offset, last_stage, |
719 | bytes_transferred); | |
17ad9b35 | 720 | |
17ad9b35 | 721 | /* if page is unmodified, continue to the next */ |
87cf878b | 722 | if (pages > 0) { |
5f718a15 | 723 | last_sent_block = block; |
17ad9b35 OW |
724 | break; |
725 | } | |
ad96090a | 726 | } |
4c8ae0f6 | 727 | } |
ae3a7047 | 728 | |
b23a9a5c | 729 | last_seen_block = block; |
e44359c3 | 730 | last_offset = offset; |
0fcd8d31 | 731 | |
87cf878b | 732 | return pages; |
ad96090a BS |
733 | } |
734 | ||
735 | static uint64_t bytes_transferred; | |
736 | ||
2b0ce079 MH |
737 | void acct_update_position(QEMUFile *f, size_t size, bool zero) |
738 | { | |
739 | uint64_t pages = size / TARGET_PAGE_SIZE; | |
740 | if (zero) { | |
741 | acct_info.dup_pages += pages; | |
742 | } else { | |
743 | acct_info.norm_pages += pages; | |
744 | bytes_transferred += size; | |
745 | qemu_update_position(f, size); | |
746 | } | |
747 | } | |
748 | ||
ad96090a BS |
749 | static ram_addr_t ram_save_remaining(void) |
750 | { | |
c6bf8e0e | 751 | return migration_dirty_pages; |
ad96090a BS |
752 | } |
753 | ||
754 | uint64_t ram_bytes_remaining(void) | |
755 | { | |
756 | return ram_save_remaining() * TARGET_PAGE_SIZE; | |
757 | } | |
758 | ||
759 | uint64_t ram_bytes_transferred(void) | |
760 | { | |
761 | return bytes_transferred; | |
762 | } | |
763 | ||
764 | uint64_t ram_bytes_total(void) | |
765 | { | |
d17b5288 AW |
766 | RAMBlock *block; |
767 | uint64_t total = 0; | |
768 | ||
0dc3f44a MD |
769 | rcu_read_lock(); |
770 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) | |
9b8424d5 | 771 | total += block->used_length; |
0dc3f44a | 772 | rcu_read_unlock(); |
d17b5288 | 773 | return total; |
ad96090a BS |
774 | } |
775 | ||
905f26f2 GA |
776 | void free_xbzrle_decoded_buf(void) |
777 | { | |
778 | g_free(xbzrle_decoded_buf); | |
779 | xbzrle_decoded_buf = NULL; | |
780 | } | |
781 | ||
8e21cd32 OW |
782 | static void migration_end(void) |
783 | { | |
244eaa75 PB |
784 | if (migration_bitmap) { |
785 | memory_global_dirty_log_stop(); | |
786 | g_free(migration_bitmap); | |
787 | migration_bitmap = NULL; | |
788 | } | |
17ad9b35 | 789 | |
fd8cec93 | 790 | XBZRLE_cache_lock(); |
244eaa75 | 791 | if (XBZRLE.cache) { |
17ad9b35 | 792 | cache_fini(XBZRLE.cache); |
17ad9b35 OW |
793 | g_free(XBZRLE.encoded_buf); |
794 | g_free(XBZRLE.current_buf); | |
17ad9b35 | 795 | XBZRLE.cache = NULL; |
f6c6483b OW |
796 | XBZRLE.encoded_buf = NULL; |
797 | XBZRLE.current_buf = NULL; | |
17ad9b35 | 798 | } |
fd8cec93 | 799 | XBZRLE_cache_unlock(); |
8e21cd32 OW |
800 | } |
801 | ||
9b5bfab0 JQ |
802 | static void ram_migration_cancel(void *opaque) |
803 | { | |
804 | migration_end(); | |
805 | } | |
806 | ||
5a170775 JQ |
807 | static void reset_ram_globals(void) |
808 | { | |
b23a9a5c | 809 | last_seen_block = NULL; |
5f718a15 | 810 | last_sent_block = NULL; |
5a170775 | 811 | last_offset = 0; |
f798b07f | 812 | last_version = ram_list.version; |
78d07ae7 | 813 | ram_bulk_stage = true; |
5a170775 JQ |
814 | } |
815 | ||
4508bd9e JQ |
816 | #define MAX_WAIT 50 /* ms, half buffered_file limit */ |
817 | ||
0dc3f44a MD |
818 | |
819 | /* Each of ram_save_setup, ram_save_iterate and ram_save_complete has | |
820 | * long-running RCU critical section. When rcu-reclaims in the code | |
821 | * start to become numerous it will be necessary to reduce the | |
822 | * granularity of these critical sections. | |
823 | */ | |
824 | ||
d1315aac | 825 | static int ram_save_setup(QEMUFile *f, void *opaque) |
ad96090a | 826 | { |
d1315aac | 827 | RAMBlock *block; |
e30d1d8c | 828 | int64_t ram_bitmap_pages; /* Size of bitmap in pages, including gaps */ |
c6bf8e0e | 829 | |
7ca1dfad CV |
830 | mig_throttle_on = false; |
831 | dirty_rate_high_cnt = 0; | |
71411d35 | 832 | bitmap_sync_count = 0; |
6c1b663c | 833 | migration_bitmap_sync_init(); |
ad96090a | 834 | |
17ad9b35 | 835 | if (migrate_use_xbzrle()) { |
d97326ee | 836 | XBZRLE_cache_lock(); |
17ad9b35 OW |
837 | XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() / |
838 | TARGET_PAGE_SIZE, | |
839 | TARGET_PAGE_SIZE); | |
840 | if (!XBZRLE.cache) { | |
d97326ee DDAG |
841 | XBZRLE_cache_unlock(); |
842 | error_report("Error creating cache"); | |
17ad9b35 OW |
843 | return -1; |
844 | } | |
d97326ee | 845 | XBZRLE_cache_unlock(); |
a17b2fd3 OW |
846 | |
847 | /* We prefer not to abort if there is no memory */ | |
848 | XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE); | |
849 | if (!XBZRLE.encoded_buf) { | |
d97326ee | 850 | error_report("Error allocating encoded_buf"); |
a17b2fd3 OW |
851 | return -1; |
852 | } | |
853 | ||
854 | XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE); | |
855 | if (!XBZRLE.current_buf) { | |
d97326ee | 856 | error_report("Error allocating current_buf"); |
a17b2fd3 OW |
857 | g_free(XBZRLE.encoded_buf); |
858 | XBZRLE.encoded_buf = NULL; | |
859 | return -1; | |
860 | } | |
861 | ||
004d4c10 | 862 | acct_clear(); |
17ad9b35 OW |
863 | } |
864 | ||
ae3a7047 | 865 | /* iothread lock needed for ram_list.dirty_memory[] */ |
9b095037 PB |
866 | qemu_mutex_lock_iothread(); |
867 | qemu_mutex_lock_ramlist(); | |
0dc3f44a | 868 | rcu_read_lock(); |
9b095037 PB |
869 | bytes_transferred = 0; |
870 | reset_ram_globals(); | |
871 | ||
e30d1d8c DDAG |
872 | ram_bitmap_pages = last_ram_offset() >> TARGET_PAGE_BITS; |
873 | migration_bitmap = bitmap_new(ram_bitmap_pages); | |
874 | bitmap_set(migration_bitmap, 0, ram_bitmap_pages); | |
875 | ||
876 | /* | |
877 | * Count the total number of pages used by ram blocks not including any | |
878 | * gaps due to alignment or unplugs. | |
879 | */ | |
880 | migration_dirty_pages = 0; | |
0dc3f44a | 881 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
e30d1d8c DDAG |
882 | uint64_t block_pages; |
883 | ||
9b8424d5 | 884 | block_pages = block->used_length >> TARGET_PAGE_BITS; |
e30d1d8c DDAG |
885 | migration_dirty_pages += block_pages; |
886 | } | |
887 | ||
d1315aac | 888 | memory_global_dirty_log_start(); |
c6bf8e0e | 889 | migration_bitmap_sync(); |
0dc3f44a | 890 | qemu_mutex_unlock_ramlist(); |
9b095037 | 891 | qemu_mutex_unlock_iothread(); |
ad96090a | 892 | |
d1315aac | 893 | qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE); |
97ab12d4 | 894 | |
0dc3f44a | 895 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
d1315aac JQ |
896 | qemu_put_byte(f, strlen(block->idstr)); |
897 | qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); | |
9b8424d5 | 898 | qemu_put_be64(f, block->used_length); |
ad96090a BS |
899 | } |
900 | ||
0dc3f44a | 901 | rcu_read_unlock(); |
0033b8b4 MH |
902 | |
903 | ram_control_before_iterate(f, RAM_CONTROL_SETUP); | |
904 | ram_control_after_iterate(f, RAM_CONTROL_SETUP); | |
905 | ||
d1315aac JQ |
906 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
907 | ||
908 | return 0; | |
909 | } | |
910 | ||
16310a3c | 911 | static int ram_save_iterate(QEMUFile *f, void *opaque) |
d1315aac | 912 | { |
d1315aac JQ |
913 | int ret; |
914 | int i; | |
e4ed1541 | 915 | int64_t t0; |
0fcd8d31 | 916 | int pages_sent = 0; |
d1315aac | 917 | |
0dc3f44a | 918 | rcu_read_lock(); |
f798b07f UD |
919 | if (ram_list.version != last_version) { |
920 | reset_ram_globals(); | |
921 | } | |
922 | ||
0dc3f44a MD |
923 | /* Read version before ram_list.blocks */ |
924 | smp_rmb(); | |
925 | ||
0033b8b4 MH |
926 | ram_control_before_iterate(f, RAM_CONTROL_ROUND); |
927 | ||
bc72ad67 | 928 | t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
4508bd9e | 929 | i = 0; |
2975725f | 930 | while ((ret = qemu_file_rate_limit(f)) == 0) { |
0fcd8d31 | 931 | int pages; |
ad96090a | 932 | |
0fcd8d31 JQ |
933 | pages = ram_find_and_save_block(f, false, &bytes_transferred); |
934 | /* no more pages to sent */ | |
935 | if (pages == 0) { | |
ad96090a BS |
936 | break; |
937 | } | |
0fcd8d31 | 938 | pages_sent += pages; |
004d4c10 | 939 | acct_info.iterations++; |
7ca1dfad | 940 | check_guest_throttling(); |
4508bd9e JQ |
941 | /* we want to check in the 1st loop, just in case it was the 1st time |
942 | and we had to sync the dirty bitmap. | |
943 | qemu_get_clock_ns() is a bit expensive, so we only check each some | |
944 | iterations | |
945 | */ | |
946 | if ((i & 63) == 0) { | |
bc72ad67 | 947 | uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000; |
4508bd9e | 948 | if (t1 > MAX_WAIT) { |
ef37a699 | 949 | DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n", |
4508bd9e JQ |
950 | t1, i); |
951 | break; | |
952 | } | |
953 | } | |
954 | i++; | |
ad96090a | 955 | } |
0dc3f44a | 956 | rcu_read_unlock(); |
fb3409de | 957 | |
0033b8b4 MH |
958 | /* |
959 | * Must occur before EOS (or any QEMUFile operation) | |
960 | * because of RDMA protocol. | |
961 | */ | |
962 | ram_control_after_iterate(f, RAM_CONTROL_ROUND); | |
963 | ||
6cd0beda LL |
964 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
965 | bytes_transferred += 8; | |
966 | ||
967 | ret = qemu_file_get_error(f); | |
2975725f JQ |
968 | if (ret < 0) { |
969 | return ret; | |
970 | } | |
971 | ||
0fcd8d31 | 972 | return pages_sent; |
16310a3c JQ |
973 | } |
974 | ||
ae3a7047 | 975 | /* Called with iothread lock */ |
16310a3c JQ |
976 | static int ram_save_complete(QEMUFile *f, void *opaque) |
977 | { | |
0dc3f44a MD |
978 | rcu_read_lock(); |
979 | ||
9c339485 | 980 | migration_bitmap_sync(); |
b2a8658e | 981 | |
0033b8b4 MH |
982 | ram_control_before_iterate(f, RAM_CONTROL_FINISH); |
983 | ||
ad96090a | 984 | /* try transferring iterative blocks of memory */ |
3a697f69 | 985 | |
16310a3c | 986 | /* flush all remaining blocks regardless of rate limiting */ |
6c779f22 | 987 | while (true) { |
0fcd8d31 | 988 | int pages; |
3fc250b4 | 989 | |
0fcd8d31 | 990 | pages = ram_find_and_save_block(f, true, &bytes_transferred); |
6c779f22 | 991 | /* no more blocks to sent */ |
0fcd8d31 | 992 | if (pages == 0) { |
6c779f22 | 993 | break; |
ad96090a | 994 | } |
ad96090a | 995 | } |
0033b8b4 MH |
996 | |
997 | ram_control_after_iterate(f, RAM_CONTROL_FINISH); | |
244eaa75 | 998 | migration_end(); |
ad96090a | 999 | |
0dc3f44a | 1000 | rcu_read_unlock(); |
ad96090a BS |
1001 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
1002 | ||
5b3c9638 | 1003 | return 0; |
ad96090a BS |
1004 | } |
1005 | ||
e4ed1541 JQ |
1006 | static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size) |
1007 | { | |
1008 | uint64_t remaining_size; | |
1009 | ||
1010 | remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE; | |
1011 | ||
1012 | if (remaining_size < max_size) { | |
32c835ba | 1013 | qemu_mutex_lock_iothread(); |
0dc3f44a | 1014 | rcu_read_lock(); |
e4ed1541 | 1015 | migration_bitmap_sync(); |
0dc3f44a | 1016 | rcu_read_unlock(); |
32c835ba | 1017 | qemu_mutex_unlock_iothread(); |
e4ed1541 JQ |
1018 | remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE; |
1019 | } | |
1020 | return remaining_size; | |
1021 | } | |
1022 | ||
17ad9b35 OW |
1023 | static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) |
1024 | { | |
17ad9b35 OW |
1025 | unsigned int xh_len; |
1026 | int xh_flags; | |
1027 | ||
905f26f2 GA |
1028 | if (!xbzrle_decoded_buf) { |
1029 | xbzrle_decoded_buf = g_malloc(TARGET_PAGE_SIZE); | |
17ad9b35 OW |
1030 | } |
1031 | ||
1032 | /* extract RLE header */ | |
1033 | xh_flags = qemu_get_byte(f); | |
1034 | xh_len = qemu_get_be16(f); | |
1035 | ||
1036 | if (xh_flags != ENCODING_FLAG_XBZRLE) { | |
0971f1be | 1037 | error_report("Failed to load XBZRLE page - wrong compression!"); |
17ad9b35 OW |
1038 | return -1; |
1039 | } | |
1040 | ||
1041 | if (xh_len > TARGET_PAGE_SIZE) { | |
0971f1be | 1042 | error_report("Failed to load XBZRLE page - len overflow!"); |
17ad9b35 OW |
1043 | return -1; |
1044 | } | |
1045 | /* load data and decode */ | |
905f26f2 | 1046 | qemu_get_buffer(f, xbzrle_decoded_buf, xh_len); |
17ad9b35 OW |
1047 | |
1048 | /* decode RLE */ | |
fb626663 CG |
1049 | if (xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host, |
1050 | TARGET_PAGE_SIZE) == -1) { | |
0971f1be | 1051 | error_report("Failed to load XBZRLE page - decode error!"); |
fb626663 | 1052 | return -1; |
17ad9b35 OW |
1053 | } |
1054 | ||
fb626663 | 1055 | return 0; |
17ad9b35 OW |
1056 | } |
1057 | ||
0dc3f44a MD |
1058 | /* Must be called from within a rcu critical section. |
1059 | * Returns a pointer from within the RCU-protected ram_list. | |
1060 | */ | |
a55bbe31 AW |
1061 | static inline void *host_from_stream_offset(QEMUFile *f, |
1062 | ram_addr_t offset, | |
1063 | int flags) | |
1064 | { | |
1065 | static RAMBlock *block = NULL; | |
1066 | char id[256]; | |
1067 | uint8_t len; | |
1068 | ||
1069 | if (flags & RAM_SAVE_FLAG_CONTINUE) { | |
9b8424d5 | 1070 | if (!block || block->max_length <= offset) { |
0971f1be | 1071 | error_report("Ack, bad migration stream!"); |
a55bbe31 AW |
1072 | return NULL; |
1073 | } | |
1074 | ||
dc94a7ed | 1075 | return memory_region_get_ram_ptr(block->mr) + offset; |
a55bbe31 AW |
1076 | } |
1077 | ||
1078 | len = qemu_get_byte(f); | |
1079 | qemu_get_buffer(f, (uint8_t *)id, len); | |
1080 | id[len] = 0; | |
1081 | ||
0dc3f44a | 1082 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
9b8424d5 MT |
1083 | if (!strncmp(id, block->idstr, sizeof(id)) && |
1084 | block->max_length > offset) { | |
dc94a7ed | 1085 | return memory_region_get_ram_ptr(block->mr) + offset; |
0be839a2 | 1086 | } |
a55bbe31 AW |
1087 | } |
1088 | ||
0971f1be | 1089 | error_report("Can't find block %s!", id); |
a55bbe31 AW |
1090 | return NULL; |
1091 | } | |
1092 | ||
44c3b58c MH |
1093 | /* |
1094 | * If a page (or a whole RDMA chunk) has been | |
1095 | * determined to be zero, then zap it. | |
1096 | */ | |
1097 | void ram_handle_compressed(void *host, uint8_t ch, uint64_t size) | |
1098 | { | |
d613a56f | 1099 | if (ch != 0 || !is_zero_range(host, size)) { |
44c3b58c | 1100 | memset(host, ch, size); |
44c3b58c MH |
1101 | } |
1102 | } | |
1103 | ||
7908c78d | 1104 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
ad96090a | 1105 | { |
5b0e9dd4 | 1106 | int flags = 0, ret = 0; |
3a697f69 OW |
1107 | static uint64_t seq_iter; |
1108 | ||
1109 | seq_iter++; | |
ad96090a | 1110 | |
21a246a4 | 1111 | if (version_id != 4) { |
4798fe55 | 1112 | ret = -EINVAL; |
ad96090a BS |
1113 | } |
1114 | ||
0dc3f44a MD |
1115 | /* This RCU critical section can be very long running. |
1116 | * When RCU reclaims in the code start to become numerous, | |
1117 | * it will be necessary to reduce the granularity of this | |
1118 | * critical section. | |
1119 | */ | |
1120 | rcu_read_lock(); | |
5b0e9dd4 PL |
1121 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { |
1122 | ram_addr_t addr, total_ram_bytes; | |
1123 | void *host; | |
1124 | uint8_t ch; | |
ad96090a | 1125 | |
5b0e9dd4 | 1126 | addr = qemu_get_be64(f); |
ad96090a BS |
1127 | flags = addr & ~TARGET_PAGE_MASK; |
1128 | addr &= TARGET_PAGE_MASK; | |
1129 | ||
5b0e9dd4 PL |
1130 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { |
1131 | case RAM_SAVE_FLAG_MEM_SIZE: | |
21a246a4 | 1132 | /* Synchronize RAM block list */ |
5b0e9dd4 PL |
1133 | total_ram_bytes = addr; |
1134 | while (!ret && total_ram_bytes) { | |
21a246a4 C |
1135 | RAMBlock *block; |
1136 | uint8_t len; | |
5b0e9dd4 PL |
1137 | char id[256]; |
1138 | ram_addr_t length; | |
21a246a4 C |
1139 | |
1140 | len = qemu_get_byte(f); | |
1141 | qemu_get_buffer(f, (uint8_t *)id, len); | |
1142 | id[len] = 0; | |
1143 | length = qemu_get_be64(f); | |
1144 | ||
0dc3f44a | 1145 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
21a246a4 | 1146 | if (!strncmp(id, block->idstr, sizeof(id))) { |
b0cc3f83 MT |
1147 | if (length != block->used_length) { |
1148 | Error *local_err = NULL; | |
1149 | ||
1150 | ret = qemu_ram_resize(block->offset, length, &local_err); | |
1151 | if (local_err) { | |
565f65d2 | 1152 | error_report_err(local_err); |
b0cc3f83 | 1153 | } |
97ab12d4 | 1154 | } |
21a246a4 | 1155 | break; |
97ab12d4 | 1156 | } |
21a246a4 | 1157 | } |
97ab12d4 | 1158 | |
21a246a4 | 1159 | if (!block) { |
0971f1be LT |
1160 | error_report("Unknown ramblock \"%s\", cannot " |
1161 | "accept migration", id); | |
21a246a4 | 1162 | ret = -EINVAL; |
db80face | 1163 | } |
21a246a4 C |
1164 | |
1165 | total_ram_bytes -= length; | |
ad96090a | 1166 | } |
5b0e9dd4 PL |
1167 | break; |
1168 | case RAM_SAVE_FLAG_COMPRESS: | |
f09f2189 | 1169 | host = host_from_stream_offset(f, addr, flags); |
492fb99c | 1170 | if (!host) { |
db80face | 1171 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
4798fe55 | 1172 | ret = -EINVAL; |
db80face | 1173 | break; |
492fb99c | 1174 | } |
97ab12d4 | 1175 | ch = qemu_get_byte(f); |
44c3b58c | 1176 | ram_handle_compressed(host, ch, TARGET_PAGE_SIZE); |
5b0e9dd4 PL |
1177 | break; |
1178 | case RAM_SAVE_FLAG_PAGE: | |
f09f2189 | 1179 | host = host_from_stream_offset(f, addr, flags); |
0ff1f9f5 | 1180 | if (!host) { |
db80face | 1181 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
4798fe55 | 1182 | ret = -EINVAL; |
db80face | 1183 | break; |
0ff1f9f5 | 1184 | } |
97ab12d4 | 1185 | qemu_get_buffer(f, host, TARGET_PAGE_SIZE); |
5b0e9dd4 PL |
1186 | break; |
1187 | case RAM_SAVE_FLAG_XBZRLE: | |
1188 | host = host_from_stream_offset(f, addr, flags); | |
17ad9b35 | 1189 | if (!host) { |
db80face | 1190 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
4798fe55 | 1191 | ret = -EINVAL; |
db80face | 1192 | break; |
17ad9b35 | 1193 | } |
17ad9b35 | 1194 | if (load_xbzrle(f, addr, host) < 0) { |
db80face PL |
1195 | error_report("Failed to decompress XBZRLE page at " |
1196 | RAM_ADDR_FMT, addr); | |
17ad9b35 | 1197 | ret = -EINVAL; |
db80face | 1198 | break; |
17ad9b35 | 1199 | } |
db80face | 1200 | break; |
5b0e9dd4 PL |
1201 | case RAM_SAVE_FLAG_EOS: |
1202 | /* normal exit */ | |
db80face | 1203 | break; |
5b0e9dd4 PL |
1204 | default: |
1205 | if (flags & RAM_SAVE_FLAG_HOOK) { | |
1206 | ram_control_load_hook(f, flags); | |
1207 | } else { | |
1208 | error_report("Unknown combination of migration flags: %#x", | |
1209 | flags); | |
1210 | ret = -EINVAL; | |
1211 | } | |
1212 | } | |
1213 | if (!ret) { | |
1214 | ret = qemu_file_get_error(f); | |
ad96090a | 1215 | } |
db80face | 1216 | } |
ad96090a | 1217 | |
0dc3f44a | 1218 | rcu_read_unlock(); |
ef37a699 IM |
1219 | DPRINTF("Completed load of VM with exit code %d seq iteration " |
1220 | "%" PRIu64 "\n", ret, seq_iter); | |
3a697f69 | 1221 | return ret; |
ad96090a BS |
1222 | } |
1223 | ||
0d6ab3ab | 1224 | static SaveVMHandlers savevm_ram_handlers = { |
d1315aac | 1225 | .save_live_setup = ram_save_setup, |
16310a3c JQ |
1226 | .save_live_iterate = ram_save_iterate, |
1227 | .save_live_complete = ram_save_complete, | |
e4ed1541 | 1228 | .save_live_pending = ram_save_pending, |
7908c78d | 1229 | .load_state = ram_load, |
9b5bfab0 | 1230 | .cancel = ram_migration_cancel, |
7908c78d JQ |
1231 | }; |
1232 | ||
0d6ab3ab DDAG |
1233 | void ram_mig_init(void) |
1234 | { | |
d97326ee | 1235 | qemu_mutex_init(&XBZRLE.lock); |
0d6ab3ab DDAG |
1236 | register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL); |
1237 | } | |
1238 | ||
0dfa5ef9 IY |
1239 | struct soundhw { |
1240 | const char *name; | |
1241 | const char *descr; | |
1242 | int enabled; | |
1243 | int isa; | |
1244 | union { | |
4a0f031d | 1245 | int (*init_isa) (ISABus *bus); |
0dfa5ef9 IY |
1246 | int (*init_pci) (PCIBus *bus); |
1247 | } init; | |
1248 | }; | |
1249 | ||
36cd6f6f PB |
1250 | static struct soundhw soundhw[9]; |
1251 | static int soundhw_count; | |
ad96090a | 1252 | |
36cd6f6f PB |
1253 | void isa_register_soundhw(const char *name, const char *descr, |
1254 | int (*init_isa)(ISABus *bus)) | |
1255 | { | |
1256 | assert(soundhw_count < ARRAY_SIZE(soundhw) - 1); | |
1257 | soundhw[soundhw_count].name = name; | |
1258 | soundhw[soundhw_count].descr = descr; | |
1259 | soundhw[soundhw_count].isa = 1; | |
1260 | soundhw[soundhw_count].init.init_isa = init_isa; | |
1261 | soundhw_count++; | |
1262 | } | |
ad96090a | 1263 | |
36cd6f6f PB |
1264 | void pci_register_soundhw(const char *name, const char *descr, |
1265 | int (*init_pci)(PCIBus *bus)) | |
1266 | { | |
1267 | assert(soundhw_count < ARRAY_SIZE(soundhw) - 1); | |
1268 | soundhw[soundhw_count].name = name; | |
1269 | soundhw[soundhw_count].descr = descr; | |
1270 | soundhw[soundhw_count].isa = 0; | |
1271 | soundhw[soundhw_count].init.init_pci = init_pci; | |
1272 | soundhw_count++; | |
1273 | } | |
ad96090a BS |
1274 | |
1275 | void select_soundhw(const char *optarg) | |
1276 | { | |
1277 | struct soundhw *c; | |
1278 | ||
c8057f95 | 1279 | if (is_help_option(optarg)) { |
ad96090a BS |
1280 | show_valid_cards: |
1281 | ||
36cd6f6f PB |
1282 | if (soundhw_count) { |
1283 | printf("Valid sound card names (comma separated):\n"); | |
1284 | for (c = soundhw; c->name; ++c) { | |
1285 | printf ("%-11s %s\n", c->name, c->descr); | |
1286 | } | |
1287 | printf("\n-soundhw all will enable all of the above\n"); | |
1288 | } else { | |
1289 | printf("Machine has no user-selectable audio hardware " | |
1290 | "(it may or may not have always-present audio hardware).\n"); | |
ad96090a | 1291 | } |
c8057f95 | 1292 | exit(!is_help_option(optarg)); |
ad96090a BS |
1293 | } |
1294 | else { | |
1295 | size_t l; | |
1296 | const char *p; | |
1297 | char *e; | |
1298 | int bad_card = 0; | |
1299 | ||
1300 | if (!strcmp(optarg, "all")) { | |
1301 | for (c = soundhw; c->name; ++c) { | |
1302 | c->enabled = 1; | |
1303 | } | |
1304 | return; | |
1305 | } | |
1306 | ||
1307 | p = optarg; | |
1308 | while (*p) { | |
1309 | e = strchr(p, ','); | |
1310 | l = !e ? strlen(p) : (size_t) (e - p); | |
1311 | ||
1312 | for (c = soundhw; c->name; ++c) { | |
1313 | if (!strncmp(c->name, p, l) && !c->name[l]) { | |
1314 | c->enabled = 1; | |
1315 | break; | |
1316 | } | |
1317 | } | |
1318 | ||
1319 | if (!c->name) { | |
1320 | if (l > 80) { | |
0971f1be | 1321 | error_report("Unknown sound card name (too big to show)"); |
ad96090a BS |
1322 | } |
1323 | else { | |
0971f1be LT |
1324 | error_report("Unknown sound card name `%.*s'", |
1325 | (int) l, p); | |
ad96090a BS |
1326 | } |
1327 | bad_card = 1; | |
1328 | } | |
1329 | p += l + (e != NULL); | |
1330 | } | |
1331 | ||
1332 | if (bad_card) { | |
1333 | goto show_valid_cards; | |
1334 | } | |
1335 | } | |
1336 | } | |
0dfa5ef9 | 1337 | |
f81222bc | 1338 | void audio_init(void) |
0dfa5ef9 IY |
1339 | { |
1340 | struct soundhw *c; | |
f81222bc PB |
1341 | ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL); |
1342 | PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL); | |
0dfa5ef9 IY |
1343 | |
1344 | for (c = soundhw; c->name; ++c) { | |
1345 | if (c->enabled) { | |
1346 | if (c->isa) { | |
f81222bc | 1347 | if (!isa_bus) { |
0971f1be | 1348 | error_report("ISA bus not available for %s", c->name); |
f81222bc | 1349 | exit(1); |
0dfa5ef9 | 1350 | } |
f81222bc | 1351 | c->init.init_isa(isa_bus); |
0dfa5ef9 | 1352 | } else { |
f81222bc | 1353 | if (!pci_bus) { |
0971f1be | 1354 | error_report("PCI bus not available for %s", c->name); |
f81222bc | 1355 | exit(1); |
0dfa5ef9 | 1356 | } |
f81222bc | 1357 | c->init.init_pci(pci_bus); |
0dfa5ef9 IY |
1358 | } |
1359 | } | |
1360 | } | |
1361 | } | |
ad96090a BS |
1362 | |
1363 | int qemu_uuid_parse(const char *str, uint8_t *uuid) | |
1364 | { | |
1365 | int ret; | |
1366 | ||
1367 | if (strlen(str) != 36) { | |
1368 | return -1; | |
1369 | } | |
1370 | ||
1371 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], | |
1372 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], | |
1373 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], | |
1374 | &uuid[15]); | |
1375 | ||
1376 | if (ret != 16) { | |
1377 | return -1; | |
1378 | } | |
ad96090a BS |
1379 | return 0; |
1380 | } | |
1381 | ||
0c764a9d | 1382 | void do_acpitable_option(const QemuOpts *opts) |
ad96090a BS |
1383 | { |
1384 | #ifdef TARGET_I386 | |
23084327 LE |
1385 | Error *err = NULL; |
1386 | ||
1387 | acpi_table_add(opts, &err); | |
1388 | if (err) { | |
4a44d85e SA |
1389 | error_report("Wrong acpi table provided: %s", |
1390 | error_get_pretty(err)); | |
23084327 | 1391 | error_free(err); |
ad96090a BS |
1392 | exit(1); |
1393 | } | |
1394 | #endif | |
1395 | } | |
1396 | ||
4f953d2f | 1397 | void do_smbios_option(QemuOpts *opts) |
ad96090a BS |
1398 | { |
1399 | #ifdef TARGET_I386 | |
4f953d2f | 1400 | smbios_entry_add(opts); |
ad96090a BS |
1401 | #endif |
1402 | } | |
1403 | ||
1404 | void cpudef_init(void) | |
1405 | { | |
1406 | #if defined(cpudef_setup) | |
1407 | cpudef_setup(); /* parse cpu definitions in target config file */ | |
1408 | #endif | |
1409 | } | |
1410 | ||
ad96090a BS |
1411 | int kvm_available(void) |
1412 | { | |
1413 | #ifdef CONFIG_KVM | |
1414 | return 1; | |
1415 | #else | |
1416 | return 0; | |
1417 | #endif | |
1418 | } | |
1419 | ||
1420 | int xen_available(void) | |
1421 | { | |
1422 | #ifdef CONFIG_XEN | |
1423 | return 1; | |
1424 | #else | |
1425 | return 0; | |
1426 | #endif | |
1427 | } | |
99afc91d DB |
1428 | |
1429 | ||
1430 | TargetInfo *qmp_query_target(Error **errp) | |
1431 | { | |
1432 | TargetInfo *info = g_malloc0(sizeof(*info)); | |
1433 | ||
c02a9552 | 1434 | info->arch = g_strdup(TARGET_NAME); |
99afc91d DB |
1435 | |
1436 | return info; | |
1437 | } | |
7ca1dfad CV |
1438 | |
1439 | /* Stub function that's gets run on the vcpu when its brought out of the | |
1440 | VM to run inside qemu via async_run_on_cpu()*/ | |
1441 | static void mig_sleep_cpu(void *opq) | |
1442 | { | |
1443 | qemu_mutex_unlock_iothread(); | |
1444 | g_usleep(30*1000); | |
1445 | qemu_mutex_lock_iothread(); | |
1446 | } | |
1447 | ||
1448 | /* To reduce the dirty rate explicitly disallow the VCPUs from spending | |
1449 | much time in the VM. The migration thread will try to catchup. | |
1450 | Workload will experience a performance drop. | |
1451 | */ | |
7ca1dfad CV |
1452 | static void mig_throttle_guest_down(void) |
1453 | { | |
38fcbd3f AF |
1454 | CPUState *cpu; |
1455 | ||
7ca1dfad | 1456 | qemu_mutex_lock_iothread(); |
38fcbd3f AF |
1457 | CPU_FOREACH(cpu) { |
1458 | async_run_on_cpu(cpu, mig_sleep_cpu, NULL); | |
1459 | } | |
7ca1dfad CV |
1460 | qemu_mutex_unlock_iothread(); |
1461 | } | |
1462 | ||
1463 | static void check_guest_throttling(void) | |
1464 | { | |
1465 | static int64_t t0; | |
1466 | int64_t t1; | |
1467 | ||
1468 | if (!mig_throttle_on) { | |
1469 | return; | |
1470 | } | |
1471 | ||
1472 | if (!t0) { | |
bc72ad67 | 1473 | t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
7ca1dfad CV |
1474 | return; |
1475 | } | |
1476 | ||
bc72ad67 | 1477 | t1 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
7ca1dfad CV |
1478 | |
1479 | /* If it has been more than 40 ms since the last time the guest | |
1480 | * was throttled then do it again. | |
1481 | */ | |
1482 | if (40 < (t1-t0)/1000000) { | |
1483 | mig_throttle_guest_down(); | |
1484 | t0 = t1; | |
1485 | } | |
1486 | } |