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 | 4875253 | 2017-03-21 08:56:07 -0700 | [diff] [blame] | 14 | #define BLK_RQ_STAT_BATCH 64 |
| 15 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 16 | struct blk_queue_stats { |
| 17 | struct list_head callbacks; |
| 18 | spinlock_t lock; |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame^] | 19 | bool enable_accounting; |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | unsigned int blk_stat_rq_ddir(const struct request *rq) |
| 23 | { |
| 24 | return rq_data_dir(rq); |
| 25 | } |
| 26 | EXPORT_SYMBOL_GPL(blk_stat_rq_ddir); |
| 27 | |
| 28 | static void blk_stat_init(struct blk_rq_stat *stat) |
| 29 | { |
| 30 | stat->min = -1ULL; |
| 31 | stat->max = stat->nr_samples = stat->mean = 0; |
| 32 | stat->batch = stat->nr_batch = 0; |
| 33 | } |
| 34 | |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 35 | static void blk_stat_flush_batch(struct blk_rq_stat *stat) |
| 36 | { |
| 37 | const s32 nr_batch = READ_ONCE(stat->nr_batch); |
Shaohua Li | 209200e | 2016-12-02 17:13:09 -0800 | [diff] [blame] | 38 | const s32 nr_samples = READ_ONCE(stat->nr_samples); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 39 | |
| 40 | if (!nr_batch) |
| 41 | return; |
| 42 | if (!nr_samples) |
| 43 | stat->mean = div64_s64(stat->batch, nr_batch); |
| 44 | else { |
| 45 | stat->mean = div64_s64((stat->mean * nr_samples) + |
| 46 | stat->batch, |
| 47 | nr_batch + nr_samples); |
| 48 | } |
| 49 | |
| 50 | stat->nr_samples += nr_batch; |
| 51 | stat->nr_batch = stat->batch = 0; |
| 52 | } |
| 53 | |
| 54 | static void blk_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src) |
| 55 | { |
Omar Sandoval | 7d8d001 | 2017-03-16 09:46:14 -0600 | [diff] [blame] | 56 | blk_stat_flush_batch(src); |
| 57 | |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 58 | if (!src->nr_samples) |
| 59 | return; |
| 60 | |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 61 | dst->min = min(dst->min, src->min); |
| 62 | dst->max = max(dst->max, src->max); |
| 63 | |
| 64 | if (!dst->nr_samples) |
| 65 | dst->mean = src->mean; |
| 66 | else { |
| 67 | dst->mean = div64_s64((src->mean * src->nr_samples) + |
| 68 | (dst->mean * dst->nr_samples), |
| 69 | dst->nr_samples + src->nr_samples); |
| 70 | } |
| 71 | dst->nr_samples += src->nr_samples; |
| 72 | } |
| 73 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 74 | static void __blk_stat_add(struct blk_rq_stat *stat, u64 value) |
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 | stat->min = min(stat->min, value); |
| 77 | stat->max = max(stat->max, value); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 78 | |
| 79 | if (stat->batch + value < stat->batch || |
| 80 | stat->nr_batch + 1 == BLK_RQ_STAT_BATCH) |
| 81 | blk_stat_flush_batch(stat); |
| 82 | |
| 83 | stat->batch += value; |
| 84 | stat->nr_batch++; |
| 85 | } |
| 86 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 87 | void blk_stat_add(struct request *rq) |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 88 | { |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 89 | struct request_queue *q = rq->q; |
| 90 | struct blk_stat_callback *cb; |
| 91 | struct blk_rq_stat *stat; |
| 92 | int bucket; |
| 93 | s64 now, value; |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 94 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 95 | now = __blk_stat_time(ktime_to_ns(ktime_get())); |
| 96 | if (now < blk_stat_time(&rq->issue_stat)) |
| 97 | return; |
| 98 | |
| 99 | value = now - blk_stat_time(&rq->issue_stat); |
| 100 | |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame^] | 101 | blk_throtl_stat_add(rq, value); |
| 102 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 103 | rcu_read_lock(); |
| 104 | list_for_each_entry_rcu(cb, &q->stats->callbacks, list) { |
| 105 | if (blk_stat_is_active(cb)) { |
| 106 | bucket = cb->bucket_fn(rq); |
| 107 | stat = &this_cpu_ptr(cb->cpu_stat)[bucket]; |
| 108 | __blk_stat_add(stat, value); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 109 | } |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 110 | } |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 111 | rcu_read_unlock(); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 114 | static void blk_stat_timer_fn(unsigned long data) |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 115 | { |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 116 | struct blk_stat_callback *cb = (void *)data; |
| 117 | unsigned int bucket; |
| 118 | int cpu; |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 119 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 120 | for (bucket = 0; bucket < cb->buckets; bucket++) |
| 121 | blk_stat_init(&cb->stat[bucket]); |
| 122 | |
| 123 | for_each_online_cpu(cpu) { |
| 124 | struct blk_rq_stat *cpu_stat; |
| 125 | |
| 126 | cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu); |
| 127 | for (bucket = 0; bucket < cb->buckets; bucket++) { |
| 128 | blk_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]); |
| 129 | blk_stat_init(&cpu_stat[bucket]); |
| 130 | } |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 133 | cb->timer_fn(cb); |
| 134 | } |
| 135 | |
| 136 | struct blk_stat_callback * |
| 137 | blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *), |
| 138 | unsigned int (*bucket_fn)(const struct request *), |
| 139 | unsigned int buckets, void *data) |
| 140 | { |
| 141 | struct blk_stat_callback *cb; |
| 142 | |
| 143 | cb = kmalloc(sizeof(*cb), GFP_KERNEL); |
| 144 | if (!cb) |
| 145 | return NULL; |
| 146 | |
| 147 | cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat), |
| 148 | GFP_KERNEL); |
| 149 | if (!cb->stat) { |
| 150 | kfree(cb); |
| 151 | return NULL; |
| 152 | } |
| 153 | cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat), |
| 154 | __alignof__(struct blk_rq_stat)); |
| 155 | if (!cb->cpu_stat) { |
| 156 | kfree(cb->stat); |
| 157 | kfree(cb); |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | cb->timer_fn = timer_fn; |
| 162 | cb->bucket_fn = bucket_fn; |
| 163 | cb->data = data; |
| 164 | cb->buckets = buckets; |
| 165 | setup_timer(&cb->timer, blk_stat_timer_fn, (unsigned long)cb); |
| 166 | |
| 167 | return cb; |
| 168 | } |
| 169 | EXPORT_SYMBOL_GPL(blk_stat_alloc_callback); |
| 170 | |
| 171 | void blk_stat_add_callback(struct request_queue *q, |
| 172 | struct blk_stat_callback *cb) |
| 173 | { |
| 174 | unsigned int bucket; |
| 175 | int cpu; |
| 176 | |
| 177 | for_each_possible_cpu(cpu) { |
| 178 | struct blk_rq_stat *cpu_stat; |
| 179 | |
| 180 | cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu); |
| 181 | for (bucket = 0; bucket < cb->buckets; bucket++) |
| 182 | blk_stat_init(&cpu_stat[bucket]); |
| 183 | } |
| 184 | |
| 185 | spin_lock(&q->stats->lock); |
| 186 | list_add_tail_rcu(&cb->list, &q->stats->callbacks); |
| 187 | set_bit(QUEUE_FLAG_STATS, &q->queue_flags); |
| 188 | spin_unlock(&q->stats->lock); |
| 189 | } |
| 190 | EXPORT_SYMBOL_GPL(blk_stat_add_callback); |
| 191 | |
| 192 | void blk_stat_remove_callback(struct request_queue *q, |
| 193 | struct blk_stat_callback *cb) |
| 194 | { |
| 195 | spin_lock(&q->stats->lock); |
| 196 | list_del_rcu(&cb->list); |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame^] | 197 | if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting) |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 198 | clear_bit(QUEUE_FLAG_STATS, &q->queue_flags); |
| 199 | spin_unlock(&q->stats->lock); |
| 200 | |
| 201 | del_timer_sync(&cb->timer); |
| 202 | } |
| 203 | EXPORT_SYMBOL_GPL(blk_stat_remove_callback); |
| 204 | |
| 205 | static void blk_stat_free_callback_rcu(struct rcu_head *head) |
| 206 | { |
| 207 | struct blk_stat_callback *cb; |
| 208 | |
| 209 | cb = container_of(head, struct blk_stat_callback, rcu); |
| 210 | free_percpu(cb->cpu_stat); |
| 211 | kfree(cb->stat); |
| 212 | kfree(cb); |
| 213 | } |
| 214 | |
| 215 | void blk_stat_free_callback(struct blk_stat_callback *cb) |
| 216 | { |
Jens Axboe | a83b576 | 2017-03-21 17:20:01 -0600 | [diff] [blame] | 217 | if (cb) |
| 218 | call_rcu(&cb->rcu, blk_stat_free_callback_rcu); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 219 | } |
| 220 | EXPORT_SYMBOL_GPL(blk_stat_free_callback); |
| 221 | |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame^] | 222 | void blk_stat_enable_accounting(struct request_queue *q) |
| 223 | { |
| 224 | spin_lock(&q->stats->lock); |
| 225 | q->stats->enable_accounting = true; |
| 226 | set_bit(QUEUE_FLAG_STATS, &q->queue_flags); |
| 227 | spin_unlock(&q->stats->lock); |
| 228 | } |
| 229 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 230 | struct blk_queue_stats *blk_alloc_queue_stats(void) |
| 231 | { |
| 232 | struct blk_queue_stats *stats; |
| 233 | |
| 234 | stats = kmalloc(sizeof(*stats), GFP_KERNEL); |
| 235 | if (!stats) |
| 236 | return NULL; |
| 237 | |
| 238 | INIT_LIST_HEAD(&stats->callbacks); |
| 239 | spin_lock_init(&stats->lock); |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame^] | 240 | stats->enable_accounting = false; |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 241 | |
| 242 | return stats; |
| 243 | } |
| 244 | |
| 245 | void blk_free_queue_stats(struct blk_queue_stats *stats) |
| 246 | { |
| 247 | if (!stats) |
| 248 | return; |
| 249 | |
| 250 | WARN_ON(!list_empty(&stats->callbacks)); |
| 251 | |
| 252 | kfree(stats); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 253 | } |