bset.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #ifndef _BCACHE_BSET_H
  2. #define _BCACHE_BSET_H
  3. /*
  4. * BKEYS:
  5. *
  6. * A bkey contains a key, a size field, a variable number of pointers, and some
  7. * ancillary flag bits.
  8. *
  9. * We use two different functions for validating bkeys, bch_ptr_invalid and
  10. * bch_ptr_bad().
  11. *
  12. * bch_ptr_invalid() primarily filters out keys and pointers that would be
  13. * invalid due to some sort of bug, whereas bch_ptr_bad() filters out keys and
  14. * pointer that occur in normal practice but don't point to real data.
  15. *
  16. * The one exception to the rule that ptr_invalid() filters out invalid keys is
  17. * that it also filters out keys of size 0 - these are keys that have been
  18. * completely overwritten. It'd be safe to delete these in memory while leaving
  19. * them on disk, just unnecessary work - so we filter them out when resorting
  20. * instead.
  21. *
  22. * We can't filter out stale keys when we're resorting, because garbage
  23. * collection needs to find them to ensure bucket gens don't wrap around -
  24. * unless we're rewriting the btree node those stale keys still exist on disk.
  25. *
  26. * We also implement functions here for removing some number of sectors from the
  27. * front or the back of a bkey - this is mainly used for fixing overlapping
  28. * extents, by removing the overlapping sectors from the older key.
  29. *
  30. * BSETS:
  31. *
  32. * A bset is an array of bkeys laid out contiguously in memory in sorted order,
  33. * along with a header. A btree node is made up of a number of these, written at
  34. * different times.
  35. *
  36. * There could be many of them on disk, but we never allow there to be more than
  37. * 4 in memory - we lazily resort as needed.
  38. *
  39. * We implement code here for creating and maintaining auxiliary search trees
  40. * (described below) for searching an individial bset, and on top of that we
  41. * implement a btree iterator.
  42. *
  43. * BTREE ITERATOR:
  44. *
  45. * Most of the code in bcache doesn't care about an individual bset - it needs
  46. * to search entire btree nodes and iterate over them in sorted order.
  47. *
  48. * The btree iterator code serves both functions; it iterates through the keys
  49. * in a btree node in sorted order, starting from either keys after a specific
  50. * point (if you pass it a search key) or the start of the btree node.
  51. *
  52. * AUXILIARY SEARCH TREES:
  53. *
  54. * Since keys are variable length, we can't use a binary search on a bset - we
  55. * wouldn't be able to find the start of the next key. But binary searches are
  56. * slow anyways, due to terrible cache behaviour; bcache originally used binary
  57. * searches and that code topped out at under 50k lookups/second.
  58. *
  59. * So we need to construct some sort of lookup table. Since we only insert keys
  60. * into the last (unwritten) set, most of the keys within a given btree node are
  61. * usually in sets that are mostly constant. We use two different types of
  62. * lookup tables to take advantage of this.
  63. *
  64. * Both lookup tables share in common that they don't index every key in the
  65. * set; they index one key every BSET_CACHELINE bytes, and then a linear search
  66. * is used for the rest.
  67. *
  68. * For sets that have been written to disk and are no longer being inserted
  69. * into, we construct a binary search tree in an array - traversing a binary
  70. * search tree in an array gives excellent locality of reference and is very
  71. * fast, since both children of any node are adjacent to each other in memory
  72. * (and their grandchildren, and great grandchildren...) - this means
  73. * prefetching can be used to great effect.
  74. *
  75. * It's quite useful performance wise to keep these nodes small - not just
  76. * because they're more likely to be in L2, but also because we can prefetch
  77. * more nodes on a single cacheline and thus prefetch more iterations in advance
  78. * when traversing this tree.
  79. *
  80. * Nodes in the auxiliary search tree must contain both a key to compare against
  81. * (we don't want to fetch the key from the set, that would defeat the purpose),
  82. * and a pointer to the key. We use a few tricks to compress both of these.
  83. *
  84. * To compress the pointer, we take advantage of the fact that one node in the
  85. * search tree corresponds to precisely BSET_CACHELINE bytes in the set. We have
  86. * a function (to_inorder()) that takes the index of a node in a binary tree and
  87. * returns what its index would be in an inorder traversal, so we only have to
  88. * store the low bits of the offset.
  89. *
  90. * The key is 84 bits (KEY_DEV + key->key, the offset on the device). To
  91. * compress that, we take advantage of the fact that when we're traversing the
  92. * search tree at every iteration we know that both our search key and the key
  93. * we're looking for lie within some range - bounded by our previous
  94. * comparisons. (We special case the start of a search so that this is true even
  95. * at the root of the tree).
  96. *
  97. * So we know the key we're looking for is between a and b, and a and b don't
  98. * differ higher than bit 50, we don't need to check anything higher than bit
  99. * 50.
  100. *
  101. * We don't usually need the rest of the bits, either; we only need enough bits
  102. * to partition the key range we're currently checking. Consider key n - the
  103. * key our auxiliary search tree node corresponds to, and key p, the key
  104. * immediately preceding n. The lowest bit we need to store in the auxiliary
  105. * search tree is the highest bit that differs between n and p.
  106. *
  107. * Note that this could be bit 0 - we might sometimes need all 80 bits to do the
  108. * comparison. But we'd really like our nodes in the auxiliary search tree to be
  109. * of fixed size.
  110. *
  111. * The solution is to make them fixed size, and when we're constructing a node
  112. * check if p and n differed in the bits we needed them to. If they don't we
  113. * flag that node, and when doing lookups we fallback to comparing against the
  114. * real key. As long as this doesn't happen to often (and it seems to reliably
  115. * happen a bit less than 1% of the time), we win - even on failures, that key
  116. * is then more likely to be in cache than if we were doing binary searches all
  117. * the way, since we're touching so much less memory.
  118. *
  119. * The keys in the auxiliary search tree are stored in (software) floating
  120. * point, with an exponent and a mantissa. The exponent needs to be big enough
  121. * to address all the bits in the original key, but the number of bits in the
  122. * mantissa is somewhat arbitrary; more bits just gets us fewer failures.
  123. *
  124. * We need 7 bits for the exponent and 3 bits for the key's offset (since keys
  125. * are 8 byte aligned); using 22 bits for the mantissa means a node is 4 bytes.
  126. * We need one node per 128 bytes in the btree node, which means the auxiliary
  127. * search trees take up 3% as much memory as the btree itself.
  128. *
  129. * Constructing these auxiliary search trees is moderately expensive, and we
  130. * don't want to be constantly rebuilding the search tree for the last set
  131. * whenever we insert another key into it. For the unwritten set, we use a much
  132. * simpler lookup table - it's just a flat array, so index i in the lookup table
  133. * corresponds to the i range of BSET_CACHELINE bytes in the set. Indexing
  134. * within each byte range works the same as with the auxiliary search trees.
  135. *
  136. * These are much easier to keep up to date when we insert a key - we do it
  137. * somewhat lazily; when we shift a key up we usually just increment the pointer
  138. * to it, only when it would overflow do we go to the trouble of finding the
  139. * first key in that range of bytes again.
  140. */
  141. /* Btree key comparison/iteration */
  142. struct btree_iter {
  143. size_t size, used;
  144. struct btree_iter_set {
  145. struct bkey *k, *end;
  146. } data[MAX_BSETS];
  147. };
  148. struct bset_tree {
  149. /*
  150. * We construct a binary tree in an array as if the array
  151. * started at 1, so that things line up on the same cachelines
  152. * better: see comments in bset.c at cacheline_to_bkey() for
  153. * details
  154. */
  155. /* size of the binary tree and prev array */
  156. unsigned size;
  157. /* function of size - precalculated for to_inorder() */
  158. unsigned extra;
  159. /* copy of the last key in the set */
  160. struct bkey end;
  161. struct bkey_float *tree;
  162. /*
  163. * The nodes in the bset tree point to specific keys - this
  164. * array holds the sizes of the previous key.
  165. *
  166. * Conceptually it's a member of struct bkey_float, but we want
  167. * to keep bkey_float to 4 bytes and prev isn't used in the fast
  168. * path.
  169. */
  170. uint8_t *prev;
  171. /* The actual btree node, with pointers to each sorted set */
  172. struct bset *data;
  173. };
  174. static __always_inline int64_t bkey_cmp(const struct bkey *l,
  175. const struct bkey *r)
  176. {
  177. return unlikely(KEY_INODE(l) != KEY_INODE(r))
  178. ? (int64_t) KEY_INODE(l) - (int64_t) KEY_INODE(r)
  179. : (int64_t) KEY_OFFSET(l) - (int64_t) KEY_OFFSET(r);
  180. }
  181. static inline size_t bkey_u64s(const struct bkey *k)
  182. {
  183. BUG_ON(KEY_CSUM(k) > 1);
  184. return 2 + KEY_PTRS(k) + (KEY_CSUM(k) ? 1 : 0);
  185. }
  186. static inline size_t bkey_bytes(const struct bkey *k)
  187. {
  188. return bkey_u64s(k) * sizeof(uint64_t);
  189. }
  190. static inline void bkey_copy(struct bkey *dest, const struct bkey *src)
  191. {
  192. memcpy(dest, src, bkey_bytes(src));
  193. }
  194. static inline void bkey_copy_key(struct bkey *dest, const struct bkey *src)
  195. {
  196. if (!src)
  197. src = &KEY(0, 0, 0);
  198. SET_KEY_INODE(dest, KEY_INODE(src));
  199. SET_KEY_OFFSET(dest, KEY_OFFSET(src));
  200. }
  201. static inline struct bkey *bkey_next(const struct bkey *k)
  202. {
  203. uint64_t *d = (void *) k;
  204. return (struct bkey *) (d + bkey_u64s(k));
  205. }
  206. /* Keylists */
  207. struct keylist {
  208. struct bkey *top;
  209. union {
  210. uint64_t *list;
  211. struct bkey *bottom;
  212. };
  213. /* Enough room for btree_split's keys without realloc */
  214. #define KEYLIST_INLINE 16
  215. uint64_t d[KEYLIST_INLINE];
  216. };
  217. static inline void bch_keylist_init(struct keylist *l)
  218. {
  219. l->top = (void *) (l->list = l->d);
  220. }
  221. static inline void bch_keylist_push(struct keylist *l)
  222. {
  223. l->top = bkey_next(l->top);
  224. }
  225. static inline void bch_keylist_add(struct keylist *l, struct bkey *k)
  226. {
  227. bkey_copy(l->top, k);
  228. bch_keylist_push(l);
  229. }
  230. static inline bool bch_keylist_empty(struct keylist *l)
  231. {
  232. return l->top == (void *) l->list;
  233. }
  234. static inline void bch_keylist_free(struct keylist *l)
  235. {
  236. if (l->list != l->d)
  237. kfree(l->list);
  238. }
  239. void bch_keylist_copy(struct keylist *, struct keylist *);
  240. struct bkey *bch_keylist_pop(struct keylist *);
  241. int bch_keylist_realloc(struct keylist *, int, struct cache_set *);
  242. void bch_bkey_copy_single_ptr(struct bkey *, const struct bkey *,
  243. unsigned);
  244. bool __bch_cut_front(const struct bkey *, struct bkey *);
  245. bool __bch_cut_back(const struct bkey *, struct bkey *);
  246. static inline bool bch_cut_front(const struct bkey *where, struct bkey *k)
  247. {
  248. BUG_ON(bkey_cmp(where, k) > 0);
  249. return __bch_cut_front(where, k);
  250. }
  251. static inline bool bch_cut_back(const struct bkey *where, struct bkey *k)
  252. {
  253. BUG_ON(bkey_cmp(where, &START_KEY(k)) < 0);
  254. return __bch_cut_back(where, k);
  255. }
  256. const char *bch_ptr_status(struct cache_set *, const struct bkey *);
  257. bool __bch_ptr_invalid(struct cache_set *, int level, const struct bkey *);
  258. bool bch_ptr_bad(struct btree *, const struct bkey *);
  259. static inline uint8_t gen_after(uint8_t a, uint8_t b)
  260. {
  261. uint8_t r = a - b;
  262. return r > 128U ? 0 : r;
  263. }
  264. static inline uint8_t ptr_stale(struct cache_set *c, const struct bkey *k,
  265. unsigned i)
  266. {
  267. return gen_after(PTR_BUCKET(c, k, i)->gen, PTR_GEN(k, i));
  268. }
  269. static inline bool ptr_available(struct cache_set *c, const struct bkey *k,
  270. unsigned i)
  271. {
  272. return (PTR_DEV(k, i) < MAX_CACHES_PER_SET) && PTR_CACHE(c, k, i);
  273. }
  274. typedef bool (*ptr_filter_fn)(struct btree *, const struct bkey *);
  275. struct bkey *bch_next_recurse_key(struct btree *, struct bkey *);
  276. struct bkey *bch_btree_iter_next(struct btree_iter *);
  277. struct bkey *bch_btree_iter_next_filter(struct btree_iter *,
  278. struct btree *, ptr_filter_fn);
  279. void bch_btree_iter_push(struct btree_iter *, struct bkey *, struct bkey *);
  280. struct bkey *__bch_btree_iter_init(struct btree *, struct btree_iter *,
  281. struct bkey *, struct bset_tree *);
  282. /* 32 bits total: */
  283. #define BKEY_MID_BITS 3
  284. #define BKEY_EXPONENT_BITS 7
  285. #define BKEY_MANTISSA_BITS 22
  286. #define BKEY_MANTISSA_MASK ((1 << BKEY_MANTISSA_BITS) - 1)
  287. struct bkey_float {
  288. unsigned exponent:BKEY_EXPONENT_BITS;
  289. unsigned m:BKEY_MID_BITS;
  290. unsigned mantissa:BKEY_MANTISSA_BITS;
  291. } __packed;
  292. /*
  293. * BSET_CACHELINE was originally intended to match the hardware cacheline size -
  294. * it used to be 64, but I realized the lookup code would touch slightly less
  295. * memory if it was 128.
  296. *
  297. * It definites the number of bytes (in struct bset) per struct bkey_float in
  298. * the auxiliar search tree - when we're done searching the bset_float tree we
  299. * have this many bytes left that we do a linear search over.
  300. *
  301. * Since (after level 5) every level of the bset_tree is on a new cacheline,
  302. * we're touching one fewer cacheline in the bset tree in exchange for one more
  303. * cacheline in the linear search - but the linear search might stop before it
  304. * gets to the second cacheline.
  305. */
  306. #define BSET_CACHELINE 128
  307. #define bset_tree_space(b) (btree_data_space(b) / BSET_CACHELINE)
  308. #define bset_tree_bytes(b) (bset_tree_space(b) * sizeof(struct bkey_float))
  309. #define bset_prev_bytes(b) (bset_tree_space(b) * sizeof(uint8_t))
  310. void bch_bset_init_next(struct btree *);
  311. void bch_bset_fix_invalidated_key(struct btree *, struct bkey *);
  312. void bch_bset_fix_lookup_table(struct btree *, struct bkey *);
  313. struct bkey *__bch_bset_search(struct btree *, struct bset_tree *,
  314. const struct bkey *);
  315. static inline struct bkey *bch_bset_search(struct btree *b, struct bset_tree *t,
  316. const struct bkey *search)
  317. {
  318. return search ? __bch_bset_search(b, t, search) : t->data->start;
  319. }
  320. bool bch_bkey_try_merge(struct btree *, struct bkey *, struct bkey *);
  321. void bch_btree_sort_lazy(struct btree *);
  322. void bch_btree_sort_into(struct btree *, struct btree *);
  323. void bch_btree_sort_and_fix_extents(struct btree *, struct btree_iter *);
  324. void bch_btree_sort_partial(struct btree *, unsigned);
  325. static inline void bch_btree_sort(struct btree *b)
  326. {
  327. bch_btree_sort_partial(b, 0);
  328. }
  329. int bch_bset_print_stats(struct cache_set *, char *);
  330. #endif