Lines Matching +full:k +full:- +full:to +full:- +full:j
1 // SPDX-License-Identifier: GPL-2.0
23 struct bkey *k, *next; in bch_dump_bset() local
25 for (k = i->start; k < bset_bkey_last(i); k = next) { in bch_dump_bset()
26 next = bkey_next(k); in bch_dump_bset()
29 (unsigned int) ((u64 *) k - i->d), i->keys); in bch_dump_bset()
31 if (b->ops->key_dump) in bch_dump_bset()
32 b->ops->key_dump(b, k); in bch_dump_bset()
34 pr_cont("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k)); in bch_dump_bset()
37 bkey_cmp(k, b->ops->is_extents ? in bch_dump_bset()
48 for (i = 0; i <= b->nsets; i++) in bch_dump_bucket()
49 bch_dump_bset(b, b->set[i].data, in bch_dump_bucket()
50 bset_sector_offset(b, b->set[i].data)); in bch_dump_bucket()
58 struct bkey *k; in __bch_count_data() local
62 if (b->ops->is_extents) in __bch_count_data()
63 for_each_key(b, k, &iter) in __bch_count_data()
64 ret += KEY_SIZE(k); in __bch_count_data()
71 struct bkey *k, *p = NULL; in __bch_check_keys() local
77 for_each_key(b, k, &iter) { in __bch_check_keys()
78 if (b->ops->is_extents) { in __bch_check_keys()
80 if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0) in __bch_check_keys()
83 if (bch_ptr_invalid(b, k)) in __bch_check_keys()
87 if (p && bkey_cmp(p, &START_KEY(k)) > 0) in __bch_check_keys()
90 if (bch_ptr_bad(b, k)) in __bch_check_keys()
94 if (p && !bkey_cmp(p, k)) in __bch_check_keys()
97 p = k; in __bch_check_keys()
101 if (p && bkey_cmp(p, &b->key) > 0) in __bch_check_keys()
117 struct bkey *k = iter->heap.data->k, *next = bkey_next(k); in bch_btree_iter_next_check() local
119 if (next < iter->heap.data->end && in bch_btree_iter_next_check()
120 bkey_cmp(k, iter->b->ops->is_extents ? in bch_btree_iter_next_check()
122 bch_dump_bucket(iter->b); in bch_btree_iter_next_check()
139 uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p; in __bch_keylist_realloc()
151 return -ENOMEM; in __bch_keylist_realloc()
154 memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize); in __bch_keylist_realloc()
156 l->keys_p = new_keys; in __bch_keylist_realloc()
157 l->top_p = new_keys + oldsize; in __bch_keylist_realloc()
162 /* Pop the top key of keylist by pointing l->top to its previous key */
165 struct bkey *k = l->keys; in bch_keylist_pop() local
167 if (k == l->top) in bch_keylist_pop()
170 while (bkey_next(k) != l->top) in bch_keylist_pop()
171 k = bkey_next(k); in bch_keylist_pop()
173 return l->top = k; in bch_keylist_pop()
176 /* Pop the bottom key of keylist and update l->top_p */
179 l->top_p -= bkey_u64s(l->keys); in bch_keylist_pop_front()
181 memmove(l->keys, in bch_keylist_pop_front()
182 bkey_next(l->keys), in bch_keylist_pop_front()
195 dest->ptr[0] = src->ptr[i]; in bch_bkey_copy_single_ptr()
201 bool __bch_cut_front(const struct bkey *where, struct bkey *k) in __bch_cut_front() argument
205 if (bkey_cmp(where, &START_KEY(k)) <= 0) in __bch_cut_front()
208 if (bkey_cmp(where, k) < 0) in __bch_cut_front()
209 len = KEY_OFFSET(k) - KEY_OFFSET(where); in __bch_cut_front()
211 bkey_copy_key(k, where); in __bch_cut_front()
213 for (i = 0; i < KEY_PTRS(k); i++) in __bch_cut_front()
214 SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len); in __bch_cut_front()
216 BUG_ON(len > KEY_SIZE(k)); in __bch_cut_front()
217 SET_KEY_SIZE(k, len); in __bch_cut_front()
221 bool __bch_cut_back(const struct bkey *where, struct bkey *k) in __bch_cut_back() argument
225 if (bkey_cmp(where, k) >= 0) in __bch_cut_back()
228 BUG_ON(KEY_INODE(where) != KEY_INODE(k)); in __bch_cut_back()
230 if (bkey_cmp(where, &START_KEY(k)) > 0) in __bch_cut_back()
231 len = KEY_OFFSET(where) - KEY_START(k); in __bch_cut_back()
233 bkey_copy_key(k, where); in __bch_cut_back()
235 BUG_ON(len > KEY_SIZE(k)); in __bch_cut_back()
236 SET_KEY_SIZE(k, len); in __bch_cut_back()
245 #define BKEY_MANTISSA_BITS (32 - BKEY_MID_BITS - BKEY_EXPONENT_BITS)
246 #define BKEY_MANTISSA_MASK ((1 << BKEY_MANTISSA_BITS) - 1)
255 * BSET_CACHELINE was originally intended to match the hardware cacheline size -
256 * it used to be 64, but I realized the lookup code would touch slightly less
260 * the auxiliar search tree - when we're done searching the bset_float tree we
265 * cacheline in the linear search - but the linear search might stop before it
266 * gets to the second cacheline.
274 return PAGE_SIZE << b->page_order; in btree_keys_bytes()
298 struct bset_tree *t = b->set; in bch_btree_keys_free()
301 kfree(t->prev); in bch_btree_keys_free()
303 free_pages((unsigned long) t->prev, in bch_btree_keys_free()
307 kfree(t->tree); in bch_btree_keys_free()
309 free_pages((unsigned long) t->tree, in bch_btree_keys_free()
312 free_pages((unsigned long) t->data, b->page_order); in bch_btree_keys_free()
314 t->prev = NULL; in bch_btree_keys_free()
315 t->tree = NULL; in bch_btree_keys_free()
316 t->data = NULL; in bch_btree_keys_free()
323 struct bset_tree *t = b->set; in bch_btree_keys_alloc()
325 BUG_ON(t->data); in bch_btree_keys_alloc()
327 b->page_order = page_order; in bch_btree_keys_alloc()
329 t->data = (void *) __get_free_pages(__GFP_COMP|gfp, b->page_order); in bch_btree_keys_alloc()
330 if (!t->data) in bch_btree_keys_alloc()
333 t->tree = bset_tree_bytes(b) < PAGE_SIZE in bch_btree_keys_alloc()
336 if (!t->tree) in bch_btree_keys_alloc()
339 t->prev = bset_prev_bytes(b) < PAGE_SIZE in bch_btree_keys_alloc()
342 if (!t->prev) in bch_btree_keys_alloc()
348 return -ENOMEM; in bch_btree_keys_alloc()
354 b->ops = ops; in bch_btree_keys_init()
355 b->expensive_debug_checks = expensive_debug_checks; in bch_btree_keys_init()
356 b->nsets = 0; in bch_btree_keys_init()
357 b->last_set_unwritten = 0; in bch_btree_keys_init()
363 * b->set[0].data is allocated in bch_btree_keys_alloc(), so we in bch_btree_keys_init()
364 * don't have to initiate b->set[].size and b->set[].data here in bch_btree_keys_init()
372 * return array index next to j when does in-order traverse
375 static unsigned int inorder_next(unsigned int j, unsigned int size) in inorder_next() argument
377 if (j * 2 + 1 < size) { in inorder_next()
378 j = j * 2 + 1; in inorder_next()
380 while (j * 2 < size) in inorder_next()
381 j *= 2; in inorder_next()
383 j >>= ffz(j) + 1; in inorder_next()
385 return j; in inorder_next()
389 * return array index previous to j when does in-order traverse
392 static unsigned int inorder_prev(unsigned int j, unsigned int size) in inorder_prev() argument
394 if (j * 2 < size) { in inorder_prev()
395 j = j * 2; in inorder_prev()
397 while (j * 2 + 1 < size) in inorder_prev()
398 j = j * 2 + 1; in inorder_prev()
400 j >>= ffs(j); in inorder_prev()
402 return j; in inorder_prev()
410 * a heap), it converts a node in the tree - referenced by array index - to the
413 * Also tested for every j, size up to size somewhere around 6 million.
417 * extra = (size - rounddown_pow_of_two(size - 1)) << 1;
419 static unsigned int __to_inorder(unsigned int j, in __to_inorder() argument
423 unsigned int b = fls(j); in __to_inorder()
424 unsigned int shift = fls(size - 1) - b; in __to_inorder()
426 j ^= 1U << (b - 1); in __to_inorder()
427 j <<= 1; in __to_inorder()
428 j |= 1; in __to_inorder()
429 j <<= shift; in __to_inorder()
431 if (j > extra) in __to_inorder()
432 j -= (j - extra) >> 1; in __to_inorder()
434 return j; in __to_inorder()
438 * Return the cacheline index in bset_tree->data, where j is index
441 static unsigned int to_inorder(unsigned int j, struct bset_tree *t) in to_inorder() argument
443 return __to_inorder(j, t->size, t->extra); in to_inorder()
446 static unsigned int __inorder_to_tree(unsigned int j, in __inorder_to_tree() argument
452 if (j > extra) in __inorder_to_tree()
453 j += j - extra; in __inorder_to_tree()
455 shift = ffs(j); in __inorder_to_tree()
457 j >>= shift; in __inorder_to_tree()
458 j |= roundup_pow_of_two(size) >> shift; in __inorder_to_tree()
460 return j; in __inorder_to_tree()
465 * tree, j is the cacheline index of t->data.
467 static unsigned int inorder_to_tree(unsigned int j, struct bset_tree *t) in inorder_to_tree() argument
469 return __inorder_to_tree(j, t->size, t->extra); in inorder_to_tree()
482 (size - rounddown_pow_of_two(size - 1)) << 1;
483 unsigned int i = 1, j = rounddown_pow_of_two(size - 1);
490 if (__inorder_to_tree(i, size, extra) != j)
491 panic("size %10u j %10u i %10u", size, j, i);
493 if (__to_inorder(j, size, extra) != i)
494 panic("size %10u j %10u i %10u", size, j, i);
496 if (j == rounddown_pow_of_two(size) - 1)
499 BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
501 j = inorder_next(j, size);
505 done += size - 1;
511 * Cacheline/offset <-> bkey pointer arithmetic:
513 * t->tree is a binary search tree in an array; each node corresponds to a key
514 * in one cacheline in t->set (BSET_CACHELINE bytes).
516 * This means we don't have to store the full index of the key that a node in
517 * the binary tree points to; to_inorder() gives us the cacheline, and then
518 * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
520 * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
523 * To construct the bfloat for an arbitrary key we need to know what the key
524 * immediately preceding it is: we have to check if the two keys differ in the
525 * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
526 * of the previous key so we can walk backwards to it from t->tree[j]'s key.
533 return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8; in cacheline_to_bkey()
536 static unsigned int bkey_to_cacheline(struct bset_tree *t, struct bkey *k) in bkey_to_cacheline() argument
538 return ((void *) k - (void *) t->data) / BSET_CACHELINE; in bkey_to_cacheline()
543 struct bkey *k) in bkey_to_cacheline_offset() argument
545 return (u64 *) k - (u64 *) cacheline_to_bkey(t, cacheline, 0); in bkey_to_cacheline_offset()
548 static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned int j) in tree_to_bkey() argument
550 return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m); in tree_to_bkey()
553 static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned int j) in tree_to_prev_bkey() argument
555 return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]); in tree_to_prev_bkey()
559 * For the write set - the one we're currently inserting keys into - we don't
560 * maintain a full search tree, we just keep a simple lookup table in t->prev.
564 return cacheline_to_bkey(t, cacheline, t->prev[cacheline]); in table_to_bkey()
570 low |= (high << 1) << (63U - shift); in shrd128()
576 * If most significant bit of f->exponent is not set, then
577 * - f->exponent >> 6 is 0
578 * - p[0] points to bkey->low
579 * - p[-1] borrows bits from KEY_INODE() of bkey->high
580 * if most isgnificant bits of f->exponent is set, then
581 * - f->exponent >> 6 is 1
582 * - p[0] points to bits from KEY_INODE() of bkey->high
583 * - p[-1] points to other bits from KEY_INODE() of
584 * bkey->high too.
585 * See make_bfloat() to check when most significant bit of f->exponent
588 static inline unsigned int bfloat_mantissa(const struct bkey *k, in bfloat_mantissa() argument
591 const uint64_t *p = &k->low - (f->exponent >> 6); in bfloat_mantissa()
593 return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK; in bfloat_mantissa()
596 static void make_bfloat(struct bset_tree *t, unsigned int j) in make_bfloat() argument
598 struct bkey_float *f = &t->tree[j]; in make_bfloat()
599 struct bkey *m = tree_to_bkey(t, j); in make_bfloat()
600 struct bkey *p = tree_to_prev_bkey(t, j); in make_bfloat()
602 struct bkey *l = is_power_of_2(j) in make_bfloat()
603 ? t->data->start in make_bfloat()
604 : tree_to_prev_bkey(t, j >> ffs(j)); in make_bfloat()
606 struct bkey *r = is_power_of_2(j + 1) in make_bfloat()
607 ? bset_bkey_idx(t->data, t->data->keys - bkey_u64s(&t->end)) in make_bfloat()
608 : tree_to_bkey(t, j >> (ffz(j) + 1)); in make_bfloat()
615 * device), f->exponent records how many least significant bits in make_bfloat()
617 * bits to 1 (by +64). in make_bfloat()
618 * If l and r have same KEY_INODE value, f->exponent records in make_bfloat()
619 * how many different bits in least significant bits of bkey->low. in make_bfloat()
621 * f->exponent is used to calculate bfloat mantissa value. in make_bfloat()
624 f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64; in make_bfloat()
626 f->exponent = fls64(r->low ^ l->low); in make_bfloat()
628 f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0); in make_bfloat()
631 * Setting f->exponent = 127 flags this node as failed, and causes the in make_bfloat()
632 * lookup code to fall back to comparing against the original key. in make_bfloat()
636 f->mantissa = bfloat_mantissa(m, f) - 1; in make_bfloat()
638 f->exponent = 127; in make_bfloat()
643 if (t != b->set) { in bset_alloc_tree()
644 unsigned int j = roundup(t[-1].size, in bset_alloc_tree() local
647 t->tree = t[-1].tree + j; in bset_alloc_tree()
648 t->prev = t[-1].prev + j; in bset_alloc_tree()
651 while (t < b->set + MAX_BSETS) in bset_alloc_tree()
652 t++->size = 0; in bset_alloc_tree()
659 BUG_ON(b->last_set_unwritten); in bch_bset_build_unwritten_tree()
660 b->last_set_unwritten = 1; in bch_bset_build_unwritten_tree()
664 if (t->tree != b->set->tree + btree_keys_cachelines(b)) { in bch_bset_build_unwritten_tree()
665 t->prev[0] = bkey_to_cacheline_offset(t, 0, t->data->start); in bch_bset_build_unwritten_tree()
666 t->size = 1; in bch_bset_build_unwritten_tree()
672 if (i != b->set->data) { in bch_bset_init_next()
673 b->set[++b->nsets].data = i; in bch_bset_init_next()
674 i->seq = b->set->data->seq; in bch_bset_init_next()
676 get_random_bytes(&i->seq, sizeof(uint64_t)); in bch_bset_init_next()
678 i->magic = magic; in bch_bset_init_next()
679 i->version = 0; in bch_bset_init_next()
680 i->keys = 0; in bch_bset_init_next()
686 * Build auxiliary binary tree 'struct bset_tree *t', this tree is used to
687 * accelerate bkey search in a btree node (pointed by bset_tree->data in
690 * bset_tree->data where the searching bkey might be inside. Then a followed
697 struct bkey *prev = NULL, *k = t->data->start; in bch_bset_build_written_tree() local
698 unsigned int j, cacheline = 1; in bch_bset_build_written_tree() local
700 b->last_set_unwritten = 0; in bch_bset_build_written_tree()
704 t->size = min_t(unsigned int, in bch_bset_build_written_tree()
705 bkey_to_cacheline(t, bset_bkey_last(t->data)), in bch_bset_build_written_tree()
706 b->set->tree + btree_keys_cachelines(b) - t->tree); in bch_bset_build_written_tree()
708 if (t->size < 2) { in bch_bset_build_written_tree()
709 t->size = 0; in bch_bset_build_written_tree()
713 t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1; in bch_bset_build_written_tree()
716 for (j = inorder_next(0, t->size); in bch_bset_build_written_tree()
717 j; in bch_bset_build_written_tree()
718 j = inorder_next(j, t->size)) { in bch_bset_build_written_tree()
719 while (bkey_to_cacheline(t, k) < cacheline) { in bch_bset_build_written_tree()
720 prev = k; in bch_bset_build_written_tree()
721 k = bkey_next(k); in bch_bset_build_written_tree()
724 t->prev[j] = bkey_u64s(prev); in bch_bset_build_written_tree()
725 t->tree[j].m = bkey_to_cacheline_offset(t, cacheline++, k); in bch_bset_build_written_tree()
728 while (bkey_next(k) != bset_bkey_last(t->data)) in bch_bset_build_written_tree()
729 k = bkey_next(k); in bch_bset_build_written_tree()
731 t->end = *k; in bch_bset_build_written_tree()
734 for (j = inorder_next(0, t->size); in bch_bset_build_written_tree()
735 j; in bch_bset_build_written_tree()
736 j = inorder_next(j, t->size)) in bch_bset_build_written_tree()
737 make_bfloat(t, j); in bch_bset_build_written_tree()
742 void bch_bset_fix_invalidated_key(struct btree_keys *b, struct bkey *k) in bch_bset_fix_invalidated_key() argument
745 unsigned int inorder, j = 1; in bch_bset_fix_invalidated_key() local
747 for (t = b->set; t <= bset_tree_last(b); t++) in bch_bset_fix_invalidated_key()
748 if (k < bset_bkey_last(t->data)) in bch_bset_fix_invalidated_key()
753 if (!t->size || !bset_written(b, t)) in bch_bset_fix_invalidated_key()
756 inorder = bkey_to_cacheline(t, k); in bch_bset_fix_invalidated_key()
758 if (k == t->data->start) in bch_bset_fix_invalidated_key()
761 if (bkey_next(k) == bset_bkey_last(t->data)) { in bch_bset_fix_invalidated_key()
762 t->end = *k; in bch_bset_fix_invalidated_key()
766 j = inorder_to_tree(inorder, t); in bch_bset_fix_invalidated_key()
768 if (j && in bch_bset_fix_invalidated_key()
769 j < t->size && in bch_bset_fix_invalidated_key()
770 k == tree_to_bkey(t, j)) in bch_bset_fix_invalidated_key()
772 make_bfloat(t, j); in bch_bset_fix_invalidated_key()
773 j = j * 2; in bch_bset_fix_invalidated_key()
774 } while (j < t->size); in bch_bset_fix_invalidated_key()
776 j = inorder_to_tree(inorder + 1, t); in bch_bset_fix_invalidated_key()
778 if (j && in bch_bset_fix_invalidated_key()
779 j < t->size && in bch_bset_fix_invalidated_key()
780 k == tree_to_prev_bkey(t, j)) in bch_bset_fix_invalidated_key()
782 make_bfloat(t, j); in bch_bset_fix_invalidated_key()
783 j = j * 2 + 1; in bch_bset_fix_invalidated_key()
784 } while (j < t->size); in bch_bset_fix_invalidated_key()
789 struct bkey *k) in bch_bset_fix_lookup_table() argument
791 unsigned int shift = bkey_u64s(k); in bch_bset_fix_lookup_table()
792 unsigned int j = bkey_to_cacheline(t, k); in bch_bset_fix_lookup_table() local
795 if (!t->size) in bch_bset_fix_lookup_table()
799 * k is the key we just inserted; we need to find the entry in the in bch_bset_fix_lookup_table()
800 * lookup table for the first key that is strictly greater than k: in bch_bset_fix_lookup_table()
801 * it's either k's cacheline or the next one in bch_bset_fix_lookup_table()
803 while (j < t->size && in bch_bset_fix_lookup_table()
804 table_to_bkey(t, j) <= k) in bch_bset_fix_lookup_table()
805 j++; in bch_bset_fix_lookup_table()
811 for (; j < t->size; j++) { in bch_bset_fix_lookup_table()
812 t->prev[j] += shift; in bch_bset_fix_lookup_table()
814 if (t->prev[j] > 7) { in bch_bset_fix_lookup_table()
815 k = table_to_bkey(t, j - 1); in bch_bset_fix_lookup_table()
817 while (k < cacheline_to_bkey(t, j, 0)) in bch_bset_fix_lookup_table()
818 k = bkey_next(k); in bch_bset_fix_lookup_table()
820 t->prev[j] = bkey_to_cacheline_offset(t, j, k); in bch_bset_fix_lookup_table()
824 if (t->size == b->set->tree + btree_keys_cachelines(b) - t->tree) in bch_bset_fix_lookup_table()
827 /* Possibly add a new entry to the end of the lookup table */ in bch_bset_fix_lookup_table()
829 for (k = table_to_bkey(t, t->size - 1); in bch_bset_fix_lookup_table()
830 k != bset_bkey_last(t->data); in bch_bset_fix_lookup_table()
831 k = bkey_next(k)) in bch_bset_fix_lookup_table()
832 if (t->size == bkey_to_cacheline(t, k)) { in bch_bset_fix_lookup_table()
833 t->prev[t->size] = in bch_bset_fix_lookup_table()
834 bkey_to_cacheline_offset(t, t->size, k); in bch_bset_fix_lookup_table()
835 t->size++; in bch_bset_fix_lookup_table()
840 * Tries to merge l and r: l should be lower than r
841 * Returns true if we were able to merge. If we did merge, l will be the merged
846 if (!b->ops->key_merge) in bch_bkey_try_merge()
858 return b->ops->key_merge(b, l, r); in bch_bkey_try_merge()
866 BUG_ON(!b->last_set_unwritten); in bch_bset_insert()
867 BUG_ON(bset_byte_offset(b, t->data) + in bch_bset_insert()
868 __set_bytes(t->data, t->data->keys + bkey_u64s(insert)) > in bch_bset_insert()
869 PAGE_SIZE << b->page_order); in bch_bset_insert()
873 (void *) bset_bkey_last(t->data) - (void *) where); in bch_bset_insert()
875 t->data->keys += bkey_u64s(insert); in bch_bset_insert()
880 unsigned int bch_btree_insert_key(struct btree_keys *b, struct bkey *k, in bch_btree_insert_key() argument
884 struct bset *i = bset_tree_last(b)->data; in bch_btree_insert_key()
890 BUG_ON(b->ops->is_extents && !KEY_SIZE(k)); in bch_btree_insert_key()
895 * If k has preceding key, preceding_key_p will be set to address in bch_btree_insert_key()
896 * of k's preceding key; otherwise preceding_key_p will be set in bch_btree_insert_key()
897 * to NULL inside preceding_key(). in bch_btree_insert_key()
899 if (b->ops->is_extents) in bch_btree_insert_key()
900 preceding_key(&START_KEY(k), &preceding_key_p); in bch_btree_insert_key()
902 preceding_key(k, &preceding_key_p); in bch_btree_insert_key()
906 if (b->ops->insert_fixup(b, k, &iter, replace_key)) in bch_btree_insert_key()
912 bkey_cmp(k, b->ops->is_extents ? &START_KEY(m) : m) > 0) { in bch_btree_insert_key()
920 bch_bkey_try_merge(b, prev, k)) in bch_btree_insert_key()
925 KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m)) in bch_btree_insert_key()
930 bch_bkey_try_merge(b, k, m)) in bch_btree_insert_key()
933 bch_bset_insert(b, m, k); in bch_btree_insert_key()
934 copy: bkey_copy(m, k); in bch_btree_insert_key()
948 unsigned int li = 0, ri = t->size; in bset_search_write_set()
961 ri < t->size ? table_to_bkey(t, ri) : bset_bkey_last(t->data) in bset_search_write_set()
970 unsigned int inorder, j, n = 1; in bset_search_tree() local
975 if (p < t->size) in bset_search_tree()
976 prefetch(&t->tree[p]); in bset_search_tree()
978 j = n; in bset_search_tree()
979 f = &t->tree[j]; in bset_search_tree()
981 if (likely(f->exponent != 127)) { in bset_search_tree()
982 if (f->mantissa >= bfloat_mantissa(search, f)) in bset_search_tree()
983 n = j * 2; in bset_search_tree()
985 n = j * 2 + 1; in bset_search_tree()
987 if (bkey_cmp(tree_to_bkey(t, j), search) > 0) in bset_search_tree()
988 n = j * 2; in bset_search_tree()
990 n = j * 2 + 1; in bset_search_tree()
992 } while (n < t->size); in bset_search_tree()
994 inorder = to_inorder(j, t); in bset_search_tree()
997 * n would have been the node we recursed to - the low bit tells us if in bset_search_tree()
1001 l = cacheline_to_bkey(t, inorder, f->m); in bset_search_tree()
1003 if (++inorder != t->size) { in bset_search_tree()
1004 f = &t->tree[inorder_next(j, t->size)]; in bset_search_tree()
1005 r = cacheline_to_bkey(t, inorder, f->m); in bset_search_tree()
1007 r = bset_bkey_last(t->data); in bset_search_tree()
1009 r = cacheline_to_bkey(t, inorder, f->m); in bset_search_tree()
1011 if (--inorder) { in bset_search_tree()
1012 f = &t->tree[inorder_prev(j, t->size)]; in bset_search_tree()
1013 l = cacheline_to_bkey(t, inorder, f->m); in bset_search_tree()
1015 l = t->data->start; in bset_search_tree()
1030 * To search for the cacheline, there's three different possibilities: in __bch_bset_search()
1031 * * The set is too small to have a search tree, so we just do a linear in __bch_bset_search()
1034 * auxiliary search tree up to date would be too expensive, so we in __bch_bset_search()
1035 * use a much simpler lookup table to do a binary search - in __bch_bset_search()
1037 * * Or we use the auxiliary search tree we constructed earlier - in __bch_bset_search()
1041 if (unlikely(!t->size)) { in __bch_bset_search()
1042 i.l = t->data->start; in __bch_bset_search()
1043 i.r = bset_bkey_last(t->data); in __bch_bset_search()
1048 * differ outside those bits - so we have to special case the in __bch_bset_search()
1049 * start and end - handle that here: in __bch_bset_search()
1052 if (unlikely(bkey_cmp(search, &t->end) >= 0)) in __bch_bset_search()
1053 return bset_bkey_last(t->data); in __bch_bset_search()
1055 if (unlikely(bkey_cmp(search, t->data->start) < 0)) in __bch_bset_search()
1056 return t->data->start; in __bch_bset_search()
1060 BUG_ON(!b->nsets && in __bch_bset_search()
1061 t->size < bkey_to_cacheline(t, bset_bkey_last(t->data))); in __bch_bset_search()
1068 i.l != t->data->start && in __bch_bset_search()
1073 BUG_ON(i.r != bset_bkey_last(t->data) && in __bch_bset_search()
1093 return bkey_cmp(_l->k, _r->k) <= 0; in new_btree_iter_cmp()
1106 return !iter->heap.nr; in btree_iter_end()
1109 void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k, in bch_btree_iter_push() argument
1117 if (k != end) in bch_btree_iter_push()
1118 BUG_ON(!min_heap_push(&iter->heap, in bch_btree_iter_push()
1119 &((struct btree_iter_set) { k, end }), in bch_btree_iter_push()
1131 iter->heap.size = ARRAY_SIZE(iter->heap.preallocated); in __bch_btree_iter_init()
1132 iter->heap.nr = 0; in __bch_btree_iter_init()
1135 iter->b = b; in __bch_btree_iter_init()
1140 bch_btree_iter_push(iter, ret, bset_bkey_last(start->data)); in __bch_btree_iter_init()
1150 return __bch_btree_iter_init(b, iter, search, b->set); in bch_btree_iter_init()
1166 ret = iter->heap.data->k; in __bch_btree_iter_next()
1167 iter->heap.data->k = bkey_next(iter->heap.data->k); in __bch_btree_iter_next()
1169 if (iter->heap.data->k > iter->heap.data->end) { in __bch_btree_iter_next()
1171 iter->heap.data->k = iter->heap.data->end; in __bch_btree_iter_next()
1174 if (iter->heap.data->k == iter->heap.data->end) { in __bch_btree_iter_next()
1175 if (iter->heap.nr) { in __bch_btree_iter_next()
1176 b = min_heap_peek(&iter->heap)[0]; in __bch_btree_iter_next()
1177 min_heap_pop(&iter->heap, &callbacks, NULL); in __bch_btree_iter_next()
1181 min_heap_sift_down(&iter->heap, 0, &callbacks, NULL); in __bch_btree_iter_next()
1209 mempool_exit(&state->pool); in bch_bset_sort_state_free()
1215 spin_lock_init(&state->time.lock); in bch_bset_sort_state_init()
1217 state->page_order = page_order; in bch_bset_sort_state_init()
1218 state->crit_factor = int_sqrt(1 << page_order); in bch_bset_sort_state_init()
1220 return mempool_init_page_pool(&state->pool, 1, page_order); in bch_bset_sort_state_init()
1227 struct bkey *k, *last = NULL; in btree_mergesort() local
1228 BKEY_PADDED(k) tmp; in btree_mergesort()
1233 .less = b->ops->sort_cmp, in btree_mergesort()
1238 min_heapify_all(&iter->heap, &callbacks, NULL); in btree_mergesort()
1241 if (b->ops->sort_fixup && fixup) in btree_mergesort()
1242 k = b->ops->sort_fixup(iter, &tmp.k); in btree_mergesort()
1244 k = NULL; in btree_mergesort()
1246 if (!k) in btree_mergesort()
1247 k = __bch_btree_iter_next(iter, b->ops->sort_cmp); in btree_mergesort()
1249 if (bad(b, k)) in btree_mergesort()
1253 last = out->start; in btree_mergesort()
1254 bkey_copy(last, k); in btree_mergesort()
1255 } else if (!bch_bkey_try_merge(b, last, k)) { in btree_mergesort()
1257 bkey_copy(last, k); in btree_mergesort()
1261 out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0; in btree_mergesort()
1263 pr_debug("sorted %i keys\n", out->keys); in btree_mergesort()
1277 BUG_ON(order > state->page_order); in __btree_sort()
1279 outp = mempool_alloc(&state->pool, GFP_NOIO); in __btree_sort()
1282 order = state->page_order; in __btree_sort()
1288 b->nsets = start; in __btree_sort()
1290 if (!start && order == b->page_order) { in __btree_sort()
1297 * still be swapped here. Because state->pool is a page mempool in __btree_sort()
1302 out->magic = b->set->data->magic; in __btree_sort()
1303 out->seq = b->set->data->seq; in __btree_sort()
1304 out->version = b->set->data->version; in __btree_sort()
1305 swap(out, b->set->data); in __btree_sort()
1307 b->set[start].data->keys = out->keys; in __btree_sort()
1308 memcpy(b->set[start].data->start, out->start, in __btree_sort()
1309 (void *) bset_bkey_last(out) - (void *) out->start); in __btree_sort()
1313 mempool_free(virt_to_page(out), &state->pool); in __btree_sort()
1320 bch_time_stats_update(&state->time, start_time); in __btree_sort()
1326 size_t order = b->page_order, keys = 0; in bch_btree_sort_partial()
1331 __bch_btree_iter_init(b, &iter, NULL, &b->set[start]); in bch_btree_sort_partial()
1336 for (i = start; i <= b->nsets; i++) in bch_btree_sort_partial()
1337 keys += b->set[i].data->keys; in bch_btree_sort_partial()
1339 order = get_order(__set_bytes(b->set->data, keys)); in bch_btree_sort_partial()
1351 __btree_sort(b, iter, 0, b->page_order, true, state); in bch_btree_sort_and_fix_extents()
1364 btree_mergesort(b, new->set->data, &iter, false, true); in bch_btree_sort_into()
1366 bch_time_stats_update(&state->time, start_time); in bch_btree_sort_into()
1368 new->set->size = 0; // XXX: why? in bch_btree_sort_into()
1378 /* Don't sort if nothing to do */ in bch_btree_sort_lazy()
1379 if (!b->nsets) in bch_btree_sort_lazy()
1382 for (i = b->nsets - 1; i >= 0; --i) { in bch_btree_sort_lazy()
1383 crit *= state->crit_factor; in bch_btree_sort_lazy()
1385 if (b->set[i].data->keys < crit) { in bch_btree_sort_lazy()
1392 if (b->nsets + 1 == MAX_BSETS) { in bch_btree_sort_lazy()
1405 for (i = 0; i <= b->nsets; i++) { in bch_btree_keys_stats()
1406 struct bset_tree *t = &b->set[i]; in bch_btree_keys_stats()
1407 size_t bytes = t->data->keys * sizeof(uint64_t); in bch_btree_keys_stats()
1408 size_t j; in bch_btree_keys_stats() local
1411 stats->sets_written++; in bch_btree_keys_stats()
1412 stats->bytes_written += bytes; in bch_btree_keys_stats()
1414 stats->floats += t->size - 1; in bch_btree_keys_stats()
1416 for (j = 1; j < t->size; j++) in bch_btree_keys_stats()
1417 if (t->tree[j].exponent == 127) in bch_btree_keys_stats()
1418 stats->failed++; in bch_btree_keys_stats()
1420 stats->sets_unwritten++; in bch_btree_keys_stats()
1421 stats->bytes_unwritten += bytes; in bch_btree_keys_stats()