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;
249 int iexpon, index1, index2;
250 __u64 RH, LH, LL, xl64, result;
254 /* normalize input */
258 * figure out number of bits we need to shift and
259 * do it in one step instead of iteratively
261 if (!(x & 0x18000)) {
262 int bits = __builtin_clz(x & 0x1FFFF) - 16;
267 index1 = (x >> 8) << 1;
268 /* RH ~ 2^56/index1 */
269 RH = __RH_LH_tbl[index1 - 256];
270 /* LH ~ 2^48 * log2(index1/256) */
271 LH = __RH_LH_tbl[index1 + 1 - 256];
273 /* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */
274 xl64 = (__s64)x * RH;
278 result <<= (12 + 32);
280 index2 = xl64 & 0xff;
281 /* LL ~ 2^48*log2(1.0+index2/2^15) */
282 LL = __LL_tbl[index2];
286 LH >>= (48 - 12 - 32);
296 * for reference, see:
298 * http://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables
302 static int bucket_straw2_choose(struct crush_bucket_straw2 *bucket,
305 unsigned int i, high = 0;
308 __s64 ln, draw, high_draw = 0;
310 for (i = 0; i < bucket->h.size; i++) {
311 w = bucket->item_weights[i];
313 u = crush_hash32_3(bucket->h.hash, x,
314 bucket->h.items[i], r);
318 * for some reason slightly less than 0x10000 produces
319 * a slightly more accurate distribution... probably a
322 * the natural log lookup table maps [0,0xffff]
323 * (corresponding to real numbers [1/0x10000, 1] to
324 * [0, 0xffffffffffff] (corresponding to real numbers
327 ln = crush_ln(u) - 0x1000000000000ll;
330 * divide by 16.16 fixed-point weight. note
331 * that the ln value is negative, so a larger
332 * weight means a larger (less negative) value
335 draw = div64_s64(ln, w);
340 if (i == 0 || draw > high_draw) {
345 return bucket->h.items[high];
349 static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
351 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
352 BUG_ON(in->size == 0);
354 case CRUSH_BUCKET_UNIFORM:
355 return bucket_uniform_choose((struct crush_bucket_uniform *)in,
357 case CRUSH_BUCKET_LIST:
358 return bucket_list_choose((struct crush_bucket_list *)in,
360 case CRUSH_BUCKET_TREE:
361 return bucket_tree_choose((struct crush_bucket_tree *)in,
363 case CRUSH_BUCKET_STRAW:
364 return bucket_straw_choose((struct crush_bucket_straw *)in,
366 case CRUSH_BUCKET_STRAW2:
367 return bucket_straw2_choose((struct crush_bucket_straw2 *)in,
370 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
377 * true if device is marked "out" (failed, fully offloaded)
380 static int is_out(const struct crush_map *map,
381 const __u32 *weight, int weight_max,
384 if (item >= weight_max)
386 if (weight[item] >= 0x10000)
388 if (weight[item] == 0)
390 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
397 * crush_choose_firstn - choose numrep distinct items of given type
398 * @map: the crush_map
399 * @bucket: the bucket we are choose an item from
400 * @x: crush input value
401 * @numrep: the number of items to choose
402 * @type: the type of item to choose
403 * @out: pointer to output vector
404 * @outpos: our position in that vector
405 * @out_size: size of the out vector
406 * @tries: number of attempts to make
407 * @recurse_tries: number of attempts to have recursive chooseleaf make
408 * @local_retries: localized retries
409 * @local_fallback_retries: localized fallback retries
410 * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
411 * @stable: stable mode starts rep=0 in the recursive call for all replicas
412 * @vary_r: pass r to recursive calls
413 * @out2: second output vector for leaf items (if @recurse_to_leaf)
414 * @parent_r: r value passed from the parent
416 static int crush_choose_firstn(const struct crush_map *map,
417 struct crush_bucket *bucket,
418 const __u32 *weight, int weight_max,
419 int x, int numrep, int type,
420 int *out, int outpos,
423 unsigned int recurse_tries,
424 unsigned int local_retries,
425 unsigned int local_fallback_retries,
433 unsigned int ftotal, flocal;
434 int retry_descent, retry_bucket, skip_rep;
435 struct crush_bucket *in = bucket;
441 int count = out_size;
443 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 stable %d\n",
444 recurse_to_leaf ? "_LEAF" : "",
445 bucket->id, x, outpos, numrep,
446 tries, recurse_tries, local_retries, local_fallback_retries,
449 for (rep = stable ? 0 : outpos; rep < numrep && count > 0 ; rep++) {
450 /* keep trying until we get a non-out, non-colliding item */
455 in = bucket; /* initial bucket */
457 /* choose through intervening buckets */
463 /* r' = r + f_total */
471 if (local_fallback_retries > 0 &&
472 flocal >= (in->size>>1) &&
473 flocal > local_fallback_retries)
474 item = bucket_perm_choose(in, x, r);
476 item = crush_bucket_choose(in, x, r);
477 if (item >= map->max_devices) {
478 dprintk(" bad item %d\n", item);
485 itemtype = map->buckets[-1-item]->type;
488 dprintk(" item %d type %d\n", item, itemtype);
491 if (itemtype != type) {
493 (-1-item) >= map->max_buckets) {
494 dprintk(" bad item type %d\n", type);
498 in = map->buckets[-1-item];
504 for (i = 0; i < outpos; i++) {
505 if (out[i] == item) {
512 if (!collide && recurse_to_leaf) {
516 sub_r = r >> (vary_r-1);
519 if (crush_choose_firstn(map,
520 map->buckets[-1-item],
522 x, stable ? 1 : outpos+1, 0,
526 local_fallback_retries,
532 /* didn't get leaf */
535 /* we already have a leaf! */
543 reject = is_out(map, weight,
551 if (reject || collide) {
555 if (collide && flocal <= local_retries)
556 /* retry locally a few times */
558 else if (local_fallback_retries > 0 &&
559 flocal <= in->size + local_fallback_retries)
560 /* exhaustive bucket search */
562 else if (ftotal < tries)
563 /* then retry descent */
568 dprintk(" reject %d collide %d "
569 "ftotal %u flocal %u\n",
570 reject, collide, ftotal,
573 } while (retry_bucket);
574 } while (retry_descent);
577 dprintk("skip rep\n");
581 dprintk("CHOOSE got %d\n", item);
586 if (map->choose_tries && ftotal <= map->choose_total_tries)
587 map->choose_tries[ftotal]++;
591 dprintk("CHOOSE returns %d\n", outpos);
597 * crush_choose_indep: alternative breadth-first positionally stable mapping
600 static void crush_choose_indep(const struct crush_map *map,
601 struct crush_bucket *bucket,
602 const __u32 *weight, int weight_max,
603 int x, int left, int numrep, int type,
604 int *out, int outpos,
606 unsigned int recurse_tries,
611 struct crush_bucket *in = bucket;
612 int endpos = outpos + left;
621 dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
622 bucket->id, x, outpos, numrep);
624 /* initially my result is undefined */
625 for (rep = outpos; rep < endpos; rep++) {
626 out[rep] = CRUSH_ITEM_UNDEF;
628 out2[rep] = CRUSH_ITEM_UNDEF;
631 for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
633 if (out2 && ftotal) {
634 dprintk("%u %d a: ", ftotal, left);
635 for (rep = outpos; rep < endpos; rep++) {
636 dprintk(" %d", out[rep]);
639 dprintk("%u %d b: ", ftotal, left);
640 for (rep = outpos; rep < endpos; rep++) {
641 dprintk(" %d", out2[rep]);
646 for (rep = outpos; rep < endpos; rep++) {
647 if (out[rep] != CRUSH_ITEM_UNDEF)
650 in = bucket; /* initial bucket */
652 /* choose through intervening buckets */
654 /* note: we base the choice on the position
655 * even in the nested call. that means that
656 * if the first layer chooses the same bucket
657 * in a different position, we will tend to
658 * choose a different item in that bucket.
659 * this will involve more devices in data
660 * movement and tend to distribute the load.
665 if (in->alg == CRUSH_BUCKET_UNIFORM &&
666 in->size % numrep == 0)
667 /* r'=r+(n+1)*f_total */
668 r += (numrep+1) * ftotal;
670 /* r' = r + n*f_total */
671 r += numrep * ftotal;
675 dprintk(" empty bucket\n");
679 item = crush_bucket_choose(in, x, r);
680 if (item >= map->max_devices) {
681 dprintk(" bad item %d\n", item);
682 out[rep] = CRUSH_ITEM_NONE;
684 out2[rep] = CRUSH_ITEM_NONE;
691 itemtype = map->buckets[-1-item]->type;
694 dprintk(" item %d type %d\n", item, itemtype);
697 if (itemtype != type) {
699 (-1-item) >= map->max_buckets) {
700 dprintk(" bad item type %d\n", type);
701 out[rep] = CRUSH_ITEM_NONE;
708 in = map->buckets[-1-item];
714 for (i = outpos; i < endpos; i++) {
715 if (out[i] == item) {
723 if (recurse_to_leaf) {
725 crush_choose_indep(map,
726 map->buckets[-1-item],
732 if (out2[rep] == CRUSH_ITEM_NONE) {
733 /* placed nothing; no leaf */
737 /* we already have a leaf! */
744 is_out(map, weight, weight_max, item, x))
754 for (rep = outpos; rep < endpos; rep++) {
755 if (out[rep] == CRUSH_ITEM_UNDEF) {
756 out[rep] = CRUSH_ITEM_NONE;
758 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
759 out2[rep] = CRUSH_ITEM_NONE;
763 if (map->choose_tries && ftotal <= map->choose_total_tries)
764 map->choose_tries[ftotal]++;
768 dprintk("%u %d a: ", ftotal, left);
769 for (rep = outpos; rep < endpos; rep++) {
770 dprintk(" %d", out[rep]);
773 dprintk("%u %d b: ", ftotal, left);
774 for (rep = outpos; rep < endpos; rep++) {
775 dprintk(" %d", out2[rep]);
783 * crush_do_rule - calculate a mapping with the given input and rule
784 * @map: the crush_map
785 * @ruleno: the rule id
787 * @result: pointer to result vector
788 * @result_max: maximum result size
789 * @weight: weight vector (for map leaves)
790 * @weight_max: size of weight vector
791 * @scratch: scratch vector for private use; must be >= 3 * result_max
793 int crush_do_rule(const struct crush_map *map,
794 int ruleno, int x, int *result, int result_max,
795 const __u32 *weight, int weight_max,
800 int *b = scratch + result_max;
801 int *c = scratch + result_max*2;
808 struct crush_rule *rule;
814 * the original choose_total_tries value was off by one (it
815 * counted "retries" and not "tries"). add one.
817 int choose_tries = map->choose_total_tries + 1;
818 int choose_leaf_tries = 0;
820 * the local tries values were counted as "retries", though,
821 * and need no adjustment
823 int choose_local_retries = map->choose_local_tries;
824 int choose_local_fallback_retries = map->choose_local_fallback_tries;
826 int vary_r = map->chooseleaf_vary_r;
827 int stable = map->chooseleaf_stable;
829 if ((__u32)ruleno >= map->max_rules) {
830 dprintk(" bad ruleno %d\n", ruleno);
834 rule = map->rules[ruleno];
839 for (step = 0; step < rule->len; step++) {
841 struct crush_rule_step *curstep = &rule->steps[step];
843 switch (curstep->op) {
844 case CRUSH_RULE_TAKE:
845 if ((curstep->arg1 >= 0 &&
846 curstep->arg1 < map->max_devices) ||
847 (-1-curstep->arg1 >= 0 &&
848 -1-curstep->arg1 < map->max_buckets &&
849 map->buckets[-1-curstep->arg1])) {
850 w[0] = curstep->arg1;
853 dprintk(" bad take value %d\n", curstep->arg1);
857 case CRUSH_RULE_SET_CHOOSE_TRIES:
858 if (curstep->arg1 > 0)
859 choose_tries = curstep->arg1;
862 case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
863 if (curstep->arg1 > 0)
864 choose_leaf_tries = curstep->arg1;
867 case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
868 if (curstep->arg1 >= 0)
869 choose_local_retries = curstep->arg1;
872 case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
873 if (curstep->arg1 >= 0)
874 choose_local_fallback_retries = curstep->arg1;
877 case CRUSH_RULE_SET_CHOOSELEAF_VARY_R:
878 if (curstep->arg1 >= 0)
879 vary_r = curstep->arg1;
882 case CRUSH_RULE_SET_CHOOSELEAF_STABLE:
883 if (curstep->arg1 >= 0)
884 stable = curstep->arg1;
887 case CRUSH_RULE_CHOOSELEAF_FIRSTN:
888 case CRUSH_RULE_CHOOSE_FIRSTN:
891 case CRUSH_RULE_CHOOSELEAF_INDEP:
892 case CRUSH_RULE_CHOOSE_INDEP:
898 CRUSH_RULE_CHOOSELEAF_FIRSTN ||
900 CRUSH_RULE_CHOOSELEAF_INDEP;
905 for (i = 0; i < wsize; i++) {
908 * see CRUSH_N, CRUSH_N_MINUS macros.
909 * basically, numrep <= 0 means relative to
910 * the provided result_max
912 numrep = curstep->arg1;
914 numrep += result_max;
919 /* make sure bucket id is valid */
921 if (bno < 0 || bno >= map->max_buckets) {
922 /* w[i] is probably CRUSH_ITEM_NONE */
923 dprintk(" bad w[i] %d\n", w[i]);
928 if (choose_leaf_tries)
931 else if (map->chooseleaf_descend_once)
934 recurse_tries = choose_tries;
935 osize += crush_choose_firstn(
945 choose_local_retries,
946 choose_local_fallback_retries,
953 out_size = ((numrep < (result_max-osize)) ?
954 numrep : (result_max-osize));
964 choose_leaf_tries : 1,
973 /* copy final _leaf_ values to output set */
974 memcpy(o, c, osize*sizeof(*o));
976 /* swap o and w arrays */
984 case CRUSH_RULE_EMIT:
985 for (i = 0; i < wsize && result_len < result_max; i++) {
986 result[result_len] = w[i];
993 dprintk(" unknown op %d at step %d\n",