2 * random utiility code, for bcache but in theory not specific to bcache
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
9 #include <linux/blkdev.h>
10 #include <linux/ctype.h>
11 #include <linux/debugfs.h>
12 #include <linux/freezer.h>
13 #include <linux/kthread.h>
14 #include <linux/log2.h>
15 #include <linux/math64.h>
16 #include <linux/random.h>
17 #include <linux/seq_file.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 #include <linux/sched/clock.h>
24 #define simple_strtoint(c, end, base) simple_strtol(c, end, base)
25 #define simple_strtouint(c, end, base) simple_strtoul(c, end, base)
27 #define STRTO_H(name, type) \
28 int bch2_ ## name ## _h(const char *cp, type *res) \
32 type i = simple_ ## name(cp, &e, 10); \
34 switch (tolower(*e)) { \
64 if ((type) ~0 > 0 && \
65 (type) ~0 / 1024 <= i) \
67 if ((i > 0 && ANYSINT_MAX(type) / 1024 < i) || \
68 (i < 0 && -ANYSINT_MAX(type) / 1024 > i)) \
77 STRTO_H(strtoint, int)
78 STRTO_H(strtouint, unsigned int)
79 STRTO_H(strtoll, long long)
80 STRTO_H(strtoull, unsigned long long)
82 ssize_t bch2_hprint(char *buf, s64 v)
84 static const char units[] = "?kMGTPEZY";
88 for (u = 0; v >= 1024 || v <= -1024; u++) {
94 return sprintf(buf, "%lli", v);
97 * 103 is magic: t is in the range [-1023, 1023] and we want
98 * to turn it into [-9, 9]
100 if (v < 100 && v > -100)
101 scnprintf(dec, sizeof(dec), ".%i", t / 103);
103 return sprintf(buf, "%lli%s%c", v, dec, units[u]);
106 ssize_t bch2_scnprint_string_list(char *buf, size_t size,
107 const char * const list[],
116 for (i = 0; list[i]; i++)
117 out += scnprintf(out, buf + size - out,
118 i == selected ? "[%s] " : "%s ", list[i]);
126 ssize_t bch2_read_string_list(const char *buf, const char * const list[])
130 buf = skip_spaces(buf);
133 while (len && isspace(buf[len - 1]))
136 for (i = 0; list[i]; i++)
137 if (strlen(list[i]) == len &&
138 !memcmp(buf, list[i], len))
141 return list[i] ? i : -EINVAL;
144 ssize_t bch2_scnprint_flag_list(char *buf, size_t size,
145 const char * const list[], u64 flags)
147 char *out = buf, *end = buf + size;
148 unsigned bit, nr = 0;
156 while (flags && (bit = __ffs(flags)) < nr) {
157 out += scnprintf(out, end - out, "%s,", list[bit]);
167 u64 bch2_read_flag_list(char *opt, const char * const list[])
170 char *p, *s, *d = kstrndup(opt, PAGE_SIZE - 1, GFP_KERNEL);
177 while ((p = strsep(&s, ","))) {
178 int flag = bch2_read_string_list(p, list);
192 bool bch2_is_zero(const void *_p, size_t n)
197 for (i = 0; i < n; i++)
203 void bch2_time_stats_clear(struct time_stats *stats)
205 spin_lock(&stats->lock);
208 stats->last_duration = 0;
209 stats->max_duration = 0;
210 stats->average_duration = 0;
211 stats->average_frequency = 0;
214 spin_unlock(&stats->lock);
217 void __bch2_time_stats_update(struct time_stats *stats, u64 start_time)
219 u64 now, duration, last;
224 duration = time_after64(now, start_time)
225 ? now - start_time : 0;
226 last = time_after64(now, stats->last)
227 ? now - stats->last : 0;
229 stats->last_duration = duration;
230 stats->max_duration = max(stats->max_duration, duration);
233 stats->average_duration = ewma_add(stats->average_duration,
236 if (stats->average_frequency)
237 stats->average_frequency =
238 ewma_add(stats->average_frequency,
241 stats->average_frequency = last << 8;
243 stats->average_duration = duration << 8;
246 stats->last = now ?: 1;
249 void bch2_time_stats_update(struct time_stats *stats, u64 start_time)
251 spin_lock(&stats->lock);
252 __bch2_time_stats_update(stats, start_time);
253 spin_unlock(&stats->lock);
257 * bch2_ratelimit_delay() - return how long to delay until the next time to do
260 * @d - the struct bch_ratelimit to update
262 * Returns the amount of time to delay by, in jiffies
264 u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
266 u64 now = local_clock();
268 return time_after64(d->next, now)
269 ? nsecs_to_jiffies(d->next - now)
274 * bch2_ratelimit_increment() - increment @d by the amount of work done
276 * @d - the struct bch_ratelimit to update
277 * @done - the amount of work done, in arbitrary units
279 void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
281 u64 now = local_clock();
283 d->next += div_u64(done * NSEC_PER_SEC, d->rate);
285 if (time_before64(now + NSEC_PER_SEC, d->next))
286 d->next = now + NSEC_PER_SEC;
288 if (time_after64(now - NSEC_PER_SEC * 2, d->next))
289 d->next = now - NSEC_PER_SEC * 2;
292 int bch2_ratelimit_wait_freezable_stoppable(struct bch_ratelimit *d)
295 u64 delay = bch2_ratelimit_delay(d);
298 set_current_state(TASK_INTERRUPTIBLE);
300 if (kthread_should_stop())
306 schedule_timeout(delay);
312 * Updates pd_controller. Attempts to scale inputed values to units per second.
313 * @target: desired value
314 * @actual: current value
316 * @sign: 1 or -1; 1 if increasing the rate makes actual go up, -1 if increasing
317 * it makes actual go down.
319 void bch2_pd_controller_update(struct bch_pd_controller *pd,
320 s64 target, s64 actual, int sign)
322 s64 proportional, derivative, change;
324 unsigned long seconds_since_update = (jiffies - pd->last_update) / HZ;
326 if (seconds_since_update == 0)
329 pd->last_update = jiffies;
331 proportional = actual - target;
332 proportional *= seconds_since_update;
333 proportional = div_s64(proportional, pd->p_term_inverse);
335 derivative = actual - pd->last_actual;
336 derivative = div_s64(derivative, seconds_since_update);
337 derivative = ewma_add(pd->smoothed_derivative, derivative,
338 (pd->d_term / seconds_since_update) ?: 1);
339 derivative = derivative * pd->d_term;
340 derivative = div_s64(derivative, pd->p_term_inverse);
342 change = proportional + derivative;
344 /* Don't increase rate if not keeping up */
347 time_after64(local_clock(),
348 pd->rate.next + NSEC_PER_MSEC))
351 change *= (sign * -1);
353 pd->rate.rate = clamp_t(s64, (s64) pd->rate.rate + change,
356 pd->last_actual = actual;
357 pd->last_derivative = derivative;
358 pd->last_proportional = proportional;
359 pd->last_change = change;
360 pd->last_target = target;
363 void bch2_pd_controller_init(struct bch_pd_controller *pd)
365 pd->rate.rate = 1024;
366 pd->last_update = jiffies;
367 pd->p_term_inverse = 6000;
369 pd->d_smooth = pd->d_term;
370 pd->backpressure = 1;
373 size_t bch2_pd_controller_print_debug(struct bch_pd_controller *pd, char *buf)
375 /* 2^64 - 1 is 20 digits, plus null byte */
379 char proportional[21];
384 bch2_hprint(rate, pd->rate.rate);
385 bch2_hprint(actual, pd->last_actual);
386 bch2_hprint(target, pd->last_target);
387 bch2_hprint(proportional, pd->last_proportional);
388 bch2_hprint(derivative, pd->last_derivative);
389 bch2_hprint(change, pd->last_change);
391 next_io = div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC);
397 "proportional:\t%s\n"
399 "change:\t\t%s/sec\n"
400 "next io:\t%llims\n",
401 rate, target, actual, proportional,
402 derivative, change, next_io);
405 void bch2_bio_map(struct bio *bio, void *base)
407 size_t size = bio->bi_iter.bi_size;
408 struct bio_vec *bv = bio->bi_io_vec;
410 BUG_ON(!bio->bi_iter.bi_size);
411 BUG_ON(bio->bi_vcnt);
413 bv->bv_offset = base ? offset_in_page(base) : 0;
416 for (; size; bio->bi_vcnt++, bv++) {
418 start: bv->bv_len = min_t(size_t, PAGE_SIZE - bv->bv_offset,
420 BUG_ON(bio->bi_vcnt >= bio->bi_max_vecs);
422 bv->bv_page = is_vmalloc_addr(base)
423 ? vmalloc_to_page(base)
424 : virt_to_page(base);
433 size_t bch2_rand_range(size_t max)
438 get_random_bytes(&rand, sizeof(rand));
439 rand &= roundup_pow_of_two(max) - 1;
440 } while (rand >= max);
445 void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, void *src)
448 struct bvec_iter iter;
450 __bio_for_each_segment(bv, dst, iter, dst_iter) {
451 void *dstp = kmap_atomic(bv.bv_page);
452 memcpy(dstp + bv.bv_offset, src, bv.bv_len);
459 void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
462 struct bvec_iter iter;
464 __bio_for_each_segment(bv, src, iter, src_iter) {
465 void *srcp = kmap_atomic(bv.bv_page);
466 memcpy(dst, srcp + bv.bv_offset, bv.bv_len);
473 size_t bch_scnmemcpy(char *buf, size_t size, const char *src, size_t len)
480 n = min(size - 1, len);
487 #include "eytzinger.h"
489 static int alignment_ok(const void *base, size_t align)
491 return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
492 ((unsigned long)base & (align - 1)) == 0;
495 static void u32_swap(void *a, void *b, size_t size)
498 *(u32 *)a = *(u32 *)b;
502 static void u64_swap(void *a, void *b, size_t size)
505 *(u64 *)a = *(u64 *)b;
509 static void generic_swap(void *a, void *b, size_t size)
515 *(char *)a++ = *(char *)b;
517 } while (--size > 0);
520 static inline int do_cmp(void *base, size_t n, size_t size,
521 int (*cmp_func)(const void *, const void *, size_t),
524 return cmp_func(base + inorder_to_eytzinger0(l, n) * size,
525 base + inorder_to_eytzinger0(r, n) * size,
529 static inline void do_swap(void *base, size_t n, size_t size,
530 void (*swap_func)(void *, void *, size_t),
533 swap_func(base + inorder_to_eytzinger0(l, n) * size,
534 base + inorder_to_eytzinger0(r, n) * size,
538 void eytzinger0_sort(void *base, size_t n, size_t size,
539 int (*cmp_func)(const void *, const void *, size_t),
540 void (*swap_func)(void *, void *, size_t))
545 if (size == 4 && alignment_ok(base, 4))
546 swap_func = u32_swap;
547 else if (size == 8 && alignment_ok(base, 8))
548 swap_func = u64_swap;
550 swap_func = generic_swap;
554 for (i = n / 2 - 1; i >= 0; --i) {
555 for (r = i; r * 2 + 1 < n; r = c) {
559 do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
562 if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
565 do_swap(base, n, size, swap_func, r, c);
570 for (i = n - 1; i > 0; --i) {
571 do_swap(base, n, size, swap_func, 0, i);
573 for (r = 0; r * 2 + 1 < i; r = c) {
577 do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
580 if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
583 do_swap(base, n, size, swap_func, r, c);
588 void sort_cmp_size(void *base, size_t num, size_t size,
589 int (*cmp_func)(const void *, const void *, size_t),
590 void (*swap_func)(void *, void *, size_t size))
592 /* pre-scale counters for performance */
593 int i = (num/2 - 1) * size, n = num * size, c, r;
596 if (size == 4 && alignment_ok(base, 4))
597 swap_func = u32_swap;
598 else if (size == 8 && alignment_ok(base, 8))
599 swap_func = u64_swap;
601 swap_func = generic_swap;
605 for ( ; i >= 0; i -= size) {
606 for (r = i; r * 2 + size < n; r = c) {
609 cmp_func(base + c, base + c + size, size) < 0)
611 if (cmp_func(base + r, base + c, size) >= 0)
613 swap_func(base + r, base + c, size);
618 for (i = n - size; i > 0; i -= size) {
619 swap_func(base, base + i, size);
620 for (r = 0; r * 2 + size < i; r = c) {
623 cmp_func(base + c, base + c + size, size) < 0)
625 if (cmp_func(base + r, base + c, size) >= 0)
627 swap_func(base + r, base + c, size);
632 void mempool_free_vp(void *element, void *pool_data)
634 size_t size = (size_t) pool_data;
636 vpfree(element, size);
639 void *mempool_alloc_vp(gfp_t gfp_mask, void *pool_data)
641 size_t size = (size_t) pool_data;
643 return vpmalloc(size, gfp_mask);