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>
26 #include "eytzinger.h"
27 #include "mean_and_variance.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_base2_nbits(struct printbuf *out, u64 v, unsigned nr_bits)
247 prt_char(out, '0' + ((v >> --nr_bits) & 1));
250 void bch2_prt_u64_base2(struct printbuf *out, u64 v)
252 bch2_prt_u64_base2_nbits(out, v, fls64(v) ?: 1);
255 void bch2_print_string_as_lines(const char *prefix, const char *lines)
260 printk("%s (null)\n", prefix);
266 p = strchrnul(lines, '\n');
267 printk("%s%.*s\n", prefix, (int) (p - lines), lines);
275 int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *task, unsigned skipnr,
278 #ifdef CONFIG_STACKTRACE
279 unsigned nr_entries = 0;
282 int ret = darray_make_room_gfp(stack, 32, gfp);
286 if (!down_read_trylock(&task->signal->exec_update_lock))
290 nr_entries = stack_trace_save_tsk(task, stack->data, stack->size, skipnr + 1);
291 } while (nr_entries == stack->size &&
292 !(ret = darray_make_room_gfp(stack, stack->size * 2, gfp)));
294 stack->nr = nr_entries;
295 up_read(&task->signal->exec_update_lock);
303 void bch2_prt_backtrace(struct printbuf *out, bch_stacktrace *stack)
305 darray_for_each(*stack, i) {
306 prt_printf(out, "[<0>] %pB", (void *) *i);
311 int bch2_prt_task_backtrace(struct printbuf *out, struct task_struct *task, unsigned skipnr, gfp_t gfp)
313 bch_stacktrace stack = { 0 };
314 int ret = bch2_save_backtrace(&stack, task, skipnr + 1, gfp);
316 bch2_prt_backtrace(out, &stack);
323 void bch2_prt_datetime(struct printbuf *out, time64_t sec)
332 void bch2_prt_datetime(struct printbuf *out, time64_t sec)
335 snprintf(buf, sizeof(buf), "%ptT", &sec);
340 void bch2_pr_time_units(struct printbuf *out, u64 ns)
342 const struct time_unit *u = bch2_pick_time_units(ns);
344 prt_printf(out, "%llu %s", div_u64(ns, u->nsecs), u->name);
347 static void bch2_pr_time_units_aligned(struct printbuf *out, u64 ns)
349 const struct time_unit *u = bch2_pick_time_units(ns);
351 prt_printf(out, "%llu ", div64_u64(ns, u->nsecs));
353 prt_printf(out, "%s", u->name);
356 static inline void pr_name_and_units(struct printbuf *out, const char *name, u64 ns)
360 bch2_pr_time_units_aligned(out, ns);
364 #define TABSTOP_SIZE 12
366 void bch2_time_stats_to_text(struct printbuf *out, struct bch2_time_stats *stats)
368 struct quantiles *quantiles = time_stats_to_quantiles(stats);
369 s64 f_mean = 0, d_mean = 0;
370 u64 f_stddev = 0, d_stddev = 0;
375 spin_lock_irq(&stats->lock);
376 for_each_possible_cpu(cpu)
377 __bch2_time_stats_clear_buffer(stats, per_cpu_ptr(stats->buffer, cpu));
378 spin_unlock_irq(&stats->lock);
382 * avoid divide by zero
384 if (stats->freq_stats.n) {
385 f_mean = mean_and_variance_get_mean(stats->freq_stats);
386 f_stddev = mean_and_variance_get_stddev(stats->freq_stats);
387 d_mean = mean_and_variance_get_mean(stats->duration_stats);
388 d_stddev = mean_and_variance_get_stddev(stats->duration_stats);
391 printbuf_tabstop_push(out, out->indent + TABSTOP_SIZE);
392 prt_printf(out, "count:");
394 prt_printf(out, "%llu ",
395 stats->duration_stats.n);
396 printbuf_tabstop_pop(out);
399 printbuf_tabstops_reset(out);
401 printbuf_tabstop_push(out, out->indent + 20);
402 printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
403 printbuf_tabstop_push(out, 0);
404 printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
407 prt_printf(out, "since mount");
410 prt_printf(out, "recent");
414 printbuf_tabstops_reset(out);
415 printbuf_tabstop_push(out, out->indent + 20);
416 printbuf_tabstop_push(out, TABSTOP_SIZE);
417 printbuf_tabstop_push(out, 2);
418 printbuf_tabstop_push(out, TABSTOP_SIZE);
420 prt_printf(out, "duration of events");
422 printbuf_indent_add(out, 2);
424 pr_name_and_units(out, "min:", stats->min_duration);
425 pr_name_and_units(out, "max:", stats->max_duration);
426 pr_name_and_units(out, "total:", stats->total_duration);
428 prt_printf(out, "mean:");
430 bch2_pr_time_units_aligned(out, d_mean);
432 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->duration_stats_weighted, TIME_STATS_MV_WEIGHT));
435 prt_printf(out, "stddev:");
437 bch2_pr_time_units_aligned(out, d_stddev);
439 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->duration_stats_weighted, TIME_STATS_MV_WEIGHT));
441 printbuf_indent_sub(out, 2);
444 prt_printf(out, "time between events");
446 printbuf_indent_add(out, 2);
448 pr_name_and_units(out, "min:", stats->min_freq);
449 pr_name_and_units(out, "max:", stats->max_freq);
451 prt_printf(out, "mean:");
453 bch2_pr_time_units_aligned(out, f_mean);
455 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT));
458 prt_printf(out, "stddev:");
460 bch2_pr_time_units_aligned(out, f_stddev);
462 bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT));
464 printbuf_indent_sub(out, 2);
467 printbuf_tabstops_reset(out);
470 int i = eytzinger0_first(NR_QUANTILES);
471 const struct time_unit *u =
472 bch2_pick_time_units(quantiles->entries[i].m);
475 prt_printf(out, "quantiles (%s):\t", u->name);
476 eytzinger0_for_each(i, NR_QUANTILES) {
477 bool is_last = eytzinger0_next(i, NR_QUANTILES) == -1;
479 u64 q = max(quantiles->entries[i].m, last_q);
480 prt_printf(out, "%llu ", div_u64(q, u->nsecs));
491 * bch2_ratelimit_delay() - return how long to delay until the next time to do
493 * @d: the struct bch_ratelimit to update
494 * Returns: the amount of time to delay by, in jiffies
496 u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
498 u64 now = local_clock();
500 return time_after64(d->next, now)
501 ? nsecs_to_jiffies(d->next - now)
506 * bch2_ratelimit_increment() - increment @d by the amount of work done
507 * @d: the struct bch_ratelimit to update
508 * @done: the amount of work done, in arbitrary units
510 void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
512 u64 now = local_clock();
514 d->next += div_u64(done * NSEC_PER_SEC, d->rate);
516 if (time_before64(now + NSEC_PER_SEC, d->next))
517 d->next = now + NSEC_PER_SEC;
519 if (time_after64(now - NSEC_PER_SEC * 2, d->next))
520 d->next = now - NSEC_PER_SEC * 2;
526 * Updates pd_controller. Attempts to scale inputed values to units per second.
527 * @target: desired value
528 * @actual: current value
530 * @sign: 1 or -1; 1 if increasing the rate makes actual go up, -1 if increasing
531 * it makes actual go down.
533 void bch2_pd_controller_update(struct bch_pd_controller *pd,
534 s64 target, s64 actual, int sign)
536 s64 proportional, derivative, change;
538 unsigned long seconds_since_update = (jiffies - pd->last_update) / HZ;
540 if (seconds_since_update == 0)
543 pd->last_update = jiffies;
545 proportional = actual - target;
546 proportional *= seconds_since_update;
547 proportional = div_s64(proportional, pd->p_term_inverse);
549 derivative = actual - pd->last_actual;
550 derivative = div_s64(derivative, seconds_since_update);
551 derivative = ewma_add(pd->smoothed_derivative, derivative,
552 (pd->d_term / seconds_since_update) ?: 1);
553 derivative = derivative * pd->d_term;
554 derivative = div_s64(derivative, pd->p_term_inverse);
556 change = proportional + derivative;
558 /* Don't increase rate if not keeping up */
561 time_after64(local_clock(),
562 pd->rate.next + NSEC_PER_MSEC))
565 change *= (sign * -1);
567 pd->rate.rate = clamp_t(s64, (s64) pd->rate.rate + change,
570 pd->last_actual = actual;
571 pd->last_derivative = derivative;
572 pd->last_proportional = proportional;
573 pd->last_change = change;
574 pd->last_target = target;
577 void bch2_pd_controller_init(struct bch_pd_controller *pd)
579 pd->rate.rate = 1024;
580 pd->last_update = jiffies;
581 pd->p_term_inverse = 6000;
583 pd->d_smooth = pd->d_term;
584 pd->backpressure = 1;
587 void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_controller *pd)
589 if (!out->nr_tabstops)
590 printbuf_tabstop_push(out, 20);
592 prt_printf(out, "rate:");
594 prt_human_readable_s64(out, pd->rate.rate);
597 prt_printf(out, "target:");
599 prt_human_readable_u64(out, pd->last_target);
602 prt_printf(out, "actual:");
604 prt_human_readable_u64(out, pd->last_actual);
607 prt_printf(out, "proportional:");
609 prt_human_readable_s64(out, pd->last_proportional);
612 prt_printf(out, "derivative:");
614 prt_human_readable_s64(out, pd->last_derivative);
617 prt_printf(out, "change:");
619 prt_human_readable_s64(out, pd->last_change);
622 prt_printf(out, "next io:");
624 prt_printf(out, "%llims", div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC));
630 void bch2_bio_map(struct bio *bio, void *base, size_t size)
633 struct page *page = is_vmalloc_addr(base)
634 ? vmalloc_to_page(base)
635 : virt_to_page(base);
636 unsigned offset = offset_in_page(base);
637 unsigned len = min_t(size_t, PAGE_SIZE - offset, size);
639 BUG_ON(!bio_add_page(bio, page, len, offset));
645 int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
648 struct page *page = alloc_pages(gfp_mask, 0);
649 unsigned len = min_t(size_t, PAGE_SIZE, size);
654 if (unlikely(!bio_add_page(bio, page, len, 0))) {
665 size_t bch2_rand_range(size_t max)
673 rand = get_random_long();
674 rand &= roundup_pow_of_two(max) - 1;
675 } while (rand >= max);
680 void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, const void *src)
683 struct bvec_iter iter;
685 __bio_for_each_segment(bv, dst, iter, dst_iter) {
686 void *dstp = kmap_local_page(bv.bv_page);
688 memcpy(dstp + bv.bv_offset, src, bv.bv_len);
695 void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
698 struct bvec_iter iter;
700 __bio_for_each_segment(bv, src, iter, src_iter) {
701 void *srcp = kmap_local_page(bv.bv_page);
703 memcpy(dst, srcp + bv.bv_offset, bv.bv_len);
711 void eytzinger1_test(void)
713 unsigned inorder, eytz, size;
715 pr_info("1 based eytzinger test:");
720 unsigned extra = eytzinger1_extra(size);
723 pr_info("tree size %u", size);
725 BUG_ON(eytzinger1_prev(0, size) != eytzinger1_last(size));
726 BUG_ON(eytzinger1_next(0, size) != eytzinger1_first(size));
728 BUG_ON(eytzinger1_prev(eytzinger1_first(size), size) != 0);
729 BUG_ON(eytzinger1_next(eytzinger1_last(size), size) != 0);
732 eytzinger1_for_each(eytz, size) {
733 BUG_ON(__inorder_to_eytzinger1(inorder, size, extra) != eytz);
734 BUG_ON(__eytzinger1_to_inorder(eytz, size, extra) != inorder);
735 BUG_ON(eytz != eytzinger1_last(size) &&
736 eytzinger1_prev(eytzinger1_next(eytz, size), size) != eytz);
743 void eytzinger0_test(void)
746 unsigned inorder, eytz, size;
748 pr_info("0 based eytzinger test:");
753 unsigned extra = eytzinger0_extra(size);
756 pr_info("tree size %u", size);
758 BUG_ON(eytzinger0_prev(-1, size) != eytzinger0_last(size));
759 BUG_ON(eytzinger0_next(-1, size) != eytzinger0_first(size));
761 BUG_ON(eytzinger0_prev(eytzinger0_first(size), size) != -1);
762 BUG_ON(eytzinger0_next(eytzinger0_last(size), size) != -1);
765 eytzinger0_for_each(eytz, size) {
766 BUG_ON(__inorder_to_eytzinger0(inorder, size, extra) != eytz);
767 BUG_ON(__eytzinger0_to_inorder(eytz, size, extra) != inorder);
768 BUG_ON(eytz != eytzinger0_last(size) &&
769 eytzinger0_prev(eytzinger0_next(eytz, size), size) != eytz);
776 static inline int cmp_u16(const void *_l, const void *_r, size_t size)
778 const u16 *l = _l, *r = _r;
780 return (*l > *r) - (*r - *l);
783 static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search)
785 int i, c1 = -1, c2 = -1;
788 r = eytzinger0_find_le(test_array, nr,
789 sizeof(test_array[0]),
794 for (i = 0; i < nr; i++)
795 if (test_array[i] <= search && test_array[i] > c2)
799 eytzinger0_for_each(i, nr)
800 pr_info("[%3u] = %12u", i, test_array[i]);
801 pr_info("find_le(%2u) -> [%2zi] = %2i should be %2i",
806 void eytzinger0_find_test(void)
808 unsigned i, nr, allocated = 1 << 12;
809 u16 *test_array = kmalloc_array(allocated, sizeof(test_array[0]), GFP_KERNEL);
811 for (nr = 1; nr < allocated; nr++) {
812 pr_info("testing %u elems", nr);
814 get_random_bytes(test_array, nr * sizeof(test_array[0]));
815 eytzinger0_sort(test_array, nr, sizeof(test_array[0]), cmp_u16, NULL);
817 /* verify array is sorted correctly: */
818 eytzinger0_for_each(i, nr)
819 BUG_ON(i != eytzinger0_last(nr) &&
820 test_array[i] > test_array[eytzinger0_next(i, nr)]);
822 for (i = 0; i < U16_MAX; i += 1 << 12)
823 eytzinger0_find_test_val(test_array, nr, i);
825 for (i = 0; i < nr; i++) {
826 eytzinger0_find_test_val(test_array, nr, test_array[i] - 1);
827 eytzinger0_find_test_val(test_array, nr, test_array[i]);
828 eytzinger0_find_test_val(test_array, nr, test_array[i] + 1);
837 * Accumulate percpu counters onto one cpu's copy - only valid when access
838 * against any percpu counter is guarded against
840 u64 *bch2_acc_percpu_u64s(u64 __percpu *p, unsigned nr)
845 /* access to pcpu vars has to be blocked by other locking */
847 ret = this_cpu_ptr(p);
850 for_each_possible_cpu(cpu) {
851 u64 *i = per_cpu_ptr(p, cpu);
854 acc_u64s(ret, i, nr);
855 memset(i, 0, nr * sizeof(u64));
862 void bch2_darray_str_exit(darray_str *d)
864 darray_for_each(*d, i)
869 int bch2_split_devs(const char *_dev_name, darray_str *ret)
873 char *dev_name, *s, *orig;
875 dev_name = orig = kstrdup(_dev_name, GFP_KERNEL);
879 while ((s = strsep(&dev_name, ":"))) {
880 char *p = kstrdup(s, GFP_KERNEL);
884 if (darray_push(ret, p)) {
893 bch2_darray_str_exit(ret);