2 * Ceph - scalable distributed file system
4 * Copyright (C) 2015 Intel Corporation All Rights Reserved
6 * This is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software
9 * Foundation. See file COPYING.
14 # include <linux/string.h>
15 # include <linux/slab.h>
16 # include <linux/bug.h>
17 # include <linux/kernel.h>
18 # include <linux/crush/crush.h>
19 # include <linux/crush/hash.h>
21 # include "crush_compat.h"
25 #include "crush_ln_table.h"
27 #define dprintk(args...) /* printf(args) */
30 * Implement the core CRUSH mapping algorithm.
34 * crush_find_rule - find a crush_rule id for a given ruleset, type, and size.
36 * @ruleset: the storage ruleset id (user defined)
37 * @type: storage ruleset type (user defined)
38 * @size: output set size
40 int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
44 for (i = 0; i < map->max_rules; i++) {
46 map->rules[i]->mask.ruleset == ruleset &&
47 map->rules[i]->mask.type == type &&
48 map->rules[i]->mask.min_size <= size &&
49 map->rules[i]->mask.max_size >= size)
57 * bucket choose methods
59 * For each bucket algorithm, we have a "choose" method that, given a
60 * crush input @x and replica position (usually, position in output set) @r,
61 * will produce an item in the bucket.
65 * Choose based on a random permutation of the bucket.
67 * We used to use some prime number arithmetic to do this, but it
68 * wasn't very random, and had some other bad behaviors. Instead, we
69 * calculate an actual random permutation of the bucket members.
70 * Since this is expensive, we optimize for the r=0 case, which
71 * captures the vast majority of calls.
73 static int bucket_perm_choose(struct crush_bucket *bucket,
76 unsigned int pr = r % bucket->size;
79 /* start a new permutation if @x has changed */
80 if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
81 dprintk("bucket %d new x=%d\n", bucket->id, x);
84 /* optimize common r=0 case */
86 s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
89 bucket->perm_n = 0xffff; /* magic value, see below */
93 for (i = 0; i < bucket->size; i++)
96 } else if (bucket->perm_n == 0xffff) {
97 /* clean up after the r=0 case above */
98 for (i = 1; i < bucket->size; i++)
100 bucket->perm[bucket->perm[0]] = 0;
104 /* calculate permutation up to pr */
105 for (i = 0; i < bucket->perm_n; i++)
106 dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]);
107 while (bucket->perm_n <= pr) {
108 unsigned int p = bucket->perm_n;
109 /* no point in swapping the final entry */
110 if (p < bucket->size - 1) {
111 i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
114 unsigned int t = bucket->perm[p + i];
115 bucket->perm[p + i] = bucket->perm[p];
118 dprintk(" perm_choose swap %d with %d\n", p, p+i);
122 for (i = 0; i < bucket->size; i++)
123 dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]);
125 s = bucket->perm[pr];
127 dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id,
128 bucket->size, x, r, pr, s);
129 return bucket->items[s];
133 static int bucket_uniform_choose(struct crush_bucket_uniform *bucket,
136 return bucket_perm_choose(&bucket->h, x, r);
140 static int bucket_list_choose(struct crush_bucket_list *bucket,
145 for (i = bucket->h.size-1; i >= 0; i--) {
146 __u64 w = crush_hash32_4(bucket->h.hash, x, bucket->h.items[i],
149 dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
151 i, x, r, bucket->h.items[i], bucket->item_weights[i],
152 bucket->sum_weights[i], w);
153 w *= bucket->sum_weights[i];
155 /*dprintk(" scaled %llx\n", w);*/
156 if (w < bucket->item_weights[i])
157 return bucket->h.items[i];
160 dprintk("bad list sums for bucket %d\n", bucket->h.id);
161 return bucket->h.items[0];
166 static int height(int n)
169 while ((n & 1) == 0) {
176 static int left(int x)
179 return x - (1 << (h-1));
182 static int right(int x)
185 return x + (1 << (h-1));
188 static int terminal(int x)
193 static int bucket_tree_choose(struct crush_bucket_tree *bucket,
201 n = bucket->num_nodes >> 1;
203 while (!terminal(n)) {
205 /* pick point in [0, w) */
206 w = bucket->node_weights[n];
207 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
208 bucket->h.id) * (__u64)w;
211 /* descend to the left or right? */
213 if (t < bucket->node_weights[l])
219 return bucket->h.items[n >> 1];
225 static int bucket_straw_choose(struct crush_bucket_straw *bucket,
233 for (i = 0; i < bucket->h.size; i++) {
234 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
236 draw *= bucket->straws[i];
237 if (i == 0 || draw > high_draw) {
242 return bucket->h.items[high];
245 /* compute 2^44*log2(input+1) */
246 static __u64 crush_ln(unsigned int xin)
248 unsigned int x = xin, x1;
249 int iexpon, index1, index2;
250 __u64 RH, LH, LL, xl64, result;
254 /* normalize input */
256 while (!(x & 0x18000)) {
261 index1 = (x >> 8) << 1;
262 /* RH ~ 2^56/index1 */
263 RH = __RH_LH_tbl[index1 - 256];
264 /* LH ~ 2^48 * log2(index1/256) */
265 LH = __RH_LH_tbl[index1 + 1 - 256];
267 /* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */
268 xl64 = (__s64)x * RH;
273 result <<= (12 + 32);
276 /* LL ~ 2^48*log2(1.0+index2/2^15) */
277 LL = __LL_tbl[index2];
281 LH >>= (48 - 12 - 32);
291 * for reference, see:
293 * http://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables
297 static int bucket_straw2_choose(struct crush_bucket_straw2 *bucket,
300 unsigned int i, high = 0;
303 __s64 ln, draw, high_draw = 0;
305 for (i = 0; i < bucket->h.size; i++) {
306 w = bucket->item_weights[i];
308 u = crush_hash32_3(bucket->h.hash, x,
309 bucket->h.items[i], r);
313 * for some reason slightly less than 0x10000 produces
314 * a slightly more accurate distribution... probably a
317 * the natural log lookup table maps [0,0xffff]
318 * (corresponding to real numbers [1/0x10000, 1] to
319 * [0, 0xffffffffffff] (corresponding to real numbers
322 ln = crush_ln(u) - 0x1000000000000ll;
325 * divide by 16.16 fixed-point weight. note
326 * that the ln value is negative, so a larger
327 * weight means a larger (less negative) value
330 draw = div64_s64(ln, w);
335 if (i == 0 || draw > high_draw) {
340 return bucket->h.items[high];
344 static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
346 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
347 BUG_ON(in->size == 0);
349 case CRUSH_BUCKET_UNIFORM:
350 return bucket_uniform_choose((struct crush_bucket_uniform *)in,
352 case CRUSH_BUCKET_LIST:
353 return bucket_list_choose((struct crush_bucket_list *)in,
355 case CRUSH_BUCKET_TREE:
356 return bucket_tree_choose((struct crush_bucket_tree *)in,
358 case CRUSH_BUCKET_STRAW:
359 return bucket_straw_choose((struct crush_bucket_straw *)in,
361 case CRUSH_BUCKET_STRAW2:
362 return bucket_straw2_choose((struct crush_bucket_straw2 *)in,
365 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
372 * true if device is marked "out" (failed, fully offloaded)
375 static int is_out(const struct crush_map *map,
376 const __u32 *weight, int weight_max,
379 if (item >= weight_max)
381 if (weight[item] >= 0x10000)
383 if (weight[item] == 0)
385 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
392 * crush_choose_firstn - choose numrep distinct items of given type
393 * @map: the crush_map
394 * @bucket: the bucket we are choose an item from
395 * @x: crush input value
396 * @numrep: the number of items to choose
397 * @type: the type of item to choose
398 * @out: pointer to output vector
399 * @outpos: our position in that vector
400 * @out_size: size of the out vector
401 * @tries: number of attempts to make
402 * @recurse_tries: number of attempts to have recursive chooseleaf make
403 * @local_retries: localized retries
404 * @local_fallback_retries: localized fallback retries
405 * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
406 * @vary_r: pass r to recursive calls
407 * @out2: second output vector for leaf items (if @recurse_to_leaf)
408 * @parent_r: r value passed from the parent
410 static int crush_choose_firstn(const struct crush_map *map,
411 struct crush_bucket *bucket,
412 const __u32 *weight, int weight_max,
413 int x, int numrep, int type,
414 int *out, int outpos,
417 unsigned int recurse_tries,
418 unsigned int local_retries,
419 unsigned int local_fallback_retries,
426 unsigned int ftotal, flocal;
427 int retry_descent, retry_bucket, skip_rep;
428 struct crush_bucket *in = bucket;
434 int count = out_size;
436 dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d tries %d recurse_tries %d local_retries %d local_fallback_retries %d parent_r %d\n",
437 recurse_to_leaf ? "_LEAF" : "",
438 bucket->id, x, outpos, numrep,
439 tries, recurse_tries, local_retries, local_fallback_retries,
442 for (rep = outpos; rep < numrep && count > 0 ; rep++) {
443 /* keep trying until we get a non-out, non-colliding item */
448 in = bucket; /* initial bucket */
450 /* choose through intervening buckets */
456 /* r' = r + f_total */
464 if (local_fallback_retries > 0 &&
465 flocal >= (in->size>>1) &&
466 flocal > local_fallback_retries)
467 item = bucket_perm_choose(in, x, r);
469 item = crush_bucket_choose(in, x, r);
470 if (item >= map->max_devices) {
471 dprintk(" bad item %d\n", item);
478 itemtype = map->buckets[-1-item]->type;
481 dprintk(" item %d type %d\n", item, itemtype);
484 if (itemtype != type) {
486 (-1-item) >= map->max_buckets) {
487 dprintk(" bad item type %d\n", type);
491 in = map->buckets[-1-item];
497 for (i = 0; i < outpos; i++) {
498 if (out[i] == item) {
505 if (!collide && recurse_to_leaf) {
509 sub_r = r >> (vary_r-1);
512 if (crush_choose_firstn(map,
513 map->buckets[-1-item],
519 local_fallback_retries,
524 /* didn't get leaf */
527 /* we already have a leaf! */
535 reject = is_out(map, weight,
543 if (reject || collide) {
547 if (collide && flocal <= local_retries)
548 /* retry locally a few times */
550 else if (local_fallback_retries > 0 &&
551 flocal <= in->size + local_fallback_retries)
552 /* exhaustive bucket search */
554 else if (ftotal < tries)
555 /* then retry descent */
560 dprintk(" reject %d collide %d "
561 "ftotal %u flocal %u\n",
562 reject, collide, ftotal,
565 } while (retry_bucket);
566 } while (retry_descent);
569 dprintk("skip rep\n");
573 dprintk("CHOOSE got %d\n", item);
578 if (map->choose_tries && ftotal <= map->choose_total_tries)
579 map->choose_tries[ftotal]++;
583 dprintk("CHOOSE returns %d\n", outpos);
589 * crush_choose_indep: alternative breadth-first positionally stable mapping
592 static void crush_choose_indep(const struct crush_map *map,
593 struct crush_bucket *bucket,
594 const __u32 *weight, int weight_max,
595 int x, int left, int numrep, int type,
596 int *out, int outpos,
598 unsigned int recurse_tries,
603 struct crush_bucket *in = bucket;
604 int endpos = outpos + left;
613 dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
614 bucket->id, x, outpos, numrep);
616 /* initially my result is undefined */
617 for (rep = outpos; rep < endpos; rep++) {
618 out[rep] = CRUSH_ITEM_UNDEF;
620 out2[rep] = CRUSH_ITEM_UNDEF;
623 for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
625 if (out2 && ftotal) {
626 dprintk("%u %d a: ", ftotal, left);
627 for (rep = outpos; rep < endpos; rep++) {
628 dprintk(" %d", out[rep]);
631 dprintk("%u %d b: ", ftotal, left);
632 for (rep = outpos; rep < endpos; rep++) {
633 dprintk(" %d", out2[rep]);
638 for (rep = outpos; rep < endpos; rep++) {
639 if (out[rep] != CRUSH_ITEM_UNDEF)
642 in = bucket; /* initial bucket */
644 /* choose through intervening buckets */
646 /* note: we base the choice on the position
647 * even in the nested call. that means that
648 * if the first layer chooses the same bucket
649 * in a different position, we will tend to
650 * choose a different item in that bucket.
651 * this will involve more devices in data
652 * movement and tend to distribute the load.
657 if (in->alg == CRUSH_BUCKET_UNIFORM &&
658 in->size % numrep == 0)
659 /* r'=r+(n+1)*f_total */
660 r += (numrep+1) * ftotal;
662 /* r' = r + n*f_total */
663 r += numrep * ftotal;
667 dprintk(" empty bucket\n");
671 item = crush_bucket_choose(in, x, r);
672 if (item >= map->max_devices) {
673 dprintk(" bad item %d\n", item);
674 out[rep] = CRUSH_ITEM_NONE;
676 out2[rep] = CRUSH_ITEM_NONE;
683 itemtype = map->buckets[-1-item]->type;
686 dprintk(" item %d type %d\n", item, itemtype);
689 if (itemtype != type) {
691 (-1-item) >= map->max_buckets) {
692 dprintk(" bad item type %d\n", type);
693 out[rep] = CRUSH_ITEM_NONE;
700 in = map->buckets[-1-item];
706 for (i = outpos; i < endpos; i++) {
707 if (out[i] == item) {
715 if (recurse_to_leaf) {
717 crush_choose_indep(map,
718 map->buckets[-1-item],
724 if (out2[rep] == CRUSH_ITEM_NONE) {
725 /* placed nothing; no leaf */
729 /* we already have a leaf! */
736 is_out(map, weight, weight_max, item, x))
746 for (rep = outpos; rep < endpos; rep++) {
747 if (out[rep] == CRUSH_ITEM_UNDEF) {
748 out[rep] = CRUSH_ITEM_NONE;
750 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
751 out2[rep] = CRUSH_ITEM_NONE;
755 if (map->choose_tries && ftotal <= map->choose_total_tries)
756 map->choose_tries[ftotal]++;
760 dprintk("%u %d a: ", ftotal, left);
761 for (rep = outpos; rep < endpos; rep++) {
762 dprintk(" %d", out[rep]);
765 dprintk("%u %d b: ", ftotal, left);
766 for (rep = outpos; rep < endpos; rep++) {
767 dprintk(" %d", out2[rep]);
775 * crush_do_rule - calculate a mapping with the given input and rule
776 * @map: the crush_map
777 * @ruleno: the rule id
779 * @result: pointer to result vector
780 * @result_max: maximum result size
781 * @weight: weight vector (for map leaves)
782 * @weight_max: size of weight vector
783 * @scratch: scratch vector for private use; must be >= 3 * result_max
785 int crush_do_rule(const struct crush_map *map,
786 int ruleno, int x, int *result, int result_max,
787 const __u32 *weight, int weight_max,
792 int *b = scratch + result_max;
793 int *c = scratch + result_max*2;
800 struct crush_rule *rule;
806 * the original choose_total_tries value was off by one (it
807 * counted "retries" and not "tries"). add one.
809 int choose_tries = map->choose_total_tries + 1;
810 int choose_leaf_tries = 0;
812 * the local tries values were counted as "retries", though,
813 * and need no adjustment
815 int choose_local_retries = map->choose_local_tries;
816 int choose_local_fallback_retries = map->choose_local_fallback_tries;
818 int vary_r = map->chooseleaf_vary_r;
820 if ((__u32)ruleno >= map->max_rules) {
821 dprintk(" bad ruleno %d\n", ruleno);
825 rule = map->rules[ruleno];
830 for (step = 0; step < rule->len; step++) {
832 struct crush_rule_step *curstep = &rule->steps[step];
834 switch (curstep->op) {
835 case CRUSH_RULE_TAKE:
836 if ((curstep->arg1 >= 0 &&
837 curstep->arg1 < map->max_devices) ||
838 (-1-curstep->arg1 < map->max_buckets &&
839 map->buckets[-1-curstep->arg1])) {
840 w[0] = curstep->arg1;
843 dprintk(" bad take value %d\n", curstep->arg1);
847 case CRUSH_RULE_SET_CHOOSE_TRIES:
848 if (curstep->arg1 > 0)
849 choose_tries = curstep->arg1;
852 case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
853 if (curstep->arg1 > 0)
854 choose_leaf_tries = curstep->arg1;
857 case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
858 if (curstep->arg1 >= 0)
859 choose_local_retries = curstep->arg1;
862 case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
863 if (curstep->arg1 >= 0)
864 choose_local_fallback_retries = curstep->arg1;
867 case CRUSH_RULE_SET_CHOOSELEAF_VARY_R:
868 if (curstep->arg1 >= 0)
869 vary_r = curstep->arg1;
872 case CRUSH_RULE_CHOOSELEAF_FIRSTN:
873 case CRUSH_RULE_CHOOSE_FIRSTN:
876 case CRUSH_RULE_CHOOSELEAF_INDEP:
877 case CRUSH_RULE_CHOOSE_INDEP:
883 CRUSH_RULE_CHOOSELEAF_FIRSTN ||
885 CRUSH_RULE_CHOOSELEAF_INDEP;
890 for (i = 0; i < wsize; i++) {
892 * see CRUSH_N, CRUSH_N_MINUS macros.
893 * basically, numrep <= 0 means relative to
894 * the provided result_max
896 numrep = curstep->arg1;
898 numrep += result_max;
905 if (choose_leaf_tries)
908 else if (map->chooseleaf_descend_once)
911 recurse_tries = choose_tries;
912 osize += crush_choose_firstn(
914 map->buckets[-1-w[i]],
922 choose_local_retries,
923 choose_local_fallback_retries,
929 out_size = ((numrep < (result_max-osize)) ?
930 numrep : (result_max-osize));
933 map->buckets[-1-w[i]],
940 choose_leaf_tries : 1,
949 /* copy final _leaf_ values to output set */
950 memcpy(o, c, osize*sizeof(*o));
952 /* swap o and w arrays */
960 case CRUSH_RULE_EMIT:
961 for (i = 0; i < wsize && result_len < result_max; i++) {
962 result[result_len] = w[i];
969 dprintk(" unknown op %d at step %d\n",