crush.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #ifndef CEPH_CRUSH_CRUSH_H
  2. #define CEPH_CRUSH_CRUSH_H
  3. #ifdef __KERNEL__
  4. # include <linux/types.h>
  5. #else
  6. # include "crush_compat.h"
  7. #endif
  8. /*
  9. * CRUSH is a pseudo-random data distribution algorithm that
  10. * efficiently distributes input values (typically, data objects)
  11. * across a heterogeneous, structured storage cluster.
  12. *
  13. * The algorithm was originally described in detail in this paper
  14. * (although the algorithm has evolved somewhat since then):
  15. *
  16. * http://www.ssrc.ucsc.edu/Papers/weil-sc06.pdf
  17. *
  18. * LGPL2
  19. */
  20. #define CRUSH_MAGIC 0x00010000ul /* for detecting algorithm revisions */
  21. #define CRUSH_MAX_DEPTH 10 /* max crush hierarchy depth */
  22. #define CRUSH_MAX_RULESET (1<<8) /* max crush ruleset number */
  23. #define CRUSH_MAX_RULES CRUSH_MAX_RULESET /* should be the same as max rulesets */
  24. #define CRUSH_MAX_DEVICE_WEIGHT (100u * 0x10000u)
  25. #define CRUSH_MAX_BUCKET_WEIGHT (65535u * 0x10000u)
  26. #define CRUSH_ITEM_UNDEF 0x7ffffffe /* undefined result (internal use only) */
  27. #define CRUSH_ITEM_NONE 0x7fffffff /* no result */
  28. /*
  29. * CRUSH uses user-defined "rules" to describe how inputs should be
  30. * mapped to devices. A rule consists of sequence of steps to perform
  31. * to generate the set of output devices.
  32. */
  33. struct crush_rule_step {
  34. __u32 op;
  35. __s32 arg1;
  36. __s32 arg2;
  37. };
  38. /* step op codes */
  39. enum {
  40. CRUSH_RULE_NOOP = 0,
  41. CRUSH_RULE_TAKE = 1, /* arg1 = value to start with */
  42. CRUSH_RULE_CHOOSE_FIRSTN = 2, /* arg1 = num items to pick */
  43. /* arg2 = type */
  44. CRUSH_RULE_CHOOSE_INDEP = 3, /* same */
  45. CRUSH_RULE_EMIT = 4, /* no args */
  46. CRUSH_RULE_CHOOSELEAF_FIRSTN = 6,
  47. CRUSH_RULE_CHOOSELEAF_INDEP = 7,
  48. CRUSH_RULE_SET_CHOOSE_TRIES = 8, /* override choose_total_tries */
  49. CRUSH_RULE_SET_CHOOSELEAF_TRIES = 9, /* override chooseleaf_descend_once */
  50. CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES = 10,
  51. CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES = 11,
  52. CRUSH_RULE_SET_CHOOSELEAF_VARY_R = 12,
  53. CRUSH_RULE_SET_CHOOSELEAF_STABLE = 13
  54. };
  55. /*
  56. * for specifying choose num (arg1) relative to the max parameter
  57. * passed to do_rule
  58. */
  59. #define CRUSH_CHOOSE_N 0
  60. #define CRUSH_CHOOSE_N_MINUS(x) (-(x))
  61. /*
  62. * The rule mask is used to describe what the rule is intended for.
  63. * Given a ruleset and size of output set, we search through the
  64. * rule list for a matching rule_mask.
  65. */
  66. struct crush_rule_mask {
  67. __u8 ruleset;
  68. __u8 type;
  69. __u8 min_size;
  70. __u8 max_size;
  71. };
  72. struct crush_rule {
  73. __u32 len;
  74. struct crush_rule_mask mask;
  75. struct crush_rule_step steps[0];
  76. };
  77. #define crush_rule_size(len) (sizeof(struct crush_rule) + \
  78. (len)*sizeof(struct crush_rule_step))
  79. /*
  80. * A bucket is a named container of other items (either devices or
  81. * other buckets). Items within a bucket are chosen using one of a
  82. * few different algorithms. The table summarizes how the speed of
  83. * each option measures up against mapping stability when items are
  84. * added or removed.
  85. *
  86. * Bucket Alg Speed Additions Removals
  87. * ------------------------------------------------
  88. * uniform O(1) poor poor
  89. * list O(n) optimal poor
  90. * tree O(log n) good good
  91. * straw O(n) better better
  92. * straw2 O(n) optimal optimal
  93. */
  94. enum {
  95. CRUSH_BUCKET_UNIFORM = 1,
  96. CRUSH_BUCKET_LIST = 2,
  97. CRUSH_BUCKET_TREE = 3,
  98. CRUSH_BUCKET_STRAW = 4,
  99. CRUSH_BUCKET_STRAW2 = 5,
  100. };
  101. extern const char *crush_bucket_alg_name(int alg);
  102. /*
  103. * although tree was a legacy algorithm, it has been buggy, so
  104. * exclude it.
  105. */
  106. #define CRUSH_LEGACY_ALLOWED_BUCKET_ALGS ( \
  107. (1 << CRUSH_BUCKET_UNIFORM) | \
  108. (1 << CRUSH_BUCKET_LIST) | \
  109. (1 << CRUSH_BUCKET_STRAW))
  110. struct crush_bucket {
  111. __s32 id; /* this'll be negative */
  112. __u16 type; /* non-zero; type=0 is reserved for devices */
  113. __u8 alg; /* one of CRUSH_BUCKET_* */
  114. __u8 hash; /* which hash function to use, CRUSH_HASH_* */
  115. __u32 weight; /* 16-bit fixed point */
  116. __u32 size; /* num items */
  117. __s32 *items;
  118. /*
  119. * cached random permutation: used for uniform bucket and for
  120. * the linear search fallback for the other bucket types.
  121. */
  122. __u32 perm_x; /* @x for which *perm is defined */
  123. __u32 perm_n; /* num elements of *perm that are permuted/defined */
  124. __u32 *perm;
  125. };
  126. struct crush_bucket_uniform {
  127. struct crush_bucket h;
  128. __u32 item_weight; /* 16-bit fixed point; all items equally weighted */
  129. };
  130. struct crush_bucket_list {
  131. struct crush_bucket h;
  132. __u32 *item_weights; /* 16-bit fixed point */
  133. __u32 *sum_weights; /* 16-bit fixed point. element i is sum
  134. of weights 0..i, inclusive */
  135. };
  136. struct crush_bucket_tree {
  137. struct crush_bucket h; /* note: h.size is _tree_ size, not number of
  138. actual items */
  139. __u8 num_nodes;
  140. __u32 *node_weights;
  141. };
  142. struct crush_bucket_straw {
  143. struct crush_bucket h;
  144. __u32 *item_weights; /* 16-bit fixed point */
  145. __u32 *straws; /* 16-bit fixed point */
  146. };
  147. struct crush_bucket_straw2 {
  148. struct crush_bucket h;
  149. __u32 *item_weights; /* 16-bit fixed point */
  150. };
  151. /*
  152. * CRUSH map includes all buckets, rules, etc.
  153. */
  154. struct crush_map {
  155. struct crush_bucket **buckets;
  156. struct crush_rule **rules;
  157. __s32 max_buckets;
  158. __u32 max_rules;
  159. __s32 max_devices;
  160. /* choose local retries before re-descent */
  161. __u32 choose_local_tries;
  162. /* choose local attempts using a fallback permutation before
  163. * re-descent */
  164. __u32 choose_local_fallback_tries;
  165. /* choose attempts before giving up */
  166. __u32 choose_total_tries;
  167. /* attempt chooseleaf inner descent once for firstn mode; on
  168. * reject retry outer descent. Note that this does *not*
  169. * apply to a collision: in that case we will retry as we used
  170. * to. */
  171. __u32 chooseleaf_descend_once;
  172. /* if non-zero, feed r into chooseleaf, bit-shifted right by (r-1)
  173. * bits. a value of 1 is best for new clusters. for legacy clusters
  174. * that want to limit reshuffling, a value of 3 or 4 will make the
  175. * mappings line up a bit better with previous mappings. */
  176. __u8 chooseleaf_vary_r;
  177. /* if true, it makes chooseleaf firstn to return stable results (if
  178. * no local retry) so that data migrations would be optimal when some
  179. * device fails. */
  180. __u8 chooseleaf_stable;
  181. #ifndef __KERNEL__
  182. /*
  183. * version 0 (original) of straw_calc has various flaws. version 1
  184. * fixes a few of them.
  185. */
  186. __u8 straw_calc_version;
  187. /*
  188. * allowed bucket algs is a bitmask, here the bit positions
  189. * are CRUSH_BUCKET_*. note that these are *bits* and
  190. * CRUSH_BUCKET_* values are not, so we need to or together (1
  191. * << CRUSH_BUCKET_WHATEVER). The 0th bit is not used to
  192. * minimize confusion (bucket type values start at 1).
  193. */
  194. __u32 allowed_bucket_algs;
  195. __u32 *choose_tries;
  196. #endif
  197. };
  198. /* crush.c */
  199. extern int crush_get_bucket_item_weight(const struct crush_bucket *b, int pos);
  200. extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b);
  201. extern void crush_destroy_bucket_list(struct crush_bucket_list *b);
  202. extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b);
  203. extern void crush_destroy_bucket_straw(struct crush_bucket_straw *b);
  204. extern void crush_destroy_bucket_straw2(struct crush_bucket_straw2 *b);
  205. extern void crush_destroy_bucket(struct crush_bucket *b);
  206. extern void crush_destroy_rule(struct crush_rule *r);
  207. extern void crush_destroy(struct crush_map *map);
  208. static inline int crush_calc_tree_node(int i)
  209. {
  210. return ((i+1) << 1)-1;
  211. }
  212. #endif