blob: 696a04176e4da44d3b5e04b57170d8c47c739d67 [file] [log] [blame]
Jens Axboecf43e6b2016-11-07 21:32:37 -07001/*
2 * Block stat tracking code
3 *
4 * Copyright (C) 2016 Jens Axboe
5 */
6#include <linux/kernel.h>
Omar Sandoval34dbad52017-03-21 08:56:08 -07007#include <linux/rculist.h>
Jens Axboecf43e6b2016-11-07 21:32:37 -07008#include <linux/blk-mq.h>
9
10#include "blk-stat.h"
11#include "blk-mq.h"
Shaohua Lib9147dd2017-03-27 15:19:42 -070012#include "blk.h"
Jens Axboecf43e6b2016-11-07 21:32:37 -070013
Omar Sandoval34dbad52017-03-21 08:56:08 -070014struct blk_queue_stats {
15 struct list_head callbacks;
16 spinlock_t lock;
Shaohua Lib9147dd2017-03-27 15:19:42 -070017 bool enable_accounting;
Omar Sandoval34dbad52017-03-21 08:56:08 -070018};
19
Josef Bacik2ecbf452018-07-03 11:14:57 -040020void blk_rq_stat_init(struct blk_rq_stat *stat)
Omar Sandoval34dbad52017-03-21 08:56:08 -070021{
22 stat->min = -1ULL;
23 stat->max = stat->nr_samples = stat->mean = 0;
Shaohua Lieca8b532017-10-06 17:55:59 -070024 stat->batch = 0;
Omar Sandoval34dbad52017-03-21 08:56:08 -070025}
26
Shaohua Lieca8b532017-10-06 17:55:59 -070027/* src is a per-cpu stat, mean isn't initialized */
Josef Bacik2ecbf452018-07-03 11:14:57 -040028void blk_rq_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src)
Jens Axboecf43e6b2016-11-07 21:32:37 -070029{
30 if (!src->nr_samples)
31 return;
32
Jens Axboecf43e6b2016-11-07 21:32:37 -070033 dst->min = min(dst->min, src->min);
34 dst->max = max(dst->max, src->max);
35
Shaohua Lieca8b532017-10-06 17:55:59 -070036 dst->mean = div_u64(src->batch + dst->mean * dst->nr_samples,
37 dst->nr_samples + src->nr_samples);
38
Jens Axboecf43e6b2016-11-07 21:32:37 -070039 dst->nr_samples += src->nr_samples;
40}
41
Josef Bacik2ecbf452018-07-03 11:14:57 -040042void blk_rq_stat_add(struct blk_rq_stat *stat, u64 value)
Jens Axboecf43e6b2016-11-07 21:32:37 -070043{
Omar Sandoval34dbad52017-03-21 08:56:08 -070044 stat->min = min(stat->min, value);
45 stat->max = max(stat->max, value);
Jens Axboecf43e6b2016-11-07 21:32:37 -070046 stat->batch += value;
Shaohua Lieca8b532017-10-06 17:55:59 -070047 stat->nr_samples++;
Jens Axboecf43e6b2016-11-07 21:32:37 -070048}
49
Omar Sandoval522a7772018-05-09 02:08:53 -070050void blk_stat_add(struct request *rq, u64 now)
Jens Axboecf43e6b2016-11-07 21:32:37 -070051{
Omar Sandoval34dbad52017-03-21 08:56:08 -070052 struct request_queue *q = rq->q;
53 struct blk_stat_callback *cb;
54 struct blk_rq_stat *stat;
55 int bucket;
Omar Sandoval522a7772018-05-09 02:08:53 -070056 u64 value;
Jens Axboecf43e6b2016-11-07 21:32:37 -070057
Omar Sandoval544ccc8d2018-05-09 02:08:50 -070058 value = (now >= rq->io_start_time_ns) ? now - rq->io_start_time_ns : 0;
Omar Sandoval34dbad52017-03-21 08:56:08 -070059
Shaohua Lib9147dd2017-03-27 15:19:42 -070060 blk_throtl_stat_add(rq, value);
61
Omar Sandoval34dbad52017-03-21 08:56:08 -070062 rcu_read_lock();
63 list_for_each_entry_rcu(cb, &q->stats->callbacks, list) {
Jens Axboed3738122017-05-09 11:39:56 -060064 if (!blk_stat_is_active(cb))
65 continue;
66
67 bucket = cb->bucket_fn(rq);
68 if (bucket < 0)
69 continue;
70
71 stat = &get_cpu_ptr(cb->cpu_stat)[bucket];
Josef Bacik2ecbf452018-07-03 11:14:57 -040072 blk_rq_stat_add(stat, value);
Jens Axboed3738122017-05-09 11:39:56 -060073 put_cpu_ptr(cb->cpu_stat);
Jens Axboecf43e6b2016-11-07 21:32:37 -070074 }
Omar Sandoval34dbad52017-03-21 08:56:08 -070075 rcu_read_unlock();
Jens Axboecf43e6b2016-11-07 21:32:37 -070076}
77
Kees Cooke99e88a2017-10-16 14:43:17 -070078static void blk_stat_timer_fn(struct timer_list *t)
Jens Axboecf43e6b2016-11-07 21:32:37 -070079{
Kees Cooke99e88a2017-10-16 14:43:17 -070080 struct blk_stat_callback *cb = from_timer(cb, t, timer);
Omar Sandoval34dbad52017-03-21 08:56:08 -070081 unsigned int bucket;
82 int cpu;
Jens Axboecf43e6b2016-11-07 21:32:37 -070083
Omar Sandoval34dbad52017-03-21 08:56:08 -070084 for (bucket = 0; bucket < cb->buckets; bucket++)
Josef Bacik2ecbf452018-07-03 11:14:57 -040085 blk_rq_stat_init(&cb->stat[bucket]);
Omar Sandoval34dbad52017-03-21 08:56:08 -070086
87 for_each_online_cpu(cpu) {
88 struct blk_rq_stat *cpu_stat;
89
90 cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
91 for (bucket = 0; bucket < cb->buckets; bucket++) {
Josef Bacik2ecbf452018-07-03 11:14:57 -040092 blk_rq_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]);
93 blk_rq_stat_init(&cpu_stat[bucket]);
Omar Sandoval34dbad52017-03-21 08:56:08 -070094 }
Jens Axboecf43e6b2016-11-07 21:32:37 -070095 }
96
Omar Sandoval34dbad52017-03-21 08:56:08 -070097 cb->timer_fn(cb);
98}
99
100struct blk_stat_callback *
101blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
Stephen Batesa37244e2017-04-20 15:29:16 -0600102 int (*bucket_fn)(const struct request *),
Omar Sandoval34dbad52017-03-21 08:56:08 -0700103 unsigned int buckets, void *data)
104{
105 struct blk_stat_callback *cb;
106
107 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
108 if (!cb)
109 return NULL;
110
111 cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat),
112 GFP_KERNEL);
113 if (!cb->stat) {
114 kfree(cb);
115 return NULL;
116 }
117 cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat),
118 __alignof__(struct blk_rq_stat));
119 if (!cb->cpu_stat) {
120 kfree(cb->stat);
121 kfree(cb);
122 return NULL;
123 }
124
125 cb->timer_fn = timer_fn;
126 cb->bucket_fn = bucket_fn;
127 cb->data = data;
128 cb->buckets = buckets;
Kees Cooke99e88a2017-10-16 14:43:17 -0700129 timer_setup(&cb->timer, blk_stat_timer_fn, 0);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700130
131 return cb;
132}
Omar Sandoval34dbad52017-03-21 08:56:08 -0700133
134void blk_stat_add_callback(struct request_queue *q,
135 struct blk_stat_callback *cb)
136{
137 unsigned int bucket;
138 int cpu;
139
140 for_each_possible_cpu(cpu) {
141 struct blk_rq_stat *cpu_stat;
142
143 cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
144 for (bucket = 0; bucket < cb->buckets; bucket++)
Josef Bacik2ecbf452018-07-03 11:14:57 -0400145 blk_rq_stat_init(&cpu_stat[bucket]);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700146 }
147
148 spin_lock(&q->stats->lock);
149 list_add_tail_rcu(&cb->list, &q->stats->callbacks);
Bart Van Assche7dfdbc72018-03-07 17:10:05 -0800150 blk_queue_flag_set(QUEUE_FLAG_STATS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700151 spin_unlock(&q->stats->lock);
152}
Omar Sandoval34dbad52017-03-21 08:56:08 -0700153
154void blk_stat_remove_callback(struct request_queue *q,
155 struct blk_stat_callback *cb)
156{
157 spin_lock(&q->stats->lock);
158 list_del_rcu(&cb->list);
Shaohua Lib9147dd2017-03-27 15:19:42 -0700159 if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting)
Bart Van Assche7dfdbc72018-03-07 17:10:05 -0800160 blk_queue_flag_clear(QUEUE_FLAG_STATS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700161 spin_unlock(&q->stats->lock);
162
163 del_timer_sync(&cb->timer);
164}
Omar Sandoval34dbad52017-03-21 08:56:08 -0700165
166static void blk_stat_free_callback_rcu(struct rcu_head *head)
167{
168 struct blk_stat_callback *cb;
169
170 cb = container_of(head, struct blk_stat_callback, rcu);
171 free_percpu(cb->cpu_stat);
172 kfree(cb->stat);
173 kfree(cb);
174}
175
176void blk_stat_free_callback(struct blk_stat_callback *cb)
177{
Jens Axboea83b5762017-03-21 17:20:01 -0600178 if (cb)
179 call_rcu(&cb->rcu, blk_stat_free_callback_rcu);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700180}
Omar Sandoval34dbad52017-03-21 08:56:08 -0700181
Shaohua Lib9147dd2017-03-27 15:19:42 -0700182void blk_stat_enable_accounting(struct request_queue *q)
183{
184 spin_lock(&q->stats->lock);
185 q->stats->enable_accounting = true;
Bart Van Assche7dfdbc72018-03-07 17:10:05 -0800186 blk_queue_flag_set(QUEUE_FLAG_STATS, q);
Shaohua Lib9147dd2017-03-27 15:19:42 -0700187 spin_unlock(&q->stats->lock);
188}
Omar Sandovalf8232f22018-09-27 15:55:52 -0700189EXPORT_SYMBOL_GPL(blk_stat_enable_accounting);
Shaohua Lib9147dd2017-03-27 15:19:42 -0700190
Omar Sandoval34dbad52017-03-21 08:56:08 -0700191struct blk_queue_stats *blk_alloc_queue_stats(void)
192{
193 struct blk_queue_stats *stats;
194
195 stats = kmalloc(sizeof(*stats), GFP_KERNEL);
196 if (!stats)
197 return NULL;
198
199 INIT_LIST_HEAD(&stats->callbacks);
200 spin_lock_init(&stats->lock);
Shaohua Lib9147dd2017-03-27 15:19:42 -0700201 stats->enable_accounting = false;
Omar Sandoval34dbad52017-03-21 08:56:08 -0700202
203 return stats;
204}
205
206void blk_free_queue_stats(struct blk_queue_stats *stats)
207{
208 if (!stats)
209 return;
210
211 WARN_ON(!list_empty(&stats->callbacks));
212
213 kfree(stats);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700214}