Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Block stat tracking code |
| 3 | * |
| 4 | * Copyright (C) 2016 Jens Axboe |
| 5 | */ |
| 6 | #include <linux/kernel.h> |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 7 | #include <linux/rculist.h> |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 8 | #include <linux/blk-mq.h> |
| 9 | |
| 10 | #include "blk-stat.h" |
| 11 | #include "blk-mq.h" |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 12 | #include "blk.h" |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 13 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 14 | struct blk_queue_stats { |
| 15 | struct list_head callbacks; |
| 16 | spinlock_t lock; |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 17 | bool enable_accounting; |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 18 | }; |
| 19 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 20 | static void blk_stat_init(struct blk_rq_stat *stat) |
| 21 | { |
| 22 | stat->min = -1ULL; |
| 23 | stat->max = stat->nr_samples = stat->mean = 0; |
Shaohua Li | eca8b53 | 2017-10-06 17:55:59 -0700 | [diff] [blame] | 24 | stat->batch = 0; |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Shaohua Li | eca8b53 | 2017-10-06 17:55:59 -0700 | [diff] [blame] | 27 | /* src is a per-cpu stat, mean isn't initialized */ |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 28 | static void blk_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src) |
| 29 | { |
| 30 | if (!src->nr_samples) |
| 31 | return; |
| 32 | |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 33 | dst->min = min(dst->min, src->min); |
| 34 | dst->max = max(dst->max, src->max); |
| 35 | |
Shaohua Li | eca8b53 | 2017-10-06 17:55:59 -0700 | [diff] [blame] | 36 | dst->mean = div_u64(src->batch + dst->mean * dst->nr_samples, |
| 37 | dst->nr_samples + src->nr_samples); |
| 38 | |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 39 | dst->nr_samples += src->nr_samples; |
| 40 | } |
| 41 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 42 | static void __blk_stat_add(struct blk_rq_stat *stat, u64 value) |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 43 | { |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 44 | stat->min = min(stat->min, value); |
| 45 | stat->max = max(stat->max, value); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 46 | stat->batch += value; |
Shaohua Li | eca8b53 | 2017-10-06 17:55:59 -0700 | [diff] [blame] | 47 | stat->nr_samples++; |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 50 | void blk_stat_add(struct request *rq) |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 51 | { |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 52 | struct request_queue *q = rq->q; |
| 53 | struct blk_stat_callback *cb; |
| 54 | struct blk_rq_stat *stat; |
| 55 | int bucket; |
Shaohua Li | eca8b53 | 2017-10-06 17:55:59 -0700 | [diff] [blame] | 56 | u64 now, value; |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 57 | |
Omar Sandoval | 544ccc8d | 2018-05-09 02:08:50 -0700 | [diff] [blame^] | 58 | now = ktime_get_ns(); |
| 59 | value = (now >= rq->io_start_time_ns) ? now - rq->io_start_time_ns : 0; |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 60 | |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 61 | blk_throtl_stat_add(rq, value); |
| 62 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 63 | rcu_read_lock(); |
| 64 | list_for_each_entry_rcu(cb, &q->stats->callbacks, list) { |
Jens Axboe | d373812 | 2017-05-09 11:39:56 -0600 | [diff] [blame] | 65 | if (!blk_stat_is_active(cb)) |
| 66 | continue; |
| 67 | |
| 68 | bucket = cb->bucket_fn(rq); |
| 69 | if (bucket < 0) |
| 70 | continue; |
| 71 | |
| 72 | stat = &get_cpu_ptr(cb->cpu_stat)[bucket]; |
| 73 | __blk_stat_add(stat, value); |
| 74 | put_cpu_ptr(cb->cpu_stat); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 75 | } |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 76 | rcu_read_unlock(); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 79 | static void blk_stat_timer_fn(struct timer_list *t) |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 80 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 81 | struct blk_stat_callback *cb = from_timer(cb, t, timer); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 82 | unsigned int bucket; |
| 83 | int cpu; |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 84 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 85 | for (bucket = 0; bucket < cb->buckets; bucket++) |
| 86 | blk_stat_init(&cb->stat[bucket]); |
| 87 | |
| 88 | for_each_online_cpu(cpu) { |
| 89 | struct blk_rq_stat *cpu_stat; |
| 90 | |
| 91 | cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu); |
| 92 | for (bucket = 0; bucket < cb->buckets; bucket++) { |
| 93 | blk_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]); |
| 94 | blk_stat_init(&cpu_stat[bucket]); |
| 95 | } |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 98 | cb->timer_fn(cb); |
| 99 | } |
| 100 | |
| 101 | struct blk_stat_callback * |
| 102 | blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *), |
Stephen Bates | a37244e | 2017-04-20 15:29:16 -0600 | [diff] [blame] | 103 | int (*bucket_fn)(const struct request *), |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 104 | unsigned int buckets, void *data) |
| 105 | { |
| 106 | struct blk_stat_callback *cb; |
| 107 | |
| 108 | cb = kmalloc(sizeof(*cb), GFP_KERNEL); |
| 109 | if (!cb) |
| 110 | return NULL; |
| 111 | |
| 112 | cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat), |
| 113 | GFP_KERNEL); |
| 114 | if (!cb->stat) { |
| 115 | kfree(cb); |
| 116 | return NULL; |
| 117 | } |
| 118 | cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat), |
| 119 | __alignof__(struct blk_rq_stat)); |
| 120 | if (!cb->cpu_stat) { |
| 121 | kfree(cb->stat); |
| 122 | kfree(cb); |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | cb->timer_fn = timer_fn; |
| 127 | cb->bucket_fn = bucket_fn; |
| 128 | cb->data = data; |
| 129 | cb->buckets = buckets; |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 130 | timer_setup(&cb->timer, blk_stat_timer_fn, 0); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 131 | |
| 132 | return cb; |
| 133 | } |
| 134 | EXPORT_SYMBOL_GPL(blk_stat_alloc_callback); |
| 135 | |
| 136 | void blk_stat_add_callback(struct request_queue *q, |
| 137 | struct blk_stat_callback *cb) |
| 138 | { |
| 139 | unsigned int bucket; |
| 140 | int cpu; |
| 141 | |
| 142 | for_each_possible_cpu(cpu) { |
| 143 | struct blk_rq_stat *cpu_stat; |
| 144 | |
| 145 | cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu); |
| 146 | for (bucket = 0; bucket < cb->buckets; bucket++) |
| 147 | blk_stat_init(&cpu_stat[bucket]); |
| 148 | } |
| 149 | |
| 150 | spin_lock(&q->stats->lock); |
| 151 | list_add_tail_rcu(&cb->list, &q->stats->callbacks); |
Bart Van Assche | 7dfdbc7 | 2018-03-07 17:10:05 -0800 | [diff] [blame] | 152 | blk_queue_flag_set(QUEUE_FLAG_STATS, q); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 153 | spin_unlock(&q->stats->lock); |
| 154 | } |
| 155 | EXPORT_SYMBOL_GPL(blk_stat_add_callback); |
| 156 | |
| 157 | void blk_stat_remove_callback(struct request_queue *q, |
| 158 | struct blk_stat_callback *cb) |
| 159 | { |
| 160 | spin_lock(&q->stats->lock); |
| 161 | list_del_rcu(&cb->list); |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 162 | if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting) |
Bart Van Assche | 7dfdbc7 | 2018-03-07 17:10:05 -0800 | [diff] [blame] | 163 | blk_queue_flag_clear(QUEUE_FLAG_STATS, q); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 164 | spin_unlock(&q->stats->lock); |
| 165 | |
| 166 | del_timer_sync(&cb->timer); |
| 167 | } |
| 168 | EXPORT_SYMBOL_GPL(blk_stat_remove_callback); |
| 169 | |
| 170 | static void blk_stat_free_callback_rcu(struct rcu_head *head) |
| 171 | { |
| 172 | struct blk_stat_callback *cb; |
| 173 | |
| 174 | cb = container_of(head, struct blk_stat_callback, rcu); |
| 175 | free_percpu(cb->cpu_stat); |
| 176 | kfree(cb->stat); |
| 177 | kfree(cb); |
| 178 | } |
| 179 | |
| 180 | void blk_stat_free_callback(struct blk_stat_callback *cb) |
| 181 | { |
Jens Axboe | a83b576 | 2017-03-21 17:20:01 -0600 | [diff] [blame] | 182 | if (cb) |
| 183 | call_rcu(&cb->rcu, blk_stat_free_callback_rcu); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 184 | } |
| 185 | EXPORT_SYMBOL_GPL(blk_stat_free_callback); |
| 186 | |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 187 | void blk_stat_enable_accounting(struct request_queue *q) |
| 188 | { |
| 189 | spin_lock(&q->stats->lock); |
| 190 | q->stats->enable_accounting = true; |
Bart Van Assche | 7dfdbc7 | 2018-03-07 17:10:05 -0800 | [diff] [blame] | 191 | blk_queue_flag_set(QUEUE_FLAG_STATS, q); |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 192 | spin_unlock(&q->stats->lock); |
| 193 | } |
| 194 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 195 | struct blk_queue_stats *blk_alloc_queue_stats(void) |
| 196 | { |
| 197 | struct blk_queue_stats *stats; |
| 198 | |
| 199 | stats = kmalloc(sizeof(*stats), GFP_KERNEL); |
| 200 | if (!stats) |
| 201 | return NULL; |
| 202 | |
| 203 | INIT_LIST_HEAD(&stats->callbacks); |
| 204 | spin_lock_init(&stats->lock); |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 205 | stats->enable_accounting = false; |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 206 | |
| 207 | return stats; |
| 208 | } |
| 209 | |
| 210 | void blk_free_queue_stats(struct blk_queue_stats *stats) |
| 211 | { |
| 212 | if (!stats) |
| 213 | return; |
| 214 | |
| 215 | WARN_ON(!list_empty(&stats->callbacks)); |
| 216 | |
| 217 | kfree(stats); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 218 | } |