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);
232 bool bch2_is_zero(const void *_p, size_t n)
237 for (i = 0; i < n; i++)
243 static void bch2_quantiles_update(struct bch2_quantiles *q, u64 v)
247 while (i < ARRAY_SIZE(q->entries)) {
248 struct bch2_quantile_entry *e = q->entries + i;
250 if (unlikely(!e->step)) {
252 e->step = max_t(unsigned, v / 2, 1024);
253 } else if (e->m > v) {
254 e->m = e->m >= e->step
257 } else if (e->m < v) {
258 e->m = e->m + e->step > e->m
263 if ((e->m > v ? e->m - v : v - e->m) < e->step)
264 e->step = max_t(unsigned, e->step / 2, 1);
269 i = eytzinger0_child(i, v > e->m);
273 void bch2_prt_u64_binary(struct printbuf *out, u64 v, unsigned nr_bits)
276 prt_char(out, '0' + ((v >> --nr_bits) & 1));
279 void bch2_print_string_as_lines(const char *prefix, const char *lines)
284 printk("%s (null)\n", prefix);
290 p = strchrnul(lines, '\n');
291 printk("%s%.*s\n", prefix, (int) (p - lines), lines);
299 int bch2_prt_backtrace(struct printbuf *out, struct task_struct *task)
301 unsigned long entries[32];
302 unsigned i, nr_entries;
304 if (!down_read_trylock(&task->signal->exec_update_lock))
307 nr_entries = stack_trace_save_tsk(task, entries, ARRAY_SIZE(entries), 0);
308 for (i = 0; i < nr_entries; i++) {
309 prt_printf(out, "[<0>] %pB", (void *)entries[i]);
313 up_read(&task->signal->exec_update_lock);
319 #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
320 static inline void bch2_time_stats_update_one(struct bch2_time_stats *stats,
325 if (time_after64(end, start)) {
326 duration = end - start;
327 stats->duration_stats = mean_and_variance_update_inlined(stats->duration_stats,
329 stats->duration_stats_weighted = mean_and_variance_weighted_update(
330 stats->duration_stats_weighted,
332 stats->max_duration = max(stats->max_duration, duration);
333 stats->min_duration = min(stats->min_duration, duration);
334 bch2_quantiles_update(&stats->quantiles, duration);
337 if (time_after64(end, stats->last_event)) {
338 freq = end - stats->last_event;
339 stats->freq_stats = mean_and_variance_update_inlined(stats->freq_stats, freq);
340 stats->freq_stats_weighted = mean_and_variance_weighted_update(
341 stats->freq_stats_weighted,
343 stats->max_freq = max(stats->max_freq, freq);
344 stats->min_freq = min(stats->min_freq, freq);
345 stats->last_event = end;
349 static noinline void bch2_time_stats_clear_buffer(struct bch2_time_stats *stats,
350 struct bch2_time_stat_buffer *b)
352 struct bch2_time_stat_buffer_entry *i;
355 spin_lock_irqsave(&stats->lock, flags);
357 i < b->entries + ARRAY_SIZE(b->entries);
359 bch2_time_stats_update_one(stats, i->start, i->end);
360 spin_unlock_irqrestore(&stats->lock, flags);
365 void __bch2_time_stats_update(struct bch2_time_stats *stats, u64 start, u64 end)
369 WARN_RATELIMIT(!stats->min_duration || !stats->min_freq,
370 "time_stats: min_duration = %llu, min_freq = %llu",
371 stats->min_duration, stats->min_freq);
373 if (!stats->buffer) {
374 spin_lock_irqsave(&stats->lock, flags);
375 bch2_time_stats_update_one(stats, start, end);
377 if (mean_and_variance_weighted_get_mean(stats->freq_stats_weighted) < 32 &&
378 stats->duration_stats.n > 1024)
380 alloc_percpu_gfp(struct bch2_time_stat_buffer,
382 spin_unlock_irqrestore(&stats->lock, flags);
384 struct bch2_time_stat_buffer *b;
387 b = this_cpu_ptr(stats->buffer);
389 BUG_ON(b->nr >= ARRAY_SIZE(b->entries));
390 b->entries[b->nr++] = (struct bch2_time_stat_buffer_entry) {
395 if (unlikely(b->nr == ARRAY_SIZE(b->entries)))
396 bch2_time_stats_clear_buffer(stats, b);
402 static const struct time_unit {
407 { "us", NSEC_PER_USEC },
408 { "ms", NSEC_PER_MSEC },
409 { "s", NSEC_PER_SEC },
410 { "m", NSEC_PER_SEC * 60},
411 { "h", NSEC_PER_SEC * 3600},
415 static const struct time_unit *pick_time_units(u64 ns)
417 const struct time_unit *u;
420 u + 1 < time_units + ARRAY_SIZE(time_units) &&
421 ns >= u[1].nsecs << 1;
428 void bch2_pr_time_units(struct printbuf *out, u64 ns)
430 const struct time_unit *u = pick_time_units(ns);
432 prt_printf(out, "%llu %s", div_u64(ns, u->nsecs), u->name);
435 static void bch2_pr_time_units_aligned(struct printbuf *out, u64 ns)
437 const struct time_unit *u = pick_time_units(ns);
439 prt_printf(out, "%llu ", div64_u64(ns, u->nsecs));
441 prt_printf(out, "%s", u->name);
444 #define TABSTOP_SIZE 12
446 static inline void pr_name_and_units(struct printbuf *out, const char *name, u64 ns)
450 bch2_pr_time_units_aligned(out, ns);
454 void bch2_time_stats_to_text(struct printbuf *out, struct bch2_time_stats *stats)
456 const struct time_unit *u;
457 s64 f_mean = 0, d_mean = 0;
458 u64 q, last_q = 0, f_stddev = 0, d_stddev = 0;
461 * avoid divide by zero
463 if (stats->freq_stats.n) {
464 f_mean = mean_and_variance_get_mean(stats->freq_stats);
465 f_stddev = mean_and_variance_get_stddev(stats->freq_stats);
466 d_mean = mean_and_variance_get_mean(stats->duration_stats);
467 d_stddev = mean_and_variance_get_stddev(stats->duration_stats);
470 printbuf_tabstop_push(out, out->indent + TABSTOP_SIZE);
471 prt_printf(out, "count:");
473 prt_printf(out, "%llu ",
474 stats->duration_stats.n);
475 printbuf_tabstop_pop(out);
478 printbuf_tabstops_reset(out);
480 printbuf_tabstop_push(out, out->indent + 20);
481 printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
482 printbuf_tabstop_push(out, 0);
483 printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
486 prt_printf(out, "since mount");
489 prt_printf(out, "recent");
493 printbuf_tabstops_reset(out);
494 printbuf_tabstop_push(out, out->indent + 20);
495 printbuf_tabstop_push(out, TABSTOP_SIZE);
496 printbuf_tabstop_push(out, 2);
497 printbuf_tabstop_push(out, TABSTOP_SIZE);
499 prt_printf(out, "duration of events");
501 printbuf_indent_add(out, 2);
503 pr_name_and_units(out, "min:", stats->min_duration);
504 pr_name_and_units(out, "max:", stats->max_duration);
506 prt_printf(out, "mean:");
508 bch2_pr_time_units_aligned(out, d_mean);
510 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->duration_stats_weighted));
513 prt_printf(out, "stddev:");
515 bch2_pr_time_units_aligned(out, d_stddev);
517 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->duration_stats_weighted));
519 printbuf_indent_sub(out, 2);
522 prt_printf(out, "time between events");
524 printbuf_indent_add(out, 2);
526 pr_name_and_units(out, "min:", stats->min_freq);
527 pr_name_and_units(out, "max:", stats->max_freq);
529 prt_printf(out, "mean:");
531 bch2_pr_time_units_aligned(out, f_mean);
533 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->freq_stats_weighted));
536 prt_printf(out, "stddev:");
538 bch2_pr_time_units_aligned(out, f_stddev);
540 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->freq_stats_weighted));
542 printbuf_indent_sub(out, 2);
545 printbuf_tabstops_reset(out);
547 i = eytzinger0_first(NR_QUANTILES);
548 u = pick_time_units(stats->quantiles.entries[i].m);
550 prt_printf(out, "quantiles (%s):\t", u->name);
551 eytzinger0_for_each(i, NR_QUANTILES) {
552 bool is_last = eytzinger0_next(i, NR_QUANTILES) == -1;
554 q = max(stats->quantiles.entries[i].m, last_q);
555 prt_printf(out, "%llu ",
556 div_u64(q, u->nsecs));
563 void bch2_time_stats_exit(struct bch2_time_stats *stats)
565 free_percpu(stats->buffer);
568 void bch2_time_stats_init(struct bch2_time_stats *stats)
570 memset(stats, 0, sizeof(*stats));
571 stats->duration_stats_weighted.w = 8;
572 stats->freq_stats_weighted.w = 8;
573 stats->min_duration = U64_MAX;
574 stats->min_freq = U64_MAX;
575 spin_lock_init(&stats->lock);
581 * bch2_ratelimit_delay() - return how long to delay until the next time to do
584 * @d - the struct bch_ratelimit to update
586 * Returns the amount of time to delay by, in jiffies
588 u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
590 u64 now = local_clock();
592 return time_after64(d->next, now)
593 ? nsecs_to_jiffies(d->next - now)
598 * bch2_ratelimit_increment() - increment @d by the amount of work done
600 * @d - the struct bch_ratelimit to update
601 * @done - the amount of work done, in arbitrary units
603 void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
605 u64 now = local_clock();
607 d->next += div_u64(done * NSEC_PER_SEC, d->rate);
609 if (time_before64(now + NSEC_PER_SEC, d->next))
610 d->next = now + NSEC_PER_SEC;
612 if (time_after64(now - NSEC_PER_SEC * 2, d->next))
613 d->next = now - NSEC_PER_SEC * 2;
619 * Updates pd_controller. Attempts to scale inputed values to units per second.
620 * @target: desired value
621 * @actual: current value
623 * @sign: 1 or -1; 1 if increasing the rate makes actual go up, -1 if increasing
624 * it makes actual go down.
626 void bch2_pd_controller_update(struct bch_pd_controller *pd,
627 s64 target, s64 actual, int sign)
629 s64 proportional, derivative, change;
631 unsigned long seconds_since_update = (jiffies - pd->last_update) / HZ;
633 if (seconds_since_update == 0)
636 pd->last_update = jiffies;
638 proportional = actual - target;
639 proportional *= seconds_since_update;
640 proportional = div_s64(proportional, pd->p_term_inverse);
642 derivative = actual - pd->last_actual;
643 derivative = div_s64(derivative, seconds_since_update);
644 derivative = ewma_add(pd->smoothed_derivative, derivative,
645 (pd->d_term / seconds_since_update) ?: 1);
646 derivative = derivative * pd->d_term;
647 derivative = div_s64(derivative, pd->p_term_inverse);
649 change = proportional + derivative;
651 /* Don't increase rate if not keeping up */
654 time_after64(local_clock(),
655 pd->rate.next + NSEC_PER_MSEC))
658 change *= (sign * -1);
660 pd->rate.rate = clamp_t(s64, (s64) pd->rate.rate + change,
663 pd->last_actual = actual;
664 pd->last_derivative = derivative;
665 pd->last_proportional = proportional;
666 pd->last_change = change;
667 pd->last_target = target;
670 void bch2_pd_controller_init(struct bch_pd_controller *pd)
672 pd->rate.rate = 1024;
673 pd->last_update = jiffies;
674 pd->p_term_inverse = 6000;
676 pd->d_smooth = pd->d_term;
677 pd->backpressure = 1;
680 void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_controller *pd)
682 if (!out->nr_tabstops)
683 printbuf_tabstop_push(out, 20);
685 prt_printf(out, "rate:");
687 prt_human_readable_s64(out, pd->rate.rate);
690 prt_printf(out, "target:");
692 prt_human_readable_u64(out, pd->last_target);
695 prt_printf(out, "actual:");
697 prt_human_readable_u64(out, pd->last_actual);
700 prt_printf(out, "proportional:");
702 prt_human_readable_s64(out, pd->last_proportional);
705 prt_printf(out, "derivative:");
707 prt_human_readable_s64(out, pd->last_derivative);
710 prt_printf(out, "change:");
712 prt_human_readable_s64(out, pd->last_change);
715 prt_printf(out, "next io:");
717 prt_printf(out, "%llims", div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC));
723 void bch2_bio_map(struct bio *bio, void *base, size_t size)
726 struct page *page = is_vmalloc_addr(base)
727 ? vmalloc_to_page(base)
728 : virt_to_page(base);
729 unsigned offset = offset_in_page(base);
730 unsigned len = min_t(size_t, PAGE_SIZE - offset, size);
732 BUG_ON(!bio_add_page(bio, page, len, offset));
738 int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
741 struct page *page = alloc_page(gfp_mask);
742 unsigned len = min_t(size_t, PAGE_SIZE, size);
747 if (unlikely(!bio_add_page(bio, page, len, 0))) {
758 size_t bch2_rand_range(size_t max)
766 rand = get_random_long();
767 rand &= roundup_pow_of_two(max) - 1;
768 } while (rand >= max);
773 void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, const void *src)
776 struct bvec_iter iter;
778 __bio_for_each_segment(bv, dst, iter, dst_iter) {
779 void *dstp = kmap_atomic(bv.bv_page);
780 memcpy(dstp + bv.bv_offset, src, bv.bv_len);
787 void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
790 struct bvec_iter iter;
792 __bio_for_each_segment(bv, src, iter, src_iter) {
793 void *srcp = kmap_atomic(bv.bv_page);
794 memcpy(dst, srcp + bv.bv_offset, bv.bv_len);
801 static int alignment_ok(const void *base, size_t align)
803 return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
804 ((unsigned long)base & (align - 1)) == 0;
807 static void u32_swap(void *a, void *b, size_t size)
810 *(u32 *)a = *(u32 *)b;
814 static void u64_swap(void *a, void *b, size_t size)
817 *(u64 *)a = *(u64 *)b;
821 static void generic_swap(void *a, void *b, size_t size)
827 *(char *)a++ = *(char *)b;
829 } while (--size > 0);
832 static inline int do_cmp(void *base, size_t n, size_t size,
833 int (*cmp_func)(const void *, const void *, size_t),
836 return cmp_func(base + inorder_to_eytzinger0(l, n) * size,
837 base + inorder_to_eytzinger0(r, n) * size,
841 static inline void do_swap(void *base, size_t n, size_t size,
842 void (*swap_func)(void *, void *, size_t),
845 swap_func(base + inorder_to_eytzinger0(l, n) * size,
846 base + inorder_to_eytzinger0(r, n) * size,
850 void eytzinger0_sort(void *base, size_t n, size_t size,
851 int (*cmp_func)(const void *, const void *, size_t),
852 void (*swap_func)(void *, void *, size_t))
857 if (size == 4 && alignment_ok(base, 4))
858 swap_func = u32_swap;
859 else if (size == 8 && alignment_ok(base, 8))
860 swap_func = u64_swap;
862 swap_func = generic_swap;
866 for (i = n / 2 - 1; i >= 0; --i) {
867 for (r = i; r * 2 + 1 < n; r = c) {
871 do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
874 if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
877 do_swap(base, n, size, swap_func, r, c);
882 for (i = n - 1; i > 0; --i) {
883 do_swap(base, n, size, swap_func, 0, i);
885 for (r = 0; r * 2 + 1 < i; r = c) {
889 do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
892 if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
895 do_swap(base, n, size, swap_func, r, c);
900 void sort_cmp_size(void *base, size_t num, size_t size,
901 int (*cmp_func)(const void *, const void *, size_t),
902 void (*swap_func)(void *, void *, size_t size))
904 /* pre-scale counters for performance */
905 int i = (num/2 - 1) * size, n = num * size, c, r;
908 if (size == 4 && alignment_ok(base, 4))
909 swap_func = u32_swap;
910 else if (size == 8 && alignment_ok(base, 8))
911 swap_func = u64_swap;
913 swap_func = generic_swap;
917 for ( ; i >= 0; i -= size) {
918 for (r = i; r * 2 + size < n; r = c) {
921 cmp_func(base + c, base + c + size, size) < 0)
923 if (cmp_func(base + r, base + c, size) >= 0)
925 swap_func(base + r, base + c, size);
930 for (i = n - size; i > 0; i -= size) {
931 swap_func(base, base + i, size);
932 for (r = 0; r * 2 + size < i; r = c) {
935 cmp_func(base + c, base + c + size, size) < 0)
937 if (cmp_func(base + r, base + c, size) >= 0)
939 swap_func(base + r, base + c, size);
944 static void mempool_free_vp(void *element, void *pool_data)
946 size_t size = (size_t) pool_data;
948 vpfree(element, size);
951 static void *mempool_alloc_vp(gfp_t gfp_mask, void *pool_data)
953 size_t size = (size_t) pool_data;
955 return vpmalloc(size, gfp_mask);
958 int mempool_init_kvpmalloc_pool(mempool_t *pool, int min_nr, size_t size)
960 return size < PAGE_SIZE
961 ? mempool_init_kmalloc_pool(pool, min_nr, size)
962 : mempool_init(pool, min_nr, mempool_alloc_vp,
963 mempool_free_vp, (void *) size);
967 void eytzinger1_test(void)
969 unsigned inorder, eytz, size;
971 pr_info("1 based eytzinger test:");
976 unsigned extra = eytzinger1_extra(size);
979 pr_info("tree size %u", size);
981 BUG_ON(eytzinger1_prev(0, size) != eytzinger1_last(size));
982 BUG_ON(eytzinger1_next(0, size) != eytzinger1_first(size));
984 BUG_ON(eytzinger1_prev(eytzinger1_first(size), size) != 0);
985 BUG_ON(eytzinger1_next(eytzinger1_last(size), size) != 0);
988 eytzinger1_for_each(eytz, size) {
989 BUG_ON(__inorder_to_eytzinger1(inorder, size, extra) != eytz);
990 BUG_ON(__eytzinger1_to_inorder(eytz, size, extra) != inorder);
991 BUG_ON(eytz != eytzinger1_last(size) &&
992 eytzinger1_prev(eytzinger1_next(eytz, size), size) != eytz);
999 void eytzinger0_test(void)
1002 unsigned inorder, eytz, size;
1004 pr_info("0 based eytzinger test:");
1009 unsigned extra = eytzinger0_extra(size);
1012 pr_info("tree size %u", size);
1014 BUG_ON(eytzinger0_prev(-1, size) != eytzinger0_last(size));
1015 BUG_ON(eytzinger0_next(-1, size) != eytzinger0_first(size));
1017 BUG_ON(eytzinger0_prev(eytzinger0_first(size), size) != -1);
1018 BUG_ON(eytzinger0_next(eytzinger0_last(size), size) != -1);
1021 eytzinger0_for_each(eytz, size) {
1022 BUG_ON(__inorder_to_eytzinger0(inorder, size, extra) != eytz);
1023 BUG_ON(__eytzinger0_to_inorder(eytz, size, extra) != inorder);
1024 BUG_ON(eytz != eytzinger0_last(size) &&
1025 eytzinger0_prev(eytzinger0_next(eytz, size), size) != eytz);
1032 static inline int cmp_u16(const void *_l, const void *_r, size_t size)
1034 const u16 *l = _l, *r = _r;
1036 return (*l > *r) - (*r - *l);
1039 static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search)
1041 int i, c1 = -1, c2 = -1;
1044 r = eytzinger0_find_le(test_array, nr,
1045 sizeof(test_array[0]),
1050 for (i = 0; i < nr; i++)
1051 if (test_array[i] <= search && test_array[i] > c2)
1055 eytzinger0_for_each(i, nr)
1056 pr_info("[%3u] = %12u", i, test_array[i]);
1057 pr_info("find_le(%2u) -> [%2zi] = %2i should be %2i",
1062 void eytzinger0_find_test(void)
1064 unsigned i, nr, allocated = 1 << 12;
1065 u16 *test_array = kmalloc_array(allocated, sizeof(test_array[0]), GFP_KERNEL);
1067 for (nr = 1; nr < allocated; nr++) {
1068 pr_info("testing %u elems", nr);
1070 get_random_bytes(test_array, nr * sizeof(test_array[0]));
1071 eytzinger0_sort(test_array, nr, sizeof(test_array[0]), cmp_u16, NULL);
1073 /* verify array is sorted correctly: */
1074 eytzinger0_for_each(i, nr)
1075 BUG_ON(i != eytzinger0_last(nr) &&
1076 test_array[i] > test_array[eytzinger0_next(i, nr)]);
1078 for (i = 0; i < U16_MAX; i += 1 << 12)
1079 eytzinger0_find_test_val(test_array, nr, i);
1081 for (i = 0; i < nr; i++) {
1082 eytzinger0_find_test_val(test_array, nr, test_array[i] - 1);
1083 eytzinger0_find_test_val(test_array, nr, test_array[i]);
1084 eytzinger0_find_test_val(test_array, nr, test_array[i] + 1);
1093 * Accumulate percpu counters onto one cpu's copy - only valid when access
1094 * against any percpu counter is guarded against
1096 u64 *bch2_acc_percpu_u64s(u64 __percpu *p, unsigned nr)
1101 /* access to pcpu vars has to be blocked by other locking */
1103 ret = this_cpu_ptr(p);
1106 for_each_possible_cpu(cpu) {
1107 u64 *i = per_cpu_ptr(p, cpu);
1110 acc_u64s(ret, i, nr);
1111 memset(i, 0, nr * sizeof(u64));