Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Code for working with individual keys, and sorted sets of keys with in a |
| 3 | * btree node |
| 4 | * |
| 5 | * Copyright 2012 Google, Inc. |
| 6 | */ |
| 7 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 8 | #define pr_fmt(fmt) "bcache: %s() " fmt "\n", __func__ |
| 9 | |
| 10 | #include "util.h" |
| 11 | #include "bset.h" |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 12 | |
Kent Overstreet | dc9d98d | 2013-12-17 23:47:33 -0800 | [diff] [blame] | 13 | #include <linux/console.h> |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 14 | #include <linux/random.h> |
Geert Uytterhoeven | cd953ed | 2013-03-27 18:56:28 +0100 | [diff] [blame] | 15 | #include <linux/prefetch.h> |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 16 | |
Kent Overstreet | dc9d98d | 2013-12-17 23:47:33 -0800 | [diff] [blame] | 17 | #ifdef CONFIG_BCACHE_DEBUG |
| 18 | |
| 19 | void bch_dump_bset(struct btree_keys *b, struct bset *i, unsigned set) |
| 20 | { |
| 21 | struct bkey *k, *next; |
| 22 | |
| 23 | for (k = i->start; k < bset_bkey_last(i); k = next) { |
| 24 | next = bkey_next(k); |
| 25 | |
| 26 | printk(KERN_ERR "block %u key %zi/%u: ", set, |
| 27 | (uint64_t *) k - i->d, i->keys); |
| 28 | |
| 29 | if (b->ops->key_dump) |
| 30 | b->ops->key_dump(b, k); |
| 31 | else |
| 32 | printk("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k)); |
| 33 | |
| 34 | if (next < bset_bkey_last(i) && |
| 35 | bkey_cmp(k, b->ops->is_extents ? |
| 36 | &START_KEY(next) : next) > 0) |
| 37 | printk(KERN_ERR "Key skipped backwards\n"); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void bch_dump_bucket(struct btree_keys *b) |
| 42 | { |
| 43 | unsigned i; |
| 44 | |
| 45 | console_lock(); |
| 46 | for (i = 0; i <= b->nsets; i++) |
| 47 | bch_dump_bset(b, b->set[i].data, |
| 48 | bset_sector_offset(b, b->set[i].data)); |
| 49 | console_unlock(); |
| 50 | } |
| 51 | |
| 52 | int __bch_count_data(struct btree_keys *b) |
| 53 | { |
| 54 | unsigned ret = 0; |
| 55 | struct btree_iter iter; |
| 56 | struct bkey *k; |
| 57 | |
| 58 | if (b->ops->is_extents) |
| 59 | for_each_key(b, k, &iter) |
| 60 | ret += KEY_SIZE(k); |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | void __bch_check_keys(struct btree_keys *b, const char *fmt, ...) |
| 65 | { |
| 66 | va_list args; |
| 67 | struct bkey *k, *p = NULL; |
| 68 | struct btree_iter iter; |
| 69 | const char *err; |
| 70 | |
| 71 | for_each_key(b, k, &iter) { |
| 72 | if (b->ops->is_extents) { |
| 73 | err = "Keys out of order"; |
| 74 | if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0) |
| 75 | goto bug; |
| 76 | |
| 77 | if (bch_ptr_invalid(b, k)) |
| 78 | continue; |
| 79 | |
| 80 | err = "Overlapping keys"; |
| 81 | if (p && bkey_cmp(p, &START_KEY(k)) > 0) |
| 82 | goto bug; |
| 83 | } else { |
| 84 | if (bch_ptr_bad(b, k)) |
| 85 | continue; |
| 86 | |
| 87 | err = "Duplicate keys"; |
| 88 | if (p && !bkey_cmp(p, k)) |
| 89 | goto bug; |
| 90 | } |
| 91 | p = k; |
| 92 | } |
| 93 | #if 0 |
| 94 | err = "Key larger than btree node key"; |
| 95 | if (p && bkey_cmp(p, &b->key) > 0) |
| 96 | goto bug; |
| 97 | #endif |
| 98 | return; |
| 99 | bug: |
| 100 | bch_dump_bucket(b); |
| 101 | |
| 102 | va_start(args, fmt); |
| 103 | vprintk(fmt, args); |
| 104 | va_end(args); |
| 105 | |
| 106 | panic("bch_check_keys error: %s:\n", err); |
| 107 | } |
| 108 | |
| 109 | static void bch_btree_iter_next_check(struct btree_iter *iter) |
| 110 | { |
| 111 | struct bkey *k = iter->data->k, *next = bkey_next(k); |
| 112 | |
| 113 | if (next < iter->data->end && |
| 114 | bkey_cmp(k, iter->b->ops->is_extents ? |
| 115 | &START_KEY(next) : next) > 0) { |
| 116 | bch_dump_bucket(iter->b); |
| 117 | panic("Key skipped backwards\n"); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | #else |
| 122 | |
| 123 | static inline void bch_btree_iter_next_check(struct btree_iter *iter) {} |
| 124 | |
| 125 | #endif |
| 126 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 127 | /* Keylists */ |
| 128 | |
Kent Overstreet | 085d2a3 | 2013-11-11 18:20:51 -0800 | [diff] [blame] | 129 | int __bch_keylist_realloc(struct keylist *l, unsigned u64s) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 130 | { |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 131 | size_t oldsize = bch_keylist_nkeys(l); |
Kent Overstreet | 085d2a3 | 2013-11-11 18:20:51 -0800 | [diff] [blame] | 132 | size_t newsize = oldsize + u64s; |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 133 | uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p; |
| 134 | uint64_t *new_keys; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 135 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 136 | newsize = roundup_pow_of_two(newsize); |
| 137 | |
| 138 | if (newsize <= KEYLIST_INLINE || |
| 139 | roundup_pow_of_two(oldsize) == newsize) |
| 140 | return 0; |
| 141 | |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 142 | new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 143 | |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 144 | if (!new_keys) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 145 | return -ENOMEM; |
| 146 | |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 147 | if (!old_keys) |
| 148 | memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 149 | |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 150 | l->keys_p = new_keys; |
| 151 | l->top_p = new_keys + oldsize; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | struct bkey *bch_keylist_pop(struct keylist *l) |
| 157 | { |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 158 | struct bkey *k = l->keys; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 159 | |
| 160 | if (k == l->top) |
| 161 | return NULL; |
| 162 | |
| 163 | while (bkey_next(k) != l->top) |
| 164 | k = bkey_next(k); |
| 165 | |
| 166 | return l->top = k; |
| 167 | } |
| 168 | |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 169 | void bch_keylist_pop_front(struct keylist *l) |
| 170 | { |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 171 | l->top_p -= bkey_u64s(l->keys); |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 172 | |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 173 | memmove(l->keys, |
| 174 | bkey_next(l->keys), |
| 175 | bch_keylist_bytes(l)); |
Kent Overstreet | 26c949f | 2013-09-10 18:41:15 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 178 | /* Key/pointer manipulation */ |
| 179 | |
| 180 | void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src, |
| 181 | unsigned i) |
| 182 | { |
| 183 | BUG_ON(i > KEY_PTRS(src)); |
| 184 | |
| 185 | /* Only copy the header, key, and one pointer. */ |
| 186 | memcpy(dest, src, 2 * sizeof(uint64_t)); |
| 187 | dest->ptr[0] = src->ptr[i]; |
| 188 | SET_KEY_PTRS(dest, 1); |
| 189 | /* We didn't copy the checksum so clear that bit. */ |
| 190 | SET_KEY_CSUM(dest, 0); |
| 191 | } |
| 192 | |
| 193 | bool __bch_cut_front(const struct bkey *where, struct bkey *k) |
| 194 | { |
| 195 | unsigned i, len = 0; |
| 196 | |
| 197 | if (bkey_cmp(where, &START_KEY(k)) <= 0) |
| 198 | return false; |
| 199 | |
| 200 | if (bkey_cmp(where, k) < 0) |
| 201 | len = KEY_OFFSET(k) - KEY_OFFSET(where); |
| 202 | else |
| 203 | bkey_copy_key(k, where); |
| 204 | |
| 205 | for (i = 0; i < KEY_PTRS(k); i++) |
| 206 | SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len); |
| 207 | |
| 208 | BUG_ON(len > KEY_SIZE(k)); |
| 209 | SET_KEY_SIZE(k, len); |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | bool __bch_cut_back(const struct bkey *where, struct bkey *k) |
| 214 | { |
| 215 | unsigned len = 0; |
| 216 | |
| 217 | if (bkey_cmp(where, k) >= 0) |
| 218 | return false; |
| 219 | |
| 220 | BUG_ON(KEY_INODE(where) != KEY_INODE(k)); |
| 221 | |
| 222 | if (bkey_cmp(where, &START_KEY(k)) > 0) |
| 223 | len = KEY_OFFSET(where) - KEY_START(k); |
| 224 | |
| 225 | bkey_copy_key(k, where); |
| 226 | |
| 227 | BUG_ON(len > KEY_SIZE(k)); |
| 228 | SET_KEY_SIZE(k, len); |
| 229 | return true; |
| 230 | } |
| 231 | |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 232 | /* Auxiliary search trees */ |
| 233 | |
| 234 | /* 32 bits total: */ |
| 235 | #define BKEY_MID_BITS 3 |
| 236 | #define BKEY_EXPONENT_BITS 7 |
| 237 | #define BKEY_MANTISSA_BITS (32 - BKEY_MID_BITS - BKEY_EXPONENT_BITS) |
| 238 | #define BKEY_MANTISSA_MASK ((1 << BKEY_MANTISSA_BITS) - 1) |
| 239 | |
| 240 | struct bkey_float { |
| 241 | unsigned exponent:BKEY_EXPONENT_BITS; |
| 242 | unsigned m:BKEY_MID_BITS; |
| 243 | unsigned mantissa:BKEY_MANTISSA_BITS; |
| 244 | } __packed; |
| 245 | |
| 246 | /* |
| 247 | * BSET_CACHELINE was originally intended to match the hardware cacheline size - |
| 248 | * it used to be 64, but I realized the lookup code would touch slightly less |
| 249 | * memory if it was 128. |
| 250 | * |
| 251 | * It definites the number of bytes (in struct bset) per struct bkey_float in |
| 252 | * the auxiliar search tree - when we're done searching the bset_float tree we |
| 253 | * have this many bytes left that we do a linear search over. |
| 254 | * |
| 255 | * Since (after level 5) every level of the bset_tree is on a new cacheline, |
| 256 | * we're touching one fewer cacheline in the bset tree in exchange for one more |
| 257 | * cacheline in the linear search - but the linear search might stop before it |
| 258 | * gets to the second cacheline. |
| 259 | */ |
| 260 | |
| 261 | #define BSET_CACHELINE 128 |
| 262 | |
| 263 | /* Space required for the btree node keys */ |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 264 | static inline size_t btree_keys_bytes(struct btree_keys *b) |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 265 | { |
| 266 | return PAGE_SIZE << b->page_order; |
| 267 | } |
| 268 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 269 | static inline size_t btree_keys_cachelines(struct btree_keys *b) |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 270 | { |
| 271 | return btree_keys_bytes(b) / BSET_CACHELINE; |
| 272 | } |
| 273 | |
| 274 | /* Space required for the auxiliary search trees */ |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 275 | static inline size_t bset_tree_bytes(struct btree_keys *b) |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 276 | { |
| 277 | return btree_keys_cachelines(b) * sizeof(struct bkey_float); |
| 278 | } |
| 279 | |
| 280 | /* Space required for the prev pointers */ |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 281 | static inline size_t bset_prev_bytes(struct btree_keys *b) |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 282 | { |
| 283 | return btree_keys_cachelines(b) * sizeof(uint8_t); |
| 284 | } |
| 285 | |
| 286 | /* Memory allocation */ |
| 287 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 288 | void bch_btree_keys_free(struct btree_keys *b) |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 289 | { |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 290 | struct bset_tree *t = b->set; |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 291 | |
| 292 | if (bset_prev_bytes(b) < PAGE_SIZE) |
| 293 | kfree(t->prev); |
| 294 | else |
| 295 | free_pages((unsigned long) t->prev, |
| 296 | get_order(bset_prev_bytes(b))); |
| 297 | |
| 298 | if (bset_tree_bytes(b) < PAGE_SIZE) |
| 299 | kfree(t->tree); |
| 300 | else |
| 301 | free_pages((unsigned long) t->tree, |
| 302 | get_order(bset_tree_bytes(b))); |
| 303 | |
| 304 | free_pages((unsigned long) t->data, b->page_order); |
| 305 | |
| 306 | t->prev = NULL; |
| 307 | t->tree = NULL; |
| 308 | t->data = NULL; |
| 309 | } |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 310 | EXPORT_SYMBOL(bch_btree_keys_free); |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 311 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 312 | int bch_btree_keys_alloc(struct btree_keys *b, unsigned page_order, gfp_t gfp) |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 313 | { |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 314 | struct bset_tree *t = b->set; |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 315 | |
| 316 | BUG_ON(t->data); |
| 317 | |
| 318 | b->page_order = page_order; |
| 319 | |
| 320 | t->data = (void *) __get_free_pages(gfp, b->page_order); |
| 321 | if (!t->data) |
| 322 | goto err; |
| 323 | |
| 324 | t->tree = bset_tree_bytes(b) < PAGE_SIZE |
| 325 | ? kmalloc(bset_tree_bytes(b), gfp) |
| 326 | : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b))); |
| 327 | if (!t->tree) |
| 328 | goto err; |
| 329 | |
| 330 | t->prev = bset_prev_bytes(b) < PAGE_SIZE |
| 331 | ? kmalloc(bset_prev_bytes(b), gfp) |
| 332 | : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b))); |
| 333 | if (!t->prev) |
| 334 | goto err; |
| 335 | |
| 336 | return 0; |
| 337 | err: |
| 338 | bch_btree_keys_free(b); |
| 339 | return -ENOMEM; |
| 340 | } |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 341 | EXPORT_SYMBOL(bch_btree_keys_alloc); |
| 342 | |
| 343 | void bch_btree_keys_init(struct btree_keys *b, const struct btree_keys_ops *ops, |
| 344 | bool *expensive_debug_checks) |
| 345 | { |
| 346 | unsigned i; |
| 347 | |
| 348 | b->ops = ops; |
| 349 | b->expensive_debug_checks = expensive_debug_checks; |
| 350 | b->nsets = 0; |
| 351 | b->last_set_unwritten = 0; |
| 352 | |
| 353 | /* XXX: shouldn't be needed */ |
| 354 | for (i = 0; i < MAX_BSETS; i++) |
| 355 | b->set[i].size = 0; |
| 356 | /* |
| 357 | * Second loop starts at 1 because b->keys[0]->data is the memory we |
| 358 | * allocated |
| 359 | */ |
| 360 | for (i = 1; i < MAX_BSETS; i++) |
| 361 | b->set[i].data = NULL; |
| 362 | } |
| 363 | EXPORT_SYMBOL(bch_btree_keys_init); |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 364 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 365 | /* Binary tree stuff for auxiliary search trees */ |
| 366 | |
| 367 | static unsigned inorder_next(unsigned j, unsigned size) |
| 368 | { |
| 369 | if (j * 2 + 1 < size) { |
| 370 | j = j * 2 + 1; |
| 371 | |
| 372 | while (j * 2 < size) |
| 373 | j *= 2; |
| 374 | } else |
| 375 | j >>= ffz(j) + 1; |
| 376 | |
| 377 | return j; |
| 378 | } |
| 379 | |
| 380 | static unsigned inorder_prev(unsigned j, unsigned size) |
| 381 | { |
| 382 | if (j * 2 < size) { |
| 383 | j = j * 2; |
| 384 | |
| 385 | while (j * 2 + 1 < size) |
| 386 | j = j * 2 + 1; |
| 387 | } else |
| 388 | j >>= ffs(j); |
| 389 | |
| 390 | return j; |
| 391 | } |
| 392 | |
| 393 | /* I have no idea why this code works... and I'm the one who wrote it |
| 394 | * |
| 395 | * However, I do know what it does: |
| 396 | * Given a binary tree constructed in an array (i.e. how you normally implement |
| 397 | * a heap), it converts a node in the tree - referenced by array index - to the |
| 398 | * index it would have if you did an inorder traversal. |
| 399 | * |
| 400 | * Also tested for every j, size up to size somewhere around 6 million. |
| 401 | * |
| 402 | * The binary tree starts at array index 1, not 0 |
| 403 | * extra is a function of size: |
| 404 | * extra = (size - rounddown_pow_of_two(size - 1)) << 1; |
| 405 | */ |
| 406 | static unsigned __to_inorder(unsigned j, unsigned size, unsigned extra) |
| 407 | { |
| 408 | unsigned b = fls(j); |
| 409 | unsigned shift = fls(size - 1) - b; |
| 410 | |
| 411 | j ^= 1U << (b - 1); |
| 412 | j <<= 1; |
| 413 | j |= 1; |
| 414 | j <<= shift; |
| 415 | |
| 416 | if (j > extra) |
| 417 | j -= (j - extra) >> 1; |
| 418 | |
| 419 | return j; |
| 420 | } |
| 421 | |
| 422 | static unsigned to_inorder(unsigned j, struct bset_tree *t) |
| 423 | { |
| 424 | return __to_inorder(j, t->size, t->extra); |
| 425 | } |
| 426 | |
| 427 | static unsigned __inorder_to_tree(unsigned j, unsigned size, unsigned extra) |
| 428 | { |
| 429 | unsigned shift; |
| 430 | |
| 431 | if (j > extra) |
| 432 | j += j - extra; |
| 433 | |
| 434 | shift = ffs(j); |
| 435 | |
| 436 | j >>= shift; |
| 437 | j |= roundup_pow_of_two(size) >> shift; |
| 438 | |
| 439 | return j; |
| 440 | } |
| 441 | |
| 442 | static unsigned inorder_to_tree(unsigned j, struct bset_tree *t) |
| 443 | { |
| 444 | return __inorder_to_tree(j, t->size, t->extra); |
| 445 | } |
| 446 | |
| 447 | #if 0 |
| 448 | void inorder_test(void) |
| 449 | { |
| 450 | unsigned long done = 0; |
| 451 | ktime_t start = ktime_get(); |
| 452 | |
| 453 | for (unsigned size = 2; |
| 454 | size < 65536000; |
| 455 | size++) { |
| 456 | unsigned extra = (size - rounddown_pow_of_two(size - 1)) << 1; |
| 457 | unsigned i = 1, j = rounddown_pow_of_two(size - 1); |
| 458 | |
| 459 | if (!(size % 4096)) |
| 460 | printk(KERN_NOTICE "loop %u, %llu per us\n", size, |
| 461 | done / ktime_us_delta(ktime_get(), start)); |
| 462 | |
| 463 | while (1) { |
| 464 | if (__inorder_to_tree(i, size, extra) != j) |
| 465 | panic("size %10u j %10u i %10u", size, j, i); |
| 466 | |
| 467 | if (__to_inorder(j, size, extra) != i) |
| 468 | panic("size %10u j %10u i %10u", size, j, i); |
| 469 | |
| 470 | if (j == rounddown_pow_of_two(size) - 1) |
| 471 | break; |
| 472 | |
| 473 | BUG_ON(inorder_prev(inorder_next(j, size), size) != j); |
| 474 | |
| 475 | j = inorder_next(j, size); |
| 476 | i++; |
| 477 | } |
| 478 | |
| 479 | done += size - 1; |
| 480 | } |
| 481 | } |
| 482 | #endif |
| 483 | |
| 484 | /* |
Phil Viana | 48a7302 | 2013-06-03 09:51:42 -0300 | [diff] [blame] | 485 | * Cacheline/offset <-> bkey pointer arithmetic: |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 486 | * |
| 487 | * t->tree is a binary search tree in an array; each node corresponds to a key |
| 488 | * in one cacheline in t->set (BSET_CACHELINE bytes). |
| 489 | * |
| 490 | * This means we don't have to store the full index of the key that a node in |
| 491 | * the binary tree points to; to_inorder() gives us the cacheline, and then |
| 492 | * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes. |
| 493 | * |
Phil Viana | 48a7302 | 2013-06-03 09:51:42 -0300 | [diff] [blame] | 494 | * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 495 | * make this work. |
| 496 | * |
| 497 | * To construct the bfloat for an arbitrary key we need to know what the key |
| 498 | * immediately preceding it is: we have to check if the two keys differ in the |
| 499 | * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size |
| 500 | * of the previous key so we can walk backwards to it from t->tree[j]'s key. |
| 501 | */ |
| 502 | |
| 503 | static struct bkey *cacheline_to_bkey(struct bset_tree *t, unsigned cacheline, |
| 504 | unsigned offset) |
| 505 | { |
| 506 | return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8; |
| 507 | } |
| 508 | |
| 509 | static unsigned bkey_to_cacheline(struct bset_tree *t, struct bkey *k) |
| 510 | { |
| 511 | return ((void *) k - (void *) t->data) / BSET_CACHELINE; |
| 512 | } |
| 513 | |
| 514 | static unsigned bkey_to_cacheline_offset(struct bkey *k) |
| 515 | { |
| 516 | return ((size_t) k & (BSET_CACHELINE - 1)) / sizeof(uint64_t); |
| 517 | } |
| 518 | |
| 519 | static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned j) |
| 520 | { |
| 521 | return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m); |
| 522 | } |
| 523 | |
| 524 | static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned j) |
| 525 | { |
| 526 | return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]); |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * For the write set - the one we're currently inserting keys into - we don't |
| 531 | * maintain a full search tree, we just keep a simple lookup table in t->prev. |
| 532 | */ |
| 533 | static struct bkey *table_to_bkey(struct bset_tree *t, unsigned cacheline) |
| 534 | { |
| 535 | return cacheline_to_bkey(t, cacheline, t->prev[cacheline]); |
| 536 | } |
| 537 | |
| 538 | static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift) |
| 539 | { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 540 | low >>= shift; |
| 541 | low |= (high << 1) << (63U - shift); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 542 | return low; |
| 543 | } |
| 544 | |
| 545 | static inline unsigned bfloat_mantissa(const struct bkey *k, |
| 546 | struct bkey_float *f) |
| 547 | { |
| 548 | const uint64_t *p = &k->low - (f->exponent >> 6); |
| 549 | return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK; |
| 550 | } |
| 551 | |
| 552 | static void make_bfloat(struct bset_tree *t, unsigned j) |
| 553 | { |
| 554 | struct bkey_float *f = &t->tree[j]; |
| 555 | struct bkey *m = tree_to_bkey(t, j); |
| 556 | struct bkey *p = tree_to_prev_bkey(t, j); |
| 557 | |
| 558 | struct bkey *l = is_power_of_2(j) |
| 559 | ? t->data->start |
| 560 | : tree_to_prev_bkey(t, j >> ffs(j)); |
| 561 | |
| 562 | struct bkey *r = is_power_of_2(j + 1) |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 563 | ? bset_bkey_idx(t->data, t->data->keys - bkey_u64s(&t->end)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 564 | : tree_to_bkey(t, j >> (ffz(j) + 1)); |
| 565 | |
| 566 | BUG_ON(m < l || m > r); |
| 567 | BUG_ON(bkey_next(p) != m); |
| 568 | |
| 569 | if (KEY_INODE(l) != KEY_INODE(r)) |
| 570 | f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64; |
| 571 | else |
| 572 | f->exponent = fls64(r->low ^ l->low); |
| 573 | |
| 574 | f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0); |
| 575 | |
| 576 | /* |
| 577 | * Setting f->exponent = 127 flags this node as failed, and causes the |
| 578 | * lookup code to fall back to comparing against the original key. |
| 579 | */ |
| 580 | |
| 581 | if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f)) |
| 582 | f->mantissa = bfloat_mantissa(m, f) - 1; |
| 583 | else |
| 584 | f->exponent = 127; |
| 585 | } |
| 586 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 587 | static void bset_alloc_tree(struct btree_keys *b, struct bset_tree *t) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 588 | { |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 589 | if (t != b->set) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 590 | unsigned j = roundup(t[-1].size, |
| 591 | 64 / sizeof(struct bkey_float)); |
| 592 | |
| 593 | t->tree = t[-1].tree + j; |
| 594 | t->prev = t[-1].prev + j; |
| 595 | } |
| 596 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 597 | while (t < b->set + MAX_BSETS) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 598 | t++->size = 0; |
| 599 | } |
| 600 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 601 | static void bch_bset_build_unwritten_tree(struct btree_keys *b) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 602 | { |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 603 | struct bset_tree *t = bset_tree_last(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 604 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 605 | BUG_ON(b->last_set_unwritten); |
| 606 | b->last_set_unwritten = 1; |
| 607 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 608 | bset_alloc_tree(b, t); |
| 609 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 610 | if (t->tree != b->set->tree + btree_keys_cachelines(b)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 611 | t->prev[0] = bkey_to_cacheline_offset(t->data->start); |
| 612 | t->size = 1; |
| 613 | } |
| 614 | } |
| 615 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 616 | void bch_bset_init_next(struct btree_keys *b, struct bset *i, uint64_t magic) |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 617 | { |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 618 | if (i != b->set->data) { |
| 619 | b->set[++b->nsets].data = i; |
| 620 | i->seq = b->set->data->seq; |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 621 | } else |
| 622 | get_random_bytes(&i->seq, sizeof(uint64_t)); |
| 623 | |
| 624 | i->magic = magic; |
| 625 | i->version = 0; |
| 626 | i->keys = 0; |
| 627 | |
| 628 | bch_bset_build_unwritten_tree(b); |
| 629 | } |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 630 | EXPORT_SYMBOL(bch_bset_init_next); |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 631 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 632 | void bch_bset_build_written_tree(struct btree_keys *b) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 633 | { |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 634 | struct bset_tree *t = bset_tree_last(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 635 | struct bkey *k = t->data->start; |
| 636 | unsigned j, cacheline = 1; |
| 637 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 638 | b->last_set_unwritten = 0; |
| 639 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 640 | bset_alloc_tree(b, t); |
| 641 | |
| 642 | t->size = min_t(unsigned, |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 643 | bkey_to_cacheline(t, bset_bkey_last(t->data)), |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 644 | b->set->tree + btree_keys_cachelines(b) - t->tree); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 645 | |
| 646 | if (t->size < 2) { |
| 647 | t->size = 0; |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1; |
| 652 | |
| 653 | /* First we figure out where the first key in each cacheline is */ |
| 654 | for (j = inorder_next(0, t->size); |
| 655 | j; |
| 656 | j = inorder_next(j, t->size)) { |
| 657 | while (bkey_to_cacheline(t, k) != cacheline) |
| 658 | k = bkey_next(k); |
| 659 | |
| 660 | t->prev[j] = bkey_u64s(k); |
| 661 | k = bkey_next(k); |
| 662 | cacheline++; |
| 663 | t->tree[j].m = bkey_to_cacheline_offset(k); |
| 664 | } |
| 665 | |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 666 | while (bkey_next(k) != bset_bkey_last(t->data)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 667 | k = bkey_next(k); |
| 668 | |
| 669 | t->end = *k; |
| 670 | |
| 671 | /* Then we build the tree */ |
| 672 | for (j = inorder_next(0, t->size); |
| 673 | j; |
| 674 | j = inorder_next(j, t->size)) |
| 675 | make_bfloat(t, j); |
| 676 | } |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 677 | EXPORT_SYMBOL(bch_bset_build_written_tree); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 678 | |
Kent Overstreet | 829a60b | 2013-11-11 17:02:31 -0800 | [diff] [blame^] | 679 | /* Insert */ |
| 680 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 681 | void bch_bset_fix_invalidated_key(struct btree_keys *b, struct bkey *k) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 682 | { |
| 683 | struct bset_tree *t; |
| 684 | unsigned inorder, j = 1; |
| 685 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 686 | for (t = b->set; t <= bset_tree_last(b); t++) |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 687 | if (k < bset_bkey_last(t->data)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 688 | goto found_set; |
| 689 | |
| 690 | BUG(); |
| 691 | found_set: |
| 692 | if (!t->size || !bset_written(b, t)) |
| 693 | return; |
| 694 | |
| 695 | inorder = bkey_to_cacheline(t, k); |
| 696 | |
| 697 | if (k == t->data->start) |
| 698 | goto fix_left; |
| 699 | |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 700 | if (bkey_next(k) == bset_bkey_last(t->data)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 701 | t->end = *k; |
| 702 | goto fix_right; |
| 703 | } |
| 704 | |
| 705 | j = inorder_to_tree(inorder, t); |
| 706 | |
| 707 | if (j && |
| 708 | j < t->size && |
| 709 | k == tree_to_bkey(t, j)) |
| 710 | fix_left: do { |
| 711 | make_bfloat(t, j); |
| 712 | j = j * 2; |
| 713 | } while (j < t->size); |
| 714 | |
| 715 | j = inorder_to_tree(inorder + 1, t); |
| 716 | |
| 717 | if (j && |
| 718 | j < t->size && |
| 719 | k == tree_to_prev_bkey(t, j)) |
| 720 | fix_right: do { |
| 721 | make_bfloat(t, j); |
| 722 | j = j * 2 + 1; |
| 723 | } while (j < t->size); |
| 724 | } |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 725 | EXPORT_SYMBOL(bch_bset_fix_invalidated_key); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 726 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 727 | static void bch_bset_fix_lookup_table(struct btree_keys *b, |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 728 | struct bset_tree *t, |
| 729 | struct bkey *k) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 730 | { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 731 | unsigned shift = bkey_u64s(k); |
| 732 | unsigned j = bkey_to_cacheline(t, k); |
| 733 | |
| 734 | /* We're getting called from btree_split() or btree_gc, just bail out */ |
| 735 | if (!t->size) |
| 736 | return; |
| 737 | |
| 738 | /* k is the key we just inserted; we need to find the entry in the |
| 739 | * lookup table for the first key that is strictly greater than k: |
| 740 | * it's either k's cacheline or the next one |
| 741 | */ |
| 742 | if (j < t->size && |
| 743 | table_to_bkey(t, j) <= k) |
| 744 | j++; |
| 745 | |
| 746 | /* Adjust all the lookup table entries, and find a new key for any that |
| 747 | * have gotten too big |
| 748 | */ |
| 749 | for (; j < t->size; j++) { |
| 750 | t->prev[j] += shift; |
| 751 | |
| 752 | if (t->prev[j] > 7) { |
| 753 | k = table_to_bkey(t, j - 1); |
| 754 | |
| 755 | while (k < cacheline_to_bkey(t, j, 0)) |
| 756 | k = bkey_next(k); |
| 757 | |
| 758 | t->prev[j] = bkey_to_cacheline_offset(k); |
| 759 | } |
| 760 | } |
| 761 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 762 | if (t->size == b->set->tree + btree_keys_cachelines(b) - t->tree) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 763 | return; |
| 764 | |
| 765 | /* Possibly add a new entry to the end of the lookup table */ |
| 766 | |
| 767 | for (k = table_to_bkey(t, t->size - 1); |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 768 | k != bset_bkey_last(t->data); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 769 | k = bkey_next(k)) |
| 770 | if (t->size == bkey_to_cacheline(t, k)) { |
| 771 | t->prev[t->size] = bkey_to_cacheline_offset(k); |
| 772 | t->size++; |
| 773 | } |
| 774 | } |
| 775 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 776 | void bch_bset_insert(struct btree_keys *b, struct bkey *where, |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 777 | struct bkey *insert) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 778 | { |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 779 | struct bset_tree *t = bset_tree_last(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 780 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 781 | BUG_ON(!b->last_set_unwritten); |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 782 | BUG_ON(bset_byte_offset(b, t->data) + |
| 783 | __set_bytes(t->data, t->data->keys + bkey_u64s(insert)) > |
| 784 | PAGE_SIZE << b->page_order); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 785 | |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 786 | memmove((uint64_t *) where + bkey_u64s(insert), |
| 787 | where, |
| 788 | (void *) bset_bkey_last(t->data) - (void *) where); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 789 | |
Kent Overstreet | ee81128 | 2013-12-17 23:49:49 -0800 | [diff] [blame] | 790 | t->data->keys += bkey_u64s(insert); |
| 791 | bkey_copy(where, insert); |
| 792 | bch_bset_fix_lookup_table(b, t, where); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 793 | } |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 794 | EXPORT_SYMBOL(bch_bset_insert); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 795 | |
Kent Overstreet | 829a60b | 2013-11-11 17:02:31 -0800 | [diff] [blame^] | 796 | unsigned bch_btree_insert_key(struct btree_keys *b, struct bkey *k, |
| 797 | struct bkey *replace_key) |
| 798 | { |
| 799 | unsigned status = BTREE_INSERT_STATUS_NO_INSERT; |
| 800 | struct bset *i = bset_tree_last(b)->data; |
| 801 | struct bkey *m, *prev = NULL; |
| 802 | struct btree_iter iter; |
| 803 | |
| 804 | BUG_ON(b->ops->is_extents && !KEY_SIZE(k)); |
| 805 | |
| 806 | m = bch_btree_iter_init(b, &iter, b->ops->is_extents |
| 807 | ? PRECEDING_KEY(&START_KEY(k)) |
| 808 | : PRECEDING_KEY(k)); |
| 809 | |
| 810 | if (b->ops->insert_fixup(b, k, &iter, replace_key)) |
| 811 | return status; |
| 812 | |
| 813 | status = BTREE_INSERT_STATUS_INSERT; |
| 814 | |
| 815 | while (m != bset_bkey_last(i) && |
| 816 | bkey_cmp(k, b->ops->is_extents ? &START_KEY(m) : m) > 0) |
| 817 | prev = m, m = bkey_next(m); |
| 818 | |
| 819 | /* prev is in the tree, if we merge we're done */ |
| 820 | status = BTREE_INSERT_STATUS_BACK_MERGE; |
| 821 | if (prev && |
| 822 | bch_bkey_try_merge(b, prev, k)) |
| 823 | goto merged; |
| 824 | #if 0 |
| 825 | status = BTREE_INSERT_STATUS_OVERWROTE; |
| 826 | if (m != bset_bkey_last(i) && |
| 827 | KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m)) |
| 828 | goto copy; |
| 829 | #endif |
| 830 | status = BTREE_INSERT_STATUS_FRONT_MERGE; |
| 831 | if (m != bset_bkey_last(i) && |
| 832 | bch_bkey_try_merge(b, k, m)) |
| 833 | goto copy; |
| 834 | |
| 835 | bch_bset_insert(b, m, k); |
| 836 | copy: bkey_copy(m, k); |
| 837 | merged: |
| 838 | return status; |
| 839 | } |
| 840 | EXPORT_SYMBOL(bch_btree_insert_key); |
| 841 | |
| 842 | /* Lookup */ |
| 843 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 844 | struct bset_search_iter { |
| 845 | struct bkey *l, *r; |
| 846 | }; |
| 847 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 848 | static struct bset_search_iter bset_search_write_set(struct bset_tree *t, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 849 | const struct bkey *search) |
| 850 | { |
| 851 | unsigned li = 0, ri = t->size; |
| 852 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 853 | while (li + 1 != ri) { |
| 854 | unsigned m = (li + ri) >> 1; |
| 855 | |
| 856 | if (bkey_cmp(table_to_bkey(t, m), search) > 0) |
| 857 | ri = m; |
| 858 | else |
| 859 | li = m; |
| 860 | } |
| 861 | |
| 862 | return (struct bset_search_iter) { |
| 863 | table_to_bkey(t, li), |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 864 | ri < t->size ? table_to_bkey(t, ri) : bset_bkey_last(t->data) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 865 | }; |
| 866 | } |
| 867 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 868 | static struct bset_search_iter bset_search_tree(struct bset_tree *t, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 869 | const struct bkey *search) |
| 870 | { |
| 871 | struct bkey *l, *r; |
| 872 | struct bkey_float *f; |
| 873 | unsigned inorder, j, n = 1; |
| 874 | |
| 875 | do { |
| 876 | unsigned p = n << 4; |
| 877 | p &= ((int) (p - t->size)) >> 31; |
| 878 | |
| 879 | prefetch(&t->tree[p]); |
| 880 | |
| 881 | j = n; |
| 882 | f = &t->tree[j]; |
| 883 | |
| 884 | /* |
| 885 | * n = (f->mantissa > bfloat_mantissa()) |
| 886 | * ? j * 2 |
| 887 | * : j * 2 + 1; |
| 888 | * |
| 889 | * We need to subtract 1 from f->mantissa for the sign bit trick |
| 890 | * to work - that's done in make_bfloat() |
| 891 | */ |
| 892 | if (likely(f->exponent != 127)) |
| 893 | n = j * 2 + (((unsigned) |
| 894 | (f->mantissa - |
| 895 | bfloat_mantissa(search, f))) >> 31); |
| 896 | else |
| 897 | n = (bkey_cmp(tree_to_bkey(t, j), search) > 0) |
| 898 | ? j * 2 |
| 899 | : j * 2 + 1; |
| 900 | } while (n < t->size); |
| 901 | |
| 902 | inorder = to_inorder(j, t); |
| 903 | |
| 904 | /* |
| 905 | * n would have been the node we recursed to - the low bit tells us if |
| 906 | * we recursed left or recursed right. |
| 907 | */ |
| 908 | if (n & 1) { |
| 909 | l = cacheline_to_bkey(t, inorder, f->m); |
| 910 | |
| 911 | if (++inorder != t->size) { |
| 912 | f = &t->tree[inorder_next(j, t->size)]; |
| 913 | r = cacheline_to_bkey(t, inorder, f->m); |
| 914 | } else |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 915 | r = bset_bkey_last(t->data); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 916 | } else { |
| 917 | r = cacheline_to_bkey(t, inorder, f->m); |
| 918 | |
| 919 | if (--inorder) { |
| 920 | f = &t->tree[inorder_prev(j, t->size)]; |
| 921 | l = cacheline_to_bkey(t, inorder, f->m); |
| 922 | } else |
| 923 | l = t->data->start; |
| 924 | } |
| 925 | |
| 926 | return (struct bset_search_iter) {l, r}; |
| 927 | } |
| 928 | |
Kent Overstreet | c052dd9 | 2013-11-11 17:35:24 -0800 | [diff] [blame] | 929 | struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 930 | const struct bkey *search) |
| 931 | { |
| 932 | struct bset_search_iter i; |
| 933 | |
| 934 | /* |
| 935 | * First, we search for a cacheline, then lastly we do a linear search |
| 936 | * within that cacheline. |
| 937 | * |
| 938 | * To search for the cacheline, there's three different possibilities: |
| 939 | * * The set is too small to have a search tree, so we just do a linear |
| 940 | * search over the whole set. |
| 941 | * * The set is the one we're currently inserting into; keeping a full |
| 942 | * auxiliary search tree up to date would be too expensive, so we |
| 943 | * use a much simpler lookup table to do a binary search - |
| 944 | * bset_search_write_set(). |
| 945 | * * Or we use the auxiliary search tree we constructed earlier - |
| 946 | * bset_search_tree() |
| 947 | */ |
| 948 | |
| 949 | if (unlikely(!t->size)) { |
| 950 | i.l = t->data->start; |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 951 | i.r = bset_bkey_last(t->data); |
Kent Overstreet | c052dd9 | 2013-11-11 17:35:24 -0800 | [diff] [blame] | 952 | } else if (bset_written(b, t)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 953 | /* |
| 954 | * Each node in the auxiliary search tree covers a certain range |
| 955 | * of bits, and keys above and below the set it covers might |
| 956 | * differ outside those bits - so we have to special case the |
| 957 | * start and end - handle that here: |
| 958 | */ |
| 959 | |
| 960 | if (unlikely(bkey_cmp(search, &t->end) >= 0)) |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 961 | return bset_bkey_last(t->data); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 962 | |
| 963 | if (unlikely(bkey_cmp(search, t->data->start) < 0)) |
| 964 | return t->data->start; |
| 965 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 966 | i = bset_search_tree(t, search); |
| 967 | } else { |
Kent Overstreet | c052dd9 | 2013-11-11 17:35:24 -0800 | [diff] [blame] | 968 | BUG_ON(!b->nsets && |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 969 | t->size < bkey_to_cacheline(t, bset_bkey_last(t->data))); |
| 970 | |
| 971 | i = bset_search_write_set(t, search); |
| 972 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 973 | |
Kent Overstreet | c052dd9 | 2013-11-11 17:35:24 -0800 | [diff] [blame] | 974 | if (btree_keys_expensive_checks(b)) { |
| 975 | BUG_ON(bset_written(b, t) && |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 976 | i.l != t->data->start && |
| 977 | bkey_cmp(tree_to_prev_bkey(t, |
| 978 | inorder_to_tree(bkey_to_cacheline(t, i.l), t)), |
| 979 | search) > 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 980 | |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 981 | BUG_ON(i.r != bset_bkey_last(t->data) && |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 982 | bkey_cmp(i.r, search) <= 0); |
| 983 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 984 | |
| 985 | while (likely(i.l != i.r) && |
| 986 | bkey_cmp(i.l, search) <= 0) |
| 987 | i.l = bkey_next(i.l); |
| 988 | |
| 989 | return i.l; |
| 990 | } |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 991 | EXPORT_SYMBOL(__bch_bset_search); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 992 | |
| 993 | /* Btree iterator */ |
| 994 | |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 995 | typedef bool (btree_iter_cmp_fn)(struct btree_iter_set, |
| 996 | struct btree_iter_set); |
| 997 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 998 | static inline bool btree_iter_cmp(struct btree_iter_set l, |
| 999 | struct btree_iter_set r) |
| 1000 | { |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1001 | return bkey_cmp(l.k, r.k) > 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | static inline bool btree_iter_end(struct btree_iter *iter) |
| 1005 | { |
| 1006 | return !iter->used; |
| 1007 | } |
| 1008 | |
| 1009 | void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k, |
| 1010 | struct bkey *end) |
| 1011 | { |
| 1012 | if (k != end) |
| 1013 | BUG_ON(!heap_add(iter, |
| 1014 | ((struct btree_iter_set) { k, end }), |
| 1015 | btree_iter_cmp)); |
| 1016 | } |
| 1017 | |
Kent Overstreet | c052dd9 | 2013-11-11 17:35:24 -0800 | [diff] [blame] | 1018 | static struct bkey *__bch_btree_iter_init(struct btree_keys *b, |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1019 | struct btree_iter *iter, |
| 1020 | struct bkey *search, |
| 1021 | struct bset_tree *start) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1022 | { |
| 1023 | struct bkey *ret = NULL; |
| 1024 | iter->size = ARRAY_SIZE(iter->data); |
| 1025 | iter->used = 0; |
| 1026 | |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 1027 | #ifdef CONFIG_BCACHE_DEBUG |
| 1028 | iter->b = b; |
| 1029 | #endif |
| 1030 | |
Kent Overstreet | c052dd9 | 2013-11-11 17:35:24 -0800 | [diff] [blame] | 1031 | for (; start <= bset_tree_last(b); start++) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1032 | ret = bch_bset_search(b, start, search); |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 1033 | bch_btree_iter_push(iter, ret, bset_bkey_last(start->data)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | return ret; |
| 1037 | } |
| 1038 | |
Kent Overstreet | c052dd9 | 2013-11-11 17:35:24 -0800 | [diff] [blame] | 1039 | struct bkey *bch_btree_iter_init(struct btree_keys *b, |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1040 | struct btree_iter *iter, |
| 1041 | struct bkey *search) |
| 1042 | { |
Kent Overstreet | c052dd9 | 2013-11-11 17:35:24 -0800 | [diff] [blame] | 1043 | return __bch_btree_iter_init(b, iter, search, b->set); |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1044 | } |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1045 | EXPORT_SYMBOL(bch_btree_iter_init); |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1046 | |
| 1047 | static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter, |
| 1048 | btree_iter_cmp_fn *cmp) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1049 | { |
| 1050 | struct btree_iter_set unused; |
| 1051 | struct bkey *ret = NULL; |
| 1052 | |
| 1053 | if (!btree_iter_end(iter)) { |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 1054 | bch_btree_iter_next_check(iter); |
| 1055 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1056 | ret = iter->data->k; |
| 1057 | iter->data->k = bkey_next(iter->data->k); |
| 1058 | |
| 1059 | if (iter->data->k > iter->data->end) { |
Kent Overstreet | cc0f4ea | 2013-03-27 12:47:45 -0700 | [diff] [blame] | 1060 | WARN_ONCE(1, "bset was corrupt!\n"); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1061 | iter->data->k = iter->data->end; |
| 1062 | } |
| 1063 | |
| 1064 | if (iter->data->k == iter->data->end) |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1065 | heap_pop(iter, unused, cmp); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1066 | else |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1067 | heap_sift(iter, 0, cmp); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | return ret; |
| 1071 | } |
| 1072 | |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1073 | struct bkey *bch_btree_iter_next(struct btree_iter *iter) |
| 1074 | { |
| 1075 | return __bch_btree_iter_next(iter, btree_iter_cmp); |
| 1076 | |
| 1077 | } |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1078 | EXPORT_SYMBOL(bch_btree_iter_next); |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1079 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1080 | struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter, |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1081 | struct btree_keys *b, ptr_filter_fn fn) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1082 | { |
| 1083 | struct bkey *ret; |
| 1084 | |
| 1085 | do { |
| 1086 | ret = bch_btree_iter_next(iter); |
| 1087 | } while (ret && fn(b, ret)); |
| 1088 | |
| 1089 | return ret; |
| 1090 | } |
| 1091 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1092 | /* Mergesort */ |
| 1093 | |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1094 | void bch_bset_sort_state_free(struct bset_sort_state *state) |
| 1095 | { |
| 1096 | if (state->pool) |
| 1097 | mempool_destroy(state->pool); |
| 1098 | } |
| 1099 | |
| 1100 | int bch_bset_sort_state_init(struct bset_sort_state *state, unsigned page_order) |
| 1101 | { |
| 1102 | spin_lock_init(&state->time.lock); |
| 1103 | |
| 1104 | state->page_order = page_order; |
| 1105 | state->crit_factor = int_sqrt(1 << page_order); |
| 1106 | |
| 1107 | state->pool = mempool_create_page_pool(1, page_order); |
| 1108 | if (!state->pool) |
| 1109 | return -ENOMEM; |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1113 | EXPORT_SYMBOL(bch_bset_sort_state_init); |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1114 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1115 | static void btree_mergesort(struct btree_keys *b, struct bset *out, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1116 | struct btree_iter *iter, |
| 1117 | bool fixup, bool remove_stale) |
| 1118 | { |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1119 | int i; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1120 | struct bkey *k, *last = NULL; |
Kent Overstreet | ef71ec0 | 2013-12-17 17:51:02 -0800 | [diff] [blame] | 1121 | BKEY_PADDED(k) tmp; |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1122 | bool (*bad)(struct btree_keys *, const struct bkey *) = remove_stale |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1123 | ? bch_ptr_bad |
| 1124 | : bch_ptr_invalid; |
| 1125 | |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1126 | /* Heapify the iterator, using our comparison function */ |
| 1127 | for (i = iter->used / 2 - 1; i >= 0; --i) |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 1128 | heap_sift(iter, i, b->ops->sort_cmp); |
Kent Overstreet | 911c961 | 2013-07-28 18:35:09 -0700 | [diff] [blame] | 1129 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1130 | while (!btree_iter_end(iter)) { |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 1131 | if (b->ops->sort_fixup && fixup) |
| 1132 | k = b->ops->sort_fixup(iter, &tmp.k); |
Kent Overstreet | ef71ec0 | 2013-12-17 17:51:02 -0800 | [diff] [blame] | 1133 | else |
| 1134 | k = NULL; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1135 | |
Kent Overstreet | ef71ec0 | 2013-12-17 17:51:02 -0800 | [diff] [blame] | 1136 | if (!k) |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 1137 | k = __bch_btree_iter_next(iter, b->ops->sort_cmp); |
Kent Overstreet | ef71ec0 | 2013-12-17 17:51:02 -0800 | [diff] [blame] | 1138 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1139 | if (bad(b, k)) |
| 1140 | continue; |
| 1141 | |
| 1142 | if (!last) { |
| 1143 | last = out->start; |
| 1144 | bkey_copy(last, k); |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 1145 | } else if (!bch_bkey_try_merge(b, last, k)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1146 | last = bkey_next(last); |
| 1147 | bkey_copy(last, k); |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0; |
| 1152 | |
| 1153 | pr_debug("sorted %i keys", out->keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1156 | static void __btree_sort(struct btree_keys *b, struct btree_iter *iter, |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1157 | unsigned start, unsigned order, bool fixup, |
| 1158 | struct bset_sort_state *state) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1159 | { |
| 1160 | uint64_t start_time; |
Kent Overstreet | 0a45114 | 2013-12-18 00:01:06 -0800 | [diff] [blame] | 1161 | bool used_mempool = false; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1162 | struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOIO, |
| 1163 | order); |
| 1164 | if (!out) { |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1165 | BUG_ON(order > state->page_order); |
| 1166 | |
| 1167 | out = page_address(mempool_alloc(state->pool, GFP_NOIO)); |
Kent Overstreet | 0a45114 | 2013-12-18 00:01:06 -0800 | [diff] [blame] | 1168 | used_mempool = true; |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1169 | order = state->page_order; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | start_time = local_clock(); |
| 1173 | |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1174 | btree_mergesort(b, out, iter, fixup, false); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1175 | b->nsets = start; |
| 1176 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1177 | if (!start && order == b->page_order) { |
| 1178 | /* |
| 1179 | * Our temporary buffer is the same size as the btree node's |
| 1180 | * buffer, we can just swap buffers instead of doing a big |
| 1181 | * memcpy() |
| 1182 | */ |
| 1183 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1184 | out->magic = b->set->data->magic; |
| 1185 | out->seq = b->set->data->seq; |
| 1186 | out->version = b->set->data->version; |
| 1187 | swap(out, b->set->data); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1188 | } else { |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1189 | b->set[start].data->keys = out->keys; |
| 1190 | memcpy(b->set[start].data->start, out->start, |
Kent Overstreet | fafff81 | 2013-12-17 21:56:21 -0800 | [diff] [blame] | 1191 | (void *) bset_bkey_last(out) - (void *) out->start); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Kent Overstreet | 0a45114 | 2013-12-18 00:01:06 -0800 | [diff] [blame] | 1194 | if (used_mempool) |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1195 | mempool_free(virt_to_page(out), state->pool); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1196 | else |
| 1197 | free_pages((unsigned long) out, order); |
| 1198 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1199 | bch_bset_build_written_tree(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1200 | |
Kent Overstreet | 65d22e9 | 2013-07-31 00:03:54 -0700 | [diff] [blame] | 1201 | if (!start) |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1202 | bch_time_stats_update(&state->time, start_time); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1205 | void bch_btree_sort_partial(struct btree_keys *b, unsigned start, |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1206 | struct bset_sort_state *state) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1207 | { |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1208 | size_t order = b->page_order, keys = 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1209 | struct btree_iter iter; |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1210 | int oldsize = bch_count_data(b); |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 1211 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1212 | __bch_btree_iter_init(b, &iter, NULL, &b->set[start]); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1213 | |
| 1214 | if (start) { |
| 1215 | unsigned i; |
| 1216 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1217 | for (i = start; i <= b->nsets; i++) |
| 1218 | keys += b->set[i].data->keys; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1219 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1220 | order = get_order(__set_bytes(b->set->data, keys)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1223 | __btree_sort(b, &iter, start, order, false, state); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1224 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1225 | EBUG_ON(oldsize >= 0 && bch_count_data(b) != oldsize); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1226 | } |
Kent Overstreet | 65d4523 | 2013-12-20 17:22:05 -0800 | [diff] [blame] | 1227 | EXPORT_SYMBOL(bch_btree_sort_partial); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1228 | |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1229 | void bch_btree_sort_and_fix_extents(struct btree_keys *b, |
| 1230 | struct btree_iter *iter, |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1231 | struct bset_sort_state *state) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1232 | { |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1233 | __btree_sort(b, iter, 0, b->page_order, true, state); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1236 | void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new, |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1237 | struct bset_sort_state *state) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1238 | { |
| 1239 | uint64_t start_time = local_clock(); |
| 1240 | |
| 1241 | struct btree_iter iter; |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1242 | bch_btree_iter_init(b, &iter, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1243 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1244 | btree_mergesort(b, new->set->data, &iter, false, true); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1245 | |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1246 | bch_time_stats_update(&state->time, start_time); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1247 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1248 | new->set->size = 0; // XXX: why? |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Kent Overstreet | 6ded34d | 2013-05-11 15:59:37 -0700 | [diff] [blame] | 1251 | #define SORT_CRIT (4096 / sizeof(uint64_t)) |
| 1252 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1253 | void bch_btree_sort_lazy(struct btree_keys *b, struct bset_sort_state *state) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1254 | { |
Kent Overstreet | 6ded34d | 2013-05-11 15:59:37 -0700 | [diff] [blame] | 1255 | unsigned crit = SORT_CRIT; |
| 1256 | int i; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1257 | |
Kent Overstreet | 6ded34d | 2013-05-11 15:59:37 -0700 | [diff] [blame] | 1258 | /* Don't sort if nothing to do */ |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1259 | if (!b->nsets) |
Kent Overstreet | 6ded34d | 2013-05-11 15:59:37 -0700 | [diff] [blame] | 1260 | goto out; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1261 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1262 | for (i = b->nsets - 1; i >= 0; --i) { |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1263 | crit *= state->crit_factor; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1264 | |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1265 | if (b->set[i].data->keys < crit) { |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1266 | bch_btree_sort_partial(b, i, state); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1267 | return; |
| 1268 | } |
| 1269 | } |
| 1270 | |
Kent Overstreet | 6ded34d | 2013-05-11 15:59:37 -0700 | [diff] [blame] | 1271 | /* Sort if we'd overflow */ |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1272 | if (b->nsets + 1 == MAX_BSETS) { |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 1273 | bch_btree_sort(b, state); |
Kent Overstreet | 6ded34d | 2013-05-11 15:59:37 -0700 | [diff] [blame] | 1274 | return; |
| 1275 | } |
| 1276 | |
| 1277 | out: |
Kent Overstreet | 89ebb4a | 2013-11-11 18:38:51 -0800 | [diff] [blame] | 1278 | bch_bset_build_written_tree(b); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1279 | } |
Kent Overstreet | a85e968 | 2013-12-20 17:28:16 -0800 | [diff] [blame] | 1280 | EXPORT_SYMBOL(bch_btree_sort_lazy); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1281 | |
Kent Overstreet | f67342d | 2013-11-11 19:25:55 -0800 | [diff] [blame] | 1282 | void bch_btree_keys_stats(struct btree_keys *b, struct bset_stats *stats) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1283 | { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1284 | unsigned i; |
| 1285 | |
Kent Overstreet | f67342d | 2013-11-11 19:25:55 -0800 | [diff] [blame] | 1286 | for (i = 0; i <= b->nsets; i++) { |
| 1287 | struct bset_tree *t = &b->set[i]; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1288 | size_t bytes = t->data->keys * sizeof(uint64_t); |
| 1289 | size_t j; |
| 1290 | |
Kent Overstreet | f67342d | 2013-11-11 19:25:55 -0800 | [diff] [blame] | 1291 | if (bset_written(b, t)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1292 | stats->sets_written++; |
| 1293 | stats->bytes_written += bytes; |
| 1294 | |
| 1295 | stats->floats += t->size - 1; |
| 1296 | |
| 1297 | for (j = 1; j < t->size; j++) |
| 1298 | if (t->tree[j].exponent == 127) |
| 1299 | stats->failed++; |
| 1300 | } else { |
| 1301 | stats->sets_unwritten++; |
| 1302 | stats->bytes_unwritten += bytes; |
| 1303 | } |
| 1304 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1305 | } |