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/module.h>
13 #include <linux/random.h>
14 #include <linux/seq_file.h>
15 #include <linux/types.h>
17 #include <linux/freezer.h>
18 #include <linux/kthread.h>
22 #define simple_strtoint(c, end, base) simple_strtol(c, end, base)
23 #define simple_strtouint(c, end, base) simple_strtoul(c, end, base)
25 #define STRTO_H(name, type) \
26 int bch2_ ## name ## _h(const char *cp, type *res) \
30 type i = simple_ ## name(cp, &e, 10); \
32 switch (tolower(*e)) { \
62 if ((type) ~0 > 0 && \
63 (type) ~0 / 1024 <= i) \
65 if ((i > 0 && ANYSINT_MAX(type) / 1024 < i) || \
66 (i < 0 && -ANYSINT_MAX(type) / 1024 > i)) \
75 STRTO_H(strtoint, int)
76 STRTO_H(strtouint, unsigned int)
77 STRTO_H(strtoll, long long)
78 STRTO_H(strtoull, unsigned long long)
80 ssize_t bch2_hprint(char *buf, s64 v)
82 static const char units[] = "?kMGTPEZY";
86 for (u = 0; v >= 1024 || v <= -1024; u++) {
92 return sprintf(buf, "%lli", v);
95 * 103 is magic: t is in the range [-1023, 1023] and we want
96 * to turn it into [-9, 9]
98 if (v < 100 && v > -100)
99 snprintf(dec, sizeof(dec), ".%i", t / 103);
101 return sprintf(buf, "%lli%s%c", v, dec, units[u]);
104 ssize_t bch2_snprint_string_list(char *buf, size_t size, const char * const list[],
110 for (i = 0; list[i]; i++)
111 out += snprintf(out, buf + size - out,
112 i == selected ? "[%s] " : "%s ", list[i]);
118 ssize_t bch2_read_string_list(const char *buf, const char * const list[])
121 char *s, *d = kstrndup(buf, PAGE_SIZE - 1, GFP_KERNEL);
127 for (i = 0; list[i]; i++)
128 if (!strcmp(list[i], s))
139 bool bch2_is_zero(const void *_p, size_t n)
144 for (i = 0; i < n; i++)
150 void bch2_time_stats_clear(struct time_stats *stats)
152 spin_lock(&stats->lock);
155 stats->last_duration = 0;
156 stats->max_duration = 0;
157 stats->average_duration = 0;
158 stats->average_frequency = 0;
161 spin_unlock(&stats->lock);
164 void __bch2_time_stats_update(struct time_stats *stats, u64 start_time)
166 u64 now, duration, last;
171 duration = time_after64(now, start_time)
172 ? now - start_time : 0;
173 last = time_after64(now, stats->last)
174 ? now - stats->last : 0;
176 stats->last_duration = duration;
177 stats->max_duration = max(stats->max_duration, duration);
180 stats->average_duration = ewma_add(stats->average_duration,
183 if (stats->average_frequency)
184 stats->average_frequency =
185 ewma_add(stats->average_frequency,
188 stats->average_frequency = last << 8;
190 stats->average_duration = duration << 8;
193 stats->last = now ?: 1;
196 void bch2_time_stats_update(struct time_stats *stats, u64 start_time)
198 spin_lock(&stats->lock);
199 __bch2_time_stats_update(stats, start_time);
200 spin_unlock(&stats->lock);
204 * bch2_ratelimit_delay() - return how long to delay until the next time to do
207 * @d - the struct bch_ratelimit to update
209 * Returns the amount of time to delay by, in jiffies
211 u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
213 u64 now = local_clock();
215 return time_after64(d->next, now)
216 ? nsecs_to_jiffies(d->next - now)
221 * bch2_ratelimit_increment() - increment @d by the amount of work done
223 * @d - the struct bch_ratelimit to update
224 * @done - the amount of work done, in arbitrary units
226 void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
228 u64 now = local_clock();
230 d->next += div_u64(done * NSEC_PER_SEC, d->rate);
232 if (time_before64(now + NSEC_PER_SEC, d->next))
233 d->next = now + NSEC_PER_SEC;
235 if (time_after64(now - NSEC_PER_SEC * 2, d->next))
236 d->next = now - NSEC_PER_SEC * 2;
239 int bch2_ratelimit_wait_freezable_stoppable(struct bch_ratelimit *d)
242 u64 delay = bch2_ratelimit_delay(d);
245 set_current_state(TASK_INTERRUPTIBLE);
247 if (kthread_should_stop())
253 schedule_timeout(delay);
259 * Updates pd_controller. Attempts to scale inputed values to units per second.
260 * @target: desired value
261 * @actual: current value
263 * @sign: 1 or -1; 1 if increasing the rate makes actual go up, -1 if increasing
264 * it makes actual go down.
266 void bch2_pd_controller_update(struct bch_pd_controller *pd,
267 s64 target, s64 actual, int sign)
269 s64 proportional, derivative, change;
271 unsigned long seconds_since_update = (jiffies - pd->last_update) / HZ;
273 if (seconds_since_update == 0)
276 pd->last_update = jiffies;
278 proportional = actual - target;
279 proportional *= seconds_since_update;
280 proportional = div_s64(proportional, pd->p_term_inverse);
282 derivative = actual - pd->last_actual;
283 derivative = div_s64(derivative, seconds_since_update);
284 derivative = ewma_add(pd->smoothed_derivative, derivative,
285 (pd->d_term / seconds_since_update) ?: 1);
286 derivative = derivative * pd->d_term;
287 derivative = div_s64(derivative, pd->p_term_inverse);
289 change = proportional + derivative;
291 /* Don't increase rate if not keeping up */
294 time_after64(local_clock(),
295 pd->rate.next + NSEC_PER_MSEC))
298 change *= (sign * -1);
300 pd->rate.rate = clamp_t(s64, (s64) pd->rate.rate + change,
303 pd->last_actual = actual;
304 pd->last_derivative = derivative;
305 pd->last_proportional = proportional;
306 pd->last_change = change;
307 pd->last_target = target;
310 void bch2_pd_controller_init(struct bch_pd_controller *pd)
312 pd->rate.rate = 1024;
313 pd->last_update = jiffies;
314 pd->p_term_inverse = 6000;
316 pd->d_smooth = pd->d_term;
317 pd->backpressure = 1;
320 size_t bch2_pd_controller_print_debug(struct bch_pd_controller *pd, char *buf)
322 /* 2^64 - 1 is 20 digits, plus null byte */
326 char proportional[21];
331 bch2_hprint(rate, pd->rate.rate);
332 bch2_hprint(actual, pd->last_actual);
333 bch2_hprint(target, pd->last_target);
334 bch2_hprint(proportional, pd->last_proportional);
335 bch2_hprint(derivative, pd->last_derivative);
336 bch2_hprint(change, pd->last_change);
338 next_io = div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC);
344 "proportional:\t%s\n"
346 "change:\t\t%s/sec\n"
347 "next io:\t%llims\n",
348 rate, target, actual, proportional,
349 derivative, change, next_io);
352 void bch2_bio_map(struct bio *bio, void *base)
354 size_t size = bio->bi_iter.bi_size;
355 struct bio_vec *bv = bio->bi_io_vec;
357 BUG_ON(!bio->bi_iter.bi_size);
358 BUG_ON(bio->bi_vcnt);
360 bv->bv_offset = base ? offset_in_page(base) : 0;
363 for (; size; bio->bi_vcnt++, bv++) {
365 start: bv->bv_len = min_t(size_t, PAGE_SIZE - bv->bv_offset,
367 BUG_ON(bio->bi_vcnt >= bio->bi_max_vecs);
369 bv->bv_page = is_vmalloc_addr(base)
370 ? vmalloc_to_page(base)
371 : virt_to_page(base);
380 size_t bch2_rand_range(size_t max)
385 get_random_bytes(&rand, sizeof(rand));
386 rand &= roundup_pow_of_two(max) - 1;
387 } while (rand >= max);
392 void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, void *src)
395 struct bvec_iter iter;
397 __bio_for_each_segment(bv, dst, iter, dst_iter) {
398 void *dstp = kmap_atomic(bv.bv_page);
399 memcpy(dstp + bv.bv_offset, src, bv.bv_len);
406 void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
409 struct bvec_iter iter;
411 __bio_for_each_segment(bv, src, iter, src_iter) {
412 void *srcp = kmap_atomic(bv.bv_page);
413 memcpy(dst, srcp + bv.bv_offset, bv.bv_len);