crush.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_MAX_SET 10 /* max size of a mapping result */
  19. /*
  20. * CRUSH uses user-defined "rules" to describe how inputs should be
  21. * mapped to devices. A rule consists of sequence of steps to perform
  22. * to generate the set of output devices.
  23. */
  24. struct crush_rule_step {
  25. __u32 op;
  26. __s32 arg1;
  27. __s32 arg2;
  28. };
  29. /* step op codes */
  30. enum {
  31. CRUSH_RULE_NOOP = 0,
  32. CRUSH_RULE_TAKE = 1, /* arg1 = value to start with */
  33. CRUSH_RULE_CHOOSE_FIRSTN = 2, /* arg1 = num items to pick */
  34. /* arg2 = type */
  35. CRUSH_RULE_CHOOSE_INDEP = 3, /* same */
  36. CRUSH_RULE_EMIT = 4, /* no args */
  37. CRUSH_RULE_CHOOSE_LEAF_FIRSTN = 6,
  38. CRUSH_RULE_CHOOSE_LEAF_INDEP = 7,
  39. };
  40. /*
  41. * for specifying choose num (arg1) relative to the max parameter
  42. * passed to do_rule
  43. */
  44. #define CRUSH_CHOOSE_N 0
  45. #define CRUSH_CHOOSE_N_MINUS(x) (-(x))
  46. /*
  47. * The rule mask is used to describe what the rule is intended for.
  48. * Given a ruleset and size of output set, we search through the
  49. * rule list for a matching rule_mask.
  50. */
  51. struct crush_rule_mask {
  52. __u8 ruleset;
  53. __u8 type;
  54. __u8 min_size;
  55. __u8 max_size;
  56. };
  57. struct crush_rule {
  58. __u32 len;
  59. struct crush_rule_mask mask;
  60. struct crush_rule_step steps[0];
  61. };
  62. #define crush_rule_size(len) (sizeof(struct crush_rule) + \
  63. (len)*sizeof(struct crush_rule_step))
  64. /*
  65. * A bucket is a named container of other items (either devices or
  66. * other buckets). Items within a bucket are chosen using one of a
  67. * few different algorithms. The table summarizes how the speed of
  68. * each option measures up against mapping stability when items are
  69. * added or removed.
  70. *
  71. * Bucket Alg Speed Additions Removals
  72. * ------------------------------------------------
  73. * uniform O(1) poor poor
  74. * list O(n) optimal poor
  75. * tree O(log n) good good
  76. * straw O(n) optimal optimal
  77. */
  78. enum {
  79. CRUSH_BUCKET_UNIFORM = 1,
  80. CRUSH_BUCKET_LIST = 2,
  81. CRUSH_BUCKET_TREE = 3,
  82. CRUSH_BUCKET_STRAW = 4
  83. };
  84. extern const char *crush_bucket_alg_name(int alg);
  85. struct crush_bucket {
  86. __s32 id; /* this'll be negative */
  87. __u16 type; /* non-zero; type=0 is reserved for devices */
  88. __u8 alg; /* one of CRUSH_BUCKET_* */
  89. __u8 hash; /* which hash function to use, CRUSH_HASH_* */
  90. __u32 weight; /* 16-bit fixed point */
  91. __u32 size; /* num items */
  92. __s32 *items;
  93. /*
  94. * cached random permutation: used for uniform bucket and for
  95. * the linear search fallback for the other bucket types.
  96. */
  97. __u32 perm_x; /* @x for which *perm is defined */
  98. __u32 perm_n; /* num elements of *perm that are permuted/defined */
  99. __u32 *perm;
  100. };
  101. struct crush_bucket_uniform {
  102. struct crush_bucket h;
  103. __u32 item_weight; /* 16-bit fixed point; all items equally weighted */
  104. };
  105. struct crush_bucket_list {
  106. struct crush_bucket h;
  107. __u32 *item_weights; /* 16-bit fixed point */
  108. __u32 *sum_weights; /* 16-bit fixed point. element i is sum
  109. of weights 0..i, inclusive */
  110. };
  111. struct crush_bucket_tree {
  112. struct crush_bucket h; /* note: h.size is _tree_ size, not number of
  113. actual items */
  114. __u8 num_nodes;
  115. __u32 *node_weights;
  116. };
  117. struct crush_bucket_straw {
  118. struct crush_bucket h;
  119. __u32 *item_weights; /* 16-bit fixed point */
  120. __u32 *straws; /* 16-bit fixed point */
  121. };
  122. /*
  123. * CRUSH map includes all buckets, rules, etc.
  124. */
  125. struct crush_map {
  126. struct crush_bucket **buckets;
  127. struct crush_rule **rules;
  128. /*
  129. * Parent pointers to identify the parent bucket a device or
  130. * bucket in the hierarchy. If an item appears more than
  131. * once, this is the _last_ time it appeared (where buckets
  132. * are processed in bucket id order, from -1 on down to
  133. * -max_buckets.
  134. */
  135. __u32 *bucket_parents;
  136. __u32 *device_parents;
  137. __s32 max_buckets;
  138. __u32 max_rules;
  139. __s32 max_devices;
  140. };
  141. /* crush.c */
  142. extern int crush_get_bucket_item_weight(const struct crush_bucket *b, int pos);
  143. extern void crush_calc_parents(struct crush_map *map);
  144. extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b);
  145. extern void crush_destroy_bucket_list(struct crush_bucket_list *b);
  146. extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b);
  147. extern void crush_destroy_bucket_straw(struct crush_bucket_straw *b);
  148. extern void crush_destroy_bucket(struct crush_bucket *b);
  149. extern void crush_destroy(struct crush_map *map);
  150. static inline int crush_calc_tree_node(int i)
  151. {
  152. return ((i+1) << 1)-1;
  153. }
  154. #endif