• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

crush.h

Go to the documentation of this file.
00001 #ifndef _CRUSH_CRUSH_H
00002 #define _CRUSH_CRUSH_H
00003 
00004 #include<stdint.h>
00005 
00006 /*
00007  * CRUSH is a pseudo-random data distribution algorithm that
00008  * efficiently distributes input values (typically, data objects)
00009  * across a heterogeneous, structured storage cluster.
00010  *
00011  * The algorithm was originally described in detail in this paper
00012  * (although the algorithm has evolved somewhat since then):
00013  *
00014  *     http://www.ssrc.ucsc.edu/Papers/weil-sc06.pdf
00015  *
00016  * LGPL2
00017  */
00018 
00019 
00020 #define CRUSH_MAGIC 0x00010000ul   /* for detecting algorithm revisions */
00021 
00022 
00023 #define CRUSH_MAX_DEPTH 10  /* max crush hierarchy depth */
00024 #define CRUSH_MAX_SET   10  /* max size of a mapping result */
00025 
00026 
00027 /*
00028  * CRUSH uses user-defined "rules" to describe how inputs should be
00029  * mapped to devices.  A rule consists of sequence of steps to perform
00030  * to generate the set of output devices.
00031  */
00032 struct crush_rule_step {
00033         uint32_t op;
00034         int32_t arg1;
00035         int32_t arg2;
00036 };
00037 
00038 /* step op codes */
00039 enum {
00040         CRUSH_RULE_NOOP = 0,
00041         CRUSH_RULE_TAKE = 1,          /* arg1 = value to start with */
00042         CRUSH_RULE_CHOOSE_FIRSTN = 2, /* arg1 = num items to pick */
00043                                       /* arg2 = type */
00044         CRUSH_RULE_CHOOSE_INDEP = 3,  /* same */
00045         CRUSH_RULE_EMIT = 4,          /* no args */
00046         CRUSH_RULE_CHOOSE_LEAF_FIRSTN = 6,
00047         CRUSH_RULE_CHOOSE_LEAF_INDEP = 7,
00048 };
00049 
00050 /*
00051  * for specifying choose num (arg1) relative to the max parameter
00052  * passed to do_rule
00053  */
00054 #define CRUSH_CHOOSE_N            0
00055 #define CRUSH_CHOOSE_N_MINUS(x)   (-(x))
00056 
00057 /*
00058  * The rule mask is used to describe what the rule is intended for.
00059  * Given a ruleset and size of output set, we search through the
00060  * rule list for a matching rule_mask.
00061  */
00062 struct crush_rule_mask {
00063         uint8_t ruleset;
00064         uint8_t type;
00065         uint8_t min_size;
00066         uint8_t max_size;
00067 };
00068 
00069 struct crush_rule {
00070         uint32_t len;
00071         struct crush_rule_mask mask;
00072         struct crush_rule_step steps[0];
00073 };
00074 
00075 #define crush_rule_size(len) (sizeof(struct crush_rule) + \
00076                               (len)*sizeof(struct crush_rule_step))
00077 
00078 
00079 
00080 /*
00081  * A bucket is a named container of other items (either devices or
00082  * other buckets).  Items within a bucket are chosen using one of a
00083  * few different algorithms.  The table summarizes how the speed of
00084  * each option measures up against mapping stability when items are
00085  * added or removed.
00086  *
00087  *  Bucket Alg     Speed       Additions    Removals
00088  *  ------------------------------------------------
00089  *  uniform         O(1)       poor         poor
00090  *  list            O(n)       optimal      poor
00091  *  tree            O(log n)   good         good
00092  *  straw           O(n)       optimal      optimal
00093  */
00094 enum {
00095         CRUSH_BUCKET_UNIFORM = 1,
00096         CRUSH_BUCKET_LIST = 2,
00097         CRUSH_BUCKET_TREE = 3,
00098         CRUSH_BUCKET_STRAW = 4
00099 };
00100 extern const char *crush_bucket_alg_name(int alg);
00101 
00102 struct crush_bucket {
00103         int32_t id;        /* this'll be negative */
00104         uint16_t type;      /* non-zero; type=0 is reserved for devices */
00105         uint8_t alg;        /* one of CRUSH_BUCKET_* */
00106         uint8_t hash;       /* which hash function to use, CRUSH_HASH_* */
00107         uint32_t weight;    /* 16-bit fixed point */
00108         uint32_t size;      /* num items */
00109         int32_t *items;
00110 
00111         /*
00112          * cached random permutation: used for uniform bucket and for
00113          * the linear search fallback for the other bucket types.
00114          */
00115         int64_t perm_x;  /* @x for which *perm is defined */
00116         uint32_t perm_n;  /* num elements of *perm that are permuted/defined */
00117         uint32_t *perm;
00118 };
00119 
00120 struct crush_bucket_uniform {
00121         struct crush_bucket h;
00122         uint32_t item_weight;  /* 16-bit fixed point; all items equally weighted */
00123 };
00124 
00125 struct crush_bucket_list {
00126         struct crush_bucket h;
00127         uint32_t *item_weights;  /* 16-bit fixed point */
00128         uint32_t *sum_weights;   /* 16-bit fixed point.  element i is sum
00129                                  of weights 0..i, inclusive */
00130 };
00131 
00132 struct crush_bucket_tree {
00133         struct crush_bucket h;  /* note: h.size is _tree_ size, not number of
00134                                    actual items */
00135         uint8_t num_nodes;
00136         uint32_t *node_weights;
00137 };
00138 
00139 struct crush_bucket_straw {
00140         struct crush_bucket h;
00141         uint32_t *item_weights;   /* 16-bit fixed point */
00142         uint32_t *straws;         /* 16-bit fixed point */
00143 };
00144 
00145 
00146 
00147 /*
00148  * CRUSH map includes all buckets, rules, etc.
00149  */
00150 struct crush_map {
00151         struct crush_bucket **buckets;
00152         struct crush_rule **rules;
00153 
00154         /*
00155          * Parent pointers to identify the parent bucket a device or
00156          * bucket in the hierarchy.  If an item appears more than
00157          * once, this is the _last_ time it appeared (where buckets
00158          * are processed in bucket id order, from -1 on down to
00159          * -max_buckets.
00160          */
00161         uint32_t *bucket_parents;
00162         uint32_t *device_parents;
00163 
00164         int32_t max_buckets;
00165         uint32_t max_rules;
00166         int32_t max_devices;
00167 };
00168 
00169 
00170 /* crush.c */
00171 extern int crush_get_bucket_item_weight(struct crush_bucket *b, int pos);
00172 extern void crush_calc_parents(struct crush_map *map);
00173 extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b);
00174 extern void crush_destroy_bucket_list(struct crush_bucket_list *b);
00175 extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b);
00176 extern void crush_destroy_bucket_straw(struct crush_bucket_straw *b);
00177 extern void crush_destroy_bucket(struct crush_bucket *b);
00178 extern void crush_destroy(struct crush_map *map);
00179 //}
00180 #endif

Generated on Mon Oct 11 2010 13:09:26 for CppDistributors by  doxygen 1.7.2