1e7c033c3SPaolo Bonzini /* 2e7c033c3SPaolo Bonzini * Hierarchical Bitmap Data Type 3e7c033c3SPaolo Bonzini * 4e7c033c3SPaolo Bonzini * Copyright Red Hat, Inc., 2012 5e7c033c3SPaolo Bonzini * 6e7c033c3SPaolo Bonzini * Author: Paolo Bonzini <pbonzini@redhat.com> 7e7c033c3SPaolo Bonzini * 8e7c033c3SPaolo Bonzini * This work is licensed under the terms of the GNU GPL, version 2 or 9e7c033c3SPaolo Bonzini * later. See the COPYING file in the top-level directory. 10e7c033c3SPaolo Bonzini */ 11e7c033c3SPaolo Bonzini 12e7c033c3SPaolo Bonzini #include "qemu/osdep.h" 13e7c033c3SPaolo Bonzini #include "qemu/hbitmap.h" 14e7c033c3SPaolo Bonzini #include "qemu/host-utils.h" 15e7c033c3SPaolo Bonzini #include "trace.h" 16a3b52535SVladimir Sementsov-Ogievskiy #include "crypto/hash.h" 17e7c033c3SPaolo Bonzini 18e7c033c3SPaolo Bonzini /* HBitmaps provides an array of bits. The bits are stored as usual in an 19e7c033c3SPaolo Bonzini * array of unsigned longs, but HBitmap is also optimized to provide fast 20e7c033c3SPaolo Bonzini * iteration over set bits; going from one bit to the next is O(logB n) 21e7c033c3SPaolo Bonzini * worst case, with B = sizeof(long) * CHAR_BIT: the result is low enough 22e7c033c3SPaolo Bonzini * that the number of levels is in fact fixed. 23e7c033c3SPaolo Bonzini * 24e7c033c3SPaolo Bonzini * In order to do this, it stacks multiple bitmaps with progressively coarser 25e7c033c3SPaolo Bonzini * granularity; in all levels except the last, bit N is set iff the N-th 26e7c033c3SPaolo Bonzini * unsigned long is nonzero in the immediately next level. When iteration 27e7c033c3SPaolo Bonzini * completes on the last level it can examine the 2nd-last level to quickly 28e7c033c3SPaolo Bonzini * skip entire words, and even do so recursively to skip blocks of 64 words or 29e7c033c3SPaolo Bonzini * powers thereof (32 on 32-bit machines). 30e7c033c3SPaolo Bonzini * 31e7c033c3SPaolo Bonzini * Given an index in the bitmap, it can be split in group of bits like 32e7c033c3SPaolo Bonzini * this (for the 64-bit case): 33e7c033c3SPaolo Bonzini * 34e7c033c3SPaolo Bonzini * bits 0-57 => word in the last bitmap | bits 58-63 => bit in the word 35e7c033c3SPaolo Bonzini * bits 0-51 => word in the 2nd-last bitmap | bits 52-57 => bit in the word 36e7c033c3SPaolo Bonzini * bits 0-45 => word in the 3rd-last bitmap | bits 46-51 => bit in the word 37e7c033c3SPaolo Bonzini * 38e7c033c3SPaolo Bonzini * So it is easy to move up simply by shifting the index right by 39e7c033c3SPaolo Bonzini * log2(BITS_PER_LONG) bits. To move down, you shift the index left 40e7c033c3SPaolo Bonzini * similarly, and add the word index within the group. Iteration uses 41e7c033c3SPaolo Bonzini * ffs (find first set bit) to find the next word to examine; this 42e7c033c3SPaolo Bonzini * operation can be done in constant time in most current architectures. 43e7c033c3SPaolo Bonzini * 44e7c033c3SPaolo Bonzini * Setting or clearing a range of m bits on all levels, the work to perform 45e7c033c3SPaolo Bonzini * is O(m + m/W + m/W^2 + ...), which is O(m) like on a regular bitmap. 46e7c033c3SPaolo Bonzini * 47e7c033c3SPaolo Bonzini * When iterating on a bitmap, each bit (on any level) is only visited 48e7c033c3SPaolo Bonzini * once. Hence, The total cost of visiting a bitmap with m bits in it is 49e7c033c3SPaolo Bonzini * the number of bits that are set in all bitmaps. Unless the bitmap is 50e7c033c3SPaolo Bonzini * extremely sparse, this is also O(m + m/W + m/W^2 + ...), so the amortized 51e7c033c3SPaolo Bonzini * cost of advancing from one bit to the next is usually constant (worst case 52e7c033c3SPaolo Bonzini * O(logB n) as in the non-amortized complexity). 53e7c033c3SPaolo Bonzini */ 54e7c033c3SPaolo Bonzini 55e7c033c3SPaolo Bonzini struct HBitmap { 56*76d570dcSVladimir Sementsov-Ogievskiy /* Size of the bitmap, as requested in hbitmap_alloc. */ 57*76d570dcSVladimir Sementsov-Ogievskiy uint64_t orig_size; 58*76d570dcSVladimir Sementsov-Ogievskiy 59e7c033c3SPaolo Bonzini /* Number of total bits in the bottom level. */ 60e7c033c3SPaolo Bonzini uint64_t size; 61e7c033c3SPaolo Bonzini 62e7c033c3SPaolo Bonzini /* Number of set bits in the bottom level. */ 63e7c033c3SPaolo Bonzini uint64_t count; 64e7c033c3SPaolo Bonzini 65e7c033c3SPaolo Bonzini /* A scaling factor. Given a granularity of G, each bit in the bitmap will 66e7c033c3SPaolo Bonzini * will actually represent a group of 2^G elements. Each operation on a 67e7c033c3SPaolo Bonzini * range of bits first rounds the bits to determine which group they land 68e7c033c3SPaolo Bonzini * in, and then affect the entire page; iteration will only visit the first 69e7c033c3SPaolo Bonzini * bit of each group. Here is an example of operations in a size-16, 70e7c033c3SPaolo Bonzini * granularity-1 HBitmap: 71e7c033c3SPaolo Bonzini * 72e7c033c3SPaolo Bonzini * initial state 00000000 73e7c033c3SPaolo Bonzini * set(start=0, count=9) 11111000 (iter: 0, 2, 4, 6, 8) 74e7c033c3SPaolo Bonzini * reset(start=1, count=3) 00111000 (iter: 4, 6, 8) 75e7c033c3SPaolo Bonzini * set(start=9, count=2) 00111100 (iter: 4, 6, 8, 10) 76e7c033c3SPaolo Bonzini * reset(start=5, count=5) 00000000 77e7c033c3SPaolo Bonzini * 78e7c033c3SPaolo Bonzini * From an implementation point of view, when setting or resetting bits, 79e7c033c3SPaolo Bonzini * the bitmap will scale bit numbers right by this amount of bits. When 80e7c033c3SPaolo Bonzini * iterating, the bitmap will scale bit numbers left by this amount of 81e7c033c3SPaolo Bonzini * bits. 82e7c033c3SPaolo Bonzini */ 83e7c033c3SPaolo Bonzini int granularity; 84e7c033c3SPaolo Bonzini 8507ac4cdbSFam Zheng /* A meta dirty bitmap to track the dirtiness of bits in this HBitmap. */ 8607ac4cdbSFam Zheng HBitmap *meta; 8707ac4cdbSFam Zheng 88e7c033c3SPaolo Bonzini /* A number of progressively less coarse bitmaps (i.e. level 0 is the 89e7c033c3SPaolo Bonzini * coarsest). Each bit in level N represents a word in level N+1 that 90e7c033c3SPaolo Bonzini * has a set bit, except the last level where each bit represents the 91e7c033c3SPaolo Bonzini * actual bitmap. 92e7c033c3SPaolo Bonzini * 93e7c033c3SPaolo Bonzini * Note that all bitmaps have the same number of levels. Even a 1-bit 94e7c033c3SPaolo Bonzini * bitmap will still allocate HBITMAP_LEVELS arrays. 95e7c033c3SPaolo Bonzini */ 96e7c033c3SPaolo Bonzini unsigned long *levels[HBITMAP_LEVELS]; 978515efbeSJohn Snow 988515efbeSJohn Snow /* The length of each levels[] array. */ 998515efbeSJohn Snow uint64_t sizes[HBITMAP_LEVELS]; 100e7c033c3SPaolo Bonzini }; 101e7c033c3SPaolo Bonzini 102e7c033c3SPaolo Bonzini /* Advance hbi to the next nonzero word and return it. hbi->pos 103e7c033c3SPaolo Bonzini * is updated. Returns zero if we reach the end of the bitmap. 104e7c033c3SPaolo Bonzini */ 105e7c033c3SPaolo Bonzini unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi) 106e7c033c3SPaolo Bonzini { 107e7c033c3SPaolo Bonzini size_t pos = hbi->pos; 108e7c033c3SPaolo Bonzini const HBitmap *hb = hbi->hb; 109e7c033c3SPaolo Bonzini unsigned i = HBITMAP_LEVELS - 1; 110e7c033c3SPaolo Bonzini 111e7c033c3SPaolo Bonzini unsigned long cur; 112e7c033c3SPaolo Bonzini do { 113f63ea4e9SVladimir Sementsov-Ogievskiy i--; 114e7c033c3SPaolo Bonzini pos >>= BITS_PER_LEVEL; 115f63ea4e9SVladimir Sementsov-Ogievskiy cur = hbi->cur[i] & hb->levels[i][pos]; 116e7c033c3SPaolo Bonzini } while (cur == 0); 117e7c033c3SPaolo Bonzini 118e7c033c3SPaolo Bonzini /* Check for end of iteration. We always use fewer than BITS_PER_LONG 119e7c033c3SPaolo Bonzini * bits in the level 0 bitmap; thus we can repurpose the most significant 120e7c033c3SPaolo Bonzini * bit as a sentinel. The sentinel is set in hbitmap_alloc and ensures 121e7c033c3SPaolo Bonzini * that the above loop ends even without an explicit check on i. 122e7c033c3SPaolo Bonzini */ 123e7c033c3SPaolo Bonzini 124e7c033c3SPaolo Bonzini if (i == 0 && cur == (1UL << (BITS_PER_LONG - 1))) { 125e7c033c3SPaolo Bonzini return 0; 126e7c033c3SPaolo Bonzini } 127e7c033c3SPaolo Bonzini for (; i < HBITMAP_LEVELS - 1; i++) { 128e7c033c3SPaolo Bonzini /* Shift back pos to the left, matching the right shifts above. 129e7c033c3SPaolo Bonzini * The index of this word's least significant set bit provides 130e7c033c3SPaolo Bonzini * the low-order bits. 131e7c033c3SPaolo Bonzini */ 13218331e7cSRichard Henderson assert(cur); 13318331e7cSRichard Henderson pos = (pos << BITS_PER_LEVEL) + ctzl(cur); 134e7c033c3SPaolo Bonzini hbi->cur[i] = cur & (cur - 1); 135e7c033c3SPaolo Bonzini 136e7c033c3SPaolo Bonzini /* Set up next level for iteration. */ 137e7c033c3SPaolo Bonzini cur = hb->levels[i + 1][pos]; 138e7c033c3SPaolo Bonzini } 139e7c033c3SPaolo Bonzini 140e7c033c3SPaolo Bonzini hbi->pos = pos; 141e7c033c3SPaolo Bonzini trace_hbitmap_iter_skip_words(hbi->hb, hbi, pos, cur); 142e7c033c3SPaolo Bonzini 143e7c033c3SPaolo Bonzini assert(cur); 144e7c033c3SPaolo Bonzini return cur; 145e7c033c3SPaolo Bonzini } 146e7c033c3SPaolo Bonzini 147a33fbb4fSMax Reitz int64_t hbitmap_iter_next(HBitmapIter *hbi, bool advance) 148f63ea4e9SVladimir Sementsov-Ogievskiy { 149f63ea4e9SVladimir Sementsov-Ogievskiy unsigned long cur = hbi->cur[HBITMAP_LEVELS - 1] & 150f63ea4e9SVladimir Sementsov-Ogievskiy hbi->hb->levels[HBITMAP_LEVELS - 1][hbi->pos]; 151f63ea4e9SVladimir Sementsov-Ogievskiy int64_t item; 152f63ea4e9SVladimir Sementsov-Ogievskiy 153f63ea4e9SVladimir Sementsov-Ogievskiy if (cur == 0) { 154f63ea4e9SVladimir Sementsov-Ogievskiy cur = hbitmap_iter_skip_words(hbi); 155f63ea4e9SVladimir Sementsov-Ogievskiy if (cur == 0) { 156f63ea4e9SVladimir Sementsov-Ogievskiy return -1; 157f63ea4e9SVladimir Sementsov-Ogievskiy } 158f63ea4e9SVladimir Sementsov-Ogievskiy } 159f63ea4e9SVladimir Sementsov-Ogievskiy 160a33fbb4fSMax Reitz if (advance) { 161f63ea4e9SVladimir Sementsov-Ogievskiy /* The next call will resume work from the next bit. */ 162f63ea4e9SVladimir Sementsov-Ogievskiy hbi->cur[HBITMAP_LEVELS - 1] = cur & (cur - 1); 163a33fbb4fSMax Reitz } else { 164a33fbb4fSMax Reitz hbi->cur[HBITMAP_LEVELS - 1] = cur; 165a33fbb4fSMax Reitz } 166f63ea4e9SVladimir Sementsov-Ogievskiy item = ((uint64_t)hbi->pos << BITS_PER_LEVEL) + ctzl(cur); 167f63ea4e9SVladimir Sementsov-Ogievskiy 168f63ea4e9SVladimir Sementsov-Ogievskiy return item << hbi->granularity; 169f63ea4e9SVladimir Sementsov-Ogievskiy } 170f63ea4e9SVladimir Sementsov-Ogievskiy 171e7c033c3SPaolo Bonzini void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first) 172e7c033c3SPaolo Bonzini { 173e7c033c3SPaolo Bonzini unsigned i, bit; 174e7c033c3SPaolo Bonzini uint64_t pos; 175e7c033c3SPaolo Bonzini 176e7c033c3SPaolo Bonzini hbi->hb = hb; 177e7c033c3SPaolo Bonzini pos = first >> hb->granularity; 1781b095244SPaolo Bonzini assert(pos < hb->size); 179e7c033c3SPaolo Bonzini hbi->pos = pos >> BITS_PER_LEVEL; 180e7c033c3SPaolo Bonzini hbi->granularity = hb->granularity; 181e7c033c3SPaolo Bonzini 182e7c033c3SPaolo Bonzini for (i = HBITMAP_LEVELS; i-- > 0; ) { 183e7c033c3SPaolo Bonzini bit = pos & (BITS_PER_LONG - 1); 184e7c033c3SPaolo Bonzini pos >>= BITS_PER_LEVEL; 185e7c033c3SPaolo Bonzini 186e7c033c3SPaolo Bonzini /* Drop bits representing items before first. */ 187e7c033c3SPaolo Bonzini hbi->cur[i] = hb->levels[i][pos] & ~((1UL << bit) - 1); 188e7c033c3SPaolo Bonzini 189e7c033c3SPaolo Bonzini /* We have already added level i+1, so the lowest set bit has 190e7c033c3SPaolo Bonzini * been processed. Clear it. 191e7c033c3SPaolo Bonzini */ 192e7c033c3SPaolo Bonzini if (i != HBITMAP_LEVELS - 1) { 193e7c033c3SPaolo Bonzini hbi->cur[i] &= ~(1UL << bit); 194e7c033c3SPaolo Bonzini } 195e7c033c3SPaolo Bonzini } 196e7c033c3SPaolo Bonzini } 197e7c033c3SPaolo Bonzini 198*76d570dcSVladimir Sementsov-Ogievskiy int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t start, uint64_t count) 19956207df5SVladimir Sementsov-Ogievskiy { 20056207df5SVladimir Sementsov-Ogievskiy size_t pos = (start >> hb->granularity) >> BITS_PER_LEVEL; 20156207df5SVladimir Sementsov-Ogievskiy unsigned long *last_lev = hb->levels[HBITMAP_LEVELS - 1]; 20256207df5SVladimir Sementsov-Ogievskiy unsigned long cur = last_lev[pos]; 203*76d570dcSVladimir Sementsov-Ogievskiy unsigned start_bit_offset; 204*76d570dcSVladimir Sementsov-Ogievskiy uint64_t end_bit, sz; 20556207df5SVladimir Sementsov-Ogievskiy int64_t res; 20656207df5SVladimir Sementsov-Ogievskiy 207*76d570dcSVladimir Sementsov-Ogievskiy if (start >= hb->orig_size || count == 0) { 208*76d570dcSVladimir Sementsov-Ogievskiy return -1; 209*76d570dcSVladimir Sementsov-Ogievskiy } 210*76d570dcSVladimir Sementsov-Ogievskiy 211*76d570dcSVladimir Sementsov-Ogievskiy end_bit = count > hb->orig_size - start ? 212*76d570dcSVladimir Sementsov-Ogievskiy hb->size : 213*76d570dcSVladimir Sementsov-Ogievskiy ((start + count - 1) >> hb->granularity) + 1; 214*76d570dcSVladimir Sementsov-Ogievskiy sz = (end_bit + BITS_PER_LONG - 1) >> BITS_PER_LEVEL; 215*76d570dcSVladimir Sementsov-Ogievskiy 216*76d570dcSVladimir Sementsov-Ogievskiy /* There may be some zero bits in @cur before @start. We are not interested 217*76d570dcSVladimir Sementsov-Ogievskiy * in them, let's set them. 218*76d570dcSVladimir Sementsov-Ogievskiy */ 219*76d570dcSVladimir Sementsov-Ogievskiy start_bit_offset = (start >> hb->granularity) & (BITS_PER_LONG - 1); 22056207df5SVladimir Sementsov-Ogievskiy cur |= (1UL << start_bit_offset) - 1; 22156207df5SVladimir Sementsov-Ogievskiy assert((start >> hb->granularity) < hb->size); 22256207df5SVladimir Sementsov-Ogievskiy 22356207df5SVladimir Sementsov-Ogievskiy if (cur == (unsigned long)-1) { 22456207df5SVladimir Sementsov-Ogievskiy do { 22556207df5SVladimir Sementsov-Ogievskiy pos++; 22656207df5SVladimir Sementsov-Ogievskiy } while (pos < sz && last_lev[pos] == (unsigned long)-1); 22756207df5SVladimir Sementsov-Ogievskiy 22856207df5SVladimir Sementsov-Ogievskiy if (pos >= sz) { 22956207df5SVladimir Sementsov-Ogievskiy return -1; 23056207df5SVladimir Sementsov-Ogievskiy } 23156207df5SVladimir Sementsov-Ogievskiy 23256207df5SVladimir Sementsov-Ogievskiy cur = last_lev[pos]; 23356207df5SVladimir Sementsov-Ogievskiy } 23456207df5SVladimir Sementsov-Ogievskiy 23556207df5SVladimir Sementsov-Ogievskiy res = (pos << BITS_PER_LEVEL) + ctol(cur); 236*76d570dcSVladimir Sementsov-Ogievskiy if (res >= end_bit) { 23756207df5SVladimir Sementsov-Ogievskiy return -1; 23856207df5SVladimir Sementsov-Ogievskiy } 23956207df5SVladimir Sementsov-Ogievskiy 24056207df5SVladimir Sementsov-Ogievskiy res = res << hb->granularity; 24156207df5SVladimir Sementsov-Ogievskiy if (res < start) { 24256207df5SVladimir Sementsov-Ogievskiy assert(((start - res) >> hb->granularity) == 0); 24356207df5SVladimir Sementsov-Ogievskiy return start; 24456207df5SVladimir Sementsov-Ogievskiy } 24556207df5SVladimir Sementsov-Ogievskiy 24656207df5SVladimir Sementsov-Ogievskiy return res; 24756207df5SVladimir Sementsov-Ogievskiy } 24856207df5SVladimir Sementsov-Ogievskiy 249e7c033c3SPaolo Bonzini bool hbitmap_empty(const HBitmap *hb) 250e7c033c3SPaolo Bonzini { 251e7c033c3SPaolo Bonzini return hb->count == 0; 252e7c033c3SPaolo Bonzini } 253e7c033c3SPaolo Bonzini 254e7c033c3SPaolo Bonzini int hbitmap_granularity(const HBitmap *hb) 255e7c033c3SPaolo Bonzini { 256e7c033c3SPaolo Bonzini return hb->granularity; 257e7c033c3SPaolo Bonzini } 258e7c033c3SPaolo Bonzini 259e7c033c3SPaolo Bonzini uint64_t hbitmap_count(const HBitmap *hb) 260e7c033c3SPaolo Bonzini { 261e7c033c3SPaolo Bonzini return hb->count << hb->granularity; 262e7c033c3SPaolo Bonzini } 263e7c033c3SPaolo Bonzini 264e7c033c3SPaolo Bonzini /* Count the number of set bits between start and end, not accounting for 265e7c033c3SPaolo Bonzini * the granularity. Also an example of how to use hbitmap_iter_next_word. 266e7c033c3SPaolo Bonzini */ 267e7c033c3SPaolo Bonzini static uint64_t hb_count_between(HBitmap *hb, uint64_t start, uint64_t last) 268e7c033c3SPaolo Bonzini { 269e7c033c3SPaolo Bonzini HBitmapIter hbi; 270e7c033c3SPaolo Bonzini uint64_t count = 0; 271e7c033c3SPaolo Bonzini uint64_t end = last + 1; 272e7c033c3SPaolo Bonzini unsigned long cur; 273e7c033c3SPaolo Bonzini size_t pos; 274e7c033c3SPaolo Bonzini 275e7c033c3SPaolo Bonzini hbitmap_iter_init(&hbi, hb, start << hb->granularity); 276e7c033c3SPaolo Bonzini for (;;) { 277e7c033c3SPaolo Bonzini pos = hbitmap_iter_next_word(&hbi, &cur); 278e7c033c3SPaolo Bonzini if (pos >= (end >> BITS_PER_LEVEL)) { 279e7c033c3SPaolo Bonzini break; 280e7c033c3SPaolo Bonzini } 281591b320aSPeter Maydell count += ctpopl(cur); 282e7c033c3SPaolo Bonzini } 283e7c033c3SPaolo Bonzini 284e7c033c3SPaolo Bonzini if (pos == (end >> BITS_PER_LEVEL)) { 285e7c033c3SPaolo Bonzini /* Drop bits representing the END-th and subsequent items. */ 286e7c033c3SPaolo Bonzini int bit = end & (BITS_PER_LONG - 1); 287e7c033c3SPaolo Bonzini cur &= (1UL << bit) - 1; 288591b320aSPeter Maydell count += ctpopl(cur); 289e7c033c3SPaolo Bonzini } 290e7c033c3SPaolo Bonzini 291e7c033c3SPaolo Bonzini return count; 292e7c033c3SPaolo Bonzini } 293e7c033c3SPaolo Bonzini 294e7c033c3SPaolo Bonzini /* Setting starts at the last layer and propagates up if an element 29507ac4cdbSFam Zheng * changes. 296e7c033c3SPaolo Bonzini */ 297e7c033c3SPaolo Bonzini static inline bool hb_set_elem(unsigned long *elem, uint64_t start, uint64_t last) 298e7c033c3SPaolo Bonzini { 299e7c033c3SPaolo Bonzini unsigned long mask; 30007ac4cdbSFam Zheng unsigned long old; 301e7c033c3SPaolo Bonzini 302e7c033c3SPaolo Bonzini assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL)); 303e7c033c3SPaolo Bonzini assert(start <= last); 304e7c033c3SPaolo Bonzini 305e7c033c3SPaolo Bonzini mask = 2UL << (last & (BITS_PER_LONG - 1)); 306e7c033c3SPaolo Bonzini mask -= 1UL << (start & (BITS_PER_LONG - 1)); 30707ac4cdbSFam Zheng old = *elem; 308e7c033c3SPaolo Bonzini *elem |= mask; 30907ac4cdbSFam Zheng return old != *elem; 310e7c033c3SPaolo Bonzini } 311e7c033c3SPaolo Bonzini 31207ac4cdbSFam Zheng /* The recursive workhorse (the depth is limited to HBITMAP_LEVELS)... 31307ac4cdbSFam Zheng * Returns true if at least one bit is changed. */ 31407ac4cdbSFam Zheng static bool hb_set_between(HBitmap *hb, int level, uint64_t start, 31507ac4cdbSFam Zheng uint64_t last) 316e7c033c3SPaolo Bonzini { 317e7c033c3SPaolo Bonzini size_t pos = start >> BITS_PER_LEVEL; 318e7c033c3SPaolo Bonzini size_t lastpos = last >> BITS_PER_LEVEL; 319e7c033c3SPaolo Bonzini bool changed = false; 320e7c033c3SPaolo Bonzini size_t i; 321e7c033c3SPaolo Bonzini 322e7c033c3SPaolo Bonzini i = pos; 323e7c033c3SPaolo Bonzini if (i < lastpos) { 324e7c033c3SPaolo Bonzini uint64_t next = (start | (BITS_PER_LONG - 1)) + 1; 325e7c033c3SPaolo Bonzini changed |= hb_set_elem(&hb->levels[level][i], start, next - 1); 326e7c033c3SPaolo Bonzini for (;;) { 327e7c033c3SPaolo Bonzini start = next; 328e7c033c3SPaolo Bonzini next += BITS_PER_LONG; 329e7c033c3SPaolo Bonzini if (++i == lastpos) { 330e7c033c3SPaolo Bonzini break; 331e7c033c3SPaolo Bonzini } 332e7c033c3SPaolo Bonzini changed |= (hb->levels[level][i] == 0); 333e7c033c3SPaolo Bonzini hb->levels[level][i] = ~0UL; 334e7c033c3SPaolo Bonzini } 335e7c033c3SPaolo Bonzini } 336e7c033c3SPaolo Bonzini changed |= hb_set_elem(&hb->levels[level][i], start, last); 337e7c033c3SPaolo Bonzini 338e7c033c3SPaolo Bonzini /* If there was any change in this layer, we may have to update 339e7c033c3SPaolo Bonzini * the one above. 340e7c033c3SPaolo Bonzini */ 341e7c033c3SPaolo Bonzini if (level > 0 && changed) { 342e7c033c3SPaolo Bonzini hb_set_between(hb, level - 1, pos, lastpos); 343e7c033c3SPaolo Bonzini } 34407ac4cdbSFam Zheng return changed; 345e7c033c3SPaolo Bonzini } 346e7c033c3SPaolo Bonzini 347e7c033c3SPaolo Bonzini void hbitmap_set(HBitmap *hb, uint64_t start, uint64_t count) 348e7c033c3SPaolo Bonzini { 349e7c033c3SPaolo Bonzini /* Compute range in the last layer. */ 35007ac4cdbSFam Zheng uint64_t first, n; 351e7c033c3SPaolo Bonzini uint64_t last = start + count - 1; 352e7c033c3SPaolo Bonzini 353e7c033c3SPaolo Bonzini trace_hbitmap_set(hb, start, count, 354e7c033c3SPaolo Bonzini start >> hb->granularity, last >> hb->granularity); 355e7c033c3SPaolo Bonzini 35607ac4cdbSFam Zheng first = start >> hb->granularity; 357e7c033c3SPaolo Bonzini last >>= hb->granularity; 3580e321191SVladimir Sementsov-Ogievskiy assert(last < hb->size); 35907ac4cdbSFam Zheng n = last - first + 1; 360e7c033c3SPaolo Bonzini 36107ac4cdbSFam Zheng hb->count += n - hb_count_between(hb, first, last); 36207ac4cdbSFam Zheng if (hb_set_between(hb, HBITMAP_LEVELS - 1, first, last) && 36307ac4cdbSFam Zheng hb->meta) { 36407ac4cdbSFam Zheng hbitmap_set(hb->meta, start, count); 36507ac4cdbSFam Zheng } 366e7c033c3SPaolo Bonzini } 367e7c033c3SPaolo Bonzini 368e7c033c3SPaolo Bonzini /* Resetting works the other way round: propagate up if the new 369e7c033c3SPaolo Bonzini * value is zero. 370e7c033c3SPaolo Bonzini */ 371e7c033c3SPaolo Bonzini static inline bool hb_reset_elem(unsigned long *elem, uint64_t start, uint64_t last) 372e7c033c3SPaolo Bonzini { 373e7c033c3SPaolo Bonzini unsigned long mask; 374e7c033c3SPaolo Bonzini bool blanked; 375e7c033c3SPaolo Bonzini 376e7c033c3SPaolo Bonzini assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL)); 377e7c033c3SPaolo Bonzini assert(start <= last); 378e7c033c3SPaolo Bonzini 379e7c033c3SPaolo Bonzini mask = 2UL << (last & (BITS_PER_LONG - 1)); 380e7c033c3SPaolo Bonzini mask -= 1UL << (start & (BITS_PER_LONG - 1)); 381e7c033c3SPaolo Bonzini blanked = *elem != 0 && ((*elem & ~mask) == 0); 382e7c033c3SPaolo Bonzini *elem &= ~mask; 383e7c033c3SPaolo Bonzini return blanked; 384e7c033c3SPaolo Bonzini } 385e7c033c3SPaolo Bonzini 38607ac4cdbSFam Zheng /* The recursive workhorse (the depth is limited to HBITMAP_LEVELS)... 38707ac4cdbSFam Zheng * Returns true if at least one bit is changed. */ 38807ac4cdbSFam Zheng static bool hb_reset_between(HBitmap *hb, int level, uint64_t start, 38907ac4cdbSFam Zheng uint64_t last) 390e7c033c3SPaolo Bonzini { 391e7c033c3SPaolo Bonzini size_t pos = start >> BITS_PER_LEVEL; 392e7c033c3SPaolo Bonzini size_t lastpos = last >> BITS_PER_LEVEL; 393e7c033c3SPaolo Bonzini bool changed = false; 394e7c033c3SPaolo Bonzini size_t i; 395e7c033c3SPaolo Bonzini 396e7c033c3SPaolo Bonzini i = pos; 397e7c033c3SPaolo Bonzini if (i < lastpos) { 398e7c033c3SPaolo Bonzini uint64_t next = (start | (BITS_PER_LONG - 1)) + 1; 399e7c033c3SPaolo Bonzini 400e7c033c3SPaolo Bonzini /* Here we need a more complex test than when setting bits. Even if 401e7c033c3SPaolo Bonzini * something was changed, we must not blank bits in the upper level 402e7c033c3SPaolo Bonzini * unless the lower-level word became entirely zero. So, remove pos 403e7c033c3SPaolo Bonzini * from the upper-level range if bits remain set. 404e7c033c3SPaolo Bonzini */ 405e7c033c3SPaolo Bonzini if (hb_reset_elem(&hb->levels[level][i], start, next - 1)) { 406e7c033c3SPaolo Bonzini changed = true; 407e7c033c3SPaolo Bonzini } else { 408e7c033c3SPaolo Bonzini pos++; 409e7c033c3SPaolo Bonzini } 410e7c033c3SPaolo Bonzini 411e7c033c3SPaolo Bonzini for (;;) { 412e7c033c3SPaolo Bonzini start = next; 413e7c033c3SPaolo Bonzini next += BITS_PER_LONG; 414e7c033c3SPaolo Bonzini if (++i == lastpos) { 415e7c033c3SPaolo Bonzini break; 416e7c033c3SPaolo Bonzini } 417e7c033c3SPaolo Bonzini changed |= (hb->levels[level][i] != 0); 418e7c033c3SPaolo Bonzini hb->levels[level][i] = 0UL; 419e7c033c3SPaolo Bonzini } 420e7c033c3SPaolo Bonzini } 421e7c033c3SPaolo Bonzini 422e7c033c3SPaolo Bonzini /* Same as above, this time for lastpos. */ 423e7c033c3SPaolo Bonzini if (hb_reset_elem(&hb->levels[level][i], start, last)) { 424e7c033c3SPaolo Bonzini changed = true; 425e7c033c3SPaolo Bonzini } else { 426e7c033c3SPaolo Bonzini lastpos--; 427e7c033c3SPaolo Bonzini } 428e7c033c3SPaolo Bonzini 429e7c033c3SPaolo Bonzini if (level > 0 && changed) { 430e7c033c3SPaolo Bonzini hb_reset_between(hb, level - 1, pos, lastpos); 431e7c033c3SPaolo Bonzini } 43207ac4cdbSFam Zheng 43307ac4cdbSFam Zheng return changed; 43407ac4cdbSFam Zheng 435e7c033c3SPaolo Bonzini } 436e7c033c3SPaolo Bonzini 437e7c033c3SPaolo Bonzini void hbitmap_reset(HBitmap *hb, uint64_t start, uint64_t count) 438e7c033c3SPaolo Bonzini { 439e7c033c3SPaolo Bonzini /* Compute range in the last layer. */ 44007ac4cdbSFam Zheng uint64_t first; 441e7c033c3SPaolo Bonzini uint64_t last = start + count - 1; 442e7c033c3SPaolo Bonzini 443e7c033c3SPaolo Bonzini trace_hbitmap_reset(hb, start, count, 444e7c033c3SPaolo Bonzini start >> hb->granularity, last >> hb->granularity); 445e7c033c3SPaolo Bonzini 44607ac4cdbSFam Zheng first = start >> hb->granularity; 447e7c033c3SPaolo Bonzini last >>= hb->granularity; 4480e321191SVladimir Sementsov-Ogievskiy assert(last < hb->size); 449e7c033c3SPaolo Bonzini 45007ac4cdbSFam Zheng hb->count -= hb_count_between(hb, first, last); 45107ac4cdbSFam Zheng if (hb_reset_between(hb, HBITMAP_LEVELS - 1, first, last) && 45207ac4cdbSFam Zheng hb->meta) { 45307ac4cdbSFam Zheng hbitmap_set(hb->meta, start, count); 45407ac4cdbSFam Zheng } 455e7c033c3SPaolo Bonzini } 456e7c033c3SPaolo Bonzini 457c6a8c328SWen Congyang void hbitmap_reset_all(HBitmap *hb) 458c6a8c328SWen Congyang { 459c6a8c328SWen Congyang unsigned int i; 460c6a8c328SWen Congyang 461c6a8c328SWen Congyang /* Same as hbitmap_alloc() except for memset() instead of malloc() */ 462c6a8c328SWen Congyang for (i = HBITMAP_LEVELS; --i >= 1; ) { 463c6a8c328SWen Congyang memset(hb->levels[i], 0, hb->sizes[i] * sizeof(unsigned long)); 464c6a8c328SWen Congyang } 465c6a8c328SWen Congyang 466c6a8c328SWen Congyang hb->levels[0][0] = 1UL << (BITS_PER_LONG - 1); 467c6a8c328SWen Congyang hb->count = 0; 468c6a8c328SWen Congyang } 469c6a8c328SWen Congyang 47020a579deSMax Reitz bool hbitmap_is_serializable(const HBitmap *hb) 47120a579deSMax Reitz { 47220a579deSMax Reitz /* Every serialized chunk must be aligned to 64 bits so that endianness 47320a579deSMax Reitz * requirements can be fulfilled on both 64 bit and 32 bit hosts. 474ecbfa281SEric Blake * We have hbitmap_serialization_align() which converts this 47520a579deSMax Reitz * alignment requirement from bitmap bits to items covered (e.g. sectors). 47620a579deSMax Reitz * That value is: 47720a579deSMax Reitz * 64 << hb->granularity 47820a579deSMax Reitz * Since this value must not exceed UINT64_MAX, hb->granularity must be 47920a579deSMax Reitz * less than 58 (== 64 - 6, where 6 is ld(64), i.e. 1 << 6 == 64). 48020a579deSMax Reitz * 481ecbfa281SEric Blake * In order for hbitmap_serialization_align() to always return a 48220a579deSMax Reitz * meaningful value, bitmaps that are to be serialized must have a 48320a579deSMax Reitz * granularity of less than 58. */ 48420a579deSMax Reitz 48520a579deSMax Reitz return hb->granularity < 58; 48620a579deSMax Reitz } 48720a579deSMax Reitz 488e7c033c3SPaolo Bonzini bool hbitmap_get(const HBitmap *hb, uint64_t item) 489e7c033c3SPaolo Bonzini { 490e7c033c3SPaolo Bonzini /* Compute position and bit in the last layer. */ 491e7c033c3SPaolo Bonzini uint64_t pos = item >> hb->granularity; 492e7c033c3SPaolo Bonzini unsigned long bit = 1UL << (pos & (BITS_PER_LONG - 1)); 4930e321191SVladimir Sementsov-Ogievskiy assert(pos < hb->size); 494e7c033c3SPaolo Bonzini 495e7c033c3SPaolo Bonzini return (hb->levels[HBITMAP_LEVELS - 1][pos >> BITS_PER_LEVEL] & bit) != 0; 496e7c033c3SPaolo Bonzini } 497e7c033c3SPaolo Bonzini 498ecbfa281SEric Blake uint64_t hbitmap_serialization_align(const HBitmap *hb) 4998258888eSVladimir Sementsov-Ogievskiy { 50020a579deSMax Reitz assert(hbitmap_is_serializable(hb)); 5016725f887SMax Reitz 5028258888eSVladimir Sementsov-Ogievskiy /* Require at least 64 bit granularity to be safe on both 64 bit and 32 bit 5038258888eSVladimir Sementsov-Ogievskiy * hosts. */ 5046725f887SMax Reitz return UINT64_C(64) << hb->granularity; 5058258888eSVladimir Sementsov-Ogievskiy } 5068258888eSVladimir Sementsov-Ogievskiy 5078258888eSVladimir Sementsov-Ogievskiy /* Start should be aligned to serialization granularity, chunk size should be 5088258888eSVladimir Sementsov-Ogievskiy * aligned to serialization granularity too, except for last chunk. 5098258888eSVladimir Sementsov-Ogievskiy */ 5108258888eSVladimir Sementsov-Ogievskiy static void serialization_chunk(const HBitmap *hb, 5118258888eSVladimir Sementsov-Ogievskiy uint64_t start, uint64_t count, 5128258888eSVladimir Sementsov-Ogievskiy unsigned long **first_el, uint64_t *el_count) 5138258888eSVladimir Sementsov-Ogievskiy { 5148258888eSVladimir Sementsov-Ogievskiy uint64_t last = start + count - 1; 515ecbfa281SEric Blake uint64_t gran = hbitmap_serialization_align(hb); 5168258888eSVladimir Sementsov-Ogievskiy 5178258888eSVladimir Sementsov-Ogievskiy assert((start & (gran - 1)) == 0); 5188258888eSVladimir Sementsov-Ogievskiy assert((last >> hb->granularity) < hb->size); 5198258888eSVladimir Sementsov-Ogievskiy if ((last >> hb->granularity) != hb->size - 1) { 5208258888eSVladimir Sementsov-Ogievskiy assert((count & (gran - 1)) == 0); 5218258888eSVladimir Sementsov-Ogievskiy } 5228258888eSVladimir Sementsov-Ogievskiy 5238258888eSVladimir Sementsov-Ogievskiy start = (start >> hb->granularity) >> BITS_PER_LEVEL; 5248258888eSVladimir Sementsov-Ogievskiy last = (last >> hb->granularity) >> BITS_PER_LEVEL; 5258258888eSVladimir Sementsov-Ogievskiy 5268258888eSVladimir Sementsov-Ogievskiy *first_el = &hb->levels[HBITMAP_LEVELS - 1][start]; 5278258888eSVladimir Sementsov-Ogievskiy *el_count = last - start + 1; 5288258888eSVladimir Sementsov-Ogievskiy } 5298258888eSVladimir Sementsov-Ogievskiy 5308258888eSVladimir Sementsov-Ogievskiy uint64_t hbitmap_serialization_size(const HBitmap *hb, 5318258888eSVladimir Sementsov-Ogievskiy uint64_t start, uint64_t count) 5328258888eSVladimir Sementsov-Ogievskiy { 5338258888eSVladimir Sementsov-Ogievskiy uint64_t el_count; 5348258888eSVladimir Sementsov-Ogievskiy unsigned long *cur; 5358258888eSVladimir Sementsov-Ogievskiy 5368258888eSVladimir Sementsov-Ogievskiy if (!count) { 5378258888eSVladimir Sementsov-Ogievskiy return 0; 5388258888eSVladimir Sementsov-Ogievskiy } 5398258888eSVladimir Sementsov-Ogievskiy serialization_chunk(hb, start, count, &cur, &el_count); 5408258888eSVladimir Sementsov-Ogievskiy 5418258888eSVladimir Sementsov-Ogievskiy return el_count * sizeof(unsigned long); 5428258888eSVladimir Sementsov-Ogievskiy } 5438258888eSVladimir Sementsov-Ogievskiy 5448258888eSVladimir Sementsov-Ogievskiy void hbitmap_serialize_part(const HBitmap *hb, uint8_t *buf, 5458258888eSVladimir Sementsov-Ogievskiy uint64_t start, uint64_t count) 5468258888eSVladimir Sementsov-Ogievskiy { 5478258888eSVladimir Sementsov-Ogievskiy uint64_t el_count; 5488258888eSVladimir Sementsov-Ogievskiy unsigned long *cur, *end; 5498258888eSVladimir Sementsov-Ogievskiy 5508258888eSVladimir Sementsov-Ogievskiy if (!count) { 5518258888eSVladimir Sementsov-Ogievskiy return; 5528258888eSVladimir Sementsov-Ogievskiy } 5538258888eSVladimir Sementsov-Ogievskiy serialization_chunk(hb, start, count, &cur, &el_count); 5548258888eSVladimir Sementsov-Ogievskiy end = cur + el_count; 5558258888eSVladimir Sementsov-Ogievskiy 5568258888eSVladimir Sementsov-Ogievskiy while (cur != end) { 5578258888eSVladimir Sementsov-Ogievskiy unsigned long el = 5588258888eSVladimir Sementsov-Ogievskiy (BITS_PER_LONG == 32 ? cpu_to_le32(*cur) : cpu_to_le64(*cur)); 5598258888eSVladimir Sementsov-Ogievskiy 5608258888eSVladimir Sementsov-Ogievskiy memcpy(buf, &el, sizeof(el)); 5618258888eSVladimir Sementsov-Ogievskiy buf += sizeof(el); 5628258888eSVladimir Sementsov-Ogievskiy cur++; 5638258888eSVladimir Sementsov-Ogievskiy } 5648258888eSVladimir Sementsov-Ogievskiy } 5658258888eSVladimir Sementsov-Ogievskiy 5668258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_part(HBitmap *hb, uint8_t *buf, 5678258888eSVladimir Sementsov-Ogievskiy uint64_t start, uint64_t count, 5688258888eSVladimir Sementsov-Ogievskiy bool finish) 5698258888eSVladimir Sementsov-Ogievskiy { 5708258888eSVladimir Sementsov-Ogievskiy uint64_t el_count; 5718258888eSVladimir Sementsov-Ogievskiy unsigned long *cur, *end; 5728258888eSVladimir Sementsov-Ogievskiy 5738258888eSVladimir Sementsov-Ogievskiy if (!count) { 5748258888eSVladimir Sementsov-Ogievskiy return; 5758258888eSVladimir Sementsov-Ogievskiy } 5768258888eSVladimir Sementsov-Ogievskiy serialization_chunk(hb, start, count, &cur, &el_count); 5778258888eSVladimir Sementsov-Ogievskiy end = cur + el_count; 5788258888eSVladimir Sementsov-Ogievskiy 5798258888eSVladimir Sementsov-Ogievskiy while (cur != end) { 5808258888eSVladimir Sementsov-Ogievskiy memcpy(cur, buf, sizeof(*cur)); 5818258888eSVladimir Sementsov-Ogievskiy 5828258888eSVladimir Sementsov-Ogievskiy if (BITS_PER_LONG == 32) { 5838258888eSVladimir Sementsov-Ogievskiy le32_to_cpus((uint32_t *)cur); 5848258888eSVladimir Sementsov-Ogievskiy } else { 5858258888eSVladimir Sementsov-Ogievskiy le64_to_cpus((uint64_t *)cur); 5868258888eSVladimir Sementsov-Ogievskiy } 5878258888eSVladimir Sementsov-Ogievskiy 5888258888eSVladimir Sementsov-Ogievskiy buf += sizeof(unsigned long); 5898258888eSVladimir Sementsov-Ogievskiy cur++; 5908258888eSVladimir Sementsov-Ogievskiy } 5918258888eSVladimir Sementsov-Ogievskiy if (finish) { 5928258888eSVladimir Sementsov-Ogievskiy hbitmap_deserialize_finish(hb); 5938258888eSVladimir Sementsov-Ogievskiy } 5948258888eSVladimir Sementsov-Ogievskiy } 5958258888eSVladimir Sementsov-Ogievskiy 5968258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_zeroes(HBitmap *hb, uint64_t start, uint64_t count, 5978258888eSVladimir Sementsov-Ogievskiy bool finish) 5988258888eSVladimir Sementsov-Ogievskiy { 5998258888eSVladimir Sementsov-Ogievskiy uint64_t el_count; 6008258888eSVladimir Sementsov-Ogievskiy unsigned long *first; 6018258888eSVladimir Sementsov-Ogievskiy 6028258888eSVladimir Sementsov-Ogievskiy if (!count) { 6038258888eSVladimir Sementsov-Ogievskiy return; 6048258888eSVladimir Sementsov-Ogievskiy } 6058258888eSVladimir Sementsov-Ogievskiy serialization_chunk(hb, start, count, &first, &el_count); 6068258888eSVladimir Sementsov-Ogievskiy 6078258888eSVladimir Sementsov-Ogievskiy memset(first, 0, el_count * sizeof(unsigned long)); 6088258888eSVladimir Sementsov-Ogievskiy if (finish) { 6098258888eSVladimir Sementsov-Ogievskiy hbitmap_deserialize_finish(hb); 6108258888eSVladimir Sementsov-Ogievskiy } 6118258888eSVladimir Sementsov-Ogievskiy } 6128258888eSVladimir Sementsov-Ogievskiy 6136bdc8b71SVladimir Sementsov-Ogievskiy void hbitmap_deserialize_ones(HBitmap *hb, uint64_t start, uint64_t count, 6146bdc8b71SVladimir Sementsov-Ogievskiy bool finish) 6156bdc8b71SVladimir Sementsov-Ogievskiy { 6166bdc8b71SVladimir Sementsov-Ogievskiy uint64_t el_count; 6176bdc8b71SVladimir Sementsov-Ogievskiy unsigned long *first; 6186bdc8b71SVladimir Sementsov-Ogievskiy 6196bdc8b71SVladimir Sementsov-Ogievskiy if (!count) { 6206bdc8b71SVladimir Sementsov-Ogievskiy return; 6216bdc8b71SVladimir Sementsov-Ogievskiy } 6226bdc8b71SVladimir Sementsov-Ogievskiy serialization_chunk(hb, start, count, &first, &el_count); 6236bdc8b71SVladimir Sementsov-Ogievskiy 6246bdc8b71SVladimir Sementsov-Ogievskiy memset(first, 0xff, el_count * sizeof(unsigned long)); 6256bdc8b71SVladimir Sementsov-Ogievskiy if (finish) { 6266bdc8b71SVladimir Sementsov-Ogievskiy hbitmap_deserialize_finish(hb); 6276bdc8b71SVladimir Sementsov-Ogievskiy } 6286bdc8b71SVladimir Sementsov-Ogievskiy } 6296bdc8b71SVladimir Sementsov-Ogievskiy 6308258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_finish(HBitmap *bitmap) 6318258888eSVladimir Sementsov-Ogievskiy { 6328258888eSVladimir Sementsov-Ogievskiy int64_t i, size, prev_size; 6338258888eSVladimir Sementsov-Ogievskiy int lev; 6348258888eSVladimir Sementsov-Ogievskiy 6358258888eSVladimir Sementsov-Ogievskiy /* restore levels starting from penultimate to zero level, assuming 6368258888eSVladimir Sementsov-Ogievskiy * that the last level is ok */ 6378258888eSVladimir Sementsov-Ogievskiy size = MAX((bitmap->size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1); 6388258888eSVladimir Sementsov-Ogievskiy for (lev = HBITMAP_LEVELS - 1; lev-- > 0; ) { 6398258888eSVladimir Sementsov-Ogievskiy prev_size = size; 6408258888eSVladimir Sementsov-Ogievskiy size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1); 6418258888eSVladimir Sementsov-Ogievskiy memset(bitmap->levels[lev], 0, size * sizeof(unsigned long)); 6428258888eSVladimir Sementsov-Ogievskiy 6438258888eSVladimir Sementsov-Ogievskiy for (i = 0; i < prev_size; ++i) { 6448258888eSVladimir Sementsov-Ogievskiy if (bitmap->levels[lev + 1][i]) { 6458258888eSVladimir Sementsov-Ogievskiy bitmap->levels[lev][i >> BITS_PER_LEVEL] |= 6468258888eSVladimir Sementsov-Ogievskiy 1UL << (i & (BITS_PER_LONG - 1)); 6478258888eSVladimir Sementsov-Ogievskiy } 6488258888eSVladimir Sementsov-Ogievskiy } 6498258888eSVladimir Sementsov-Ogievskiy } 6508258888eSVladimir Sementsov-Ogievskiy 6518258888eSVladimir Sementsov-Ogievskiy bitmap->levels[0][0] |= 1UL << (BITS_PER_LONG - 1); 6523260cdffSLiang Li bitmap->count = hb_count_between(bitmap, 0, bitmap->size - 1); 6538258888eSVladimir Sementsov-Ogievskiy } 6548258888eSVladimir Sementsov-Ogievskiy 655e7c033c3SPaolo Bonzini void hbitmap_free(HBitmap *hb) 656e7c033c3SPaolo Bonzini { 657e7c033c3SPaolo Bonzini unsigned i; 65807ac4cdbSFam Zheng assert(!hb->meta); 659e7c033c3SPaolo Bonzini for (i = HBITMAP_LEVELS; i-- > 0; ) { 660e7c033c3SPaolo Bonzini g_free(hb->levels[i]); 661e7c033c3SPaolo Bonzini } 662e7c033c3SPaolo Bonzini g_free(hb); 663e7c033c3SPaolo Bonzini } 664e7c033c3SPaolo Bonzini 665e7c033c3SPaolo Bonzini HBitmap *hbitmap_alloc(uint64_t size, int granularity) 666e7c033c3SPaolo Bonzini { 667e1cf5582SMarkus Armbruster HBitmap *hb = g_new0(struct HBitmap, 1); 668e7c033c3SPaolo Bonzini unsigned i; 669e7c033c3SPaolo Bonzini 670*76d570dcSVladimir Sementsov-Ogievskiy hb->orig_size = size; 671*76d570dcSVladimir Sementsov-Ogievskiy 672e7c033c3SPaolo Bonzini assert(granularity >= 0 && granularity < 64); 673e7c033c3SPaolo Bonzini size = (size + (1ULL << granularity) - 1) >> granularity; 674e7c033c3SPaolo Bonzini assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE)); 675e7c033c3SPaolo Bonzini 676e7c033c3SPaolo Bonzini hb->size = size; 677e7c033c3SPaolo Bonzini hb->granularity = granularity; 678e7c033c3SPaolo Bonzini for (i = HBITMAP_LEVELS; i-- > 0; ) { 679e7c033c3SPaolo Bonzini size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1); 6808515efbeSJohn Snow hb->sizes[i] = size; 681e1cf5582SMarkus Armbruster hb->levels[i] = g_new0(unsigned long, size); 682e7c033c3SPaolo Bonzini } 683e7c033c3SPaolo Bonzini 684e7c033c3SPaolo Bonzini /* We necessarily have free bits in level 0 due to the definition 685e7c033c3SPaolo Bonzini * of HBITMAP_LEVELS, so use one for a sentinel. This speeds up 686e7c033c3SPaolo Bonzini * hbitmap_iter_skip_words. 687e7c033c3SPaolo Bonzini */ 688e7c033c3SPaolo Bonzini assert(size == 1); 689e7c033c3SPaolo Bonzini hb->levels[0][0] |= 1UL << (BITS_PER_LONG - 1); 690e7c033c3SPaolo Bonzini return hb; 691e7c033c3SPaolo Bonzini } 692be58721dSJohn Snow 693ce1ffea8SJohn Snow void hbitmap_truncate(HBitmap *hb, uint64_t size) 694ce1ffea8SJohn Snow { 695ce1ffea8SJohn Snow bool shrink; 696ce1ffea8SJohn Snow unsigned i; 697ce1ffea8SJohn Snow uint64_t num_elements = size; 698ce1ffea8SJohn Snow uint64_t old; 699ce1ffea8SJohn Snow 700ce1ffea8SJohn Snow /* Size comes in as logical elements, adjust for granularity. */ 701ce1ffea8SJohn Snow size = (size + (1ULL << hb->granularity) - 1) >> hb->granularity; 702ce1ffea8SJohn Snow assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE)); 703ce1ffea8SJohn Snow shrink = size < hb->size; 704ce1ffea8SJohn Snow 705ce1ffea8SJohn Snow /* bit sizes are identical; nothing to do. */ 706ce1ffea8SJohn Snow if (size == hb->size) { 707ce1ffea8SJohn Snow return; 708ce1ffea8SJohn Snow } 709ce1ffea8SJohn Snow 710ce1ffea8SJohn Snow /* If we're losing bits, let's clear those bits before we invalidate all of 711ce1ffea8SJohn Snow * our invariants. This helps keep the bitcount consistent, and will prevent 712ce1ffea8SJohn Snow * us from carrying around garbage bits beyond the end of the map. 713ce1ffea8SJohn Snow */ 714ce1ffea8SJohn Snow if (shrink) { 715ce1ffea8SJohn Snow /* Don't clear partial granularity groups; 716ce1ffea8SJohn Snow * start at the first full one. */ 7176725f887SMax Reitz uint64_t start = ROUND_UP(num_elements, UINT64_C(1) << hb->granularity); 718ce1ffea8SJohn Snow uint64_t fix_count = (hb->size << hb->granularity) - start; 719ce1ffea8SJohn Snow 720ce1ffea8SJohn Snow assert(fix_count); 721ce1ffea8SJohn Snow hbitmap_reset(hb, start, fix_count); 722ce1ffea8SJohn Snow } 723ce1ffea8SJohn Snow 724ce1ffea8SJohn Snow hb->size = size; 725ce1ffea8SJohn Snow for (i = HBITMAP_LEVELS; i-- > 0; ) { 726ce1ffea8SJohn Snow size = MAX(BITS_TO_LONGS(size), 1); 727ce1ffea8SJohn Snow if (hb->sizes[i] == size) { 728ce1ffea8SJohn Snow break; 729ce1ffea8SJohn Snow } 730ce1ffea8SJohn Snow old = hb->sizes[i]; 731ce1ffea8SJohn Snow hb->sizes[i] = size; 732ce1ffea8SJohn Snow hb->levels[i] = g_realloc(hb->levels[i], size * sizeof(unsigned long)); 733ce1ffea8SJohn Snow if (!shrink) { 734ce1ffea8SJohn Snow memset(&hb->levels[i][old], 0x00, 735ce1ffea8SJohn Snow (size - old) * sizeof(*hb->levels[i])); 736ce1ffea8SJohn Snow } 737ce1ffea8SJohn Snow } 73807ac4cdbSFam Zheng if (hb->meta) { 73907ac4cdbSFam Zheng hbitmap_truncate(hb->meta, hb->size << hb->granularity); 74007ac4cdbSFam Zheng } 741ce1ffea8SJohn Snow } 742ce1ffea8SJohn Snow 743fa000f2fSVladimir Sementsov-Ogievskiy bool hbitmap_can_merge(const HBitmap *a, const HBitmap *b) 744fa000f2fSVladimir Sementsov-Ogievskiy { 745fa000f2fSVladimir Sementsov-Ogievskiy return (a->size == b->size) && (a->granularity == b->granularity); 746fa000f2fSVladimir Sementsov-Ogievskiy } 747ce1ffea8SJohn Snow 748be58721dSJohn Snow /** 749be58721dSJohn Snow * Given HBitmaps A and B, let A := A (BITOR) B. 750be58721dSJohn Snow * Bitmap B will not be modified. 751be58721dSJohn Snow * 752be58721dSJohn Snow * @return true if the merge was successful, 753be58721dSJohn Snow * false if it was not attempted. 754be58721dSJohn Snow */ 755fa000f2fSVladimir Sementsov-Ogievskiy bool hbitmap_merge(const HBitmap *a, const HBitmap *b, HBitmap *result) 756be58721dSJohn Snow { 757be58721dSJohn Snow int i; 758be58721dSJohn Snow uint64_t j; 759be58721dSJohn Snow 760fa000f2fSVladimir Sementsov-Ogievskiy if (!hbitmap_can_merge(a, b) || !hbitmap_can_merge(a, result)) { 761be58721dSJohn Snow return false; 762be58721dSJohn Snow } 763fa000f2fSVladimir Sementsov-Ogievskiy assert(hbitmap_can_merge(b, result)); 764be58721dSJohn Snow 765be58721dSJohn Snow if (hbitmap_count(b) == 0) { 766be58721dSJohn Snow return true; 767be58721dSJohn Snow } 768be58721dSJohn Snow 769be58721dSJohn Snow /* This merge is O(size), as BITS_PER_LONG and HBITMAP_LEVELS are constant. 770be58721dSJohn Snow * It may be possible to improve running times for sparsely populated maps 771be58721dSJohn Snow * by using hbitmap_iter_next, but this is suboptimal for dense maps. 772be58721dSJohn Snow */ 773be58721dSJohn Snow for (i = HBITMAP_LEVELS - 1; i >= 0; i--) { 774be58721dSJohn Snow for (j = 0; j < a->sizes[i]; j++) { 775fa000f2fSVladimir Sementsov-Ogievskiy result->levels[i][j] = a->levels[i][j] | b->levels[i][j]; 776be58721dSJohn Snow } 777be58721dSJohn Snow } 778be58721dSJohn Snow 779d1dde714SEric Blake /* Recompute the dirty count */ 780d1dde714SEric Blake result->count = hb_count_between(result, 0, result->size - 1); 781d1dde714SEric Blake 782be58721dSJohn Snow return true; 783be58721dSJohn Snow } 78407ac4cdbSFam Zheng 78507ac4cdbSFam Zheng HBitmap *hbitmap_create_meta(HBitmap *hb, int chunk_size) 78607ac4cdbSFam Zheng { 78707ac4cdbSFam Zheng assert(!(chunk_size & (chunk_size - 1))); 78807ac4cdbSFam Zheng assert(!hb->meta); 78907ac4cdbSFam Zheng hb->meta = hbitmap_alloc(hb->size << hb->granularity, 79007ac4cdbSFam Zheng hb->granularity + ctz32(chunk_size)); 79107ac4cdbSFam Zheng return hb->meta; 79207ac4cdbSFam Zheng } 79307ac4cdbSFam Zheng 79407ac4cdbSFam Zheng void hbitmap_free_meta(HBitmap *hb) 79507ac4cdbSFam Zheng { 79607ac4cdbSFam Zheng assert(hb->meta); 79707ac4cdbSFam Zheng hbitmap_free(hb->meta); 79807ac4cdbSFam Zheng hb->meta = NULL; 79907ac4cdbSFam Zheng } 800a3b52535SVladimir Sementsov-Ogievskiy 801a3b52535SVladimir Sementsov-Ogievskiy char *hbitmap_sha256(const HBitmap *bitmap, Error **errp) 802a3b52535SVladimir Sementsov-Ogievskiy { 803a3b52535SVladimir Sementsov-Ogievskiy size_t size = bitmap->sizes[HBITMAP_LEVELS - 1] * sizeof(unsigned long); 804a3b52535SVladimir Sementsov-Ogievskiy char *data = (char *)bitmap->levels[HBITMAP_LEVELS - 1]; 805a3b52535SVladimir Sementsov-Ogievskiy char *hash = NULL; 806a3b52535SVladimir Sementsov-Ogievskiy qcrypto_hash_digest(QCRYPTO_HASH_ALG_SHA256, data, size, &hash, errp); 807a3b52535SVladimir Sementsov-Ogievskiy 808a3b52535SVladimir Sementsov-Ogievskiy return hash; 809a3b52535SVladimir Sementsov-Ogievskiy } 810