blob: 9e3a53d87de02bb631d36605ae1e959cbce0a113 [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
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
8#include "bcache.h"
9#include "btree.h"
10#include "debug.h"
11
12#include <linux/random.h>
Geert Uytterhoevencd953ed2013-03-27 18:56:28 +010013#include <linux/prefetch.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070014
15/* Keylists */
16
Kent Overstreet085d2a32013-11-11 18:20:51 -080017int __bch_keylist_realloc(struct keylist *l, unsigned u64s)
Kent Overstreetcafe5632013-03-23 16:11:31 -070018{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070019 size_t oldsize = bch_keylist_nkeys(l);
Kent Overstreet085d2a32013-11-11 18:20:51 -080020 size_t newsize = oldsize + u64s;
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070021 uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p;
22 uint64_t *new_keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -070023
Kent Overstreetcafe5632013-03-23 16:11:31 -070024 newsize = roundup_pow_of_two(newsize);
25
26 if (newsize <= KEYLIST_INLINE ||
27 roundup_pow_of_two(oldsize) == newsize)
28 return 0;
29
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070030 new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO);
Kent Overstreetcafe5632013-03-23 16:11:31 -070031
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070032 if (!new_keys)
Kent Overstreetcafe5632013-03-23 16:11:31 -070033 return -ENOMEM;
34
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070035 if (!old_keys)
36 memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize);
Kent Overstreetcafe5632013-03-23 16:11:31 -070037
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070038 l->keys_p = new_keys;
39 l->top_p = new_keys + oldsize;
Kent Overstreetcafe5632013-03-23 16:11:31 -070040
41 return 0;
42}
43
44struct bkey *bch_keylist_pop(struct keylist *l)
45{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070046 struct bkey *k = l->keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -070047
48 if (k == l->top)
49 return NULL;
50
51 while (bkey_next(k) != l->top)
52 k = bkey_next(k);
53
54 return l->top = k;
55}
56
Kent Overstreet26c949f2013-09-10 18:41:15 -070057void bch_keylist_pop_front(struct keylist *l)
58{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070059 l->top_p -= bkey_u64s(l->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -070060
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070061 memmove(l->keys,
62 bkey_next(l->keys),
63 bch_keylist_bytes(l));
Kent Overstreet26c949f2013-09-10 18:41:15 -070064}
65
Kent Overstreetcafe5632013-03-23 16:11:31 -070066/* Pointer validation */
67
Kent Overstreetd5cc66e2013-07-24 23:06:40 -070068static bool __ptr_invalid(struct cache_set *c, const struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -070069{
70 unsigned i;
Kent Overstreetcafe5632013-03-23 16:11:31 -070071
72 for (i = 0; i < KEY_PTRS(k); i++)
73 if (ptr_available(c, k, i)) {
74 struct cache *ca = PTR_CACHE(c, k, i);
75 size_t bucket = PTR_BUCKET_NR(c, k, i);
76 size_t r = bucket_remainder(c, PTR_OFFSET(k, i));
77
78 if (KEY_SIZE(k) + r > c->sb.bucket_size ||
79 bucket < ca->sb.first_bucket ||
80 bucket >= ca->sb.nbuckets)
Kent Overstreetd5cc66e2013-07-24 23:06:40 -070081 return true;
Kent Overstreetcafe5632013-03-23 16:11:31 -070082 }
83
84 return false;
Kent Overstreetd5cc66e2013-07-24 23:06:40 -070085}
86
87bool bch_btree_ptr_invalid(struct cache_set *c, const struct bkey *k)
88{
89 char buf[80];
90
91 if (!KEY_PTRS(k) || !KEY_SIZE(k) || KEY_DIRTY(k))
92 goto bad;
93
94 if (__ptr_invalid(c, k))
95 goto bad;
96
97 return false;
Kent Overstreetcafe5632013-03-23 16:11:31 -070098bad:
Kent Overstreet85b14922013-05-14 20:33:16 -070099 bch_bkey_to_text(buf, sizeof(buf), k);
Kent Overstreetd5cc66e2013-07-24 23:06:40 -0700100 cache_bug(c, "spotted btree ptr %s: %s", buf, bch_ptr_status(c, k));
101 return true;
102}
103
104bool bch_extent_ptr_invalid(struct cache_set *c, const struct bkey *k)
105{
106 char buf[80];
107
108 if (!KEY_SIZE(k))
109 return true;
110
111 if (KEY_SIZE(k) > KEY_OFFSET(k))
112 goto bad;
113
114 if (__ptr_invalid(c, k))
115 goto bad;
116
117 return false;
118bad:
119 bch_bkey_to_text(buf, sizeof(buf), k);
120 cache_bug(c, "spotted extent %s: %s", buf, bch_ptr_status(c, k));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700121 return true;
122}
123
Kent Overstreet280481d2013-10-24 16:36:03 -0700124static bool ptr_bad_expensive_checks(struct btree *b, const struct bkey *k,
125 unsigned ptr)
126{
127 struct bucket *g = PTR_BUCKET(b->c, k, ptr);
128 char buf[80];
129
130 if (mutex_trylock(&b->c->bucket_lock)) {
131 if (b->level) {
132 if (KEY_DIRTY(k) ||
133 g->prio != BTREE_PRIO ||
134 (b->c->gc_mark_valid &&
135 GC_MARK(g) != GC_MARK_METADATA))
136 goto err;
137
138 } else {
139 if (g->prio == BTREE_PRIO)
140 goto err;
141
142 if (KEY_DIRTY(k) &&
143 b->c->gc_mark_valid &&
144 GC_MARK(g) != GC_MARK_DIRTY)
145 goto err;
146 }
147 mutex_unlock(&b->c->bucket_lock);
148 }
149
150 return false;
151err:
152 mutex_unlock(&b->c->bucket_lock);
153 bch_bkey_to_text(buf, sizeof(buf), k);
154 btree_bug(b,
155"inconsistent pointer %s: bucket %zu pin %i prio %i gen %i last_gc %i mark %llu gc_gen %i",
156 buf, PTR_BUCKET_NR(b->c, k, ptr), atomic_read(&g->pin),
157 g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen);
158 return true;
159}
160
Kent Overstreetcafe5632013-03-23 16:11:31 -0700161bool bch_ptr_bad(struct btree *b, const struct bkey *k)
162{
163 struct bucket *g;
164 unsigned i, stale;
165
166 if (!bkey_cmp(k, &ZERO_KEY) ||
167 !KEY_PTRS(k) ||
168 bch_ptr_invalid(b, k))
169 return true;
170
Kent Overstreetd56d0002013-08-09 21:14:13 -0700171 for (i = 0; i < KEY_PTRS(k); i++)
Kent Overstreete58ff152013-07-24 18:14:44 -0700172 if (!ptr_available(b->c, k, i))
173 return true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700174
Kent Overstreetd56d0002013-08-09 21:14:13 -0700175 if (!expensive_debug_checks(b->c) && KEY_DIRTY(k))
176 return false;
177
178 for (i = 0; i < KEY_PTRS(k); i++) {
Kent Overstreete58ff152013-07-24 18:14:44 -0700179 g = PTR_BUCKET(b->c, k, i);
180 stale = ptr_stale(b->c, k, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700181
Kent Overstreete58ff152013-07-24 18:14:44 -0700182 btree_bug_on(stale > 96, b,
183 "key too stale: %i, need_gc %u",
184 stale, b->c->need_gc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700185
Kent Overstreete58ff152013-07-24 18:14:44 -0700186 btree_bug_on(stale && KEY_DIRTY(k) && KEY_SIZE(k),
187 b, "stale dirty pointer");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700188
Kent Overstreete58ff152013-07-24 18:14:44 -0700189 if (stale)
190 return true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700191
Kent Overstreet280481d2013-10-24 16:36:03 -0700192 if (expensive_debug_checks(b->c) &&
193 ptr_bad_expensive_checks(b, k, i))
194 return true;
Kent Overstreete58ff152013-07-24 18:14:44 -0700195 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700196
197 return false;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700198}
199
200/* Key/pointer manipulation */
201
202void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
203 unsigned i)
204{
205 BUG_ON(i > KEY_PTRS(src));
206
207 /* Only copy the header, key, and one pointer. */
208 memcpy(dest, src, 2 * sizeof(uint64_t));
209 dest->ptr[0] = src->ptr[i];
210 SET_KEY_PTRS(dest, 1);
211 /* We didn't copy the checksum so clear that bit. */
212 SET_KEY_CSUM(dest, 0);
213}
214
215bool __bch_cut_front(const struct bkey *where, struct bkey *k)
216{
217 unsigned i, len = 0;
218
219 if (bkey_cmp(where, &START_KEY(k)) <= 0)
220 return false;
221
222 if (bkey_cmp(where, k) < 0)
223 len = KEY_OFFSET(k) - KEY_OFFSET(where);
224 else
225 bkey_copy_key(k, where);
226
227 for (i = 0; i < KEY_PTRS(k); i++)
228 SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len);
229
230 BUG_ON(len > KEY_SIZE(k));
231 SET_KEY_SIZE(k, len);
232 return true;
233}
234
235bool __bch_cut_back(const struct bkey *where, struct bkey *k)
236{
237 unsigned len = 0;
238
239 if (bkey_cmp(where, k) >= 0)
240 return false;
241
242 BUG_ON(KEY_INODE(where) != KEY_INODE(k));
243
244 if (bkey_cmp(where, &START_KEY(k)) > 0)
245 len = KEY_OFFSET(where) - KEY_START(k);
246
247 bkey_copy_key(k, where);
248
249 BUG_ON(len > KEY_SIZE(k));
250 SET_KEY_SIZE(k, len);
251 return true;
252}
253
254static uint64_t merge_chksums(struct bkey *l, struct bkey *r)
255{
256 return (l->ptr[KEY_PTRS(l)] + r->ptr[KEY_PTRS(r)]) &
257 ~((uint64_t)1 << 63);
258}
259
260/* Tries to merge l and r: l should be lower than r
261 * Returns true if we were able to merge. If we did merge, l will be the merged
262 * key, r will be untouched.
263 */
264bool bch_bkey_try_merge(struct btree *b, struct bkey *l, struct bkey *r)
265{
266 unsigned i;
267
268 if (key_merging_disabled(b->c))
269 return false;
270
271 if (KEY_PTRS(l) != KEY_PTRS(r) ||
272 KEY_DIRTY(l) != KEY_DIRTY(r) ||
273 bkey_cmp(l, &START_KEY(r)))
274 return false;
275
276 for (i = 0; i < KEY_PTRS(l); i++)
277 if (l->ptr[i] + PTR(0, KEY_SIZE(l), 0) != r->ptr[i] ||
278 PTR_BUCKET_NR(b->c, l, i) != PTR_BUCKET_NR(b->c, r, i))
279 return false;
280
281 /* Keys with no pointers aren't restricted to one bucket and could
282 * overflow KEY_SIZE
283 */
284 if (KEY_SIZE(l) + KEY_SIZE(r) > USHRT_MAX) {
285 SET_KEY_OFFSET(l, KEY_OFFSET(l) + USHRT_MAX - KEY_SIZE(l));
286 SET_KEY_SIZE(l, USHRT_MAX);
287
288 bch_cut_front(l, r);
289 return false;
290 }
291
292 if (KEY_CSUM(l)) {
293 if (KEY_CSUM(r))
294 l->ptr[KEY_PTRS(l)] = merge_chksums(l, r);
295 else
296 SET_KEY_CSUM(l, 0);
297 }
298
299 SET_KEY_OFFSET(l, KEY_OFFSET(l) + KEY_SIZE(r));
300 SET_KEY_SIZE(l, KEY_SIZE(l) + KEY_SIZE(r));
301
302 return true;
303}
304
305/* Binary tree stuff for auxiliary search trees */
306
307static unsigned inorder_next(unsigned j, unsigned size)
308{
309 if (j * 2 + 1 < size) {
310 j = j * 2 + 1;
311
312 while (j * 2 < size)
313 j *= 2;
314 } else
315 j >>= ffz(j) + 1;
316
317 return j;
318}
319
320static unsigned inorder_prev(unsigned j, unsigned size)
321{
322 if (j * 2 < size) {
323 j = j * 2;
324
325 while (j * 2 + 1 < size)
326 j = j * 2 + 1;
327 } else
328 j >>= ffs(j);
329
330 return j;
331}
332
333/* I have no idea why this code works... and I'm the one who wrote it
334 *
335 * However, I do know what it does:
336 * Given a binary tree constructed in an array (i.e. how you normally implement
337 * a heap), it converts a node in the tree - referenced by array index - to the
338 * index it would have if you did an inorder traversal.
339 *
340 * Also tested for every j, size up to size somewhere around 6 million.
341 *
342 * The binary tree starts at array index 1, not 0
343 * extra is a function of size:
344 * extra = (size - rounddown_pow_of_two(size - 1)) << 1;
345 */
346static unsigned __to_inorder(unsigned j, unsigned size, unsigned extra)
347{
348 unsigned b = fls(j);
349 unsigned shift = fls(size - 1) - b;
350
351 j ^= 1U << (b - 1);
352 j <<= 1;
353 j |= 1;
354 j <<= shift;
355
356 if (j > extra)
357 j -= (j - extra) >> 1;
358
359 return j;
360}
361
362static unsigned to_inorder(unsigned j, struct bset_tree *t)
363{
364 return __to_inorder(j, t->size, t->extra);
365}
366
367static unsigned __inorder_to_tree(unsigned j, unsigned size, unsigned extra)
368{
369 unsigned shift;
370
371 if (j > extra)
372 j += j - extra;
373
374 shift = ffs(j);
375
376 j >>= shift;
377 j |= roundup_pow_of_two(size) >> shift;
378
379 return j;
380}
381
382static unsigned inorder_to_tree(unsigned j, struct bset_tree *t)
383{
384 return __inorder_to_tree(j, t->size, t->extra);
385}
386
387#if 0
388void inorder_test(void)
389{
390 unsigned long done = 0;
391 ktime_t start = ktime_get();
392
393 for (unsigned size = 2;
394 size < 65536000;
395 size++) {
396 unsigned extra = (size - rounddown_pow_of_two(size - 1)) << 1;
397 unsigned i = 1, j = rounddown_pow_of_two(size - 1);
398
399 if (!(size % 4096))
400 printk(KERN_NOTICE "loop %u, %llu per us\n", size,
401 done / ktime_us_delta(ktime_get(), start));
402
403 while (1) {
404 if (__inorder_to_tree(i, size, extra) != j)
405 panic("size %10u j %10u i %10u", size, j, i);
406
407 if (__to_inorder(j, size, extra) != i)
408 panic("size %10u j %10u i %10u", size, j, i);
409
410 if (j == rounddown_pow_of_two(size) - 1)
411 break;
412
413 BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
414
415 j = inorder_next(j, size);
416 i++;
417 }
418
419 done += size - 1;
420 }
421}
422#endif
423
424/*
Phil Viana48a73022013-06-03 09:51:42 -0300425 * Cacheline/offset <-> bkey pointer arithmetic:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700426 *
427 * t->tree is a binary search tree in an array; each node corresponds to a key
428 * in one cacheline in t->set (BSET_CACHELINE bytes).
429 *
430 * This means we don't have to store the full index of the key that a node in
431 * the binary tree points to; to_inorder() gives us the cacheline, and then
432 * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
433 *
Phil Viana48a73022013-06-03 09:51:42 -0300434 * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
Kent Overstreetcafe5632013-03-23 16:11:31 -0700435 * make this work.
436 *
437 * To construct the bfloat for an arbitrary key we need to know what the key
438 * immediately preceding it is: we have to check if the two keys differ in the
439 * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
440 * of the previous key so we can walk backwards to it from t->tree[j]'s key.
441 */
442
443static struct bkey *cacheline_to_bkey(struct bset_tree *t, unsigned cacheline,
444 unsigned offset)
445{
446 return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
447}
448
449static unsigned bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
450{
451 return ((void *) k - (void *) t->data) / BSET_CACHELINE;
452}
453
454static unsigned bkey_to_cacheline_offset(struct bkey *k)
455{
456 return ((size_t) k & (BSET_CACHELINE - 1)) / sizeof(uint64_t);
457}
458
459static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned j)
460{
461 return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
462}
463
464static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned j)
465{
466 return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
467}
468
469/*
470 * For the write set - the one we're currently inserting keys into - we don't
471 * maintain a full search tree, we just keep a simple lookup table in t->prev.
472 */
473static struct bkey *table_to_bkey(struct bset_tree *t, unsigned cacheline)
474{
475 return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
476}
477
478static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
479{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700480 low >>= shift;
481 low |= (high << 1) << (63U - shift);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700482 return low;
483}
484
485static inline unsigned bfloat_mantissa(const struct bkey *k,
486 struct bkey_float *f)
487{
488 const uint64_t *p = &k->low - (f->exponent >> 6);
489 return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
490}
491
492static void make_bfloat(struct bset_tree *t, unsigned j)
493{
494 struct bkey_float *f = &t->tree[j];
495 struct bkey *m = tree_to_bkey(t, j);
496 struct bkey *p = tree_to_prev_bkey(t, j);
497
498 struct bkey *l = is_power_of_2(j)
499 ? t->data->start
500 : tree_to_prev_bkey(t, j >> ffs(j));
501
502 struct bkey *r = is_power_of_2(j + 1)
Kent Overstreetfafff812013-12-17 21:56:21 -0800503 ? bset_bkey_idx(t->data, t->data->keys - bkey_u64s(&t->end))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700504 : tree_to_bkey(t, j >> (ffz(j) + 1));
505
506 BUG_ON(m < l || m > r);
507 BUG_ON(bkey_next(p) != m);
508
509 if (KEY_INODE(l) != KEY_INODE(r))
510 f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
511 else
512 f->exponent = fls64(r->low ^ l->low);
513
514 f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
515
516 /*
517 * Setting f->exponent = 127 flags this node as failed, and causes the
518 * lookup code to fall back to comparing against the original key.
519 */
520
521 if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
522 f->mantissa = bfloat_mantissa(m, f) - 1;
523 else
524 f->exponent = 127;
525}
526
527static void bset_alloc_tree(struct btree *b, struct bset_tree *t)
528{
529 if (t != b->sets) {
530 unsigned j = roundup(t[-1].size,
531 64 / sizeof(struct bkey_float));
532
533 t->tree = t[-1].tree + j;
534 t->prev = t[-1].prev + j;
535 }
536
537 while (t < b->sets + MAX_BSETS)
538 t++->size = 0;
539}
540
541static void bset_build_unwritten_tree(struct btree *b)
542{
543 struct bset_tree *t = b->sets + b->nsets;
544
545 bset_alloc_tree(b, t);
546
547 if (t->tree != b->sets->tree + bset_tree_space(b)) {
548 t->prev[0] = bkey_to_cacheline_offset(t->data->start);
549 t->size = 1;
550 }
551}
552
553static void bset_build_written_tree(struct btree *b)
554{
555 struct bset_tree *t = b->sets + b->nsets;
556 struct bkey *k = t->data->start;
557 unsigned j, cacheline = 1;
558
559 bset_alloc_tree(b, t);
560
561 t->size = min_t(unsigned,
Kent Overstreetfafff812013-12-17 21:56:21 -0800562 bkey_to_cacheline(t, bset_bkey_last(t->data)),
Kent Overstreetcafe5632013-03-23 16:11:31 -0700563 b->sets->tree + bset_tree_space(b) - t->tree);
564
565 if (t->size < 2) {
566 t->size = 0;
567 return;
568 }
569
570 t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
571
572 /* First we figure out where the first key in each cacheline is */
573 for (j = inorder_next(0, t->size);
574 j;
575 j = inorder_next(j, t->size)) {
576 while (bkey_to_cacheline(t, k) != cacheline)
577 k = bkey_next(k);
578
579 t->prev[j] = bkey_u64s(k);
580 k = bkey_next(k);
581 cacheline++;
582 t->tree[j].m = bkey_to_cacheline_offset(k);
583 }
584
Kent Overstreetfafff812013-12-17 21:56:21 -0800585 while (bkey_next(k) != bset_bkey_last(t->data))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700586 k = bkey_next(k);
587
588 t->end = *k;
589
590 /* Then we build the tree */
591 for (j = inorder_next(0, t->size);
592 j;
593 j = inorder_next(j, t->size))
594 make_bfloat(t, j);
595}
596
597void bch_bset_fix_invalidated_key(struct btree *b, struct bkey *k)
598{
599 struct bset_tree *t;
600 unsigned inorder, j = 1;
601
602 for (t = b->sets; t <= &b->sets[b->nsets]; t++)
Kent Overstreetfafff812013-12-17 21:56:21 -0800603 if (k < bset_bkey_last(t->data))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700604 goto found_set;
605
606 BUG();
607found_set:
608 if (!t->size || !bset_written(b, t))
609 return;
610
611 inorder = bkey_to_cacheline(t, k);
612
613 if (k == t->data->start)
614 goto fix_left;
615
Kent Overstreetfafff812013-12-17 21:56:21 -0800616 if (bkey_next(k) == bset_bkey_last(t->data)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700617 t->end = *k;
618 goto fix_right;
619 }
620
621 j = inorder_to_tree(inorder, t);
622
623 if (j &&
624 j < t->size &&
625 k == tree_to_bkey(t, j))
626fix_left: do {
627 make_bfloat(t, j);
628 j = j * 2;
629 } while (j < t->size);
630
631 j = inorder_to_tree(inorder + 1, t);
632
633 if (j &&
634 j < t->size &&
635 k == tree_to_prev_bkey(t, j))
636fix_right: do {
637 make_bfloat(t, j);
638 j = j * 2 + 1;
639 } while (j < t->size);
640}
641
642void bch_bset_fix_lookup_table(struct btree *b, struct bkey *k)
643{
644 struct bset_tree *t = &b->sets[b->nsets];
645 unsigned shift = bkey_u64s(k);
646 unsigned j = bkey_to_cacheline(t, k);
647
648 /* We're getting called from btree_split() or btree_gc, just bail out */
649 if (!t->size)
650 return;
651
652 /* k is the key we just inserted; we need to find the entry in the
653 * lookup table for the first key that is strictly greater than k:
654 * it's either k's cacheline or the next one
655 */
656 if (j < t->size &&
657 table_to_bkey(t, j) <= k)
658 j++;
659
660 /* Adjust all the lookup table entries, and find a new key for any that
661 * have gotten too big
662 */
663 for (; j < t->size; j++) {
664 t->prev[j] += shift;
665
666 if (t->prev[j] > 7) {
667 k = table_to_bkey(t, j - 1);
668
669 while (k < cacheline_to_bkey(t, j, 0))
670 k = bkey_next(k);
671
672 t->prev[j] = bkey_to_cacheline_offset(k);
673 }
674 }
675
676 if (t->size == b->sets->tree + bset_tree_space(b) - t->tree)
677 return;
678
679 /* Possibly add a new entry to the end of the lookup table */
680
681 for (k = table_to_bkey(t, t->size - 1);
Kent Overstreetfafff812013-12-17 21:56:21 -0800682 k != bset_bkey_last(t->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700683 k = bkey_next(k))
684 if (t->size == bkey_to_cacheline(t, k)) {
685 t->prev[t->size] = bkey_to_cacheline_offset(k);
686 t->size++;
687 }
688}
689
690void bch_bset_init_next(struct btree *b)
691{
692 struct bset *i = write_block(b);
693
694 if (i != b->sets[0].data) {
695 b->sets[++b->nsets].data = i;
696 i->seq = b->sets[0].data->seq;
697 } else
698 get_random_bytes(&i->seq, sizeof(uint64_t));
699
Kent Overstreet81ab4192013-10-31 15:46:42 -0700700 i->magic = bset_magic(&b->c->sb);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700701 i->version = 0;
702 i->keys = 0;
703
704 bset_build_unwritten_tree(b);
705}
706
707struct bset_search_iter {
708 struct bkey *l, *r;
709};
710
711static struct bset_search_iter bset_search_write_set(struct btree *b,
712 struct bset_tree *t,
713 const struct bkey *search)
714{
715 unsigned li = 0, ri = t->size;
716
717 BUG_ON(!b->nsets &&
Kent Overstreetfafff812013-12-17 21:56:21 -0800718 t->size < bkey_to_cacheline(t, bset_bkey_last(t->data)));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700719
720 while (li + 1 != ri) {
721 unsigned m = (li + ri) >> 1;
722
723 if (bkey_cmp(table_to_bkey(t, m), search) > 0)
724 ri = m;
725 else
726 li = m;
727 }
728
729 return (struct bset_search_iter) {
730 table_to_bkey(t, li),
Kent Overstreetfafff812013-12-17 21:56:21 -0800731 ri < t->size ? table_to_bkey(t, ri) : bset_bkey_last(t->data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700732 };
733}
734
735static struct bset_search_iter bset_search_tree(struct btree *b,
736 struct bset_tree *t,
737 const struct bkey *search)
738{
739 struct bkey *l, *r;
740 struct bkey_float *f;
741 unsigned inorder, j, n = 1;
742
743 do {
744 unsigned p = n << 4;
745 p &= ((int) (p - t->size)) >> 31;
746
747 prefetch(&t->tree[p]);
748
749 j = n;
750 f = &t->tree[j];
751
752 /*
753 * n = (f->mantissa > bfloat_mantissa())
754 * ? j * 2
755 * : j * 2 + 1;
756 *
757 * We need to subtract 1 from f->mantissa for the sign bit trick
758 * to work - that's done in make_bfloat()
759 */
760 if (likely(f->exponent != 127))
761 n = j * 2 + (((unsigned)
762 (f->mantissa -
763 bfloat_mantissa(search, f))) >> 31);
764 else
765 n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
766 ? j * 2
767 : j * 2 + 1;
768 } while (n < t->size);
769
770 inorder = to_inorder(j, t);
771
772 /*
773 * n would have been the node we recursed to - the low bit tells us if
774 * we recursed left or recursed right.
775 */
776 if (n & 1) {
777 l = cacheline_to_bkey(t, inorder, f->m);
778
779 if (++inorder != t->size) {
780 f = &t->tree[inorder_next(j, t->size)];
781 r = cacheline_to_bkey(t, inorder, f->m);
782 } else
Kent Overstreetfafff812013-12-17 21:56:21 -0800783 r = bset_bkey_last(t->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700784 } else {
785 r = cacheline_to_bkey(t, inorder, f->m);
786
787 if (--inorder) {
788 f = &t->tree[inorder_prev(j, t->size)];
789 l = cacheline_to_bkey(t, inorder, f->m);
790 } else
791 l = t->data->start;
792 }
793
794 return (struct bset_search_iter) {l, r};
795}
796
797struct bkey *__bch_bset_search(struct btree *b, struct bset_tree *t,
798 const struct bkey *search)
799{
800 struct bset_search_iter i;
801
802 /*
803 * First, we search for a cacheline, then lastly we do a linear search
804 * within that cacheline.
805 *
806 * To search for the cacheline, there's three different possibilities:
807 * * The set is too small to have a search tree, so we just do a linear
808 * search over the whole set.
809 * * The set is the one we're currently inserting into; keeping a full
810 * auxiliary search tree up to date would be too expensive, so we
811 * use a much simpler lookup table to do a binary search -
812 * bset_search_write_set().
813 * * Or we use the auxiliary search tree we constructed earlier -
814 * bset_search_tree()
815 */
816
817 if (unlikely(!t->size)) {
818 i.l = t->data->start;
Kent Overstreetfafff812013-12-17 21:56:21 -0800819 i.r = bset_bkey_last(t->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700820 } else if (bset_written(b, t)) {
821 /*
822 * Each node in the auxiliary search tree covers a certain range
823 * of bits, and keys above and below the set it covers might
824 * differ outside those bits - so we have to special case the
825 * start and end - handle that here:
826 */
827
828 if (unlikely(bkey_cmp(search, &t->end) >= 0))
Kent Overstreetfafff812013-12-17 21:56:21 -0800829 return bset_bkey_last(t->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700830
831 if (unlikely(bkey_cmp(search, t->data->start) < 0))
832 return t->data->start;
833
834 i = bset_search_tree(b, t, search);
835 } else
836 i = bset_search_write_set(b, t, search);
837
Kent Overstreet280481d2013-10-24 16:36:03 -0700838 if (expensive_debug_checks(b->c)) {
839 BUG_ON(bset_written(b, t) &&
840 i.l != t->data->start &&
841 bkey_cmp(tree_to_prev_bkey(t,
842 inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
843 search) > 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700844
Kent Overstreetfafff812013-12-17 21:56:21 -0800845 BUG_ON(i.r != bset_bkey_last(t->data) &&
Kent Overstreet280481d2013-10-24 16:36:03 -0700846 bkey_cmp(i.r, search) <= 0);
847 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700848
849 while (likely(i.l != i.r) &&
850 bkey_cmp(i.l, search) <= 0)
851 i.l = bkey_next(i.l);
852
853 return i.l;
854}
855
856/* Btree iterator */
857
Kent Overstreet911c9612013-07-28 18:35:09 -0700858typedef bool (btree_iter_cmp_fn)(struct btree_iter_set,
859 struct btree_iter_set);
860
Kent Overstreetcafe5632013-03-23 16:11:31 -0700861static inline bool btree_iter_cmp(struct btree_iter_set l,
862 struct btree_iter_set r)
863{
Kent Overstreet911c9612013-07-28 18:35:09 -0700864 return bkey_cmp(l.k, r.k) > 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700865}
866
867static inline bool btree_iter_end(struct btree_iter *iter)
868{
869 return !iter->used;
870}
871
872void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
873 struct bkey *end)
874{
875 if (k != end)
876 BUG_ON(!heap_add(iter,
877 ((struct btree_iter_set) { k, end }),
878 btree_iter_cmp));
879}
880
Kent Overstreet911c9612013-07-28 18:35:09 -0700881static struct bkey *__bch_btree_iter_init(struct btree *b,
882 struct btree_iter *iter,
883 struct bkey *search,
884 struct bset_tree *start)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700885{
886 struct bkey *ret = NULL;
887 iter->size = ARRAY_SIZE(iter->data);
888 iter->used = 0;
889
Kent Overstreet280481d2013-10-24 16:36:03 -0700890#ifdef CONFIG_BCACHE_DEBUG
891 iter->b = b;
892#endif
893
Kent Overstreetcafe5632013-03-23 16:11:31 -0700894 for (; start <= &b->sets[b->nsets]; start++) {
895 ret = bch_bset_search(b, start, search);
Kent Overstreetfafff812013-12-17 21:56:21 -0800896 bch_btree_iter_push(iter, ret, bset_bkey_last(start->data));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700897 }
898
899 return ret;
900}
901
Kent Overstreet911c9612013-07-28 18:35:09 -0700902struct bkey *bch_btree_iter_init(struct btree *b,
903 struct btree_iter *iter,
904 struct bkey *search)
905{
906 return __bch_btree_iter_init(b, iter, search, b->sets);
907}
908
909static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter,
910 btree_iter_cmp_fn *cmp)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700911{
912 struct btree_iter_set unused;
913 struct bkey *ret = NULL;
914
915 if (!btree_iter_end(iter)) {
Kent Overstreet280481d2013-10-24 16:36:03 -0700916 bch_btree_iter_next_check(iter);
917
Kent Overstreetcafe5632013-03-23 16:11:31 -0700918 ret = iter->data->k;
919 iter->data->k = bkey_next(iter->data->k);
920
921 if (iter->data->k > iter->data->end) {
Kent Overstreetcc0f4ea2013-03-27 12:47:45 -0700922 WARN_ONCE(1, "bset was corrupt!\n");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700923 iter->data->k = iter->data->end;
924 }
925
926 if (iter->data->k == iter->data->end)
Kent Overstreet911c9612013-07-28 18:35:09 -0700927 heap_pop(iter, unused, cmp);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700928 else
Kent Overstreet911c9612013-07-28 18:35:09 -0700929 heap_sift(iter, 0, cmp);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700930 }
931
932 return ret;
933}
934
Kent Overstreet911c9612013-07-28 18:35:09 -0700935struct bkey *bch_btree_iter_next(struct btree_iter *iter)
936{
937 return __bch_btree_iter_next(iter, btree_iter_cmp);
938
939}
940
Kent Overstreetcafe5632013-03-23 16:11:31 -0700941struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
942 struct btree *b, ptr_filter_fn fn)
943{
944 struct bkey *ret;
945
946 do {
947 ret = bch_btree_iter_next(iter);
948 } while (ret && fn(b, ret));
949
950 return ret;
951}
952
Kent Overstreetcafe5632013-03-23 16:11:31 -0700953/* Mergesort */
954
Kent Overstreet84786432013-09-23 23:17:35 -0700955static void sort_key_next(struct btree_iter *iter,
956 struct btree_iter_set *i)
957{
958 i->k = bkey_next(i->k);
959
960 if (i->k == i->end)
961 *i = iter->data[--iter->used];
962}
963
Kent Overstreet911c9612013-07-28 18:35:09 -0700964/*
965 * Returns true if l > r - unless l == r, in which case returns true if l is
966 * older than r.
967 *
968 * Necessary for btree_sort_fixup() - if there are multiple keys that compare
969 * equal in different sets, we have to process them newest to oldest.
970 */
971static inline bool sort_extent_cmp(struct btree_iter_set l,
972 struct btree_iter_set r)
973{
974 int64_t c = bkey_cmp(&START_KEY(l.k), &START_KEY(r.k));
975
976 return c ? c > 0 : l.k < r.k;
977}
978
979static inline bool sort_cmp(struct btree_iter_set l,
980 struct btree_iter_set r)
981{
982 int64_t c = bkey_cmp(l.k, r.k);
983
984 return c ? c > 0 : l.k < r.k;
985}
986
987static struct bkey *btree_sort_fixup_extents(struct btree_iter *iter,
988 struct bkey *tmp)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700989{
990 while (iter->used > 1) {
991 struct btree_iter_set *top = iter->data, *i = top + 1;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700992
993 if (iter->used > 2 &&
Kent Overstreet911c9612013-07-28 18:35:09 -0700994 sort_extent_cmp(i[0], i[1]))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700995 i++;
996
Kent Overstreet84786432013-09-23 23:17:35 -0700997 if (bkey_cmp(top->k, &START_KEY(i->k)) <= 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700998 break;
999
Kent Overstreet84786432013-09-23 23:17:35 -07001000 if (!KEY_SIZE(i->k)) {
1001 sort_key_next(iter, i);
Kent Overstreet911c9612013-07-28 18:35:09 -07001002 heap_sift(iter, i - top, sort_extent_cmp);
Kent Overstreet84786432013-09-23 23:17:35 -07001003 continue;
1004 }
1005
1006 if (top->k > i->k) {
1007 if (bkey_cmp(top->k, i->k) >= 0)
1008 sort_key_next(iter, i);
1009 else
1010 bch_cut_front(top->k, i->k);
1011
Kent Overstreet911c9612013-07-28 18:35:09 -07001012 heap_sift(iter, i - top, sort_extent_cmp);
Kent Overstreet84786432013-09-23 23:17:35 -07001013 } else {
1014 /* can't happen because of comparison func */
1015 BUG_ON(!bkey_cmp(&START_KEY(top->k), &START_KEY(i->k)));
Kent Overstreetef71ec02013-12-17 17:51:02 -08001016
1017 if (bkey_cmp(i->k, top->k) < 0) {
1018 bkey_copy(tmp, top->k);
1019
1020 bch_cut_back(&START_KEY(i->k), tmp);
1021 bch_cut_front(i->k, top->k);
1022 heap_sift(iter, 0, btree_iter_cmp);
1023
1024 return tmp;
1025 } else {
1026 bch_cut_back(&START_KEY(i->k), top->k);
1027 }
Kent Overstreet84786432013-09-23 23:17:35 -07001028 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001029 }
Kent Overstreetef71ec02013-12-17 17:51:02 -08001030
1031 return NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001032}
1033
1034static void btree_mergesort(struct btree *b, struct bset *out,
1035 struct btree_iter *iter,
1036 bool fixup, bool remove_stale)
1037{
Kent Overstreet911c9612013-07-28 18:35:09 -07001038 int i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001039 struct bkey *k, *last = NULL;
Kent Overstreetef71ec02013-12-17 17:51:02 -08001040 BKEY_PADDED(k) tmp;
Kent Overstreet911c9612013-07-28 18:35:09 -07001041 btree_iter_cmp_fn *cmp = b->level
1042 ? sort_cmp
1043 : sort_extent_cmp;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001044 bool (*bad)(struct btree *, const struct bkey *) = remove_stale
1045 ? bch_ptr_bad
1046 : bch_ptr_invalid;
1047
Kent Overstreet911c9612013-07-28 18:35:09 -07001048 /* Heapify the iterator, using our comparison function */
1049 for (i = iter->used / 2 - 1; i >= 0; --i)
1050 heap_sift(iter, i, cmp);
1051
Kent Overstreetcafe5632013-03-23 16:11:31 -07001052 while (!btree_iter_end(iter)) {
1053 if (fixup && !b->level)
Kent Overstreet911c9612013-07-28 18:35:09 -07001054 k = btree_sort_fixup_extents(iter, &tmp.k);
Kent Overstreetef71ec02013-12-17 17:51:02 -08001055 else
1056 k = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001057
Kent Overstreetef71ec02013-12-17 17:51:02 -08001058 if (!k)
Kent Overstreet911c9612013-07-28 18:35:09 -07001059 k = __bch_btree_iter_next(iter, cmp);
Kent Overstreetef71ec02013-12-17 17:51:02 -08001060
Kent Overstreetcafe5632013-03-23 16:11:31 -07001061 if (bad(b, k))
1062 continue;
1063
1064 if (!last) {
1065 last = out->start;
1066 bkey_copy(last, k);
1067 } else if (b->level ||
1068 !bch_bkey_try_merge(b, last, k)) {
1069 last = bkey_next(last);
1070 bkey_copy(last, k);
1071 }
1072 }
1073
1074 out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
1075
1076 pr_debug("sorted %i keys", out->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001077}
1078
1079static void __btree_sort(struct btree *b, struct btree_iter *iter,
1080 unsigned start, unsigned order, bool fixup)
1081{
1082 uint64_t start_time;
1083 bool remove_stale = !b->written;
Kent Overstreet0a451142013-12-18 00:01:06 -08001084 bool used_mempool = false;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001085 struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOIO,
1086 order);
1087 if (!out) {
Kent Overstreet0a451142013-12-18 00:01:06 -08001088 out = page_address(mempool_alloc(b->c->sort_pool, GFP_NOIO));
1089 used_mempool = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001090 order = ilog2(bucket_pages(b->c));
1091 }
1092
1093 start_time = local_clock();
1094
1095 btree_mergesort(b, out, iter, fixup, remove_stale);
1096 b->nsets = start;
1097
Kent Overstreetcafe5632013-03-23 16:11:31 -07001098 if (!start && order == b->page_order) {
1099 /*
1100 * Our temporary buffer is the same size as the btree node's
1101 * buffer, we can just swap buffers instead of doing a big
1102 * memcpy()
1103 */
1104
Kent Overstreet81ab4192013-10-31 15:46:42 -07001105 out->magic = bset_magic(&b->c->sb);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001106 out->seq = b->sets[0].data->seq;
1107 out->version = b->sets[0].data->version;
1108 swap(out, b->sets[0].data);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001109 } else {
1110 b->sets[start].data->keys = out->keys;
1111 memcpy(b->sets[start].data->start, out->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08001112 (void *) bset_bkey_last(out) - (void *) out->start);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001113 }
1114
Kent Overstreet0a451142013-12-18 00:01:06 -08001115 if (used_mempool)
1116 mempool_free(virt_to_page(out), b->c->sort_pool);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001117 else
1118 free_pages((unsigned long) out, order);
1119
1120 if (b->written)
1121 bset_build_written_tree(b);
1122
Kent Overstreet65d22e92013-07-31 00:03:54 -07001123 if (!start)
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001124 bch_time_stats_update(&b->c->sort_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001125}
1126
1127void bch_btree_sort_partial(struct btree *b, unsigned start)
1128{
Kent Overstreet280481d2013-10-24 16:36:03 -07001129 size_t order = b->page_order, keys = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001130 struct btree_iter iter;
Kent Overstreet280481d2013-10-24 16:36:03 -07001131 int oldsize = bch_count_data(b);
1132
Kent Overstreetcafe5632013-03-23 16:11:31 -07001133 __bch_btree_iter_init(b, &iter, NULL, &b->sets[start]);
1134
1135 BUG_ON(b->sets[b->nsets].data == write_block(b) &&
1136 (b->sets[b->nsets].size || b->nsets));
1137
Kent Overstreetcafe5632013-03-23 16:11:31 -07001138
1139 if (start) {
1140 unsigned i;
1141
1142 for (i = start; i <= b->nsets; i++)
1143 keys += b->sets[i].data->keys;
1144
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001145 order = roundup_pow_of_two(__set_bytes(b->sets->data,
1146 keys)) / PAGE_SIZE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001147 if (order)
1148 order = ilog2(order);
1149 }
1150
1151 __btree_sort(b, &iter, start, order, false);
1152
Kent Overstreet280481d2013-10-24 16:36:03 -07001153 EBUG_ON(b->written && oldsize >= 0 && bch_count_data(b) != oldsize);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001154}
1155
1156void bch_btree_sort_and_fix_extents(struct btree *b, struct btree_iter *iter)
1157{
1158 BUG_ON(!b->written);
1159 __btree_sort(b, iter, 0, b->page_order, true);
1160}
1161
1162void bch_btree_sort_into(struct btree *b, struct btree *new)
1163{
1164 uint64_t start_time = local_clock();
1165
1166 struct btree_iter iter;
1167 bch_btree_iter_init(b, &iter, NULL);
1168
1169 btree_mergesort(b, new->sets->data, &iter, false, true);
1170
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001171 bch_time_stats_update(&b->c->sort_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001172
1173 bkey_copy_key(&new->key, &b->key);
1174 new->sets->size = 0;
1175}
1176
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001177#define SORT_CRIT (4096 / sizeof(uint64_t))
1178
Kent Overstreetcafe5632013-03-23 16:11:31 -07001179void bch_btree_sort_lazy(struct btree *b)
1180{
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001181 unsigned crit = SORT_CRIT;
1182 int i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001183
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001184 /* Don't sort if nothing to do */
1185 if (!b->nsets)
1186 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001187
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001188 /* If not a leaf node, always sort */
1189 if (b->level) {
1190 bch_btree_sort(b);
1191 return;
1192 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001193
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001194 for (i = b->nsets - 1; i >= 0; --i) {
1195 crit *= b->c->sort_crit_factor;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001196
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001197 if (b->sets[i].data->keys < crit) {
1198 bch_btree_sort_partial(b, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001199 return;
1200 }
1201 }
1202
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001203 /* Sort if we'd overflow */
1204 if (b->nsets + 1 == MAX_BSETS) {
1205 bch_btree_sort(b);
1206 return;
1207 }
1208
1209out:
Kent Overstreetcafe5632013-03-23 16:11:31 -07001210 bset_build_written_tree(b);
1211}
1212
1213/* Sysfs stuff */
1214
1215struct bset_stats {
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001216 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001217 size_t nodes;
1218 size_t sets_written, sets_unwritten;
1219 size_t bytes_written, bytes_unwritten;
1220 size_t floats, failed;
1221};
1222
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001223static int btree_bset_stats(struct btree_op *op, struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001224{
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001225 struct bset_stats *stats = container_of(op, struct bset_stats, op);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001226 unsigned i;
1227
1228 stats->nodes++;
1229
1230 for (i = 0; i <= b->nsets; i++) {
1231 struct bset_tree *t = &b->sets[i];
1232 size_t bytes = t->data->keys * sizeof(uint64_t);
1233 size_t j;
1234
1235 if (bset_written(b, t)) {
1236 stats->sets_written++;
1237 stats->bytes_written += bytes;
1238
1239 stats->floats += t->size - 1;
1240
1241 for (j = 1; j < t->size; j++)
1242 if (t->tree[j].exponent == 127)
1243 stats->failed++;
1244 } else {
1245 stats->sets_unwritten++;
1246 stats->bytes_unwritten += bytes;
1247 }
1248 }
1249
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001250 return MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001251}
1252
1253int bch_bset_print_stats(struct cache_set *c, char *buf)
1254{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001255 struct bset_stats t;
1256 int ret;
1257
Kent Overstreetcafe5632013-03-23 16:11:31 -07001258 memset(&t, 0, sizeof(struct bset_stats));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001259 bch_btree_op_init(&t.op, -1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001260
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001261 ret = bch_btree_map_nodes(&t.op, c, &ZERO_KEY, btree_bset_stats);
1262 if (ret < 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001263 return ret;
1264
1265 return snprintf(buf, PAGE_SIZE,
1266 "btree nodes: %zu\n"
1267 "written sets: %zu\n"
1268 "unwritten sets: %zu\n"
1269 "written key bytes: %zu\n"
1270 "unwritten key bytes: %zu\n"
1271 "floats: %zu\n"
1272 "failed: %zu\n",
1273 t.nodes,
1274 t.sets_written, t.sets_unwritten,
1275 t.bytes_written, t.bytes_unwritten,
1276 t.floats, t.failed);
1277}