1 // SPDX-License-Identifier: GPL-2.0
3 * random utiility code, for bcache but in theory not specific to bcache
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
10 #include <linux/blkdev.h>
11 #include <linux/console.h>
12 #include <linux/ctype.h>
13 #include <linux/debugfs.h>
14 #include <linux/freezer.h>
15 #include <linux/kthread.h>
16 #include <linux/log2.h>
17 #include <linux/math64.h>
18 #include <linux/percpu.h>
19 #include <linux/preempt.h>
20 #include <linux/random.h>
21 #include <linux/seq_file.h>
22 #include <linux/string.h>
23 #include <linux/types.h>
24 #include <linux/sched/clock.h>
25 #include <linux/mean_and_variance.h>
27 #include "eytzinger.h"
30 static const char si_units[] = "?kMGTPEZY";
32 /* string_get_size units: */
33 static const char *const units_2[] = {
34 "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
36 static const char *const units_10[] = {
37 "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
40 static int parse_u64(const char *cp, u64 *res)
42 const char *start = cp;
52 if (v > U64_MAX - (*cp - '0'))
56 } while (isdigit(*cp));
62 static int bch2_pow(u64 n, u64 p, u64 *res)
67 if (*res > div_u64(U64_MAX, n))
74 static int parse_unit_suffix(const char *cp, u64 *res)
76 const char *start = cp;
84 for (u = 1; u < strlen(si_units); u++)
85 if (*cp == si_units[u]) {
90 for (u = 0; u < ARRAY_SIZE(units_2); u++)
91 if (!strncmp(cp, units_2[u], strlen(units_2[u]))) {
92 cp += strlen(units_2[u]);
96 for (u = 0; u < ARRAY_SIZE(units_10); u++)
97 if (!strncmp(cp, units_10[u], strlen(units_10[u]))) {
98 cp += strlen(units_10[u]);
106 ret = bch2_pow(base, u, res);
113 #define parse_or_ret(cp, _f) \
121 static int __bch2_strtou64_h(const char *cp, u64 *res)
123 const char *start = cp;
124 u64 v = 0, b, f_n = 0, f_d = 1;
127 parse_or_ret(cp, parse_u64(cp, &v));
131 ret = parse_u64(cp, &f_n);
136 ret = bch2_pow(10, ret, &f_d);
141 parse_or_ret(cp, parse_unit_suffix(cp, &b));
143 if (v > div_u64(U64_MAX, b))
147 if (f_n > div_u64(U64_MAX, b))
150 f_n = div_u64(f_n * b, f_d);
159 static int __bch2_strtoh(const char *cp, u64 *res,
160 u64 t_max, bool t_signed)
162 bool positive = *cp != '-';
165 if (*cp == '+' || *cp == '-')
168 parse_or_ret(cp, __bch2_strtou64_h(cp, &v));
191 #define STRTO_H(name, type) \
192 int bch2_ ## name ## _h(const char *cp, type *res) \
195 int ret = __bch2_strtoh(cp, &v, ANYSINT_MAX(type), \
196 ANYSINT_MAX(type) != ((type) ~0ULL)); \
201 STRTO_H(strtoint, int)
202 STRTO_H(strtouint, unsigned int)
203 STRTO_H(strtoll, long long)
204 STRTO_H(strtoull, unsigned long long)
205 STRTO_H(strtou64, u64)
207 u64 bch2_read_flag_list(char *opt, const char * const list[])
210 char *p, *s, *d = kstrdup(opt, GFP_KERNEL);
217 while ((p = strsep(&s, ","))) {
218 int flag = match_string(list, -1, p);
233 bool bch2_is_zero(const void *_p, size_t n)
238 for (i = 0; i < n; i++)
244 void bch2_prt_u64_binary(struct printbuf *out, u64 v, unsigned nr_bits)
247 prt_char(out, '0' + ((v >> --nr_bits) & 1));
250 void bch2_print_string_as_lines(const char *prefix, const char *lines)
255 printk("%s (null)\n", prefix);
261 p = strchrnul(lines, '\n');
262 printk("%s%.*s\n", prefix, (int) (p - lines), lines);
270 int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *task)
272 #ifdef CONFIG_STACKTRACE
273 unsigned nr_entries = 0;
277 ret = darray_make_room(stack, 32);
281 if (!down_read_trylock(&task->signal->exec_update_lock))
285 nr_entries = stack_trace_save_tsk(task, stack->data, stack->size, 0);
286 } while (nr_entries == stack->size &&
287 !(ret = darray_make_room(stack, stack->size * 2)));
289 stack->nr = nr_entries;
290 up_read(&task->signal->exec_update_lock);
298 void bch2_prt_backtrace(struct printbuf *out, bch_stacktrace *stack)
302 darray_for_each(*stack, i) {
303 prt_printf(out, "[<0>] %pB", (void *) *i);
308 int bch2_prt_task_backtrace(struct printbuf *out, struct task_struct *task)
310 bch_stacktrace stack = { 0 };
311 int ret = bch2_save_backtrace(&stack, task);
313 bch2_prt_backtrace(out, &stack);
320 #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
321 static void bch2_quantiles_update(struct bch2_quantiles *q, u64 v)
325 while (i < ARRAY_SIZE(q->entries)) {
326 struct bch2_quantile_entry *e = q->entries + i;
328 if (unlikely(!e->step)) {
330 e->step = max_t(unsigned, v / 2, 1024);
331 } else if (e->m > v) {
332 e->m = e->m >= e->step
335 } else if (e->m < v) {
336 e->m = e->m + e->step > e->m
341 if ((e->m > v ? e->m - v : v - e->m) < e->step)
342 e->step = max_t(unsigned, e->step / 2, 1);
347 i = eytzinger0_child(i, v > e->m);
351 static inline void bch2_time_stats_update_one(struct bch2_time_stats *stats,
356 if (time_after64(end, start)) {
357 duration = end - start;
358 mean_and_variance_update(&stats->duration_stats, duration);
359 mean_and_variance_weighted_update(&stats->duration_stats_weighted, duration);
360 stats->max_duration = max(stats->max_duration, duration);
361 stats->min_duration = min(stats->min_duration, duration);
362 bch2_quantiles_update(&stats->quantiles, duration);
365 if (time_after64(end, stats->last_event)) {
366 freq = end - stats->last_event;
367 mean_and_variance_update(&stats->freq_stats, freq);
368 mean_and_variance_weighted_update(&stats->freq_stats_weighted, freq);
369 stats->max_freq = max(stats->max_freq, freq);
370 stats->min_freq = min(stats->min_freq, freq);
371 stats->last_event = end;
375 static noinline void bch2_time_stats_clear_buffer(struct bch2_time_stats *stats,
376 struct bch2_time_stat_buffer *b)
378 struct bch2_time_stat_buffer_entry *i;
381 spin_lock_irqsave(&stats->lock, flags);
383 i < b->entries + ARRAY_SIZE(b->entries);
385 bch2_time_stats_update_one(stats, i->start, i->end);
386 spin_unlock_irqrestore(&stats->lock, flags);
391 void __bch2_time_stats_update(struct bch2_time_stats *stats, u64 start, u64 end)
395 WARN_RATELIMIT(!stats->min_duration || !stats->min_freq,
396 "time_stats: min_duration = %llu, min_freq = %llu",
397 stats->min_duration, stats->min_freq);
399 if (!stats->buffer) {
400 spin_lock_irqsave(&stats->lock, flags);
401 bch2_time_stats_update_one(stats, start, end);
403 if (mean_and_variance_weighted_get_mean(stats->freq_stats_weighted) < 32 &&
404 stats->duration_stats.n > 1024)
406 alloc_percpu_gfp(struct bch2_time_stat_buffer,
408 spin_unlock_irqrestore(&stats->lock, flags);
410 struct bch2_time_stat_buffer *b;
413 b = this_cpu_ptr(stats->buffer);
415 BUG_ON(b->nr >= ARRAY_SIZE(b->entries));
416 b->entries[b->nr++] = (struct bch2_time_stat_buffer_entry) {
421 if (unlikely(b->nr == ARRAY_SIZE(b->entries)))
422 bch2_time_stats_clear_buffer(stats, b);
428 static const struct time_unit {
433 { "us", NSEC_PER_USEC },
434 { "ms", NSEC_PER_MSEC },
435 { "s", NSEC_PER_SEC },
436 { "m", (u64) NSEC_PER_SEC * 60},
437 { "h", (u64) NSEC_PER_SEC * 3600},
441 static const struct time_unit *pick_time_units(u64 ns)
443 const struct time_unit *u;
446 u + 1 < time_units + ARRAY_SIZE(time_units) &&
447 ns >= u[1].nsecs << 1;
454 void bch2_pr_time_units(struct printbuf *out, u64 ns)
456 const struct time_unit *u = pick_time_units(ns);
458 prt_printf(out, "%llu %s", div_u64(ns, u->nsecs), u->name);
461 static void bch2_pr_time_units_aligned(struct printbuf *out, u64 ns)
463 const struct time_unit *u = pick_time_units(ns);
465 prt_printf(out, "%llu ", div64_u64(ns, u->nsecs));
467 prt_printf(out, "%s", u->name);
470 #define TABSTOP_SIZE 12
472 static inline void pr_name_and_units(struct printbuf *out, const char *name, u64 ns)
476 bch2_pr_time_units_aligned(out, ns);
480 void bch2_time_stats_to_text(struct printbuf *out, struct bch2_time_stats *stats)
482 const struct time_unit *u;
483 s64 f_mean = 0, d_mean = 0;
484 u64 q, last_q = 0, f_stddev = 0, d_stddev = 0;
487 * avoid divide by zero
489 if (stats->freq_stats.n) {
490 f_mean = mean_and_variance_get_mean(stats->freq_stats);
491 f_stddev = mean_and_variance_get_stddev(stats->freq_stats);
492 d_mean = mean_and_variance_get_mean(stats->duration_stats);
493 d_stddev = mean_and_variance_get_stddev(stats->duration_stats);
496 printbuf_tabstop_push(out, out->indent + TABSTOP_SIZE);
497 prt_printf(out, "count:");
499 prt_printf(out, "%llu ",
500 stats->duration_stats.n);
501 printbuf_tabstop_pop(out);
504 printbuf_tabstops_reset(out);
506 printbuf_tabstop_push(out, out->indent + 20);
507 printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
508 printbuf_tabstop_push(out, 0);
509 printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
512 prt_printf(out, "since mount");
515 prt_printf(out, "recent");
519 printbuf_tabstops_reset(out);
520 printbuf_tabstop_push(out, out->indent + 20);
521 printbuf_tabstop_push(out, TABSTOP_SIZE);
522 printbuf_tabstop_push(out, 2);
523 printbuf_tabstop_push(out, TABSTOP_SIZE);
525 prt_printf(out, "duration of events");
527 printbuf_indent_add(out, 2);
529 pr_name_and_units(out, "min:", stats->min_duration);
530 pr_name_and_units(out, "max:", stats->max_duration);
532 prt_printf(out, "mean:");
534 bch2_pr_time_units_aligned(out, d_mean);
536 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->duration_stats_weighted));
539 prt_printf(out, "stddev:");
541 bch2_pr_time_units_aligned(out, d_stddev);
543 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->duration_stats_weighted));
545 printbuf_indent_sub(out, 2);
548 prt_printf(out, "time between events");
550 printbuf_indent_add(out, 2);
552 pr_name_and_units(out, "min:", stats->min_freq);
553 pr_name_and_units(out, "max:", stats->max_freq);
555 prt_printf(out, "mean:");
557 bch2_pr_time_units_aligned(out, f_mean);
559 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->freq_stats_weighted));
562 prt_printf(out, "stddev:");
564 bch2_pr_time_units_aligned(out, f_stddev);
566 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->freq_stats_weighted));
568 printbuf_indent_sub(out, 2);
571 printbuf_tabstops_reset(out);
573 i = eytzinger0_first(NR_QUANTILES);
574 u = pick_time_units(stats->quantiles.entries[i].m);
576 prt_printf(out, "quantiles (%s):\t", u->name);
577 eytzinger0_for_each(i, NR_QUANTILES) {
578 bool is_last = eytzinger0_next(i, NR_QUANTILES) == -1;
580 q = max(stats->quantiles.entries[i].m, last_q);
581 prt_printf(out, "%llu ",
582 div_u64(q, u->nsecs));
589 void bch2_time_stats_exit(struct bch2_time_stats *stats)
591 free_percpu(stats->buffer);
594 void bch2_time_stats_init(struct bch2_time_stats *stats)
596 memset(stats, 0, sizeof(*stats));
597 stats->duration_stats_weighted.weight = 8;
598 stats->freq_stats_weighted.weight = 8;
599 stats->min_duration = U64_MAX;
600 stats->min_freq = U64_MAX;
601 spin_lock_init(&stats->lock);
607 * bch2_ratelimit_delay() - return how long to delay until the next time to do
610 * @d - the struct bch_ratelimit to update
612 * Returns the amount of time to delay by, in jiffies
614 u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
616 u64 now = local_clock();
618 return time_after64(d->next, now)
619 ? nsecs_to_jiffies(d->next - now)
624 * bch2_ratelimit_increment() - increment @d by the amount of work done
626 * @d - the struct bch_ratelimit to update
627 * @done - the amount of work done, in arbitrary units
629 void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
631 u64 now = local_clock();
633 d->next += div_u64(done * NSEC_PER_SEC, d->rate);
635 if (time_before64(now + NSEC_PER_SEC, d->next))
636 d->next = now + NSEC_PER_SEC;
638 if (time_after64(now - NSEC_PER_SEC * 2, d->next))
639 d->next = now - NSEC_PER_SEC * 2;
645 * Updates pd_controller. Attempts to scale inputed values to units per second.
646 * @target: desired value
647 * @actual: current value
649 * @sign: 1 or -1; 1 if increasing the rate makes actual go up, -1 if increasing
650 * it makes actual go down.
652 void bch2_pd_controller_update(struct bch_pd_controller *pd,
653 s64 target, s64 actual, int sign)
655 s64 proportional, derivative, change;
657 unsigned long seconds_since_update = (jiffies - pd->last_update) / HZ;
659 if (seconds_since_update == 0)
662 pd->last_update = jiffies;
664 proportional = actual - target;
665 proportional *= seconds_since_update;
666 proportional = div_s64(proportional, pd->p_term_inverse);
668 derivative = actual - pd->last_actual;
669 derivative = div_s64(derivative, seconds_since_update);
670 derivative = ewma_add(pd->smoothed_derivative, derivative,
671 (pd->d_term / seconds_since_update) ?: 1);
672 derivative = derivative * pd->d_term;
673 derivative = div_s64(derivative, pd->p_term_inverse);
675 change = proportional + derivative;
677 /* Don't increase rate if not keeping up */
680 time_after64(local_clock(),
681 pd->rate.next + NSEC_PER_MSEC))
684 change *= (sign * -1);
686 pd->rate.rate = clamp_t(s64, (s64) pd->rate.rate + change,
689 pd->last_actual = actual;
690 pd->last_derivative = derivative;
691 pd->last_proportional = proportional;
692 pd->last_change = change;
693 pd->last_target = target;
696 void bch2_pd_controller_init(struct bch_pd_controller *pd)
698 pd->rate.rate = 1024;
699 pd->last_update = jiffies;
700 pd->p_term_inverse = 6000;
702 pd->d_smooth = pd->d_term;
703 pd->backpressure = 1;
706 void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_controller *pd)
708 if (!out->nr_tabstops)
709 printbuf_tabstop_push(out, 20);
711 prt_printf(out, "rate:");
713 prt_human_readable_s64(out, pd->rate.rate);
716 prt_printf(out, "target:");
718 prt_human_readable_u64(out, pd->last_target);
721 prt_printf(out, "actual:");
723 prt_human_readable_u64(out, pd->last_actual);
726 prt_printf(out, "proportional:");
728 prt_human_readable_s64(out, pd->last_proportional);
731 prt_printf(out, "derivative:");
733 prt_human_readable_s64(out, pd->last_derivative);
736 prt_printf(out, "change:");
738 prt_human_readable_s64(out, pd->last_change);
741 prt_printf(out, "next io:");
743 prt_printf(out, "%llims", div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC));
749 void bch2_bio_map(struct bio *bio, void *base, size_t size)
752 struct page *page = is_vmalloc_addr(base)
753 ? vmalloc_to_page(base)
754 : virt_to_page(base);
755 unsigned offset = offset_in_page(base);
756 unsigned len = min_t(size_t, PAGE_SIZE - offset, size);
758 BUG_ON(!bio_add_page(bio, page, len, offset));
764 int bch2_bio_alloc_pages_noprof(struct bio *bio, size_t size, gfp_t gfp_mask)
767 struct page *page = alloc_pages_noprof(gfp_mask, 0);
768 unsigned len = min_t(size_t, PAGE_SIZE, size);
773 if (unlikely(!bio_add_page(bio, page, len, 0))) {
784 size_t bch2_rand_range(size_t max)
792 rand = get_random_long();
793 rand &= roundup_pow_of_two(max) - 1;
794 } while (rand >= max);
799 void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, const void *src)
802 struct bvec_iter iter;
804 __bio_for_each_segment(bv, dst, iter, dst_iter) {
805 void *dstp = kmap_local_page(bv.bv_page);
807 memcpy(dstp + bv.bv_offset, src, bv.bv_len);
814 void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
817 struct bvec_iter iter;
819 __bio_for_each_segment(bv, src, iter, src_iter) {
820 void *srcp = kmap_local_page(bv.bv_page);
822 memcpy(dst, srcp + bv.bv_offset, bv.bv_len);
829 static int alignment_ok(const void *base, size_t align)
831 return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
832 ((unsigned long)base & (align - 1)) == 0;
835 static void u32_swap(void *a, void *b, size_t size)
838 *(u32 *)a = *(u32 *)b;
842 static void u64_swap(void *a, void *b, size_t size)
845 *(u64 *)a = *(u64 *)b;
849 static void generic_swap(void *a, void *b, size_t size)
855 *(char *)a++ = *(char *)b;
857 } while (--size > 0);
860 static inline int do_cmp(void *base, size_t n, size_t size,
861 int (*cmp_func)(const void *, const void *, size_t),
864 return cmp_func(base + inorder_to_eytzinger0(l, n) * size,
865 base + inorder_to_eytzinger0(r, n) * size,
869 static inline void do_swap(void *base, size_t n, size_t size,
870 void (*swap_func)(void *, void *, size_t),
873 swap_func(base + inorder_to_eytzinger0(l, n) * size,
874 base + inorder_to_eytzinger0(r, n) * size,
878 void eytzinger0_sort(void *base, size_t n, size_t size,
879 int (*cmp_func)(const void *, const void *, size_t),
880 void (*swap_func)(void *, void *, size_t))
885 if (size == 4 && alignment_ok(base, 4))
886 swap_func = u32_swap;
887 else if (size == 8 && alignment_ok(base, 8))
888 swap_func = u64_swap;
890 swap_func = generic_swap;
894 for (i = n / 2 - 1; i >= 0; --i) {
895 for (r = i; r * 2 + 1 < n; r = c) {
899 do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
902 if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
905 do_swap(base, n, size, swap_func, r, c);
910 for (i = n - 1; i > 0; --i) {
911 do_swap(base, n, size, swap_func, 0, i);
913 for (r = 0; r * 2 + 1 < i; r = c) {
917 do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
920 if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
923 do_swap(base, n, size, swap_func, r, c);
928 void sort_cmp_size(void *base, size_t num, size_t size,
929 int (*cmp_func)(const void *, const void *, size_t),
930 void (*swap_func)(void *, void *, size_t size))
932 /* pre-scale counters for performance */
933 int i = (num/2 - 1) * size, n = num * size, c, r;
936 if (size == 4 && alignment_ok(base, 4))
937 swap_func = u32_swap;
938 else if (size == 8 && alignment_ok(base, 8))
939 swap_func = u64_swap;
941 swap_func = generic_swap;
945 for ( ; i >= 0; i -= size) {
946 for (r = i; r * 2 + size < n; r = c) {
949 cmp_func(base + c, base + c + size, size) < 0)
951 if (cmp_func(base + r, base + c, size) >= 0)
953 swap_func(base + r, base + c, size);
958 for (i = n - size; i > 0; i -= size) {
959 swap_func(base, base + i, size);
960 for (r = 0; r * 2 + size < i; r = c) {
963 cmp_func(base + c, base + c + size, size) < 0)
965 if (cmp_func(base + r, base + c, size) >= 0)
967 swap_func(base + r, base + c, size);
972 static void mempool_free_vp(void *element, void *pool_data)
974 size_t size = (size_t) pool_data;
976 vpfree(element, size);
979 static void *mempool_alloc_vp(gfp_t gfp_mask, void *pool_data)
981 size_t size = (size_t) pool_data;
983 return vpmalloc(size, gfp_mask);
986 int mempool_init_kvpmalloc_pool(mempool_t *pool, int min_nr, size_t size)
988 return size < PAGE_SIZE
989 ? mempool_init_kmalloc_pool(pool, min_nr, size)
990 : mempool_init(pool, min_nr, mempool_alloc_vp,
991 mempool_free_vp, (void *) size);
995 void eytzinger1_test(void)
997 unsigned inorder, eytz, size;
999 pr_info("1 based eytzinger test:");
1004 unsigned extra = eytzinger1_extra(size);
1007 pr_info("tree size %u", size);
1009 BUG_ON(eytzinger1_prev(0, size) != eytzinger1_last(size));
1010 BUG_ON(eytzinger1_next(0, size) != eytzinger1_first(size));
1012 BUG_ON(eytzinger1_prev(eytzinger1_first(size), size) != 0);
1013 BUG_ON(eytzinger1_next(eytzinger1_last(size), size) != 0);
1016 eytzinger1_for_each(eytz, size) {
1017 BUG_ON(__inorder_to_eytzinger1(inorder, size, extra) != eytz);
1018 BUG_ON(__eytzinger1_to_inorder(eytz, size, extra) != inorder);
1019 BUG_ON(eytz != eytzinger1_last(size) &&
1020 eytzinger1_prev(eytzinger1_next(eytz, size), size) != eytz);
1027 void eytzinger0_test(void)
1030 unsigned inorder, eytz, size;
1032 pr_info("0 based eytzinger test:");
1037 unsigned extra = eytzinger0_extra(size);
1040 pr_info("tree size %u", size);
1042 BUG_ON(eytzinger0_prev(-1, size) != eytzinger0_last(size));
1043 BUG_ON(eytzinger0_next(-1, size) != eytzinger0_first(size));
1045 BUG_ON(eytzinger0_prev(eytzinger0_first(size), size) != -1);
1046 BUG_ON(eytzinger0_next(eytzinger0_last(size), size) != -1);
1049 eytzinger0_for_each(eytz, size) {
1050 BUG_ON(__inorder_to_eytzinger0(inorder, size, extra) != eytz);
1051 BUG_ON(__eytzinger0_to_inorder(eytz, size, extra) != inorder);
1052 BUG_ON(eytz != eytzinger0_last(size) &&
1053 eytzinger0_prev(eytzinger0_next(eytz, size), size) != eytz);
1060 static inline int cmp_u16(const void *_l, const void *_r, size_t size)
1062 const u16 *l = _l, *r = _r;
1064 return (*l > *r) - (*r - *l);
1067 static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search)
1069 int i, c1 = -1, c2 = -1;
1072 r = eytzinger0_find_le(test_array, nr,
1073 sizeof(test_array[0]),
1078 for (i = 0; i < nr; i++)
1079 if (test_array[i] <= search && test_array[i] > c2)
1083 eytzinger0_for_each(i, nr)
1084 pr_info("[%3u] = %12u", i, test_array[i]);
1085 pr_info("find_le(%2u) -> [%2zi] = %2i should be %2i",
1090 void eytzinger0_find_test(void)
1092 unsigned i, nr, allocated = 1 << 12;
1093 u16 *test_array = kmalloc_array(allocated, sizeof(test_array[0]), GFP_KERNEL);
1095 for (nr = 1; nr < allocated; nr++) {
1096 pr_info("testing %u elems", nr);
1098 get_random_bytes(test_array, nr * sizeof(test_array[0]));
1099 eytzinger0_sort(test_array, nr, sizeof(test_array[0]), cmp_u16, NULL);
1101 /* verify array is sorted correctly: */
1102 eytzinger0_for_each(i, nr)
1103 BUG_ON(i != eytzinger0_last(nr) &&
1104 test_array[i] > test_array[eytzinger0_next(i, nr)]);
1106 for (i = 0; i < U16_MAX; i += 1 << 12)
1107 eytzinger0_find_test_val(test_array, nr, i);
1109 for (i = 0; i < nr; i++) {
1110 eytzinger0_find_test_val(test_array, nr, test_array[i] - 1);
1111 eytzinger0_find_test_val(test_array, nr, test_array[i]);
1112 eytzinger0_find_test_val(test_array, nr, test_array[i] + 1);
1121 * Accumulate percpu counters onto one cpu's copy - only valid when access
1122 * against any percpu counter is guarded against
1124 u64 *bch2_acc_percpu_u64s(u64 __percpu *p, unsigned nr)
1129 /* access to pcpu vars has to be blocked by other locking */
1131 ret = this_cpu_ptr(p);
1134 for_each_possible_cpu(cpu) {
1135 u64 *i = per_cpu_ptr(p, cpu);
1138 acc_u64s(ret, i, nr);
1139 memset(i, 0, nr * sizeof(u64));