2 * Code for working with individual keys, and sorted sets of keys with in a
5 * Copyright 2012 Google, Inc.
12 #include <linux/random.h>
13 #include <linux/prefetch.h>
17 int bch_keylist_realloc(struct keylist *l, int nptrs, struct cache_set *c)
19 size_t oldsize = bch_keylist_nkeys(l);
20 size_t newsize = oldsize + 2 + nptrs;
21 uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p;
24 /* The journalling code doesn't handle the case where the keys to insert
25 * is bigger than an empty write: If we just return -ENOMEM here,
26 * bio_insert() and bio_invalidate() will insert the keys created so far
27 * and finish the rest when the keylist is empty.
29 if (newsize * sizeof(uint64_t) > block_bytes(c) - sizeof(struct jset))
32 newsize = roundup_pow_of_two(newsize);
34 if (newsize <= KEYLIST_INLINE ||
35 roundup_pow_of_two(oldsize) == newsize)
38 new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO);
44 memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize);
47 l->top_p = new_keys + oldsize;
52 struct bkey *bch_keylist_pop(struct keylist *l)
54 struct bkey *k = l->keys;
59 while (bkey_next(k) != l->top)
65 void bch_keylist_pop_front(struct keylist *l)
67 l->top_p -= bkey_u64s(l->keys);
71 bch_keylist_bytes(l));
74 /* Pointer validation */
76 static bool __ptr_invalid(struct cache_set *c, const struct bkey *k)
80 for (i = 0; i < KEY_PTRS(k); i++)
81 if (ptr_available(c, k, i)) {
82 struct cache *ca = PTR_CACHE(c, k, i);
83 size_t bucket = PTR_BUCKET_NR(c, k, i);
84 size_t r = bucket_remainder(c, PTR_OFFSET(k, i));
86 if (KEY_SIZE(k) + r > c->sb.bucket_size ||
87 bucket < ca->sb.first_bucket ||
88 bucket >= ca->sb.nbuckets)
95 bool bch_btree_ptr_invalid(struct cache_set *c, const struct bkey *k)
99 if (!KEY_PTRS(k) || !KEY_SIZE(k) || KEY_DIRTY(k))
102 if (__ptr_invalid(c, k))
107 bch_bkey_to_text(buf, sizeof(buf), k);
108 cache_bug(c, "spotted btree ptr %s: %s", buf, bch_ptr_status(c, k));
112 bool bch_extent_ptr_invalid(struct cache_set *c, const struct bkey *k)
119 if (KEY_SIZE(k) > KEY_OFFSET(k))
122 if (__ptr_invalid(c, k))
127 bch_bkey_to_text(buf, sizeof(buf), k);
128 cache_bug(c, "spotted extent %s: %s", buf, bch_ptr_status(c, k));
132 static bool ptr_bad_expensive_checks(struct btree *b, const struct bkey *k,
135 struct bucket *g = PTR_BUCKET(b->c, k, ptr);
138 if (mutex_trylock(&b->c->bucket_lock)) {
141 g->prio != BTREE_PRIO ||
142 (b->c->gc_mark_valid &&
143 GC_MARK(g) != GC_MARK_METADATA))
147 if (g->prio == BTREE_PRIO)
151 b->c->gc_mark_valid &&
152 GC_MARK(g) != GC_MARK_DIRTY)
155 mutex_unlock(&b->c->bucket_lock);
160 mutex_unlock(&b->c->bucket_lock);
161 bch_bkey_to_text(buf, sizeof(buf), k);
163 "inconsistent pointer %s: bucket %zu pin %i prio %i gen %i last_gc %i mark %llu gc_gen %i",
164 buf, PTR_BUCKET_NR(b->c, k, ptr), atomic_read(&g->pin),
165 g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen);
169 bool bch_ptr_bad(struct btree *b, const struct bkey *k)
174 if (!bkey_cmp(k, &ZERO_KEY) ||
176 bch_ptr_invalid(b, k))
179 for (i = 0; i < KEY_PTRS(k); i++) {
180 if (!ptr_available(b->c, k, i))
183 g = PTR_BUCKET(b->c, k, i);
184 stale = ptr_stale(b->c, k, i);
186 btree_bug_on(stale > 96, b,
187 "key too stale: %i, need_gc %u",
188 stale, b->c->need_gc);
190 btree_bug_on(stale && KEY_DIRTY(k) && KEY_SIZE(k),
191 b, "stale dirty pointer");
196 if (expensive_debug_checks(b->c) &&
197 ptr_bad_expensive_checks(b, k, i))
204 /* Key/pointer manipulation */
206 void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
209 BUG_ON(i > KEY_PTRS(src));
211 /* Only copy the header, key, and one pointer. */
212 memcpy(dest, src, 2 * sizeof(uint64_t));
213 dest->ptr[0] = src->ptr[i];
214 SET_KEY_PTRS(dest, 1);
215 /* We didn't copy the checksum so clear that bit. */
216 SET_KEY_CSUM(dest, 0);
219 bool __bch_cut_front(const struct bkey *where, struct bkey *k)
223 if (bkey_cmp(where, &START_KEY(k)) <= 0)
226 if (bkey_cmp(where, k) < 0)
227 len = KEY_OFFSET(k) - KEY_OFFSET(where);
229 bkey_copy_key(k, where);
231 for (i = 0; i < KEY_PTRS(k); i++)
232 SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len);
234 BUG_ON(len > KEY_SIZE(k));
235 SET_KEY_SIZE(k, len);
239 bool __bch_cut_back(const struct bkey *where, struct bkey *k)
243 if (bkey_cmp(where, k) >= 0)
246 BUG_ON(KEY_INODE(where) != KEY_INODE(k));
248 if (bkey_cmp(where, &START_KEY(k)) > 0)
249 len = KEY_OFFSET(where) - KEY_START(k);
251 bkey_copy_key(k, where);
253 BUG_ON(len > KEY_SIZE(k));
254 SET_KEY_SIZE(k, len);
258 static uint64_t merge_chksums(struct bkey *l, struct bkey *r)
260 return (l->ptr[KEY_PTRS(l)] + r->ptr[KEY_PTRS(r)]) &
261 ~((uint64_t)1 << 63);
264 /* Tries to merge l and r: l should be lower than r
265 * Returns true if we were able to merge. If we did merge, l will be the merged
266 * key, r will be untouched.
268 bool bch_bkey_try_merge(struct btree *b, struct bkey *l, struct bkey *r)
272 if (key_merging_disabled(b->c))
275 if (KEY_PTRS(l) != KEY_PTRS(r) ||
276 KEY_DIRTY(l) != KEY_DIRTY(r) ||
277 bkey_cmp(l, &START_KEY(r)))
280 for (i = 0; i < KEY_PTRS(l); i++)
281 if (l->ptr[i] + PTR(0, KEY_SIZE(l), 0) != r->ptr[i] ||
282 PTR_BUCKET_NR(b->c, l, i) != PTR_BUCKET_NR(b->c, r, i))
285 /* Keys with no pointers aren't restricted to one bucket and could
288 if (KEY_SIZE(l) + KEY_SIZE(r) > USHRT_MAX) {
289 SET_KEY_OFFSET(l, KEY_OFFSET(l) + USHRT_MAX - KEY_SIZE(l));
290 SET_KEY_SIZE(l, USHRT_MAX);
298 l->ptr[KEY_PTRS(l)] = merge_chksums(l, r);
303 SET_KEY_OFFSET(l, KEY_OFFSET(l) + KEY_SIZE(r));
304 SET_KEY_SIZE(l, KEY_SIZE(l) + KEY_SIZE(r));
309 /* Binary tree stuff for auxiliary search trees */
311 static unsigned inorder_next(unsigned j, unsigned size)
313 if (j * 2 + 1 < size) {
324 static unsigned inorder_prev(unsigned j, unsigned size)
329 while (j * 2 + 1 < size)
337 /* I have no idea why this code works... and I'm the one who wrote it
339 * However, I do know what it does:
340 * Given a binary tree constructed in an array (i.e. how you normally implement
341 * a heap), it converts a node in the tree - referenced by array index - to the
342 * index it would have if you did an inorder traversal.
344 * Also tested for every j, size up to size somewhere around 6 million.
346 * The binary tree starts at array index 1, not 0
347 * extra is a function of size:
348 * extra = (size - rounddown_pow_of_two(size - 1)) << 1;
350 static unsigned __to_inorder(unsigned j, unsigned size, unsigned extra)
353 unsigned shift = fls(size - 1) - b;
361 j -= (j - extra) >> 1;
366 static unsigned to_inorder(unsigned j, struct bset_tree *t)
368 return __to_inorder(j, t->size, t->extra);
371 static unsigned __inorder_to_tree(unsigned j, unsigned size, unsigned extra)
381 j |= roundup_pow_of_two(size) >> shift;
386 static unsigned inorder_to_tree(unsigned j, struct bset_tree *t)
388 return __inorder_to_tree(j, t->size, t->extra);
392 void inorder_test(void)
394 unsigned long done = 0;
395 ktime_t start = ktime_get();
397 for (unsigned size = 2;
400 unsigned extra = (size - rounddown_pow_of_two(size - 1)) << 1;
401 unsigned i = 1, j = rounddown_pow_of_two(size - 1);
404 printk(KERN_NOTICE "loop %u, %llu per us\n", size,
405 done / ktime_us_delta(ktime_get(), start));
408 if (__inorder_to_tree(i, size, extra) != j)
409 panic("size %10u j %10u i %10u", size, j, i);
411 if (__to_inorder(j, size, extra) != i)
412 panic("size %10u j %10u i %10u", size, j, i);
414 if (j == rounddown_pow_of_two(size) - 1)
417 BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
419 j = inorder_next(j, size);
429 * Cacheline/offset <-> bkey pointer arithmetic:
431 * t->tree is a binary search tree in an array; each node corresponds to a key
432 * in one cacheline in t->set (BSET_CACHELINE bytes).
434 * This means we don't have to store the full index of the key that a node in
435 * the binary tree points to; to_inorder() gives us the cacheline, and then
436 * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
438 * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
441 * To construct the bfloat for an arbitrary key we need to know what the key
442 * immediately preceding it is: we have to check if the two keys differ in the
443 * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
444 * of the previous key so we can walk backwards to it from t->tree[j]'s key.
447 static struct bkey *cacheline_to_bkey(struct bset_tree *t, unsigned cacheline,
450 return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
453 static unsigned bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
455 return ((void *) k - (void *) t->data) / BSET_CACHELINE;
458 static unsigned bkey_to_cacheline_offset(struct bkey *k)
460 return ((size_t) k & (BSET_CACHELINE - 1)) / sizeof(uint64_t);
463 static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned j)
465 return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
468 static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned j)
470 return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
474 * For the write set - the one we're currently inserting keys into - we don't
475 * maintain a full search tree, we just keep a simple lookup table in t->prev.
477 static struct bkey *table_to_bkey(struct bset_tree *t, unsigned cacheline)
479 return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
482 static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
485 low |= (high << 1) << (63U - shift);
489 static inline unsigned bfloat_mantissa(const struct bkey *k,
490 struct bkey_float *f)
492 const uint64_t *p = &k->low - (f->exponent >> 6);
493 return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
496 static void make_bfloat(struct bset_tree *t, unsigned j)
498 struct bkey_float *f = &t->tree[j];
499 struct bkey *m = tree_to_bkey(t, j);
500 struct bkey *p = tree_to_prev_bkey(t, j);
502 struct bkey *l = is_power_of_2(j)
504 : tree_to_prev_bkey(t, j >> ffs(j));
506 struct bkey *r = is_power_of_2(j + 1)
507 ? node(t->data, t->data->keys - bkey_u64s(&t->end))
508 : tree_to_bkey(t, j >> (ffz(j) + 1));
510 BUG_ON(m < l || m > r);
511 BUG_ON(bkey_next(p) != m);
513 if (KEY_INODE(l) != KEY_INODE(r))
514 f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
516 f->exponent = fls64(r->low ^ l->low);
518 f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
521 * Setting f->exponent = 127 flags this node as failed, and causes the
522 * lookup code to fall back to comparing against the original key.
525 if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
526 f->mantissa = bfloat_mantissa(m, f) - 1;
531 static void bset_alloc_tree(struct btree *b, struct bset_tree *t)
534 unsigned j = roundup(t[-1].size,
535 64 / sizeof(struct bkey_float));
537 t->tree = t[-1].tree + j;
538 t->prev = t[-1].prev + j;
541 while (t < b->sets + MAX_BSETS)
545 static void bset_build_unwritten_tree(struct btree *b)
547 struct bset_tree *t = b->sets + b->nsets;
549 bset_alloc_tree(b, t);
551 if (t->tree != b->sets->tree + bset_tree_space(b)) {
552 t->prev[0] = bkey_to_cacheline_offset(t->data->start);
557 static void bset_build_written_tree(struct btree *b)
559 struct bset_tree *t = b->sets + b->nsets;
560 struct bkey *k = t->data->start;
561 unsigned j, cacheline = 1;
563 bset_alloc_tree(b, t);
565 t->size = min_t(unsigned,
566 bkey_to_cacheline(t, end(t->data)),
567 b->sets->tree + bset_tree_space(b) - t->tree);
574 t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
576 /* First we figure out where the first key in each cacheline is */
577 for (j = inorder_next(0, t->size);
579 j = inorder_next(j, t->size)) {
580 while (bkey_to_cacheline(t, k) != cacheline)
583 t->prev[j] = bkey_u64s(k);
586 t->tree[j].m = bkey_to_cacheline_offset(k);
589 while (bkey_next(k) != end(t->data))
594 /* Then we build the tree */
595 for (j = inorder_next(0, t->size);
597 j = inorder_next(j, t->size))
601 void bch_bset_fix_invalidated_key(struct btree *b, struct bkey *k)
604 unsigned inorder, j = 1;
606 for (t = b->sets; t <= &b->sets[b->nsets]; t++)
607 if (k < end(t->data))
612 if (!t->size || !bset_written(b, t))
615 inorder = bkey_to_cacheline(t, k);
617 if (k == t->data->start)
620 if (bkey_next(k) == end(t->data)) {
625 j = inorder_to_tree(inorder, t);
629 k == tree_to_bkey(t, j))
633 } while (j < t->size);
635 j = inorder_to_tree(inorder + 1, t);
639 k == tree_to_prev_bkey(t, j))
643 } while (j < t->size);
646 void bch_bset_fix_lookup_table(struct btree *b, struct bkey *k)
648 struct bset_tree *t = &b->sets[b->nsets];
649 unsigned shift = bkey_u64s(k);
650 unsigned j = bkey_to_cacheline(t, k);
652 /* We're getting called from btree_split() or btree_gc, just bail out */
656 /* k is the key we just inserted; we need to find the entry in the
657 * lookup table for the first key that is strictly greater than k:
658 * it's either k's cacheline or the next one
661 table_to_bkey(t, j) <= k)
664 /* Adjust all the lookup table entries, and find a new key for any that
665 * have gotten too big
667 for (; j < t->size; j++) {
670 if (t->prev[j] > 7) {
671 k = table_to_bkey(t, j - 1);
673 while (k < cacheline_to_bkey(t, j, 0))
676 t->prev[j] = bkey_to_cacheline_offset(k);
680 if (t->size == b->sets->tree + bset_tree_space(b) - t->tree)
683 /* Possibly add a new entry to the end of the lookup table */
685 for (k = table_to_bkey(t, t->size - 1);
688 if (t->size == bkey_to_cacheline(t, k)) {
689 t->prev[t->size] = bkey_to_cacheline_offset(k);
694 void bch_bset_init_next(struct btree *b)
696 struct bset *i = write_block(b);
698 if (i != b->sets[0].data) {
699 b->sets[++b->nsets].data = i;
700 i->seq = b->sets[0].data->seq;
702 get_random_bytes(&i->seq, sizeof(uint64_t));
704 i->magic = bset_magic(&b->c->sb);
708 bset_build_unwritten_tree(b);
711 struct bset_search_iter {
715 static struct bset_search_iter bset_search_write_set(struct btree *b,
717 const struct bkey *search)
719 unsigned li = 0, ri = t->size;
722 t->size < bkey_to_cacheline(t, end(t->data)));
724 while (li + 1 != ri) {
725 unsigned m = (li + ri) >> 1;
727 if (bkey_cmp(table_to_bkey(t, m), search) > 0)
733 return (struct bset_search_iter) {
734 table_to_bkey(t, li),
735 ri < t->size ? table_to_bkey(t, ri) : end(t->data)
739 static struct bset_search_iter bset_search_tree(struct btree *b,
741 const struct bkey *search)
744 struct bkey_float *f;
745 unsigned inorder, j, n = 1;
749 p &= ((int) (p - t->size)) >> 31;
751 prefetch(&t->tree[p]);
757 * n = (f->mantissa > bfloat_mantissa())
761 * We need to subtract 1 from f->mantissa for the sign bit trick
762 * to work - that's done in make_bfloat()
764 if (likely(f->exponent != 127))
765 n = j * 2 + (((unsigned)
767 bfloat_mantissa(search, f))) >> 31);
769 n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
772 } while (n < t->size);
774 inorder = to_inorder(j, t);
777 * n would have been the node we recursed to - the low bit tells us if
778 * we recursed left or recursed right.
781 l = cacheline_to_bkey(t, inorder, f->m);
783 if (++inorder != t->size) {
784 f = &t->tree[inorder_next(j, t->size)];
785 r = cacheline_to_bkey(t, inorder, f->m);
789 r = cacheline_to_bkey(t, inorder, f->m);
792 f = &t->tree[inorder_prev(j, t->size)];
793 l = cacheline_to_bkey(t, inorder, f->m);
798 return (struct bset_search_iter) {l, r};
801 struct bkey *__bch_bset_search(struct btree *b, struct bset_tree *t,
802 const struct bkey *search)
804 struct bset_search_iter i;
807 * First, we search for a cacheline, then lastly we do a linear search
808 * within that cacheline.
810 * To search for the cacheline, there's three different possibilities:
811 * * The set is too small to have a search tree, so we just do a linear
812 * search over the whole set.
813 * * The set is the one we're currently inserting into; keeping a full
814 * auxiliary search tree up to date would be too expensive, so we
815 * use a much simpler lookup table to do a binary search -
816 * bset_search_write_set().
817 * * Or we use the auxiliary search tree we constructed earlier -
821 if (unlikely(!t->size)) {
822 i.l = t->data->start;
824 } else if (bset_written(b, t)) {
826 * Each node in the auxiliary search tree covers a certain range
827 * of bits, and keys above and below the set it covers might
828 * differ outside those bits - so we have to special case the
829 * start and end - handle that here:
832 if (unlikely(bkey_cmp(search, &t->end) >= 0))
835 if (unlikely(bkey_cmp(search, t->data->start) < 0))
836 return t->data->start;
838 i = bset_search_tree(b, t, search);
840 i = bset_search_write_set(b, t, search);
842 if (expensive_debug_checks(b->c)) {
843 BUG_ON(bset_written(b, t) &&
844 i.l != t->data->start &&
845 bkey_cmp(tree_to_prev_bkey(t,
846 inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
849 BUG_ON(i.r != end(t->data) &&
850 bkey_cmp(i.r, search) <= 0);
853 while (likely(i.l != i.r) &&
854 bkey_cmp(i.l, search) <= 0)
855 i.l = bkey_next(i.l);
863 * Returns true if l > r - unless l == r, in which case returns true if l is
866 * Necessary for btree_sort_fixup() - if there are multiple keys that compare
867 * equal in different sets, we have to process them newest to oldest.
869 static inline bool btree_iter_cmp(struct btree_iter_set l,
870 struct btree_iter_set r)
872 int64_t c = bkey_cmp(&START_KEY(l.k), &START_KEY(r.k));
874 return c ? c > 0 : l.k < r.k;
877 static inline bool btree_iter_end(struct btree_iter *iter)
882 void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
886 BUG_ON(!heap_add(iter,
887 ((struct btree_iter_set) { k, end }),
891 struct bkey *__bch_btree_iter_init(struct btree *b, struct btree_iter *iter,
892 struct bkey *search, struct bset_tree *start)
894 struct bkey *ret = NULL;
895 iter->size = ARRAY_SIZE(iter->data);
898 #ifdef CONFIG_BCACHE_DEBUG
902 for (; start <= &b->sets[b->nsets]; start++) {
903 ret = bch_bset_search(b, start, search);
904 bch_btree_iter_push(iter, ret, end(start->data));
910 struct bkey *bch_btree_iter_next(struct btree_iter *iter)
912 struct btree_iter_set unused;
913 struct bkey *ret = NULL;
915 if (!btree_iter_end(iter)) {
916 bch_btree_iter_next_check(iter);
919 iter->data->k = bkey_next(iter->data->k);
921 if (iter->data->k > iter->data->end) {
922 WARN_ONCE(1, "bset was corrupt!\n");
923 iter->data->k = iter->data->end;
926 if (iter->data->k == iter->data->end)
927 heap_pop(iter, unused, btree_iter_cmp);
929 heap_sift(iter, 0, btree_iter_cmp);
935 struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
936 struct btree *b, ptr_filter_fn fn)
941 ret = bch_btree_iter_next(iter);
942 } while (ret && fn(b, ret));
949 static void sort_key_next(struct btree_iter *iter,
950 struct btree_iter_set *i)
952 i->k = bkey_next(i->k);
955 *i = iter->data[--iter->used];
958 static void btree_sort_fixup(struct btree_iter *iter)
960 while (iter->used > 1) {
961 struct btree_iter_set *top = iter->data, *i = top + 1;
963 if (iter->used > 2 &&
964 btree_iter_cmp(i[0], i[1]))
967 if (bkey_cmp(top->k, &START_KEY(i->k)) <= 0)
970 if (!KEY_SIZE(i->k)) {
971 sort_key_next(iter, i);
972 heap_sift(iter, i - top, btree_iter_cmp);
977 if (bkey_cmp(top->k, i->k) >= 0)
978 sort_key_next(iter, i);
980 bch_cut_front(top->k, i->k);
982 heap_sift(iter, i - top, btree_iter_cmp);
984 /* can't happen because of comparison func */
985 BUG_ON(!bkey_cmp(&START_KEY(top->k), &START_KEY(i->k)));
986 bch_cut_back(&START_KEY(i->k), top->k);
991 static void btree_mergesort(struct btree *b, struct bset *out,
992 struct btree_iter *iter,
993 bool fixup, bool remove_stale)
995 struct bkey *k, *last = NULL;
996 bool (*bad)(struct btree *, const struct bkey *) = remove_stale
1000 while (!btree_iter_end(iter)) {
1001 if (fixup && !b->level)
1002 btree_sort_fixup(iter);
1004 k = bch_btree_iter_next(iter);
1011 } else if (b->level ||
1012 !bch_bkey_try_merge(b, last, k)) {
1013 last = bkey_next(last);
1018 out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
1020 pr_debug("sorted %i keys", out->keys);
1023 static void __btree_sort(struct btree *b, struct btree_iter *iter,
1024 unsigned start, unsigned order, bool fixup)
1026 uint64_t start_time;
1027 bool remove_stale = !b->written;
1028 struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOIO,
1031 mutex_lock(&b->c->sort_lock);
1033 order = ilog2(bucket_pages(b->c));
1036 start_time = local_clock();
1038 btree_mergesort(b, out, iter, fixup, remove_stale);
1041 if (!fixup && !start && b->written)
1042 bch_btree_verify(b, out);
1044 if (!start && order == b->page_order) {
1046 * Our temporary buffer is the same size as the btree node's
1047 * buffer, we can just swap buffers instead of doing a big
1051 out->magic = bset_magic(&b->c->sb);
1052 out->seq = b->sets[0].data->seq;
1053 out->version = b->sets[0].data->version;
1054 swap(out, b->sets[0].data);
1056 if (b->c->sort == b->sets[0].data)
1059 b->sets[start].data->keys = out->keys;
1060 memcpy(b->sets[start].data->start, out->start,
1061 (void *) end(out) - (void *) out->start);
1064 if (out == b->c->sort)
1065 mutex_unlock(&b->c->sort_lock);
1067 free_pages((unsigned long) out, order);
1070 bset_build_written_tree(b);
1073 bch_time_stats_update(&b->c->sort_time, start_time);
1076 void bch_btree_sort_partial(struct btree *b, unsigned start)
1078 size_t order = b->page_order, keys = 0;
1079 struct btree_iter iter;
1080 int oldsize = bch_count_data(b);
1082 __bch_btree_iter_init(b, &iter, NULL, &b->sets[start]);
1084 BUG_ON(b->sets[b->nsets].data == write_block(b) &&
1085 (b->sets[b->nsets].size || b->nsets));
1091 for (i = start; i <= b->nsets; i++)
1092 keys += b->sets[i].data->keys;
1094 order = roundup_pow_of_two(__set_bytes(b->sets->data,
1097 order = ilog2(order);
1100 __btree_sort(b, &iter, start, order, false);
1102 EBUG_ON(b->written && oldsize >= 0 && bch_count_data(b) != oldsize);
1105 void bch_btree_sort_and_fix_extents(struct btree *b, struct btree_iter *iter)
1107 BUG_ON(!b->written);
1108 __btree_sort(b, iter, 0, b->page_order, true);
1111 void bch_btree_sort_into(struct btree *b, struct btree *new)
1113 uint64_t start_time = local_clock();
1115 struct btree_iter iter;
1116 bch_btree_iter_init(b, &iter, NULL);
1118 btree_mergesort(b, new->sets->data, &iter, false, true);
1120 bch_time_stats_update(&b->c->sort_time, start_time);
1122 bkey_copy_key(&new->key, &b->key);
1123 new->sets->size = 0;
1126 #define SORT_CRIT (4096 / sizeof(uint64_t))
1128 void bch_btree_sort_lazy(struct btree *b)
1130 unsigned crit = SORT_CRIT;
1133 /* Don't sort if nothing to do */
1137 /* If not a leaf node, always sort */
1143 for (i = b->nsets - 1; i >= 0; --i) {
1144 crit *= b->c->sort_crit_factor;
1146 if (b->sets[i].data->keys < crit) {
1147 bch_btree_sort_partial(b, i);
1152 /* Sort if we'd overflow */
1153 if (b->nsets + 1 == MAX_BSETS) {
1159 bset_build_written_tree(b);
1167 size_t sets_written, sets_unwritten;
1168 size_t bytes_written, bytes_unwritten;
1169 size_t floats, failed;
1172 static int btree_bset_stats(struct btree_op *op, struct btree *b)
1174 struct bset_stats *stats = container_of(op, struct bset_stats, op);
1179 for (i = 0; i <= b->nsets; i++) {
1180 struct bset_tree *t = &b->sets[i];
1181 size_t bytes = t->data->keys * sizeof(uint64_t);
1184 if (bset_written(b, t)) {
1185 stats->sets_written++;
1186 stats->bytes_written += bytes;
1188 stats->floats += t->size - 1;
1190 for (j = 1; j < t->size; j++)
1191 if (t->tree[j].exponent == 127)
1194 stats->sets_unwritten++;
1195 stats->bytes_unwritten += bytes;
1199 return MAP_CONTINUE;
1202 int bch_bset_print_stats(struct cache_set *c, char *buf)
1204 struct bset_stats t;
1207 memset(&t, 0, sizeof(struct bset_stats));
1208 bch_btree_op_init(&t.op, -1);
1210 ret = bch_btree_map_nodes(&t.op, c, &ZERO_KEY, btree_bset_stats);
1214 return snprintf(buf, PAGE_SIZE,
1215 "btree nodes: %zu\n"
1216 "written sets: %zu\n"
1217 "unwritten sets: %zu\n"
1218 "written key bytes: %zu\n"
1219 "unwritten key bytes: %zu\n"
1223 t.sets_written, t.sets_unwritten,
1224 t.bytes_written, t.bytes_unwritten,
1225 t.floats, t.failed);