crush.h 5.8 KB

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