Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
Juan Quintela | 76cc7b5 | 2015-05-08 13:20:21 +0200 | [diff] [blame] | 5 | * Copyright (c) 2011-2015 Red Hat Inc |
| 6 | * |
| 7 | * Authors: |
| 8 | * Juan Quintela <quintela@redhat.com> |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 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 | */ |
Peter Maydell | 1393a48 | 2016-01-26 18:16:54 +0000 | [diff] [blame] | 28 | #include "qemu/osdep.h" |
Paolo Bonzini | 33c1187 | 2016-03-15 16:58:45 +0100 | [diff] [blame] | 29 | #include "cpu.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 30 | #include <zlib.h> |
Dr. David Alan Gilbert | 4addcd4 | 2015-12-16 11:47:36 +0000 | [diff] [blame] | 31 | #include "qapi-event.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 32 | #include "qemu/cutils.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 33 | #include "qemu/bitops.h" |
| 34 | #include "qemu/bitmap.h" |
Juan Quintela | 7205c9e | 2015-05-08 13:54:36 +0200 | [diff] [blame] | 35 | #include "qemu/main-loop.h" |
Juan Quintela | 709e3fe | 2017-04-05 21:47:50 +0200 | [diff] [blame] | 36 | #include "xbzrle.h" |
Juan Quintela | 7b1e1a2 | 2017-04-17 20:26:27 +0200 | [diff] [blame] | 37 | #include "ram.h" |
Juan Quintela | 6666c96 | 2017-04-24 20:07:27 +0200 | [diff] [blame] | 38 | #include "migration.h" |
Juan Quintela | f2a8f0a | 2017-04-24 13:42:55 +0200 | [diff] [blame] | 39 | #include "migration/register.h" |
Juan Quintela | 7b1e1a2 | 2017-04-17 20:26:27 +0200 | [diff] [blame] | 40 | #include "migration/misc.h" |
Juan Quintela | 08a0aee | 2017-04-20 18:52:18 +0200 | [diff] [blame] | 41 | #include "qemu-file.h" |
Juan Quintela | be07b0a | 2017-04-20 13:12:24 +0200 | [diff] [blame] | 42 | #include "postcopy-ram.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 43 | #include "migration/page_cache.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 44 | #include "qemu/error-report.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 45 | #include "trace.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 46 | #include "exec/ram_addr.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 47 | #include "qemu/rcu_queue.h" |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 48 | #include "migration/colo.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 49 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 50 | /***********************************************************/ |
| 51 | /* ram save/restore */ |
| 52 | |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 53 | /* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it |
| 54 | * worked for pages that where filled with the same char. We switched |
| 55 | * it to only search for the zero value. And to avoid confusion with |
| 56 | * RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it. |
| 57 | */ |
| 58 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 59 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 60 | #define RAM_SAVE_FLAG_ZERO 0x02 |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 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 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 69 | static inline bool is_zero_range(uint8_t *p, uint64_t size) |
| 70 | { |
Richard Henderson | a1febc4 | 2016-08-29 11:46:14 -0700 | [diff] [blame] | 71 | return buffer_is_zero(p, size); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 74 | XBZRLECacheStats xbzrle_counters; |
| 75 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 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; |
Juan Quintela | c00e092 | 2017-05-09 16:22:01 +0200 | [diff] [blame] | 86 | /* it will store a page full of zeros */ |
| 87 | uint8_t *zero_target_page; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 88 | } XBZRLE; |
| 89 | |
| 90 | /* buffer used for XBZRLE decoding */ |
| 91 | static uint8_t *xbzrle_decoded_buf; |
| 92 | |
| 93 | static void XBZRLE_cache_lock(void) |
| 94 | { |
| 95 | if (migrate_use_xbzrle()) |
| 96 | qemu_mutex_lock(&XBZRLE.lock); |
| 97 | } |
| 98 | |
| 99 | static void XBZRLE_cache_unlock(void) |
| 100 | { |
| 101 | if (migrate_use_xbzrle()) |
| 102 | qemu_mutex_unlock(&XBZRLE.lock); |
| 103 | } |
| 104 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 105 | /** |
| 106 | * xbzrle_cache_resize: resize the xbzrle cache |
| 107 | * |
| 108 | * This function is called from qmp_migrate_set_cache_size in main |
| 109 | * thread, possibly while a migration is in progress. A running |
| 110 | * migration may be using the cache and might finish during this call, |
| 111 | * hence changes to the cache are protected by XBZRLE.lock(). |
| 112 | * |
| 113 | * Returns the new_size or negative in case of error. |
| 114 | * |
| 115 | * @new_size: new cache size |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 116 | */ |
| 117 | int64_t xbzrle_cache_resize(int64_t new_size) |
| 118 | { |
| 119 | PageCache *new_cache; |
| 120 | int64_t ret; |
| 121 | |
| 122 | if (new_size < TARGET_PAGE_SIZE) { |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | XBZRLE_cache_lock(); |
| 127 | |
| 128 | if (XBZRLE.cache != NULL) { |
| 129 | if (pow2floor(new_size) == migrate_xbzrle_cache_size()) { |
| 130 | goto out_new_size; |
| 131 | } |
| 132 | new_cache = cache_init(new_size / TARGET_PAGE_SIZE, |
| 133 | TARGET_PAGE_SIZE); |
| 134 | if (!new_cache) { |
| 135 | error_report("Error creating cache"); |
| 136 | ret = -1; |
| 137 | goto out; |
| 138 | } |
| 139 | |
| 140 | cache_fini(XBZRLE.cache); |
| 141 | XBZRLE.cache = new_cache; |
| 142 | } |
| 143 | |
| 144 | out_new_size: |
| 145 | ret = pow2floor(new_size); |
| 146 | out: |
| 147 | XBZRLE_cache_unlock(); |
| 148 | return ret; |
| 149 | } |
| 150 | |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 151 | /* |
| 152 | * An outstanding page request, on the source, having been received |
| 153 | * and queued |
| 154 | */ |
| 155 | struct RAMSrcPageRequest { |
| 156 | RAMBlock *rb; |
| 157 | hwaddr offset; |
| 158 | hwaddr len; |
| 159 | |
| 160 | QSIMPLEQ_ENTRY(RAMSrcPageRequest) next_req; |
| 161 | }; |
| 162 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 163 | /* State of RAM for migration */ |
| 164 | struct RAMState { |
Juan Quintela | 204b88b | 2017-03-15 09:16:57 +0100 | [diff] [blame] | 165 | /* QEMUFile used for this migration */ |
| 166 | QEMUFile *f; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 167 | /* Last block that we have visited searching for dirty pages */ |
| 168 | RAMBlock *last_seen_block; |
| 169 | /* Last block from where we have sent data */ |
| 170 | RAMBlock *last_sent_block; |
Juan Quintela | 269ace2 | 2017-03-21 15:23:31 +0100 | [diff] [blame] | 171 | /* Last dirty target page we have sent */ |
| 172 | ram_addr_t last_page; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 173 | /* last ram version we have seen */ |
| 174 | uint32_t last_version; |
| 175 | /* We are in the first round */ |
| 176 | bool ram_bulk_stage; |
Juan Quintela | 8d820d6 | 2017-03-13 19:35:50 +0100 | [diff] [blame] | 177 | /* How many times we have dirty too many pages */ |
| 178 | int dirty_rate_high_cnt; |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 179 | /* these variables are used for bitmap sync */ |
| 180 | /* last time we did a full bitmap_sync */ |
| 181 | int64_t time_last_bitmap_sync; |
Juan Quintela | eac7415 | 2017-03-28 14:59:01 +0200 | [diff] [blame] | 182 | /* bytes transferred at start_time */ |
Juan Quintela | c4bdf0c | 2017-03-28 14:59:54 +0200 | [diff] [blame] | 183 | uint64_t bytes_xfer_prev; |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 184 | /* number of dirty pages since start_time */ |
Juan Quintela | 68908ed | 2017-03-28 15:05:53 +0200 | [diff] [blame] | 185 | uint64_t num_dirty_pages_period; |
Juan Quintela | b5833fd | 2017-03-13 19:49:19 +0100 | [diff] [blame] | 186 | /* xbzrle misses since the beginning of the period */ |
| 187 | uint64_t xbzrle_cache_miss_prev; |
Juan Quintela | 36040d9 | 2017-03-13 19:51:13 +0100 | [diff] [blame] | 188 | /* number of iterations at the beginning of period */ |
| 189 | uint64_t iterations_prev; |
Juan Quintela | 23b28c3 | 2017-03-13 20:51:34 +0100 | [diff] [blame] | 190 | /* Iterations since start */ |
| 191 | uint64_t iterations; |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 192 | /* protects modification of the bitmap */ |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 193 | uint64_t migration_dirty_pages; |
| 194 | /* number of dirty bits in the bitmap */ |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 195 | QemuMutex bitmap_mutex; |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 196 | /* The RAMBlock used in the last src_page_requests */ |
| 197 | RAMBlock *last_req_rb; |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 198 | /* Queue of outstanding page requests from the destination */ |
| 199 | QemuMutex src_page_req_mutex; |
| 200 | QSIMPLEQ_HEAD(src_page_requests, RAMSrcPageRequest) src_page_requests; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 201 | }; |
| 202 | typedef struct RAMState RAMState; |
| 203 | |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 204 | static RAMState *ram_state; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 205 | |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 206 | uint64_t ram_bytes_remaining(void) |
| 207 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 208 | return ram_state->migration_dirty_pages * TARGET_PAGE_SIZE; |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 211 | MigrationStats ram_counters; |
Juan Quintela | 9650689 | 2017-03-14 18:41:03 +0100 | [diff] [blame] | 212 | |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 213 | /* used by the search for pages to send */ |
| 214 | struct PageSearchStatus { |
| 215 | /* Current block being searched */ |
| 216 | RAMBlock *block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 217 | /* Current page to search from */ |
| 218 | unsigned long page; |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 219 | /* Set once we wrap around */ |
| 220 | bool complete_round; |
| 221 | }; |
| 222 | typedef struct PageSearchStatus PageSearchStatus; |
| 223 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 224 | struct CompressParam { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 225 | bool done; |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 226 | bool quit; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 227 | QEMUFile *file; |
| 228 | QemuMutex mutex; |
| 229 | QemuCond cond; |
| 230 | RAMBlock *block; |
| 231 | ram_addr_t offset; |
| 232 | }; |
| 233 | typedef struct CompressParam CompressParam; |
| 234 | |
| 235 | struct DecompressParam { |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 236 | bool done; |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 237 | bool quit; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 238 | QemuMutex mutex; |
| 239 | QemuCond cond; |
| 240 | void *des; |
Peter Maydell | d341d9f | 2016-01-22 15:09:21 +0000 | [diff] [blame] | 241 | uint8_t *compbuf; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 242 | int len; |
| 243 | }; |
| 244 | typedef struct DecompressParam DecompressParam; |
| 245 | |
| 246 | static CompressParam *comp_param; |
| 247 | static QemuThread *compress_threads; |
| 248 | /* comp_done_cond is used to wake up the migration thread when |
| 249 | * one of the compression threads has finished the compression. |
| 250 | * comp_done_lock is used to co-work with comp_done_cond. |
| 251 | */ |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 252 | static QemuMutex comp_done_lock; |
| 253 | static QemuCond comp_done_cond; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 254 | /* The empty QEMUFileOps will be used by file in CompressParam */ |
| 255 | static const QEMUFileOps empty_ops = { }; |
| 256 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 257 | static DecompressParam *decomp_param; |
| 258 | static QemuThread *decompress_threads; |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 259 | static QemuMutex decomp_done_lock; |
| 260 | static QemuCond decomp_done_cond; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 261 | |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 262 | static int do_compress_ram_page(QEMUFile *f, RAMBlock *block, |
| 263 | ram_addr_t offset); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 264 | |
| 265 | static void *do_data_compress(void *opaque) |
| 266 | { |
| 267 | CompressParam *param = opaque; |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 268 | RAMBlock *block; |
| 269 | ram_addr_t offset; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 270 | |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 271 | qemu_mutex_lock(¶m->mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 272 | while (!param->quit) { |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 273 | if (param->block) { |
| 274 | block = param->block; |
| 275 | offset = param->offset; |
| 276 | param->block = NULL; |
| 277 | qemu_mutex_unlock(¶m->mutex); |
| 278 | |
| 279 | do_compress_ram_page(param->file, block, offset); |
| 280 | |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 281 | qemu_mutex_lock(&comp_done_lock); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 282 | param->done = true; |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 283 | qemu_cond_signal(&comp_done_cond); |
| 284 | qemu_mutex_unlock(&comp_done_lock); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 285 | |
| 286 | qemu_mutex_lock(¶m->mutex); |
| 287 | } else { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 288 | qemu_cond_wait(¶m->cond, ¶m->mutex); |
| 289 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 290 | } |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 291 | qemu_mutex_unlock(¶m->mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 292 | |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | static inline void terminate_compression_threads(void) |
| 297 | { |
| 298 | int idx, thread_count; |
| 299 | |
| 300 | thread_count = migrate_compress_threads(); |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 301 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 302 | for (idx = 0; idx < thread_count; idx++) { |
| 303 | qemu_mutex_lock(&comp_param[idx].mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 304 | comp_param[idx].quit = true; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 305 | qemu_cond_signal(&comp_param[idx].cond); |
| 306 | qemu_mutex_unlock(&comp_param[idx].mutex); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void migrate_compress_threads_join(void) |
| 311 | { |
| 312 | int i, thread_count; |
| 313 | |
| 314 | if (!migrate_use_compression()) { |
| 315 | return; |
| 316 | } |
| 317 | terminate_compression_threads(); |
| 318 | thread_count = migrate_compress_threads(); |
| 319 | for (i = 0; i < thread_count; i++) { |
| 320 | qemu_thread_join(compress_threads + i); |
| 321 | qemu_fclose(comp_param[i].file); |
| 322 | qemu_mutex_destroy(&comp_param[i].mutex); |
| 323 | qemu_cond_destroy(&comp_param[i].cond); |
| 324 | } |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 325 | qemu_mutex_destroy(&comp_done_lock); |
| 326 | qemu_cond_destroy(&comp_done_cond); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 327 | g_free(compress_threads); |
| 328 | g_free(comp_param); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 329 | compress_threads = NULL; |
| 330 | comp_param = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void migrate_compress_threads_create(void) |
| 334 | { |
| 335 | int i, thread_count; |
| 336 | |
| 337 | if (!migrate_use_compression()) { |
| 338 | return; |
| 339 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 340 | thread_count = migrate_compress_threads(); |
| 341 | compress_threads = g_new0(QemuThread, thread_count); |
| 342 | comp_param = g_new0(CompressParam, thread_count); |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 343 | qemu_cond_init(&comp_done_cond); |
| 344 | qemu_mutex_init(&comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 345 | for (i = 0; i < thread_count; i++) { |
Cao jin | e110aa9 | 2016-07-29 15:10:31 +0800 | [diff] [blame] | 346 | /* comp_param[i].file is just used as a dummy buffer to save data, |
| 347 | * set its ops to empty. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 348 | */ |
| 349 | comp_param[i].file = qemu_fopen_ops(NULL, &empty_ops); |
| 350 | comp_param[i].done = true; |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 351 | comp_param[i].quit = false; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 352 | qemu_mutex_init(&comp_param[i].mutex); |
| 353 | qemu_cond_init(&comp_param[i].cond); |
| 354 | qemu_thread_create(compress_threads + i, "compress", |
| 355 | do_data_compress, comp_param + i, |
| 356 | QEMU_THREAD_JOINABLE); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 361 | * save_page_header: write page header to wire |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 362 | * |
| 363 | * If this is the 1st block, it also writes the block identification |
| 364 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 365 | * Returns the number of bytes written |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 366 | * |
| 367 | * @f: QEMUFile where to send the data |
| 368 | * @block: block that contains the page we want to send |
| 369 | * @offset: offset inside the block for the page |
| 370 | * in the lower bits, it contains flags |
| 371 | */ |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 372 | static size_t save_page_header(RAMState *rs, QEMUFile *f, RAMBlock *block, |
| 373 | ram_addr_t offset) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 374 | { |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 375 | size_t size, len; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 376 | |
Juan Quintela | 2479569 | 2017-03-21 11:45:01 +0100 | [diff] [blame] | 377 | if (block == rs->last_sent_block) { |
| 378 | offset |= RAM_SAVE_FLAG_CONTINUE; |
| 379 | } |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 380 | qemu_put_be64(f, offset); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 381 | size = 8; |
| 382 | |
| 383 | if (!(offset & RAM_SAVE_FLAG_CONTINUE)) { |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 384 | len = strlen(block->idstr); |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 385 | qemu_put_byte(f, len); |
| 386 | qemu_put_buffer(f, (uint8_t *)block->idstr, len); |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 387 | size += 1 + len; |
Juan Quintela | 2479569 | 2017-03-21 11:45:01 +0100 | [diff] [blame] | 388 | rs->last_sent_block = block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 389 | } |
| 390 | return size; |
| 391 | } |
| 392 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 393 | /** |
| 394 | * mig_throttle_guest_down: throotle down the guest |
| 395 | * |
| 396 | * Reduce amount of guest cpu execution to hopefully slow down memory |
| 397 | * writes. If guest dirty memory rate is reduced below the rate at |
| 398 | * which we can transfer pages to the destination then we should be |
| 399 | * able to complete migration. Some workloads dirty memory way too |
| 400 | * fast and will not effectively converge, even with auto-converge. |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 401 | */ |
| 402 | static void mig_throttle_guest_down(void) |
| 403 | { |
| 404 | MigrationState *s = migrate_get_current(); |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 405 | uint64_t pct_initial = s->parameters.cpu_throttle_initial; |
| 406 | uint64_t pct_icrement = s->parameters.cpu_throttle_increment; |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 407 | |
| 408 | /* We have not started throttling yet. Let's start it. */ |
| 409 | if (!cpu_throttle_active()) { |
| 410 | cpu_throttle_set(pct_initial); |
| 411 | } else { |
| 412 | /* Throttling already on, just increase the rate */ |
| 413 | cpu_throttle_set(cpu_throttle_get_percentage() + pct_icrement); |
| 414 | } |
| 415 | } |
| 416 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 417 | /** |
| 418 | * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache |
| 419 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 420 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 421 | * @current_addr: address for the zero page |
| 422 | * |
| 423 | * Update the xbzrle cache to reflect a page that's been sent as all 0. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 424 | * The important thing is that a stale (not-yet-0'd) page be replaced |
| 425 | * by the new data. |
| 426 | * As a bonus, if the page wasn't in the cache it gets added so that |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 427 | * when a small write is made into the 0'd page it gets XBZRLE sent. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 428 | */ |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 429 | static void xbzrle_cache_zero_page(RAMState *rs, ram_addr_t current_addr) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 430 | { |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 431 | if (rs->ram_bulk_stage || !migrate_use_xbzrle()) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
| 435 | /* We don't care if this fails to allocate a new cache page |
| 436 | * as long as it updated an old one */ |
Juan Quintela | c00e092 | 2017-05-09 16:22:01 +0200 | [diff] [blame] | 437 | cache_insert(XBZRLE.cache, current_addr, XBZRLE.zero_target_page, |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 438 | ram_counters.dirty_sync_count); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | #define ENCODING_FLAG_XBZRLE 0x1 |
| 442 | |
| 443 | /** |
| 444 | * save_xbzrle_page: compress and send current page |
| 445 | * |
| 446 | * Returns: 1 means that we wrote the page |
| 447 | * 0 means that page is identical to the one already sent |
| 448 | * -1 means that xbzrle would be longer than normal |
| 449 | * |
Juan Quintela | 5a98773 | 2017-03-13 19:39:02 +0100 | [diff] [blame] | 450 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 451 | * @current_data: pointer to the address of the page contents |
| 452 | * @current_addr: addr of the page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 453 | * @block: block that contains the page we want to send |
| 454 | * @offset: offset inside the block for the page |
| 455 | * @last_stage: if we are at the completion stage |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 456 | */ |
Juan Quintela | 204b88b | 2017-03-15 09:16:57 +0100 | [diff] [blame] | 457 | static int save_xbzrle_page(RAMState *rs, uint8_t **current_data, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 458 | ram_addr_t current_addr, RAMBlock *block, |
Juan Quintela | 072c251 | 2017-03-14 10:27:31 +0100 | [diff] [blame] | 459 | ram_addr_t offset, bool last_stage) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 460 | { |
| 461 | int encoded_len = 0, bytes_xbzrle; |
| 462 | uint8_t *prev_cached_page; |
| 463 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 464 | if (!cache_is_cached(XBZRLE.cache, current_addr, |
| 465 | ram_counters.dirty_sync_count)) { |
| 466 | xbzrle_counters.cache_miss++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 467 | if (!last_stage) { |
| 468 | if (cache_insert(XBZRLE.cache, current_addr, *current_data, |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 469 | ram_counters.dirty_sync_count) == -1) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 470 | return -1; |
| 471 | } else { |
| 472 | /* update *current_data when the page has been |
| 473 | inserted into cache */ |
| 474 | *current_data = get_cached_data(XBZRLE.cache, current_addr); |
| 475 | } |
| 476 | } |
| 477 | return -1; |
| 478 | } |
| 479 | |
| 480 | prev_cached_page = get_cached_data(XBZRLE.cache, current_addr); |
| 481 | |
| 482 | /* save current buffer into memory */ |
| 483 | memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE); |
| 484 | |
| 485 | /* XBZRLE encoding (if there is no overflow) */ |
| 486 | encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf, |
| 487 | TARGET_PAGE_SIZE, XBZRLE.encoded_buf, |
| 488 | TARGET_PAGE_SIZE); |
| 489 | if (encoded_len == 0) { |
Juan Quintela | 55c4446 | 2017-01-23 22:32:05 +0100 | [diff] [blame] | 490 | trace_save_xbzrle_page_skipping(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 491 | return 0; |
| 492 | } else if (encoded_len == -1) { |
Juan Quintela | 55c4446 | 2017-01-23 22:32:05 +0100 | [diff] [blame] | 493 | trace_save_xbzrle_page_overflow(); |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 494 | xbzrle_counters.overflow++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 495 | /* update data in the cache */ |
| 496 | if (!last_stage) { |
| 497 | memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE); |
| 498 | *current_data = prev_cached_page; |
| 499 | } |
| 500 | return -1; |
| 501 | } |
| 502 | |
| 503 | /* we need to update the data in the cache, in order to get the same data */ |
| 504 | if (!last_stage) { |
| 505 | memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE); |
| 506 | } |
| 507 | |
| 508 | /* Send XBZRLE based compressed page */ |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 509 | bytes_xbzrle = save_page_header(rs, rs->f, block, |
Juan Quintela | 204b88b | 2017-03-15 09:16:57 +0100 | [diff] [blame] | 510 | offset | RAM_SAVE_FLAG_XBZRLE); |
| 511 | qemu_put_byte(rs->f, ENCODING_FLAG_XBZRLE); |
| 512 | qemu_put_be16(rs->f, encoded_len); |
| 513 | qemu_put_buffer(rs->f, XBZRLE.encoded_buf, encoded_len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 514 | bytes_xbzrle += encoded_len + 1 + 2; |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 515 | xbzrle_counters.pages++; |
| 516 | xbzrle_counters.bytes += bytes_xbzrle; |
| 517 | ram_counters.transferred += bytes_xbzrle; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 518 | |
| 519 | return 1; |
| 520 | } |
| 521 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 522 | /** |
| 523 | * migration_bitmap_find_dirty: find the next dirty page from start |
Dr. David Alan Gilbert | f3f491f | 2015-11-05 18:11:01 +0000 | [diff] [blame] | 524 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 525 | * Called with rcu_read_lock() to protect migration_bitmap |
| 526 | * |
| 527 | * Returns the byte offset within memory region of the start of a dirty page |
| 528 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 529 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 530 | * @rb: RAMBlock where to search for dirty pages |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 531 | * @start: page where we start the search |
Dr. David Alan Gilbert | f3f491f | 2015-11-05 18:11:01 +0000 | [diff] [blame] | 532 | */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 533 | static inline |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 534 | unsigned long migration_bitmap_find_dirty(RAMState *rs, RAMBlock *rb, |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 535 | unsigned long start) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 536 | { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 537 | unsigned long size = rb->used_length >> TARGET_PAGE_BITS; |
| 538 | unsigned long *bitmap = rb->bmap; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 539 | unsigned long next; |
| 540 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 541 | if (rs->ram_bulk_stage && start > 0) { |
| 542 | next = start + 1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 543 | } else { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 544 | next = find_next_bit(bitmap, size, start); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 545 | } |
| 546 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 547 | return next; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Juan Quintela | 06b1068 | 2017-03-21 15:18:05 +0100 | [diff] [blame] | 550 | static inline bool migration_bitmap_clear_dirty(RAMState *rs, |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 551 | RAMBlock *rb, |
| 552 | unsigned long page) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 553 | { |
| 554 | bool ret; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 555 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 556 | ret = test_and_clear_bit(page, rb->bmap); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 557 | |
| 558 | if (ret) { |
Juan Quintela | 0d8ec88 | 2017-03-13 21:21:41 +0100 | [diff] [blame] | 559 | rs->migration_dirty_pages--; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 560 | } |
| 561 | return ret; |
| 562 | } |
| 563 | |
Juan Quintela | 15440dd | 2017-03-21 09:35:04 +0100 | [diff] [blame] | 564 | static void migration_bitmap_sync_range(RAMState *rs, RAMBlock *rb, |
| 565 | ram_addr_t start, ram_addr_t length) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 566 | { |
Juan Quintela | 0d8ec88 | 2017-03-13 21:21:41 +0100 | [diff] [blame] | 567 | rs->migration_dirty_pages += |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 568 | cpu_physical_memory_sync_dirty_bitmap(rb, start, length, |
Juan Quintela | 0d8ec88 | 2017-03-13 21:21:41 +0100 | [diff] [blame] | 569 | &rs->num_dirty_pages_period); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 572 | /** |
| 573 | * ram_pagesize_summary: calculate all the pagesizes of a VM |
| 574 | * |
| 575 | * Returns a summary bitmap of the page sizes of all RAMBlocks |
| 576 | * |
| 577 | * For VMs with just normal pages this is equivalent to the host page |
| 578 | * size. If it's got some huge pages then it's the OR of all the |
| 579 | * different page sizes. |
Dr. David Alan Gilbert | e8ca1db | 2017-02-24 18:28:29 +0000 | [diff] [blame] | 580 | */ |
| 581 | uint64_t ram_pagesize_summary(void) |
| 582 | { |
| 583 | RAMBlock *block; |
| 584 | uint64_t summary = 0; |
| 585 | |
Peter Xu | 99e1558 | 2017-05-12 12:17:39 +0800 | [diff] [blame] | 586 | RAMBLOCK_FOREACH(block) { |
Dr. David Alan Gilbert | e8ca1db | 2017-02-24 18:28:29 +0000 | [diff] [blame] | 587 | summary |= block->page_size; |
| 588 | } |
| 589 | |
| 590 | return summary; |
| 591 | } |
| 592 | |
Juan Quintela | 8d820d6 | 2017-03-13 19:35:50 +0100 | [diff] [blame] | 593 | static void migration_bitmap_sync(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 594 | { |
| 595 | RAMBlock *block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 596 | int64_t end_time; |
Juan Quintela | c4bdf0c | 2017-03-28 14:59:54 +0200 | [diff] [blame] | 597 | uint64_t bytes_xfer_now; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 598 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 599 | ram_counters.dirty_sync_count++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 600 | |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 601 | if (!rs->time_last_bitmap_sync) { |
| 602 | rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | trace_migration_bitmap_sync_start(); |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 606 | memory_global_dirty_log_sync(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 607 | |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 608 | qemu_mutex_lock(&rs->bitmap_mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 609 | rcu_read_lock(); |
Peter Xu | 99e1558 | 2017-05-12 12:17:39 +0800 | [diff] [blame] | 610 | RAMBLOCK_FOREACH(block) { |
Juan Quintela | 15440dd | 2017-03-21 09:35:04 +0100 | [diff] [blame] | 611 | migration_bitmap_sync_range(rs, block, 0, block->used_length); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 612 | } |
| 613 | rcu_read_unlock(); |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 614 | qemu_mutex_unlock(&rs->bitmap_mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 615 | |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 616 | trace_migration_bitmap_sync_end(rs->num_dirty_pages_period); |
Chao Fan | 1ffb5df | 2017-03-14 09:55:07 +0800 | [diff] [blame] | 617 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 618 | end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 619 | |
| 620 | /* more than 1 second = 1000 millisecons */ |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 621 | if (end_time > rs->time_last_bitmap_sync + 1000) { |
Felipe Franciosi | d693c6f | 2017-05-24 17:10:01 +0100 | [diff] [blame] | 622 | /* calculate period counters */ |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 623 | ram_counters.dirty_pages_rate = rs->num_dirty_pages_period * 1000 |
Felipe Franciosi | d693c6f | 2017-05-24 17:10:01 +0100 | [diff] [blame] | 624 | / (end_time - rs->time_last_bitmap_sync); |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 625 | bytes_xfer_now = ram_counters.transferred; |
Felipe Franciosi | d693c6f | 2017-05-24 17:10:01 +0100 | [diff] [blame] | 626 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 627 | if (migrate_auto_converge()) { |
| 628 | /* The following detection logic can be refined later. For now: |
| 629 | Check to see if the dirtied bytes is 50% more than the approx. |
| 630 | amount of bytes that just got transferred since the last time we |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 631 | were in this routine. If that happens twice, start or increase |
| 632 | throttling */ |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 633 | |
Felipe Franciosi | d693c6f | 2017-05-24 17:10:01 +0100 | [diff] [blame] | 634 | if ((rs->num_dirty_pages_period * TARGET_PAGE_SIZE > |
Juan Quintela | eac7415 | 2017-03-28 14:59:01 +0200 | [diff] [blame] | 635 | (bytes_xfer_now - rs->bytes_xfer_prev) / 2) && |
Felipe Franciosi | b4a3c64 | 2017-05-24 17:10:03 +0100 | [diff] [blame] | 636 | (++rs->dirty_rate_high_cnt >= 2)) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 637 | trace_migration_throttle(); |
Juan Quintela | 8d820d6 | 2017-03-13 19:35:50 +0100 | [diff] [blame] | 638 | rs->dirty_rate_high_cnt = 0; |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 639 | mig_throttle_guest_down(); |
Felipe Franciosi | d693c6f | 2017-05-24 17:10:01 +0100 | [diff] [blame] | 640 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 641 | } |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 642 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 643 | if (migrate_use_xbzrle()) { |
Juan Quintela | 23b28c3 | 2017-03-13 20:51:34 +0100 | [diff] [blame] | 644 | if (rs->iterations_prev != rs->iterations) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 645 | xbzrle_counters.cache_miss_rate = |
| 646 | (double)(xbzrle_counters.cache_miss - |
Juan Quintela | b5833fd | 2017-03-13 19:49:19 +0100 | [diff] [blame] | 647 | rs->xbzrle_cache_miss_prev) / |
Juan Quintela | 23b28c3 | 2017-03-13 20:51:34 +0100 | [diff] [blame] | 648 | (rs->iterations - rs->iterations_prev); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 649 | } |
Juan Quintela | 23b28c3 | 2017-03-13 20:51:34 +0100 | [diff] [blame] | 650 | rs->iterations_prev = rs->iterations; |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 651 | rs->xbzrle_cache_miss_prev = xbzrle_counters.cache_miss; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 652 | } |
Felipe Franciosi | d693c6f | 2017-05-24 17:10:01 +0100 | [diff] [blame] | 653 | |
| 654 | /* reset period counters */ |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 655 | rs->time_last_bitmap_sync = end_time; |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 656 | rs->num_dirty_pages_period = 0; |
Felipe Franciosi | d2a4d85 | 2017-05-24 17:10:02 +0100 | [diff] [blame] | 657 | rs->bytes_xfer_prev = bytes_xfer_now; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 658 | } |
Dr. David Alan Gilbert | 4addcd4 | 2015-12-16 11:47:36 +0000 | [diff] [blame] | 659 | if (migrate_use_events()) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 660 | qapi_event_send_migration_pass(ram_counters.dirty_sync_count, NULL); |
Dr. David Alan Gilbert | 4addcd4 | 2015-12-16 11:47:36 +0000 | [diff] [blame] | 661 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 665 | * save_zero_page: send the zero page to the stream |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 666 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 667 | * Returns the number of pages written. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 668 | * |
Juan Quintela | f7ccd61 | 2017-03-13 20:30:21 +0100 | [diff] [blame] | 669 | * @rs: current RAM state |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 670 | * @block: block that contains the page we want to send |
| 671 | * @offset: offset inside the block for the page |
| 672 | * @p: pointer to the page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 673 | */ |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 674 | static int save_zero_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, |
| 675 | uint8_t *p) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 676 | { |
| 677 | int pages = -1; |
| 678 | |
| 679 | if (is_zero_range(p, TARGET_PAGE_SIZE)) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 680 | ram_counters.duplicate++; |
| 681 | ram_counters.transferred += |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 682 | save_page_header(rs, rs->f, block, offset | RAM_SAVE_FLAG_ZERO); |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 683 | qemu_put_byte(rs->f, 0); |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 684 | ram_counters.transferred += 1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 685 | pages = 1; |
| 686 | } |
| 687 | |
| 688 | return pages; |
| 689 | } |
| 690 | |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 691 | static void ram_release_pages(const char *rbname, uint64_t offset, int pages) |
Pavel Butsykin | 53f09a1 | 2017-02-03 18:23:20 +0300 | [diff] [blame] | 692 | { |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 693 | if (!migrate_release_ram() || !migration_in_postcopy()) { |
Pavel Butsykin | 53f09a1 | 2017-02-03 18:23:20 +0300 | [diff] [blame] | 694 | return; |
| 695 | } |
| 696 | |
Juan Quintela | aaa2064 | 2017-03-21 11:35:24 +0100 | [diff] [blame] | 697 | ram_discard_range(rbname, offset, pages << TARGET_PAGE_BITS); |
Pavel Butsykin | 53f09a1 | 2017-02-03 18:23:20 +0300 | [diff] [blame] | 698 | } |
| 699 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 700 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 701 | * ram_save_page: send the given page to the stream |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 702 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 703 | * Returns the number of pages written. |
Dr. David Alan Gilbert | 3fd3c4b | 2015-12-10 16:31:46 +0000 | [diff] [blame] | 704 | * < 0 - error |
| 705 | * >=0 - Number of pages written - this might legally be 0 |
| 706 | * if xbzrle noticed the page was the same. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 707 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 708 | * @rs: current RAM state |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 709 | * @block: block that contains the page we want to send |
| 710 | * @offset: offset inside the block for the page |
| 711 | * @last_stage: if we are at the completion stage |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 712 | */ |
Juan Quintela | a0a8aa1 | 2017-03-20 22:29:07 +0100 | [diff] [blame] | 713 | static int ram_save_page(RAMState *rs, PageSearchStatus *pss, bool last_stage) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 714 | { |
| 715 | int pages = -1; |
| 716 | uint64_t bytes_xmit; |
| 717 | ram_addr_t current_addr; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 718 | uint8_t *p; |
| 719 | int ret; |
| 720 | bool send_async = true; |
zhanghailiang | a08f689 | 2016-01-15 11:37:44 +0800 | [diff] [blame] | 721 | RAMBlock *block = pss->block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 722 | ram_addr_t offset = pss->page << TARGET_PAGE_BITS; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 723 | |
Dr. David Alan Gilbert | 2f68e39 | 2015-08-13 11:51:30 +0100 | [diff] [blame] | 724 | p = block->host + offset; |
Dr. David Alan Gilbert | 1db9d8e | 2017-04-26 19:37:21 +0100 | [diff] [blame] | 725 | trace_ram_save_page(block->idstr, (uint64_t)offset, p); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 726 | |
| 727 | /* In doubt sent page as normal */ |
| 728 | bytes_xmit = 0; |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 729 | ret = ram_control_save_page(rs->f, block->offset, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 730 | offset, TARGET_PAGE_SIZE, &bytes_xmit); |
| 731 | if (bytes_xmit) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 732 | ram_counters.transferred += bytes_xmit; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 733 | pages = 1; |
| 734 | } |
| 735 | |
| 736 | XBZRLE_cache_lock(); |
| 737 | |
| 738 | current_addr = block->offset + offset; |
| 739 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 740 | if (ret != RAM_SAVE_CONTROL_NOT_SUPP) { |
| 741 | if (ret != RAM_SAVE_CONTROL_DELAYED) { |
| 742 | if (bytes_xmit > 0) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 743 | ram_counters.normal++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 744 | } else if (bytes_xmit == 0) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 745 | ram_counters.duplicate++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | } else { |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 749 | pages = save_zero_page(rs, block, offset, p); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 750 | if (pages > 0) { |
| 751 | /* Must let xbzrle know, otherwise a previous (now 0'd) cached |
| 752 | * page would be stale |
| 753 | */ |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 754 | xbzrle_cache_zero_page(rs, current_addr); |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 755 | ram_release_pages(block->idstr, offset, pages); |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 756 | } else if (!rs->ram_bulk_stage && |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 757 | !migration_in_postcopy() && migrate_use_xbzrle()) { |
Juan Quintela | 204b88b | 2017-03-15 09:16:57 +0100 | [diff] [blame] | 758 | pages = save_xbzrle_page(rs, &p, current_addr, block, |
Juan Quintela | 072c251 | 2017-03-14 10:27:31 +0100 | [diff] [blame] | 759 | offset, last_stage); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 760 | if (!last_stage) { |
| 761 | /* Can't send this cached data async, since the cache page |
| 762 | * might get updated before it gets to the wire |
| 763 | */ |
| 764 | send_async = false; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /* XBZRLE overflow or normal page */ |
| 770 | if (pages == -1) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 771 | ram_counters.transferred += |
| 772 | save_page_header(rs, rs->f, block, offset | RAM_SAVE_FLAG_PAGE); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 773 | if (send_async) { |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 774 | qemu_put_buffer_async(rs->f, p, TARGET_PAGE_SIZE, |
Pavel Butsykin | 53f09a1 | 2017-02-03 18:23:20 +0300 | [diff] [blame] | 775 | migrate_release_ram() & |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 776 | migration_in_postcopy()); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 777 | } else { |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 778 | qemu_put_buffer(rs->f, p, TARGET_PAGE_SIZE); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 779 | } |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 780 | ram_counters.transferred += TARGET_PAGE_SIZE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 781 | pages = 1; |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 782 | ram_counters.normal++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | XBZRLE_cache_unlock(); |
| 786 | |
| 787 | return pages; |
| 788 | } |
| 789 | |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 790 | static int do_compress_ram_page(QEMUFile *f, RAMBlock *block, |
| 791 | ram_addr_t offset) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 792 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 793 | RAMState *rs = ram_state; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 794 | int bytes_sent, blen; |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 795 | uint8_t *p = block->host + (offset & TARGET_PAGE_MASK); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 796 | |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 797 | bytes_sent = save_page_header(rs, f, block, offset | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 798 | RAM_SAVE_FLAG_COMPRESS_PAGE); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 799 | blen = qemu_put_compression_data(f, p, TARGET_PAGE_SIZE, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 800 | migrate_compress_level()); |
Liang Li | b3be289 | 2016-05-05 15:32:54 +0800 | [diff] [blame] | 801 | if (blen < 0) { |
| 802 | bytes_sent = 0; |
| 803 | qemu_file_set_error(migrate_get_current()->to_dst_file, blen); |
| 804 | error_report("compressed data failed!"); |
| 805 | } else { |
| 806 | bytes_sent += blen; |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 807 | ram_release_pages(block->idstr, offset & TARGET_PAGE_MASK, 1); |
Liang Li | b3be289 | 2016-05-05 15:32:54 +0800 | [diff] [blame] | 808 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 809 | |
| 810 | return bytes_sent; |
| 811 | } |
| 812 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 813 | static void flush_compressed_data(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 814 | { |
| 815 | int idx, len, thread_count; |
| 816 | |
| 817 | if (!migrate_use_compression()) { |
| 818 | return; |
| 819 | } |
| 820 | thread_count = migrate_compress_threads(); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 821 | |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 822 | qemu_mutex_lock(&comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 823 | for (idx = 0; idx < thread_count; idx++) { |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 824 | while (!comp_param[idx].done) { |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 825 | qemu_cond_wait(&comp_done_cond, &comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 826 | } |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 827 | } |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 828 | qemu_mutex_unlock(&comp_done_lock); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 829 | |
| 830 | for (idx = 0; idx < thread_count; idx++) { |
| 831 | qemu_mutex_lock(&comp_param[idx].mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 832 | if (!comp_param[idx].quit) { |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 833 | len = qemu_put_qemu_file(rs->f, comp_param[idx].file); |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 834 | ram_counters.transferred += len; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 835 | } |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 836 | qemu_mutex_unlock(&comp_param[idx].mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
| 840 | static inline void set_compress_params(CompressParam *param, RAMBlock *block, |
| 841 | ram_addr_t offset) |
| 842 | { |
| 843 | param->block = block; |
| 844 | param->offset = offset; |
| 845 | } |
| 846 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 847 | static int compress_page_with_multi_thread(RAMState *rs, RAMBlock *block, |
| 848 | ram_addr_t offset) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 849 | { |
| 850 | int idx, thread_count, bytes_xmit = -1, pages = -1; |
| 851 | |
| 852 | thread_count = migrate_compress_threads(); |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 853 | qemu_mutex_lock(&comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 854 | while (true) { |
| 855 | for (idx = 0; idx < thread_count; idx++) { |
| 856 | if (comp_param[idx].done) { |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 857 | comp_param[idx].done = false; |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 858 | bytes_xmit = qemu_put_qemu_file(rs->f, comp_param[idx].file); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 859 | qemu_mutex_lock(&comp_param[idx].mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 860 | set_compress_params(&comp_param[idx], block, offset); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 861 | qemu_cond_signal(&comp_param[idx].cond); |
| 862 | qemu_mutex_unlock(&comp_param[idx].mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 863 | pages = 1; |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 864 | ram_counters.normal++; |
| 865 | ram_counters.transferred += bytes_xmit; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 866 | break; |
| 867 | } |
| 868 | } |
| 869 | if (pages > 0) { |
| 870 | break; |
| 871 | } else { |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 872 | qemu_cond_wait(&comp_done_cond, &comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 873 | } |
| 874 | } |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 875 | qemu_mutex_unlock(&comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 876 | |
| 877 | return pages; |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * ram_save_compressed_page: compress the given page and send it to the stream |
| 882 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 883 | * Returns the number of pages written. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 884 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 885 | * @rs: current RAM state |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 886 | * @block: block that contains the page we want to send |
| 887 | * @offset: offset inside the block for the page |
| 888 | * @last_stage: if we are at the completion stage |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 889 | */ |
Juan Quintela | a0a8aa1 | 2017-03-20 22:29:07 +0100 | [diff] [blame] | 890 | static int ram_save_compressed_page(RAMState *rs, PageSearchStatus *pss, |
| 891 | bool last_stage) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 892 | { |
| 893 | int pages = -1; |
Liang Li | fc50438 | 2016-05-05 15:32:55 +0800 | [diff] [blame] | 894 | uint64_t bytes_xmit = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 895 | uint8_t *p; |
Liang Li | fc50438 | 2016-05-05 15:32:55 +0800 | [diff] [blame] | 896 | int ret, blen; |
zhanghailiang | a08f689 | 2016-01-15 11:37:44 +0800 | [diff] [blame] | 897 | RAMBlock *block = pss->block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 898 | ram_addr_t offset = pss->page << TARGET_PAGE_BITS; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 899 | |
Dr. David Alan Gilbert | 2f68e39 | 2015-08-13 11:51:30 +0100 | [diff] [blame] | 900 | p = block->host + offset; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 901 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 902 | ret = ram_control_save_page(rs->f, block->offset, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 903 | offset, TARGET_PAGE_SIZE, &bytes_xmit); |
| 904 | if (bytes_xmit) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 905 | ram_counters.transferred += bytes_xmit; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 906 | pages = 1; |
| 907 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 908 | if (ret != RAM_SAVE_CONTROL_NOT_SUPP) { |
| 909 | if (ret != RAM_SAVE_CONTROL_DELAYED) { |
| 910 | if (bytes_xmit > 0) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 911 | ram_counters.normal++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 912 | } else if (bytes_xmit == 0) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 913 | ram_counters.duplicate++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | } else { |
| 917 | /* When starting the process of a new block, the first page of |
| 918 | * the block should be sent out before other pages in the same |
| 919 | * block, and all the pages in last block should have been sent |
| 920 | * out, keeping this order is important, because the 'cont' flag |
| 921 | * is used to avoid resending the block name. |
| 922 | */ |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 923 | if (block != rs->last_sent_block) { |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 924 | flush_compressed_data(rs); |
| 925 | pages = save_zero_page(rs, block, offset, p); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 926 | if (pages == -1) { |
Liang Li | fc50438 | 2016-05-05 15:32:55 +0800 | [diff] [blame] | 927 | /* Make sure the first page is sent out before other pages */ |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 928 | bytes_xmit = save_page_header(rs, rs->f, block, offset | |
Liang Li | fc50438 | 2016-05-05 15:32:55 +0800 | [diff] [blame] | 929 | RAM_SAVE_FLAG_COMPRESS_PAGE); |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 930 | blen = qemu_put_compression_data(rs->f, p, TARGET_PAGE_SIZE, |
Liang Li | fc50438 | 2016-05-05 15:32:55 +0800 | [diff] [blame] | 931 | migrate_compress_level()); |
| 932 | if (blen > 0) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 933 | ram_counters.transferred += bytes_xmit + blen; |
| 934 | ram_counters.normal++; |
Liang Li | b3be289 | 2016-05-05 15:32:54 +0800 | [diff] [blame] | 935 | pages = 1; |
Liang Li | fc50438 | 2016-05-05 15:32:55 +0800 | [diff] [blame] | 936 | } else { |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 937 | qemu_file_set_error(rs->f, blen); |
Liang Li | fc50438 | 2016-05-05 15:32:55 +0800 | [diff] [blame] | 938 | error_report("compressed data failed!"); |
Liang Li | b3be289 | 2016-05-05 15:32:54 +0800 | [diff] [blame] | 939 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 940 | } |
Pavel Butsykin | 53f09a1 | 2017-02-03 18:23:20 +0300 | [diff] [blame] | 941 | if (pages > 0) { |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 942 | ram_release_pages(block->idstr, offset, pages); |
Pavel Butsykin | 53f09a1 | 2017-02-03 18:23:20 +0300 | [diff] [blame] | 943 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 944 | } else { |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 945 | pages = save_zero_page(rs, block, offset, p); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 946 | if (pages == -1) { |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 947 | pages = compress_page_with_multi_thread(rs, block, offset); |
Pavel Butsykin | 53f09a1 | 2017-02-03 18:23:20 +0300 | [diff] [blame] | 948 | } else { |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 949 | ram_release_pages(block->idstr, offset, pages); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | return pages; |
| 955 | } |
| 956 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 957 | /** |
| 958 | * find_dirty_block: find the next dirty page and update any state |
| 959 | * associated with the search process. |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 960 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 961 | * Returns if a page is found |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 962 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 963 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 964 | * @pss: data about the state of the current dirty page scan |
| 965 | * @again: set to false if the search has scanned the whole of RAM |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 966 | */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 967 | static bool find_dirty_block(RAMState *rs, PageSearchStatus *pss, bool *again) |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 968 | { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 969 | pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page); |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 970 | if (pss->complete_round && pss->block == rs->last_seen_block && |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 971 | pss->page >= rs->last_page) { |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 972 | /* |
| 973 | * We've been once around the RAM and haven't found anything. |
| 974 | * Give up. |
| 975 | */ |
| 976 | *again = false; |
| 977 | return false; |
| 978 | } |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 979 | if ((pss->page << TARGET_PAGE_BITS) >= pss->block->used_length) { |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 980 | /* Didn't find anything in this RAM Block */ |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 981 | pss->page = 0; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 982 | pss->block = QLIST_NEXT_RCU(pss->block, next); |
| 983 | if (!pss->block) { |
| 984 | /* Hit the end of the list */ |
| 985 | pss->block = QLIST_FIRST_RCU(&ram_list.blocks); |
| 986 | /* Flag that we've looped */ |
| 987 | pss->complete_round = true; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 988 | rs->ram_bulk_stage = false; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 989 | if (migrate_use_xbzrle()) { |
| 990 | /* If xbzrle is on, stop using the data compression at this |
| 991 | * point. In theory, xbzrle can do better than compression. |
| 992 | */ |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 993 | flush_compressed_data(rs); |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | /* Didn't find anything this time, but try again on the new block */ |
| 997 | *again = true; |
| 998 | return false; |
| 999 | } else { |
| 1000 | /* Can go around again, but... */ |
| 1001 | *again = true; |
| 1002 | /* We've found something so probably don't need to */ |
| 1003 | return true; |
| 1004 | } |
| 1005 | } |
| 1006 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1007 | /** |
| 1008 | * unqueue_page: gets a page of the queue |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1009 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1010 | * Helper for 'get_queued_page' - gets a page off the queue |
| 1011 | * |
| 1012 | * Returns the block of the page (or NULL if none available) |
| 1013 | * |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1014 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1015 | * @offset: used to return the offset within the RAMBlock |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1016 | */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1017 | static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1018 | { |
| 1019 | RAMBlock *block = NULL; |
| 1020 | |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1021 | qemu_mutex_lock(&rs->src_page_req_mutex); |
| 1022 | if (!QSIMPLEQ_EMPTY(&rs->src_page_requests)) { |
| 1023 | struct RAMSrcPageRequest *entry = |
| 1024 | QSIMPLEQ_FIRST(&rs->src_page_requests); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1025 | block = entry->rb; |
| 1026 | *offset = entry->offset; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1027 | |
| 1028 | if (entry->len > TARGET_PAGE_SIZE) { |
| 1029 | entry->len -= TARGET_PAGE_SIZE; |
| 1030 | entry->offset += TARGET_PAGE_SIZE; |
| 1031 | } else { |
| 1032 | memory_region_unref(block->mr); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1033 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1034 | g_free(entry); |
| 1035 | } |
| 1036 | } |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1037 | qemu_mutex_unlock(&rs->src_page_req_mutex); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1038 | |
| 1039 | return block; |
| 1040 | } |
| 1041 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1042 | /** |
| 1043 | * get_queued_page: unqueue a page from the postocpy requests |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1044 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1045 | * Skips pages that are already sent (!dirty) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1046 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1047 | * Returns if a queued page is found |
| 1048 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1049 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1050 | * @pss: data about the state of the current dirty page scan |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1051 | */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1052 | static bool get_queued_page(RAMState *rs, PageSearchStatus *pss) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1053 | { |
| 1054 | RAMBlock *block; |
| 1055 | ram_addr_t offset; |
| 1056 | bool dirty; |
| 1057 | |
| 1058 | do { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1059 | block = unqueue_page(rs, &offset); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1060 | /* |
| 1061 | * We're sending this page, and since it's postcopy nothing else |
| 1062 | * will dirty it, and we must make sure it doesn't get sent again |
| 1063 | * even if this queue request was received after the background |
| 1064 | * search already sent it. |
| 1065 | */ |
| 1066 | if (block) { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1067 | unsigned long page; |
| 1068 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1069 | page = offset >> TARGET_PAGE_BITS; |
| 1070 | dirty = test_bit(page, block->bmap); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1071 | if (!dirty) { |
Juan Quintela | 06b1068 | 2017-03-21 15:18:05 +0100 | [diff] [blame] | 1072 | trace_get_queued_page_not_dirty(block->idstr, (uint64_t)offset, |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1073 | page, test_bit(page, block->unsentmap)); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1074 | } else { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1075 | trace_get_queued_page(block->idstr, (uint64_t)offset, page); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | } while (block && !dirty); |
| 1080 | |
| 1081 | if (block) { |
| 1082 | /* |
| 1083 | * As soon as we start servicing pages out of order, then we have |
| 1084 | * to kill the bulk stage, since the bulk stage assumes |
| 1085 | * in (migration_bitmap_find_and_reset_dirty) that every page is |
| 1086 | * dirty, that's no longer true. |
| 1087 | */ |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1088 | rs->ram_bulk_stage = false; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1089 | |
| 1090 | /* |
| 1091 | * We want the background search to continue from the queued page |
| 1092 | * since the guest is likely to want other pages near to the page |
| 1093 | * it just requested. |
| 1094 | */ |
| 1095 | pss->block = block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1096 | pss->page = offset >> TARGET_PAGE_BITS; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | return !!block; |
| 1100 | } |
| 1101 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1102 | /** |
Juan Quintela | 5e58f96 | 2017-04-03 22:06:54 +0200 | [diff] [blame] | 1103 | * migration_page_queue_free: drop any remaining pages in the ram |
| 1104 | * request queue |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1105 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1106 | * It should be empty at the end anyway, but in error cases there may |
| 1107 | * be some left. in case that there is any page left, we drop it. |
| 1108 | * |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1109 | */ |
Juan Quintela | 83c1338 | 2017-05-04 11:45:01 +0200 | [diff] [blame] | 1110 | static void migration_page_queue_free(RAMState *rs) |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1111 | { |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1112 | struct RAMSrcPageRequest *mspr, *next_mspr; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1113 | /* This queue generally should be empty - but in the case of a failed |
| 1114 | * migration might have some droppings in. |
| 1115 | */ |
| 1116 | rcu_read_lock(); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1117 | QSIMPLEQ_FOREACH_SAFE(mspr, &rs->src_page_requests, next_req, next_mspr) { |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1118 | memory_region_unref(mspr->rb->mr); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1119 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1120 | g_free(mspr); |
| 1121 | } |
| 1122 | rcu_read_unlock(); |
| 1123 | } |
| 1124 | |
| 1125 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1126 | * ram_save_queue_pages: queue the page for transmission |
| 1127 | * |
| 1128 | * A request from postcopy destination for example. |
| 1129 | * |
| 1130 | * Returns zero on success or negative on error |
| 1131 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1132 | * @rbname: Name of the RAMBLock of the request. NULL means the |
| 1133 | * same that last one. |
| 1134 | * @start: starting address from the start of the RAMBlock |
| 1135 | * @len: length (in bytes) to send |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1136 | */ |
Juan Quintela | 9650689 | 2017-03-14 18:41:03 +0100 | [diff] [blame] | 1137 | int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t len) |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1138 | { |
| 1139 | RAMBlock *ramblock; |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1140 | RAMState *rs = ram_state; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1141 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 1142 | ram_counters.postcopy_requests++; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1143 | rcu_read_lock(); |
| 1144 | if (!rbname) { |
| 1145 | /* Reuse last RAMBlock */ |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 1146 | ramblock = rs->last_req_rb; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1147 | |
| 1148 | if (!ramblock) { |
| 1149 | /* |
| 1150 | * Shouldn't happen, we can't reuse the last RAMBlock if |
| 1151 | * it's the 1st request. |
| 1152 | */ |
| 1153 | error_report("ram_save_queue_pages no previous block"); |
| 1154 | goto err; |
| 1155 | } |
| 1156 | } else { |
| 1157 | ramblock = qemu_ram_block_by_name(rbname); |
| 1158 | |
| 1159 | if (!ramblock) { |
| 1160 | /* We shouldn't be asked for a non-existent RAMBlock */ |
| 1161 | error_report("ram_save_queue_pages no block '%s'", rbname); |
| 1162 | goto err; |
| 1163 | } |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 1164 | rs->last_req_rb = ramblock; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1165 | } |
| 1166 | trace_ram_save_queue_pages(ramblock->idstr, start, len); |
| 1167 | if (start+len > ramblock->used_length) { |
Juan Quintela | 9458ad6 | 2015-11-10 17:42:05 +0100 | [diff] [blame] | 1168 | error_report("%s request overrun start=" RAM_ADDR_FMT " len=" |
| 1169 | RAM_ADDR_FMT " blocklen=" RAM_ADDR_FMT, |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1170 | __func__, start, len, ramblock->used_length); |
| 1171 | goto err; |
| 1172 | } |
| 1173 | |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1174 | struct RAMSrcPageRequest *new_entry = |
| 1175 | g_malloc0(sizeof(struct RAMSrcPageRequest)); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1176 | new_entry->rb = ramblock; |
| 1177 | new_entry->offset = start; |
| 1178 | new_entry->len = len; |
| 1179 | |
| 1180 | memory_region_ref(ramblock->mr); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1181 | qemu_mutex_lock(&rs->src_page_req_mutex); |
| 1182 | QSIMPLEQ_INSERT_TAIL(&rs->src_page_requests, new_entry, next_req); |
| 1183 | qemu_mutex_unlock(&rs->src_page_req_mutex); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1184 | rcu_read_unlock(); |
| 1185 | |
| 1186 | return 0; |
| 1187 | |
| 1188 | err: |
| 1189 | rcu_read_unlock(); |
| 1190 | return -1; |
| 1191 | } |
| 1192 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1193 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1194 | * ram_save_target_page: save one target page |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1195 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1196 | * Returns the number of pages written |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1197 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1198 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1199 | * @ms: current migration state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1200 | * @pss: data about the page we want to send |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1201 | * @last_stage: if we are at the completion stage |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1202 | */ |
Juan Quintela | a0a8aa1 | 2017-03-20 22:29:07 +0100 | [diff] [blame] | 1203 | static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss, |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1204 | bool last_stage) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1205 | { |
| 1206 | int res = 0; |
| 1207 | |
| 1208 | /* Check the pages is dirty and if it is send it */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1209 | if (migration_bitmap_clear_dirty(rs, pss->block, pss->page)) { |
Juan Quintela | 6d358d9 | 2017-03-16 21:29:34 +0100 | [diff] [blame] | 1210 | /* |
| 1211 | * If xbzrle is on, stop using the data compression after first |
| 1212 | * round of migration even if compression is enabled. In theory, |
| 1213 | * xbzrle can do better than compression. |
| 1214 | */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1215 | if (migrate_use_compression() && |
| 1216 | (rs->ram_bulk_stage || !migrate_use_xbzrle())) { |
Juan Quintela | a0a8aa1 | 2017-03-20 22:29:07 +0100 | [diff] [blame] | 1217 | res = ram_save_compressed_page(rs, pss, last_stage); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1218 | } else { |
Juan Quintela | a0a8aa1 | 2017-03-20 22:29:07 +0100 | [diff] [blame] | 1219 | res = ram_save_page(rs, pss, last_stage); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | if (res < 0) { |
| 1223 | return res; |
| 1224 | } |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1225 | if (pss->block->unsentmap) { |
| 1226 | clear_bit(pss->page, pss->block->unsentmap); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | return res; |
| 1231 | } |
| 1232 | |
| 1233 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1234 | * ram_save_host_page: save a whole host page |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1235 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1236 | * Starting at *offset send pages up to the end of the current host |
| 1237 | * page. It's valid for the initial offset to point into the middle of |
| 1238 | * a host page in which case the remainder of the hostpage is sent. |
| 1239 | * Only dirty target pages are sent. Note that the host page size may |
| 1240 | * be a huge page for this block. |
Dr. David Alan Gilbert | 1eb3fc0 | 2017-05-17 17:58:09 +0100 | [diff] [blame] | 1241 | * The saving stops at the boundary of the used_length of the block |
| 1242 | * if the RAMBlock isn't a multiple of the host page size. |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1243 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1244 | * Returns the number of pages written or negative on error |
| 1245 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1246 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1247 | * @ms: current migration state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1248 | * @pss: data about the page we want to send |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1249 | * @last_stage: if we are at the completion stage |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1250 | */ |
Juan Quintela | a0a8aa1 | 2017-03-20 22:29:07 +0100 | [diff] [blame] | 1251 | static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss, |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1252 | bool last_stage) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1253 | { |
| 1254 | int tmppages, pages = 0; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1255 | size_t pagesize_bits = |
| 1256 | qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS; |
Dr. David Alan Gilbert | 4c011c3 | 2017-02-24 18:28:39 +0000 | [diff] [blame] | 1257 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1258 | do { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1259 | tmppages = ram_save_target_page(rs, pss, last_stage); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1260 | if (tmppages < 0) { |
| 1261 | return tmppages; |
| 1262 | } |
| 1263 | |
| 1264 | pages += tmppages; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1265 | pss->page++; |
Dr. David Alan Gilbert | 1eb3fc0 | 2017-05-17 17:58:09 +0100 | [diff] [blame] | 1266 | } while ((pss->page & (pagesize_bits - 1)) && |
| 1267 | offset_in_ramblock(pss->block, pss->page << TARGET_PAGE_BITS)); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1268 | |
| 1269 | /* The offset we leave with is the last one we looked at */ |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1270 | pss->page--; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1271 | return pages; |
| 1272 | } |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1273 | |
| 1274 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1275 | * ram_find_and_save_block: finds a dirty page and sends it to f |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1276 | * |
| 1277 | * Called within an RCU critical section. |
| 1278 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1279 | * Returns the number of pages written where zero means no dirty pages |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1280 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1281 | * @rs: current RAM state |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1282 | * @last_stage: if we are at the completion stage |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1283 | * |
| 1284 | * On systems where host-page-size > target-page-size it will send all the |
| 1285 | * pages in a host page that are dirty. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1286 | */ |
| 1287 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 1288 | static int ram_find_and_save_block(RAMState *rs, bool last_stage) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1289 | { |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 1290 | PageSearchStatus pss; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1291 | int pages = 0; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1292 | bool again, found; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1293 | |
Ashijeet Acharya | 0827b9e | 2017-02-08 19:58:45 +0530 | [diff] [blame] | 1294 | /* No dirty page as there is zero RAM */ |
| 1295 | if (!ram_bytes_total()) { |
| 1296 | return pages; |
| 1297 | } |
| 1298 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1299 | pss.block = rs->last_seen_block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1300 | pss.page = rs->last_page; |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 1301 | pss.complete_round = false; |
| 1302 | |
| 1303 | if (!pss.block) { |
| 1304 | pss.block = QLIST_FIRST_RCU(&ram_list.blocks); |
| 1305 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1306 | |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1307 | do { |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1308 | again = true; |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1309 | found = get_queued_page(rs, &pss); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1310 | |
| 1311 | if (!found) { |
| 1312 | /* priority queue empty, so just search for something dirty */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1313 | found = find_dirty_block(rs, &pss, &again); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1314 | } |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1315 | |
| 1316 | if (found) { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1317 | pages = ram_save_host_page(rs, &pss, last_stage); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1318 | } |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1319 | } while (!pages && again); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1320 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1321 | rs->last_seen_block = pss.block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1322 | rs->last_page = pss.page; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1323 | |
| 1324 | return pages; |
| 1325 | } |
| 1326 | |
| 1327 | void acct_update_position(QEMUFile *f, size_t size, bool zero) |
| 1328 | { |
| 1329 | uint64_t pages = size / TARGET_PAGE_SIZE; |
Juan Quintela | f7ccd61 | 2017-03-13 20:30:21 +0100 | [diff] [blame] | 1330 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1331 | if (zero) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 1332 | ram_counters.duplicate += pages; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1333 | } else { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 1334 | ram_counters.normal += pages; |
| 1335 | ram_counters.transferred += size; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1336 | qemu_update_position(f, size); |
| 1337 | } |
| 1338 | } |
| 1339 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1340 | uint64_t ram_bytes_total(void) |
| 1341 | { |
| 1342 | RAMBlock *block; |
| 1343 | uint64_t total = 0; |
| 1344 | |
| 1345 | rcu_read_lock(); |
Peter Xu | 99e1558 | 2017-05-12 12:17:39 +0800 | [diff] [blame] | 1346 | RAMBLOCK_FOREACH(block) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1347 | total += block->used_length; |
Peter Xu | 99e1558 | 2017-05-12 12:17:39 +0800 | [diff] [blame] | 1348 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1349 | rcu_read_unlock(); |
| 1350 | return total; |
| 1351 | } |
| 1352 | |
| 1353 | void free_xbzrle_decoded_buf(void) |
| 1354 | { |
| 1355 | g_free(xbzrle_decoded_buf); |
| 1356 | xbzrle_decoded_buf = NULL; |
| 1357 | } |
| 1358 | |
Liang Li | 6ad2a21 | 2015-11-02 15:37:03 +0800 | [diff] [blame] | 1359 | static void ram_migration_cleanup(void *opaque) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1360 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1361 | RAMState **rsp = opaque; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1362 | RAMBlock *block; |
Juan Quintela | eb859c5 | 2017-03-13 21:51:55 +0100 | [diff] [blame] | 1363 | |
Li Zhijian | 2ff6403 | 2015-07-02 20:18:05 +0800 | [diff] [blame] | 1364 | /* caller have hold iothread lock or is in a bh, so there is |
| 1365 | * no writing race against this migration_bitmap |
| 1366 | */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1367 | memory_global_dirty_log_stop(); |
| 1368 | |
| 1369 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
| 1370 | g_free(block->bmap); |
| 1371 | block->bmap = NULL; |
| 1372 | g_free(block->unsentmap); |
| 1373 | block->unsentmap = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | XBZRLE_cache_lock(); |
| 1377 | if (XBZRLE.cache) { |
| 1378 | cache_fini(XBZRLE.cache); |
| 1379 | g_free(XBZRLE.encoded_buf); |
| 1380 | g_free(XBZRLE.current_buf); |
Juan Quintela | c00e092 | 2017-05-09 16:22:01 +0200 | [diff] [blame] | 1381 | g_free(XBZRLE.zero_target_page); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1382 | XBZRLE.cache = NULL; |
| 1383 | XBZRLE.encoded_buf = NULL; |
| 1384 | XBZRLE.current_buf = NULL; |
Juan Quintela | c00e092 | 2017-05-09 16:22:01 +0200 | [diff] [blame] | 1385 | XBZRLE.zero_target_page = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1386 | } |
| 1387 | XBZRLE_cache_unlock(); |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1388 | migration_page_queue_free(*rsp); |
| 1389 | g_free(*rsp); |
| 1390 | *rsp = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1391 | } |
| 1392 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1393 | static void ram_state_reset(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1394 | { |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1395 | rs->last_seen_block = NULL; |
| 1396 | rs->last_sent_block = NULL; |
Juan Quintela | 269ace2 | 2017-03-21 15:23:31 +0100 | [diff] [blame] | 1397 | rs->last_page = 0; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1398 | rs->last_version = ram_list.version; |
| 1399 | rs->ram_bulk_stage = true; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | #define MAX_WAIT 50 /* ms, half buffered_file limit */ |
| 1403 | |
Dr. David Alan Gilbert | 4f2e425 | 2015-11-05 18:10:38 +0000 | [diff] [blame] | 1404 | /* |
| 1405 | * 'expected' is the value you expect the bitmap mostly to be full |
| 1406 | * of; it won't bother printing lines that are all this value. |
| 1407 | * If 'todump' is null the migration bitmap is dumped. |
| 1408 | */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1409 | void ram_debug_dump_bitmap(unsigned long *todump, bool expected, |
| 1410 | unsigned long pages) |
Dr. David Alan Gilbert | 4f2e425 | 2015-11-05 18:10:38 +0000 | [diff] [blame] | 1411 | { |
Dr. David Alan Gilbert | 4f2e425 | 2015-11-05 18:10:38 +0000 | [diff] [blame] | 1412 | int64_t cur; |
| 1413 | int64_t linelen = 128; |
| 1414 | char linebuf[129]; |
| 1415 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1416 | for (cur = 0; cur < pages; cur += linelen) { |
Dr. David Alan Gilbert | 4f2e425 | 2015-11-05 18:10:38 +0000 | [diff] [blame] | 1417 | int64_t curb; |
| 1418 | bool found = false; |
| 1419 | /* |
| 1420 | * Last line; catch the case where the line length |
| 1421 | * is longer than remaining ram |
| 1422 | */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1423 | if (cur + linelen > pages) { |
| 1424 | linelen = pages - cur; |
Dr. David Alan Gilbert | 4f2e425 | 2015-11-05 18:10:38 +0000 | [diff] [blame] | 1425 | } |
| 1426 | for (curb = 0; curb < linelen; curb++) { |
| 1427 | bool thisbit = test_bit(cur + curb, todump); |
| 1428 | linebuf[curb] = thisbit ? '1' : '.'; |
| 1429 | found = found || (thisbit != expected); |
| 1430 | } |
| 1431 | if (found) { |
| 1432 | linebuf[curb] = '\0'; |
| 1433 | fprintf(stderr, "0x%08" PRIx64 " : %s\n", cur, linebuf); |
| 1434 | } |
| 1435 | } |
| 1436 | } |
| 1437 | |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1438 | /* **** functions for postcopy ***** */ |
| 1439 | |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 1440 | void ram_postcopy_migrated_memory_release(MigrationState *ms) |
| 1441 | { |
| 1442 | struct RAMBlock *block; |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 1443 | |
Peter Xu | 99e1558 | 2017-05-12 12:17:39 +0800 | [diff] [blame] | 1444 | RAMBLOCK_FOREACH(block) { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1445 | unsigned long *bitmap = block->bmap; |
| 1446 | unsigned long range = block->used_length >> TARGET_PAGE_BITS; |
| 1447 | unsigned long run_start = find_next_zero_bit(bitmap, range, 0); |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 1448 | |
| 1449 | while (run_start < range) { |
| 1450 | unsigned long run_end = find_next_bit(bitmap, range, run_start + 1); |
Juan Quintela | aaa2064 | 2017-03-21 11:35:24 +0100 | [diff] [blame] | 1451 | ram_discard_range(block->idstr, run_start << TARGET_PAGE_BITS, |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 1452 | (run_end - run_start) << TARGET_PAGE_BITS); |
| 1453 | run_start = find_next_zero_bit(bitmap, range, run_end + 1); |
| 1454 | } |
| 1455 | } |
| 1456 | } |
| 1457 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1458 | /** |
| 1459 | * postcopy_send_discard_bm_ram: discard a RAMBlock |
| 1460 | * |
| 1461 | * Returns zero on success |
| 1462 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1463 | * Callback from postcopy_each_ram_send_discard for each RAMBlock |
| 1464 | * Note: At this point the 'unsentmap' is the processed bitmap combined |
| 1465 | * with the dirtymap; so a '1' means it's either dirty or unsent. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1466 | * |
| 1467 | * @ms: current migration state |
| 1468 | * @pds: state for postcopy |
| 1469 | * @start: RAMBlock starting page |
| 1470 | * @length: RAMBlock size |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1471 | */ |
| 1472 | static int postcopy_send_discard_bm_ram(MigrationState *ms, |
| 1473 | PostcopyDiscardState *pds, |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1474 | RAMBlock *block) |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1475 | { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1476 | unsigned long end = block->used_length >> TARGET_PAGE_BITS; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1477 | unsigned long current; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1478 | unsigned long *unsentmap = block->unsentmap; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1479 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1480 | for (current = 0; current < end; ) { |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1481 | unsigned long one = find_next_bit(unsentmap, end, current); |
| 1482 | |
| 1483 | if (one <= end) { |
| 1484 | unsigned long zero = find_next_zero_bit(unsentmap, end, one + 1); |
| 1485 | unsigned long discard_length; |
| 1486 | |
| 1487 | if (zero >= end) { |
| 1488 | discard_length = end - one; |
| 1489 | } else { |
| 1490 | discard_length = zero - one; |
| 1491 | } |
Dr. David Alan Gilbert | d688c62 | 2016-06-13 12:16:40 +0100 | [diff] [blame] | 1492 | if (discard_length) { |
| 1493 | postcopy_discard_send_range(ms, pds, one, discard_length); |
| 1494 | } |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1495 | current = one + discard_length; |
| 1496 | } else { |
| 1497 | current = one; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | return 0; |
| 1502 | } |
| 1503 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1504 | /** |
| 1505 | * postcopy_each_ram_send_discard: discard all RAMBlocks |
| 1506 | * |
| 1507 | * Returns 0 for success or negative for error |
| 1508 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1509 | * Utility for the outgoing postcopy code. |
| 1510 | * Calls postcopy_send_discard_bm_ram for each RAMBlock |
| 1511 | * passing it bitmap indexes and name. |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1512 | * (qemu_ram_foreach_block ends up passing unscaled lengths |
| 1513 | * which would mean postcopy code would have to deal with target page) |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1514 | * |
| 1515 | * @ms: current migration state |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1516 | */ |
| 1517 | static int postcopy_each_ram_send_discard(MigrationState *ms) |
| 1518 | { |
| 1519 | struct RAMBlock *block; |
| 1520 | int ret; |
| 1521 | |
Peter Xu | 99e1558 | 2017-05-12 12:17:39 +0800 | [diff] [blame] | 1522 | RAMBLOCK_FOREACH(block) { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1523 | PostcopyDiscardState *pds = |
| 1524 | postcopy_discard_send_init(ms, block->idstr); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1525 | |
| 1526 | /* |
| 1527 | * Postcopy sends chunks of bitmap over the wire, but it |
| 1528 | * just needs indexes at this point, avoids it having |
| 1529 | * target page specific code. |
| 1530 | */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1531 | ret = postcopy_send_discard_bm_ram(ms, pds, block); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1532 | postcopy_discard_send_finish(ms, pds); |
| 1533 | if (ret) { |
| 1534 | return ret; |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1541 | /** |
| 1542 | * postcopy_chunk_hostpages_pass: canocalize bitmap in hostpages |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1543 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1544 | * Helper for postcopy_chunk_hostpages; it's called twice to |
| 1545 | * canonicalize the two bitmaps, that are similar, but one is |
| 1546 | * inverted. |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1547 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1548 | * Postcopy requires that all target pages in a hostpage are dirty or |
| 1549 | * clean, not a mix. This function canonicalizes the bitmaps. |
| 1550 | * |
| 1551 | * @ms: current migration state |
| 1552 | * @unsent_pass: if true we need to canonicalize partially unsent host pages |
| 1553 | * otherwise we need to canonicalize partially dirty host pages |
| 1554 | * @block: block that contains the page we want to canonicalize |
| 1555 | * @pds: state for postcopy |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1556 | */ |
| 1557 | static void postcopy_chunk_hostpages_pass(MigrationState *ms, bool unsent_pass, |
| 1558 | RAMBlock *block, |
| 1559 | PostcopyDiscardState *pds) |
| 1560 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1561 | RAMState *rs = ram_state; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1562 | unsigned long *bitmap = block->bmap; |
| 1563 | unsigned long *unsentmap = block->unsentmap; |
Dr. David Alan Gilbert | 29c5917 | 2017-02-24 18:28:31 +0000 | [diff] [blame] | 1564 | unsigned int host_ratio = block->page_size / TARGET_PAGE_SIZE; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1565 | unsigned long pages = block->used_length >> TARGET_PAGE_BITS; |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1566 | unsigned long run_start; |
| 1567 | |
Dr. David Alan Gilbert | 29c5917 | 2017-02-24 18:28:31 +0000 | [diff] [blame] | 1568 | if (block->page_size == TARGET_PAGE_SIZE) { |
| 1569 | /* Easy case - TPS==HPS for a non-huge page RAMBlock */ |
| 1570 | return; |
| 1571 | } |
| 1572 | |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1573 | if (unsent_pass) { |
| 1574 | /* Find a sent page */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1575 | run_start = find_next_zero_bit(unsentmap, pages, 0); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1576 | } else { |
| 1577 | /* Find a dirty page */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1578 | run_start = find_next_bit(bitmap, pages, 0); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1581 | while (run_start < pages) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1582 | bool do_fixup = false; |
| 1583 | unsigned long fixup_start_addr; |
| 1584 | unsigned long host_offset; |
| 1585 | |
| 1586 | /* |
| 1587 | * If the start of this run of pages is in the middle of a host |
| 1588 | * page, then we need to fixup this host page. |
| 1589 | */ |
| 1590 | host_offset = run_start % host_ratio; |
| 1591 | if (host_offset) { |
| 1592 | do_fixup = true; |
| 1593 | run_start -= host_offset; |
| 1594 | fixup_start_addr = run_start; |
| 1595 | /* For the next pass */ |
| 1596 | run_start = run_start + host_ratio; |
| 1597 | } else { |
| 1598 | /* Find the end of this run */ |
| 1599 | unsigned long run_end; |
| 1600 | if (unsent_pass) { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1601 | run_end = find_next_bit(unsentmap, pages, run_start + 1); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1602 | } else { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1603 | run_end = find_next_zero_bit(bitmap, pages, run_start + 1); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1604 | } |
| 1605 | /* |
| 1606 | * If the end isn't at the start of a host page, then the |
| 1607 | * run doesn't finish at the end of a host page |
| 1608 | * and we need to discard. |
| 1609 | */ |
| 1610 | host_offset = run_end % host_ratio; |
| 1611 | if (host_offset) { |
| 1612 | do_fixup = true; |
| 1613 | fixup_start_addr = run_end - host_offset; |
| 1614 | /* |
| 1615 | * This host page has gone, the next loop iteration starts |
| 1616 | * from after the fixup |
| 1617 | */ |
| 1618 | run_start = fixup_start_addr + host_ratio; |
| 1619 | } else { |
| 1620 | /* |
| 1621 | * No discards on this iteration, next loop starts from |
| 1622 | * next sent/dirty page |
| 1623 | */ |
| 1624 | run_start = run_end + 1; |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | if (do_fixup) { |
| 1629 | unsigned long page; |
| 1630 | |
| 1631 | /* Tell the destination to discard this page */ |
| 1632 | if (unsent_pass || !test_bit(fixup_start_addr, unsentmap)) { |
| 1633 | /* For the unsent_pass we: |
| 1634 | * discard partially sent pages |
| 1635 | * For the !unsent_pass (dirty) we: |
| 1636 | * discard partially dirty pages that were sent |
| 1637 | * (any partially sent pages were already discarded |
| 1638 | * by the previous unsent_pass) |
| 1639 | */ |
| 1640 | postcopy_discard_send_range(ms, pds, fixup_start_addr, |
| 1641 | host_ratio); |
| 1642 | } |
| 1643 | |
| 1644 | /* Clean up the bitmap */ |
| 1645 | for (page = fixup_start_addr; |
| 1646 | page < fixup_start_addr + host_ratio; page++) { |
| 1647 | /* All pages in this host page are now not sent */ |
| 1648 | set_bit(page, unsentmap); |
| 1649 | |
| 1650 | /* |
| 1651 | * Remark them as dirty, updating the count for any pages |
| 1652 | * that weren't previously dirty. |
| 1653 | */ |
Juan Quintela | 0d8ec88 | 2017-03-13 21:21:41 +0100 | [diff] [blame] | 1654 | rs->migration_dirty_pages += !test_and_set_bit(page, bitmap); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | if (unsent_pass) { |
| 1659 | /* Find the next sent page for the next iteration */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1660 | run_start = find_next_zero_bit(unsentmap, pages, run_start); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1661 | } else { |
| 1662 | /* Find the next dirty page for the next iteration */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1663 | run_start = find_next_bit(bitmap, pages, run_start); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1668 | /** |
| 1669 | * postcopy_chuck_hostpages: discrad any partially sent host page |
| 1670 | * |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1671 | * Utility for the outgoing postcopy code. |
| 1672 | * |
| 1673 | * Discard any partially sent host-page size chunks, mark any partially |
Dr. David Alan Gilbert | 29c5917 | 2017-02-24 18:28:31 +0000 | [diff] [blame] | 1674 | * dirty host-page size chunks as all dirty. In this case the host-page |
| 1675 | * is the host-page for the particular RAMBlock, i.e. it might be a huge page |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1676 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1677 | * Returns zero on success |
| 1678 | * |
| 1679 | * @ms: current migration state |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1680 | * @block: block we want to work with |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1681 | */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1682 | static int postcopy_chunk_hostpages(MigrationState *ms, RAMBlock *block) |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1683 | { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1684 | PostcopyDiscardState *pds = |
| 1685 | postcopy_discard_send_init(ms, block->idstr); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1686 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1687 | /* First pass: Discard all partially sent host pages */ |
| 1688 | postcopy_chunk_hostpages_pass(ms, true, block, pds); |
| 1689 | /* |
| 1690 | * Second pass: Ensure that all partially dirty host pages are made |
| 1691 | * fully dirty. |
| 1692 | */ |
| 1693 | postcopy_chunk_hostpages_pass(ms, false, block, pds); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1694 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1695 | postcopy_discard_send_finish(ms, pds); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 1696 | return 0; |
| 1697 | } |
| 1698 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1699 | /** |
| 1700 | * ram_postcopy_send_discard_bitmap: transmit the discard bitmap |
| 1701 | * |
| 1702 | * Returns zero on success |
| 1703 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1704 | * Transmit the set of pages to be discarded after precopy to the target |
| 1705 | * these are pages that: |
| 1706 | * a) Have been previously transmitted but are now dirty again |
| 1707 | * b) Pages that have never been transmitted, this ensures that |
| 1708 | * any pages on the destination that have been mapped by background |
| 1709 | * tasks get discarded (transparent huge pages is the specific concern) |
| 1710 | * Hopefully this is pretty sparse |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1711 | * |
| 1712 | * @ms: current migration state |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1713 | */ |
| 1714 | int ram_postcopy_send_discard_bitmap(MigrationState *ms) |
| 1715 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1716 | RAMState *rs = ram_state; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1717 | RAMBlock *block; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1718 | int ret; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1719 | |
| 1720 | rcu_read_lock(); |
| 1721 | |
| 1722 | /* This should be our last sync, the src is now paused */ |
Juan Quintela | eb859c5 | 2017-03-13 21:51:55 +0100 | [diff] [blame] | 1723 | migration_bitmap_sync(rs); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1724 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1725 | /* Easiest way to make sure we don't resume in the middle of a host-page */ |
| 1726 | rs->last_seen_block = NULL; |
| 1727 | rs->last_sent_block = NULL; |
| 1728 | rs->last_page = 0; |
| 1729 | |
| 1730 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
| 1731 | unsigned long pages = block->used_length >> TARGET_PAGE_BITS; |
| 1732 | unsigned long *bitmap = block->bmap; |
| 1733 | unsigned long *unsentmap = block->unsentmap; |
| 1734 | |
| 1735 | if (!unsentmap) { |
| 1736 | /* We don't have a safe way to resize the sentmap, so |
| 1737 | * if the bitmap was resized it will be NULL at this |
| 1738 | * point. |
| 1739 | */ |
| 1740 | error_report("migration ram resized during precopy phase"); |
| 1741 | rcu_read_unlock(); |
| 1742 | return -EINVAL; |
| 1743 | } |
| 1744 | /* Deal with TPS != HPS and huge pages */ |
| 1745 | ret = postcopy_chunk_hostpages(ms, block); |
| 1746 | if (ret) { |
| 1747 | rcu_read_unlock(); |
| 1748 | return ret; |
| 1749 | } |
| 1750 | |
| 1751 | /* |
| 1752 | * Update the unsentmap to be unsentmap = unsentmap | dirty |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1753 | */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1754 | bitmap_or(unsentmap, unsentmap, bitmap, pages); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1755 | #ifdef DEBUG_POSTCOPY |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1756 | ram_debug_dump_bitmap(unsentmap, true, pages); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1757 | #endif |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1758 | } |
| 1759 | trace_ram_postcopy_send_discard_bitmap(); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1760 | |
| 1761 | ret = postcopy_each_ram_send_discard(ms); |
| 1762 | rcu_read_unlock(); |
| 1763 | |
| 1764 | return ret; |
| 1765 | } |
| 1766 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1767 | /** |
| 1768 | * ram_discard_range: discard dirtied pages at the beginning of postcopy |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1769 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1770 | * Returns zero on success |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1771 | * |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 1772 | * @rbname: name of the RAMBlock of the request. NULL means the |
| 1773 | * same that last one. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1774 | * @start: RAMBlock starting page |
| 1775 | * @length: RAMBlock size |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1776 | */ |
Juan Quintela | aaa2064 | 2017-03-21 11:35:24 +0100 | [diff] [blame] | 1777 | int ram_discard_range(const char *rbname, uint64_t start, size_t length) |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1778 | { |
| 1779 | int ret = -1; |
| 1780 | |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 1781 | trace_ram_discard_range(rbname, start, length); |
Dr. David Alan Gilbert | d3a5038 | 2017-02-24 18:28:32 +0000 | [diff] [blame] | 1782 | |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1783 | rcu_read_lock(); |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 1784 | RAMBlock *rb = qemu_ram_block_by_name(rbname); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1785 | |
| 1786 | if (!rb) { |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 1787 | error_report("ram_discard_range: Failed to find block '%s'", rbname); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1788 | goto err; |
| 1789 | } |
| 1790 | |
Dr. David Alan Gilbert | d3a5038 | 2017-02-24 18:28:32 +0000 | [diff] [blame] | 1791 | ret = ram_block_discard_range(rb, start, length); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1792 | |
| 1793 | err: |
| 1794 | rcu_read_unlock(); |
| 1795 | |
| 1796 | return ret; |
| 1797 | } |
| 1798 | |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1799 | static int ram_state_init(RAMState **rsp) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1800 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1801 | *rsp = g_new0(RAMState, 1); |
| 1802 | |
| 1803 | qemu_mutex_init(&(*rsp)->bitmap_mutex); |
| 1804 | qemu_mutex_init(&(*rsp)->src_page_req_mutex); |
| 1805 | QSIMPLEQ_INIT(&(*rsp)->src_page_requests); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1806 | |
| 1807 | if (migrate_use_xbzrle()) { |
| 1808 | XBZRLE_cache_lock(); |
Juan Quintela | c00e092 | 2017-05-09 16:22:01 +0200 | [diff] [blame] | 1809 | XBZRLE.zero_target_page = g_malloc0(TARGET_PAGE_SIZE); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1810 | XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() / |
| 1811 | TARGET_PAGE_SIZE, |
| 1812 | TARGET_PAGE_SIZE); |
| 1813 | if (!XBZRLE.cache) { |
| 1814 | XBZRLE_cache_unlock(); |
| 1815 | error_report("Error creating cache"); |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1816 | g_free(*rsp); |
| 1817 | *rsp = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1818 | return -1; |
| 1819 | } |
| 1820 | XBZRLE_cache_unlock(); |
| 1821 | |
| 1822 | /* We prefer not to abort if there is no memory */ |
| 1823 | XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE); |
| 1824 | if (!XBZRLE.encoded_buf) { |
| 1825 | error_report("Error allocating encoded_buf"); |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1826 | g_free(*rsp); |
| 1827 | *rsp = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1828 | return -1; |
| 1829 | } |
| 1830 | |
| 1831 | XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE); |
| 1832 | if (!XBZRLE.current_buf) { |
| 1833 | error_report("Error allocating current_buf"); |
| 1834 | g_free(XBZRLE.encoded_buf); |
| 1835 | XBZRLE.encoded_buf = NULL; |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1836 | g_free(*rsp); |
| 1837 | *rsp = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1838 | return -1; |
| 1839 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1840 | } |
| 1841 | |
Paolo Bonzini | 4987783 | 2016-02-15 19:57:57 +0100 | [diff] [blame] | 1842 | /* For memory_global_dirty_log_start below. */ |
| 1843 | qemu_mutex_lock_iothread(); |
| 1844 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1845 | qemu_mutex_lock_ramlist(); |
| 1846 | rcu_read_lock(); |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1847 | ram_state_reset(*rsp); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1848 | |
Ashijeet Acharya | 0827b9e | 2017-02-08 19:58:45 +0530 | [diff] [blame] | 1849 | /* Skip setting bitmap if there is no RAM */ |
| 1850 | if (ram_bytes_total()) { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1851 | RAMBlock *block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1852 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1853 | QLIST_FOREACH_RCU(block, &ram_list.blocks, next) { |
| 1854 | unsigned long pages = block->max_length >> TARGET_PAGE_BITS; |
| 1855 | |
| 1856 | block->bmap = bitmap_new(pages); |
| 1857 | bitmap_set(block->bmap, 0, pages); |
| 1858 | if (migrate_postcopy_ram()) { |
| 1859 | block->unsentmap = bitmap_new(pages); |
| 1860 | bitmap_set(block->unsentmap, 0, pages); |
| 1861 | } |
Ashijeet Acharya | 0827b9e | 2017-02-08 19:58:45 +0530 | [diff] [blame] | 1862 | } |
Dr. David Alan Gilbert | f3f491f | 2015-11-05 18:11:01 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1865 | /* |
| 1866 | * Count the total number of pages used by ram blocks not including any |
| 1867 | * gaps due to alignment or unplugs. |
| 1868 | */ |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1869 | (*rsp)->migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1870 | |
| 1871 | memory_global_dirty_log_start(); |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1872 | migration_bitmap_sync(*rsp); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1873 | qemu_mutex_unlock_ramlist(); |
Paolo Bonzini | 4987783 | 2016-02-15 19:57:57 +0100 | [diff] [blame] | 1874 | qemu_mutex_unlock_iothread(); |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 1875 | rcu_read_unlock(); |
| 1876 | |
| 1877 | return 0; |
| 1878 | } |
| 1879 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1880 | /* |
| 1881 | * Each of ram_save_setup, ram_save_iterate and ram_save_complete has |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 1882 | * long-running RCU critical section. When rcu-reclaims in the code |
| 1883 | * start to become numerous it will be necessary to reduce the |
| 1884 | * granularity of these critical sections. |
| 1885 | */ |
| 1886 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1887 | /** |
| 1888 | * ram_save_setup: Setup RAM for migration |
| 1889 | * |
| 1890 | * Returns zero to indicate success and negative for error |
| 1891 | * |
| 1892 | * @f: QEMUFile where to send the data |
| 1893 | * @opaque: RAMState pointer |
| 1894 | */ |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 1895 | static int ram_save_setup(QEMUFile *f, void *opaque) |
| 1896 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1897 | RAMState **rsp = opaque; |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 1898 | RAMBlock *block; |
| 1899 | |
| 1900 | /* migration has already setup the bitmap, reuse it. */ |
| 1901 | if (!migration_in_colo_state()) { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1902 | if (ram_state_init(rsp) != 0) { |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 1903 | return -1; |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1904 | } |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 1905 | } |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1906 | (*rsp)->f = f; |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 1907 | |
| 1908 | rcu_read_lock(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1909 | |
| 1910 | qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE); |
| 1911 | |
Peter Xu | 99e1558 | 2017-05-12 12:17:39 +0800 | [diff] [blame] | 1912 | RAMBLOCK_FOREACH(block) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1913 | qemu_put_byte(f, strlen(block->idstr)); |
| 1914 | qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); |
| 1915 | qemu_put_be64(f, block->used_length); |
Dr. David Alan Gilbert | ef08fb3 | 2017-02-24 18:28:30 +0000 | [diff] [blame] | 1916 | if (migrate_postcopy_ram() && block->page_size != qemu_host_page_size) { |
| 1917 | qemu_put_be64(f, block->page_size); |
| 1918 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | rcu_read_unlock(); |
| 1922 | |
| 1923 | ram_control_before_iterate(f, RAM_CONTROL_SETUP); |
| 1924 | ram_control_after_iterate(f, RAM_CONTROL_SETUP); |
| 1925 | |
| 1926 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 1927 | |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1931 | /** |
| 1932 | * ram_save_iterate: iterative stage for migration |
| 1933 | * |
| 1934 | * Returns zero to indicate success and negative for error |
| 1935 | * |
| 1936 | * @f: QEMUFile where to send the data |
| 1937 | * @opaque: RAMState pointer |
| 1938 | */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1939 | static int ram_save_iterate(QEMUFile *f, void *opaque) |
| 1940 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1941 | RAMState **temp = opaque; |
| 1942 | RAMState *rs = *temp; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1943 | int ret; |
| 1944 | int i; |
| 1945 | int64_t t0; |
Thomas Huth | 5c90308 | 2016-11-04 14:10:17 +0100 | [diff] [blame] | 1946 | int done = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1947 | |
| 1948 | rcu_read_lock(); |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1949 | if (ram_list.version != rs->last_version) { |
| 1950 | ram_state_reset(rs); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | /* Read version before ram_list.blocks */ |
| 1954 | smp_rmb(); |
| 1955 | |
| 1956 | ram_control_before_iterate(f, RAM_CONTROL_ROUND); |
| 1957 | |
| 1958 | t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
| 1959 | i = 0; |
| 1960 | while ((ret = qemu_file_rate_limit(f)) == 0) { |
| 1961 | int pages; |
| 1962 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 1963 | pages = ram_find_and_save_block(rs, false); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1964 | /* no more pages to sent */ |
| 1965 | if (pages == 0) { |
Thomas Huth | 5c90308 | 2016-11-04 14:10:17 +0100 | [diff] [blame] | 1966 | done = 1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1967 | break; |
| 1968 | } |
Juan Quintela | 23b28c3 | 2017-03-13 20:51:34 +0100 | [diff] [blame] | 1969 | rs->iterations++; |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 1970 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1971 | /* we want to check in the 1st loop, just in case it was the 1st time |
| 1972 | and we had to sync the dirty bitmap. |
| 1973 | qemu_get_clock_ns() is a bit expensive, so we only check each some |
| 1974 | iterations |
| 1975 | */ |
| 1976 | if ((i & 63) == 0) { |
| 1977 | uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000; |
| 1978 | if (t1 > MAX_WAIT) { |
Juan Quintela | 55c4446 | 2017-01-23 22:32:05 +0100 | [diff] [blame] | 1979 | trace_ram_save_iterate_big_wait(t1, i); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1980 | break; |
| 1981 | } |
| 1982 | } |
| 1983 | i++; |
| 1984 | } |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 1985 | flush_compressed_data(rs); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1986 | rcu_read_unlock(); |
| 1987 | |
| 1988 | /* |
| 1989 | * Must occur before EOS (or any QEMUFile operation) |
| 1990 | * because of RDMA protocol. |
| 1991 | */ |
| 1992 | ram_control_after_iterate(f, RAM_CONTROL_ROUND); |
| 1993 | |
| 1994 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 1995 | ram_counters.transferred += 8; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1996 | |
| 1997 | ret = qemu_file_get_error(f); |
| 1998 | if (ret < 0) { |
| 1999 | return ret; |
| 2000 | } |
| 2001 | |
Thomas Huth | 5c90308 | 2016-11-04 14:10:17 +0100 | [diff] [blame] | 2002 | return done; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2003 | } |
| 2004 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2005 | /** |
| 2006 | * ram_save_complete: function called to send the remaining amount of ram |
| 2007 | * |
| 2008 | * Returns zero to indicate success |
| 2009 | * |
| 2010 | * Called with iothread lock |
| 2011 | * |
| 2012 | * @f: QEMUFile where to send the data |
| 2013 | * @opaque: RAMState pointer |
| 2014 | */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2015 | static int ram_save_complete(QEMUFile *f, void *opaque) |
| 2016 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2017 | RAMState **temp = opaque; |
| 2018 | RAMState *rs = *temp; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2019 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2020 | rcu_read_lock(); |
| 2021 | |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 2022 | if (!migration_in_postcopy()) { |
Juan Quintela | 8d820d6 | 2017-03-13 19:35:50 +0100 | [diff] [blame] | 2023 | migration_bitmap_sync(rs); |
Dr. David Alan Gilbert | 663e6c1 | 2015-11-05 18:11:13 +0000 | [diff] [blame] | 2024 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2025 | |
| 2026 | ram_control_before_iterate(f, RAM_CONTROL_FINISH); |
| 2027 | |
| 2028 | /* try transferring iterative blocks of memory */ |
| 2029 | |
| 2030 | /* flush all remaining blocks regardless of rate limiting */ |
| 2031 | while (true) { |
| 2032 | int pages; |
| 2033 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 2034 | pages = ram_find_and_save_block(rs, !migration_in_colo_state()); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2035 | /* no more blocks to sent */ |
| 2036 | if (pages == 0) { |
| 2037 | break; |
| 2038 | } |
| 2039 | } |
| 2040 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 2041 | flush_compressed_data(rs); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2042 | ram_control_after_iterate(f, RAM_CONTROL_FINISH); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2043 | |
| 2044 | rcu_read_unlock(); |
Paolo Bonzini | d09a6fd | 2015-07-09 08:47:58 +0200 | [diff] [blame] | 2045 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2046 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 2047 | |
| 2048 | return 0; |
| 2049 | } |
| 2050 | |
Dr. David Alan Gilbert | c31b098 | 2015-11-05 18:10:54 +0000 | [diff] [blame] | 2051 | static void ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size, |
| 2052 | uint64_t *non_postcopiable_pending, |
| 2053 | uint64_t *postcopiable_pending) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2054 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2055 | RAMState **temp = opaque; |
| 2056 | RAMState *rs = *temp; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2057 | uint64_t remaining_size; |
| 2058 | |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 2059 | remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2060 | |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 2061 | if (!migration_in_postcopy() && |
Dr. David Alan Gilbert | 663e6c1 | 2015-11-05 18:11:13 +0000 | [diff] [blame] | 2062 | remaining_size < max_size) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2063 | qemu_mutex_lock_iothread(); |
| 2064 | rcu_read_lock(); |
Juan Quintela | 8d820d6 | 2017-03-13 19:35:50 +0100 | [diff] [blame] | 2065 | migration_bitmap_sync(rs); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2066 | rcu_read_unlock(); |
| 2067 | qemu_mutex_unlock_iothread(); |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 2068 | remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2069 | } |
Dr. David Alan Gilbert | c31b098 | 2015-11-05 18:10:54 +0000 | [diff] [blame] | 2070 | |
| 2071 | /* We can do postcopy, and all the data is postcopiable */ |
| 2072 | *postcopiable_pending += remaining_size; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) |
| 2076 | { |
| 2077 | unsigned int xh_len; |
| 2078 | int xh_flags; |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 2079 | uint8_t *loaded_data; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2080 | |
| 2081 | if (!xbzrle_decoded_buf) { |
| 2082 | xbzrle_decoded_buf = g_malloc(TARGET_PAGE_SIZE); |
| 2083 | } |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 2084 | loaded_data = xbzrle_decoded_buf; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2085 | |
| 2086 | /* extract RLE header */ |
| 2087 | xh_flags = qemu_get_byte(f); |
| 2088 | xh_len = qemu_get_be16(f); |
| 2089 | |
| 2090 | if (xh_flags != ENCODING_FLAG_XBZRLE) { |
| 2091 | error_report("Failed to load XBZRLE page - wrong compression!"); |
| 2092 | return -1; |
| 2093 | } |
| 2094 | |
| 2095 | if (xh_len > TARGET_PAGE_SIZE) { |
| 2096 | error_report("Failed to load XBZRLE page - len overflow!"); |
| 2097 | return -1; |
| 2098 | } |
| 2099 | /* load data and decode */ |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 2100 | qemu_get_buffer_in_place(f, &loaded_data, xh_len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2101 | |
| 2102 | /* decode RLE */ |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 2103 | if (xbzrle_decode_buffer(loaded_data, xh_len, host, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2104 | TARGET_PAGE_SIZE) == -1) { |
| 2105 | error_report("Failed to load XBZRLE page - decode error!"); |
| 2106 | return -1; |
| 2107 | } |
| 2108 | |
| 2109 | return 0; |
| 2110 | } |
| 2111 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2112 | /** |
| 2113 | * ram_block_from_stream: read a RAMBlock id from the migration stream |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2114 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2115 | * Must be called from within a rcu critical section. |
| 2116 | * |
| 2117 | * Returns a pointer from within the RCU-protected ram_list. |
| 2118 | * |
| 2119 | * @f: QEMUFile where to read the data from |
| 2120 | * @flags: Page flags (mostly to see if it's a continuation of previous block) |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2121 | */ |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2122 | static inline RAMBlock *ram_block_from_stream(QEMUFile *f, int flags) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2123 | { |
| 2124 | static RAMBlock *block = NULL; |
| 2125 | char id[256]; |
| 2126 | uint8_t len; |
| 2127 | |
| 2128 | if (flags & RAM_SAVE_FLAG_CONTINUE) { |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 2129 | if (!block) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2130 | error_report("Ack, bad migration stream!"); |
| 2131 | return NULL; |
| 2132 | } |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 2133 | return block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | len = qemu_get_byte(f); |
| 2137 | qemu_get_buffer(f, (uint8_t *)id, len); |
| 2138 | id[len] = 0; |
| 2139 | |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 2140 | block = qemu_ram_block_by_name(id); |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 2141 | if (!block) { |
| 2142 | error_report("Can't find block %s", id); |
| 2143 | return NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2144 | } |
| 2145 | |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 2146 | return block; |
| 2147 | } |
| 2148 | |
| 2149 | static inline void *host_from_ram_block_offset(RAMBlock *block, |
| 2150 | ram_addr_t offset) |
| 2151 | { |
| 2152 | if (!offset_in_ramblock(block, offset)) { |
| 2153 | return NULL; |
| 2154 | } |
| 2155 | |
| 2156 | return block->host + offset; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2157 | } |
| 2158 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2159 | /** |
| 2160 | * ram_handle_compressed: handle the zero page case |
| 2161 | * |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2162 | * If a page (or a whole RDMA chunk) has been |
| 2163 | * determined to be zero, then zap it. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2164 | * |
| 2165 | * @host: host address for the zero page |
| 2166 | * @ch: what the page is filled from. We only support zero |
| 2167 | * @size: size of the zero page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2168 | */ |
| 2169 | void ram_handle_compressed(void *host, uint8_t ch, uint64_t size) |
| 2170 | { |
| 2171 | if (ch != 0 || !is_zero_range(host, size)) { |
| 2172 | memset(host, ch, size); |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | static void *do_data_decompress(void *opaque) |
| 2177 | { |
| 2178 | DecompressParam *param = opaque; |
| 2179 | unsigned long pagesize; |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2180 | uint8_t *des; |
| 2181 | int len; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2182 | |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2183 | qemu_mutex_lock(¶m->mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 2184 | while (!param->quit) { |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2185 | if (param->des) { |
| 2186 | des = param->des; |
| 2187 | len = param->len; |
| 2188 | param->des = 0; |
| 2189 | qemu_mutex_unlock(¶m->mutex); |
| 2190 | |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2191 | pagesize = TARGET_PAGE_SIZE; |
| 2192 | /* uncompress() will return failed in some case, especially |
| 2193 | * when the page is dirted when doing the compression, it's |
| 2194 | * not a problem because the dirty page will be retransferred |
| 2195 | * and uncompress() won't break the data in other pages. |
| 2196 | */ |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2197 | uncompress((Bytef *)des, &pagesize, |
| 2198 | (const Bytef *)param->compbuf, len); |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2199 | |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2200 | qemu_mutex_lock(&decomp_done_lock); |
| 2201 | param->done = true; |
| 2202 | qemu_cond_signal(&decomp_done_cond); |
| 2203 | qemu_mutex_unlock(&decomp_done_lock); |
| 2204 | |
| 2205 | qemu_mutex_lock(¶m->mutex); |
| 2206 | } else { |
| 2207 | qemu_cond_wait(¶m->cond, ¶m->mutex); |
| 2208 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2209 | } |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2210 | qemu_mutex_unlock(¶m->mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2211 | |
| 2212 | return NULL; |
| 2213 | } |
| 2214 | |
Liang Li | 5533b2e | 2016-05-05 15:32:52 +0800 | [diff] [blame] | 2215 | static void wait_for_decompress_done(void) |
| 2216 | { |
| 2217 | int idx, thread_count; |
| 2218 | |
| 2219 | if (!migrate_use_compression()) { |
| 2220 | return; |
| 2221 | } |
| 2222 | |
| 2223 | thread_count = migrate_decompress_threads(); |
| 2224 | qemu_mutex_lock(&decomp_done_lock); |
| 2225 | for (idx = 0; idx < thread_count; idx++) { |
| 2226 | while (!decomp_param[idx].done) { |
| 2227 | qemu_cond_wait(&decomp_done_cond, &decomp_done_lock); |
| 2228 | } |
| 2229 | } |
| 2230 | qemu_mutex_unlock(&decomp_done_lock); |
| 2231 | } |
| 2232 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2233 | void migrate_decompress_threads_create(void) |
| 2234 | { |
| 2235 | int i, thread_count; |
| 2236 | |
Juan Quintela | 3416ab5 | 2016-04-20 11:56:01 +0200 | [diff] [blame] | 2237 | if (!migrate_use_compression()) { |
| 2238 | return; |
| 2239 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2240 | thread_count = migrate_decompress_threads(); |
| 2241 | decompress_threads = g_new0(QemuThread, thread_count); |
| 2242 | decomp_param = g_new0(DecompressParam, thread_count); |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2243 | qemu_mutex_init(&decomp_done_lock); |
| 2244 | qemu_cond_init(&decomp_done_cond); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2245 | for (i = 0; i < thread_count; i++) { |
| 2246 | qemu_mutex_init(&decomp_param[i].mutex); |
| 2247 | qemu_cond_init(&decomp_param[i].cond); |
| 2248 | decomp_param[i].compbuf = g_malloc0(compressBound(TARGET_PAGE_SIZE)); |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2249 | decomp_param[i].done = true; |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 2250 | decomp_param[i].quit = false; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2251 | qemu_thread_create(decompress_threads + i, "decompress", |
| 2252 | do_data_decompress, decomp_param + i, |
| 2253 | QEMU_THREAD_JOINABLE); |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | void migrate_decompress_threads_join(void) |
| 2258 | { |
| 2259 | int i, thread_count; |
| 2260 | |
Juan Quintela | 3416ab5 | 2016-04-20 11:56:01 +0200 | [diff] [blame] | 2261 | if (!migrate_use_compression()) { |
| 2262 | return; |
| 2263 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2264 | thread_count = migrate_decompress_threads(); |
| 2265 | for (i = 0; i < thread_count; i++) { |
| 2266 | qemu_mutex_lock(&decomp_param[i].mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 2267 | decomp_param[i].quit = true; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2268 | qemu_cond_signal(&decomp_param[i].cond); |
| 2269 | qemu_mutex_unlock(&decomp_param[i].mutex); |
| 2270 | } |
| 2271 | for (i = 0; i < thread_count; i++) { |
| 2272 | qemu_thread_join(decompress_threads + i); |
| 2273 | qemu_mutex_destroy(&decomp_param[i].mutex); |
| 2274 | qemu_cond_destroy(&decomp_param[i].cond); |
| 2275 | g_free(decomp_param[i].compbuf); |
| 2276 | } |
| 2277 | g_free(decompress_threads); |
| 2278 | g_free(decomp_param); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2279 | decompress_threads = NULL; |
| 2280 | decomp_param = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2281 | } |
| 2282 | |
Dr. David Alan Gilbert | c1bc662 | 2015-12-16 11:47:38 +0000 | [diff] [blame] | 2283 | static void decompress_data_with_multi_threads(QEMUFile *f, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2284 | void *host, int len) |
| 2285 | { |
| 2286 | int idx, thread_count; |
| 2287 | |
| 2288 | thread_count = migrate_decompress_threads(); |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2289 | qemu_mutex_lock(&decomp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2290 | while (true) { |
| 2291 | for (idx = 0; idx < thread_count; idx++) { |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2292 | if (decomp_param[idx].done) { |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2293 | decomp_param[idx].done = false; |
| 2294 | qemu_mutex_lock(&decomp_param[idx].mutex); |
Dr. David Alan Gilbert | c1bc662 | 2015-12-16 11:47:38 +0000 | [diff] [blame] | 2295 | qemu_get_buffer(f, decomp_param[idx].compbuf, len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2296 | decomp_param[idx].des = host; |
| 2297 | decomp_param[idx].len = len; |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2298 | qemu_cond_signal(&decomp_param[idx].cond); |
| 2299 | qemu_mutex_unlock(&decomp_param[idx].mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2300 | break; |
| 2301 | } |
| 2302 | } |
| 2303 | if (idx < thread_count) { |
| 2304 | break; |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2305 | } else { |
| 2306 | qemu_cond_wait(&decomp_done_cond, &decomp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2307 | } |
| 2308 | } |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2309 | qemu_mutex_unlock(&decomp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2310 | } |
| 2311 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2312 | /** |
| 2313 | * ram_postcopy_incoming_init: allocate postcopy data structures |
| 2314 | * |
| 2315 | * Returns 0 for success and negative if there was one error |
| 2316 | * |
| 2317 | * @mis: current migration incoming state |
| 2318 | * |
| 2319 | * Allocate data structures etc needed by incoming migration with |
| 2320 | * postcopy-ram. postcopy-ram's similarly names |
| 2321 | * postcopy_ram_incoming_init does the work. |
Dr. David Alan Gilbert | 1caddf8 | 2015-11-05 18:11:03 +0000 | [diff] [blame] | 2322 | */ |
| 2323 | int ram_postcopy_incoming_init(MigrationIncomingState *mis) |
| 2324 | { |
Juan Quintela | b8c4899 | 2017-03-21 17:44:30 +0100 | [diff] [blame] | 2325 | unsigned long ram_pages = last_ram_page(); |
Dr. David Alan Gilbert | 1caddf8 | 2015-11-05 18:11:03 +0000 | [diff] [blame] | 2326 | |
| 2327 | return postcopy_ram_incoming_init(mis, ram_pages); |
| 2328 | } |
| 2329 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2330 | /** |
| 2331 | * ram_load_postcopy: load a page in postcopy case |
| 2332 | * |
| 2333 | * Returns 0 for success or -errno in case of error |
| 2334 | * |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2335 | * Called in postcopy mode by ram_load(). |
| 2336 | * rcu_read_lock is taken prior to this being called. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2337 | * |
| 2338 | * @f: QEMUFile where to send the data |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2339 | */ |
| 2340 | static int ram_load_postcopy(QEMUFile *f) |
| 2341 | { |
| 2342 | int flags = 0, ret = 0; |
| 2343 | bool place_needed = false; |
Dr. David Alan Gilbert | 28abd20 | 2017-02-24 18:28:37 +0000 | [diff] [blame] | 2344 | bool matching_page_sizes = false; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2345 | MigrationIncomingState *mis = migration_incoming_get_current(); |
| 2346 | /* Temporary page that is later 'placed' */ |
| 2347 | void *postcopy_host_page = postcopy_get_tmp_page(mis); |
Dr. David Alan Gilbert | c53b7dd | 2015-11-05 18:11:12 +0000 | [diff] [blame] | 2348 | void *last_host = NULL; |
Dr. David Alan Gilbert | a3b6ff6 | 2015-11-11 14:02:28 +0000 | [diff] [blame] | 2349 | bool all_zero = false; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2350 | |
| 2351 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { |
| 2352 | ram_addr_t addr; |
| 2353 | void *host = NULL; |
| 2354 | void *page_buffer = NULL; |
| 2355 | void *place_source = NULL; |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 2356 | RAMBlock *block = NULL; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2357 | uint8_t ch; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2358 | |
| 2359 | addr = qemu_get_be64(f); |
| 2360 | flags = addr & ~TARGET_PAGE_MASK; |
| 2361 | addr &= TARGET_PAGE_MASK; |
| 2362 | |
| 2363 | trace_ram_load_postcopy_loop((uint64_t)addr, flags); |
| 2364 | place_needed = false; |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 2365 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE)) { |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 2366 | block = ram_block_from_stream(f, flags); |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 2367 | |
| 2368 | host = host_from_ram_block_offset(block, addr); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2369 | if (!host) { |
| 2370 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
| 2371 | ret = -EINVAL; |
| 2372 | break; |
| 2373 | } |
Dr. David Alan Gilbert | 28abd20 | 2017-02-24 18:28:37 +0000 | [diff] [blame] | 2374 | matching_page_sizes = block->page_size == TARGET_PAGE_SIZE; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2375 | /* |
Dr. David Alan Gilbert | 28abd20 | 2017-02-24 18:28:37 +0000 | [diff] [blame] | 2376 | * Postcopy requires that we place whole host pages atomically; |
| 2377 | * these may be huge pages for RAMBlocks that are backed by |
| 2378 | * hugetlbfs. |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2379 | * To make it atomic, the data is read into a temporary page |
| 2380 | * that's moved into place later. |
| 2381 | * The migration protocol uses, possibly smaller, target-pages |
| 2382 | * however the source ensures it always sends all the components |
| 2383 | * of a host page in order. |
| 2384 | */ |
| 2385 | page_buffer = postcopy_host_page + |
Dr. David Alan Gilbert | 28abd20 | 2017-02-24 18:28:37 +0000 | [diff] [blame] | 2386 | ((uintptr_t)host & (block->page_size - 1)); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2387 | /* If all TP are zero then we can optimise the place */ |
Dr. David Alan Gilbert | 28abd20 | 2017-02-24 18:28:37 +0000 | [diff] [blame] | 2388 | if (!((uintptr_t)host & (block->page_size - 1))) { |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2389 | all_zero = true; |
Dr. David Alan Gilbert | c53b7dd | 2015-11-05 18:11:12 +0000 | [diff] [blame] | 2390 | } else { |
| 2391 | /* not the 1st TP within the HP */ |
| 2392 | if (host != (last_host + TARGET_PAGE_SIZE)) { |
Markus Armbruster | 9af9e0f | 2015-12-18 16:35:19 +0100 | [diff] [blame] | 2393 | error_report("Non-sequential target page %p/%p", |
Dr. David Alan Gilbert | c53b7dd | 2015-11-05 18:11:12 +0000 | [diff] [blame] | 2394 | host, last_host); |
| 2395 | ret = -EINVAL; |
| 2396 | break; |
| 2397 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Dr. David Alan Gilbert | c53b7dd | 2015-11-05 18:11:12 +0000 | [diff] [blame] | 2400 | |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2401 | /* |
| 2402 | * If it's the last part of a host page then we place the host |
| 2403 | * page |
| 2404 | */ |
| 2405 | place_needed = (((uintptr_t)host + TARGET_PAGE_SIZE) & |
Dr. David Alan Gilbert | 28abd20 | 2017-02-24 18:28:37 +0000 | [diff] [blame] | 2406 | (block->page_size - 1)) == 0; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2407 | place_source = postcopy_host_page; |
| 2408 | } |
Dr. David Alan Gilbert | c53b7dd | 2015-11-05 18:11:12 +0000 | [diff] [blame] | 2409 | last_host = host; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2410 | |
| 2411 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 2412 | case RAM_SAVE_FLAG_ZERO: |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2413 | ch = qemu_get_byte(f); |
| 2414 | memset(page_buffer, ch, TARGET_PAGE_SIZE); |
| 2415 | if (ch) { |
| 2416 | all_zero = false; |
| 2417 | } |
| 2418 | break; |
| 2419 | |
| 2420 | case RAM_SAVE_FLAG_PAGE: |
| 2421 | all_zero = false; |
| 2422 | if (!place_needed || !matching_page_sizes) { |
| 2423 | qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE); |
| 2424 | } else { |
| 2425 | /* Avoids the qemu_file copy during postcopy, which is |
| 2426 | * going to do a copy later; can only do it when we |
| 2427 | * do this read in one go (matching page sizes) |
| 2428 | */ |
| 2429 | qemu_get_buffer_in_place(f, (uint8_t **)&place_source, |
| 2430 | TARGET_PAGE_SIZE); |
| 2431 | } |
| 2432 | break; |
| 2433 | case RAM_SAVE_FLAG_EOS: |
| 2434 | /* normal exit */ |
| 2435 | break; |
| 2436 | default: |
| 2437 | error_report("Unknown combination of migration flags: %#x" |
| 2438 | " (postcopy mode)", flags); |
| 2439 | ret = -EINVAL; |
| 2440 | } |
| 2441 | |
| 2442 | if (place_needed) { |
| 2443 | /* This gets called at the last target page in the host page */ |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 2444 | void *place_dest = host + TARGET_PAGE_SIZE - block->page_size; |
| 2445 | |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2446 | if (all_zero) { |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 2447 | ret = postcopy_place_page_zero(mis, place_dest, |
| 2448 | block->page_size); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2449 | } else { |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 2450 | ret = postcopy_place_page(mis, place_dest, |
| 2451 | place_source, block->page_size); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2452 | } |
| 2453 | } |
| 2454 | if (!ret) { |
| 2455 | ret = qemu_file_get_error(f); |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | return ret; |
| 2460 | } |
| 2461 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2462 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 2463 | { |
Juan Quintela | edc6012 | 2016-11-02 12:40:46 +0100 | [diff] [blame] | 2464 | int flags = 0, ret = 0, invalid_flags = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2465 | static uint64_t seq_iter; |
| 2466 | int len = 0; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2467 | /* |
| 2468 | * If system is running in postcopy mode, page inserts to host memory must |
| 2469 | * be atomic |
| 2470 | */ |
| 2471 | bool postcopy_running = postcopy_state_get() >= POSTCOPY_INCOMING_LISTENING; |
Dr. David Alan Gilbert | ef08fb3 | 2017-02-24 18:28:30 +0000 | [diff] [blame] | 2472 | /* ADVISE is earlier, it shows the source has the postcopy capability on */ |
| 2473 | bool postcopy_advised = postcopy_state_get() >= POSTCOPY_INCOMING_ADVISE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2474 | |
| 2475 | seq_iter++; |
| 2476 | |
| 2477 | if (version_id != 4) { |
| 2478 | ret = -EINVAL; |
| 2479 | } |
| 2480 | |
Juan Quintela | edc6012 | 2016-11-02 12:40:46 +0100 | [diff] [blame] | 2481 | if (!migrate_use_compression()) { |
| 2482 | invalid_flags |= RAM_SAVE_FLAG_COMPRESS_PAGE; |
| 2483 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2484 | /* This RCU critical section can be very long running. |
| 2485 | * When RCU reclaims in the code start to become numerous, |
| 2486 | * it will be necessary to reduce the granularity of this |
| 2487 | * critical section. |
| 2488 | */ |
| 2489 | rcu_read_lock(); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2490 | |
| 2491 | if (postcopy_running) { |
| 2492 | ret = ram_load_postcopy(f); |
| 2493 | } |
| 2494 | |
| 2495 | while (!postcopy_running && !ret && !(flags & RAM_SAVE_FLAG_EOS)) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2496 | ram_addr_t addr, total_ram_bytes; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 2497 | void *host = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2498 | uint8_t ch; |
| 2499 | |
| 2500 | addr = qemu_get_be64(f); |
| 2501 | flags = addr & ~TARGET_PAGE_MASK; |
| 2502 | addr &= TARGET_PAGE_MASK; |
| 2503 | |
Juan Quintela | edc6012 | 2016-11-02 12:40:46 +0100 | [diff] [blame] | 2504 | if (flags & invalid_flags) { |
| 2505 | if (flags & invalid_flags & RAM_SAVE_FLAG_COMPRESS_PAGE) { |
| 2506 | error_report("Received an unexpected compressed page"); |
| 2507 | } |
| 2508 | |
| 2509 | ret = -EINVAL; |
| 2510 | break; |
| 2511 | } |
| 2512 | |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 2513 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 2514 | RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) { |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 2515 | RAMBlock *block = ram_block_from_stream(f, flags); |
| 2516 | |
| 2517 | host = host_from_ram_block_offset(block, addr); |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 2518 | if (!host) { |
| 2519 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
| 2520 | ret = -EINVAL; |
| 2521 | break; |
| 2522 | } |
Dr. David Alan Gilbert | 1db9d8e | 2017-04-26 19:37:21 +0100 | [diff] [blame] | 2523 | trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host); |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2526 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { |
| 2527 | case RAM_SAVE_FLAG_MEM_SIZE: |
| 2528 | /* Synchronize RAM block list */ |
| 2529 | total_ram_bytes = addr; |
| 2530 | while (!ret && total_ram_bytes) { |
| 2531 | RAMBlock *block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2532 | char id[256]; |
| 2533 | ram_addr_t length; |
| 2534 | |
| 2535 | len = qemu_get_byte(f); |
| 2536 | qemu_get_buffer(f, (uint8_t *)id, len); |
| 2537 | id[len] = 0; |
| 2538 | length = qemu_get_be64(f); |
| 2539 | |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 2540 | block = qemu_ram_block_by_name(id); |
| 2541 | if (block) { |
| 2542 | if (length != block->used_length) { |
| 2543 | Error *local_err = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2544 | |
Gonglei | fa53a0e | 2016-05-10 10:04:59 +0800 | [diff] [blame] | 2545 | ret = qemu_ram_resize(block, length, |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 2546 | &local_err); |
| 2547 | if (local_err) { |
| 2548 | error_report_err(local_err); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2549 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2550 | } |
Dr. David Alan Gilbert | ef08fb3 | 2017-02-24 18:28:30 +0000 | [diff] [blame] | 2551 | /* For postcopy we need to check hugepage sizes match */ |
| 2552 | if (postcopy_advised && |
| 2553 | block->page_size != qemu_host_page_size) { |
| 2554 | uint64_t remote_page_size = qemu_get_be64(f); |
| 2555 | if (remote_page_size != block->page_size) { |
| 2556 | error_report("Mismatched RAM page size %s " |
| 2557 | "(local) %zd != %" PRId64, |
| 2558 | id, block->page_size, |
| 2559 | remote_page_size); |
| 2560 | ret = -EINVAL; |
| 2561 | } |
| 2562 | } |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 2563 | ram_control_load_hook(f, RAM_CONTROL_BLOCK_REG, |
| 2564 | block->idstr); |
| 2565 | } else { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2566 | error_report("Unknown ramblock \"%s\", cannot " |
| 2567 | "accept migration", id); |
| 2568 | ret = -EINVAL; |
| 2569 | } |
| 2570 | |
| 2571 | total_ram_bytes -= length; |
| 2572 | } |
| 2573 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 2574 | |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 2575 | case RAM_SAVE_FLAG_ZERO: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2576 | ch = qemu_get_byte(f); |
| 2577 | ram_handle_compressed(host, ch, TARGET_PAGE_SIZE); |
| 2578 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 2579 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2580 | case RAM_SAVE_FLAG_PAGE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2581 | qemu_get_buffer(f, host, TARGET_PAGE_SIZE); |
| 2582 | break; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2583 | |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 2584 | case RAM_SAVE_FLAG_COMPRESS_PAGE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2585 | len = qemu_get_be32(f); |
| 2586 | if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) { |
| 2587 | error_report("Invalid compressed data length: %d", len); |
| 2588 | ret = -EINVAL; |
| 2589 | break; |
| 2590 | } |
Dr. David Alan Gilbert | c1bc662 | 2015-12-16 11:47:38 +0000 | [diff] [blame] | 2591 | decompress_data_with_multi_threads(f, host, len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2592 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 2593 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2594 | case RAM_SAVE_FLAG_XBZRLE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2595 | if (load_xbzrle(f, addr, host) < 0) { |
| 2596 | error_report("Failed to decompress XBZRLE page at " |
| 2597 | RAM_ADDR_FMT, addr); |
| 2598 | ret = -EINVAL; |
| 2599 | break; |
| 2600 | } |
| 2601 | break; |
| 2602 | case RAM_SAVE_FLAG_EOS: |
| 2603 | /* normal exit */ |
| 2604 | break; |
| 2605 | default: |
| 2606 | if (flags & RAM_SAVE_FLAG_HOOK) { |
Dr. David Alan Gilbert | 632e3a5 | 2015-06-11 18:17:23 +0100 | [diff] [blame] | 2607 | ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2608 | } else { |
| 2609 | error_report("Unknown combination of migration flags: %#x", |
| 2610 | flags); |
| 2611 | ret = -EINVAL; |
| 2612 | } |
| 2613 | } |
| 2614 | if (!ret) { |
| 2615 | ret = qemu_file_get_error(f); |
| 2616 | } |
| 2617 | } |
| 2618 | |
Liang Li | 5533b2e | 2016-05-05 15:32:52 +0800 | [diff] [blame] | 2619 | wait_for_decompress_done(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2620 | rcu_read_unlock(); |
Juan Quintela | 55c4446 | 2017-01-23 22:32:05 +0100 | [diff] [blame] | 2621 | trace_ram_load_complete(ret, seq_iter); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2622 | return ret; |
| 2623 | } |
| 2624 | |
| 2625 | static SaveVMHandlers savevm_ram_handlers = { |
Juan Quintela | 9907e84 | 2017-06-28 11:52:24 +0200 | [diff] [blame^] | 2626 | .save_setup = ram_save_setup, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2627 | .save_live_iterate = ram_save_iterate, |
Dr. David Alan Gilbert | 763c906 | 2015-11-05 18:11:00 +0000 | [diff] [blame] | 2628 | .save_live_complete_postcopy = ram_save_complete, |
Dr. David Alan Gilbert | a3e06c3 | 2015-11-05 18:10:41 +0000 | [diff] [blame] | 2629 | .save_live_complete_precopy = ram_save_complete, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2630 | .save_live_pending = ram_save_pending, |
| 2631 | .load_state = ram_load, |
Liang Li | 6ad2a21 | 2015-11-02 15:37:03 +0800 | [diff] [blame] | 2632 | .cleanup = ram_migration_cleanup, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2633 | }; |
| 2634 | |
| 2635 | void ram_mig_init(void) |
| 2636 | { |
| 2637 | qemu_mutex_init(&XBZRLE.lock); |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2638 | register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, &ram_state); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2639 | } |