blob: 6c2f40940439c5b50a6aad0c0a560e8a2b2bf08b [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 Sandoval48752532017-03-21 08:56:07 -070014#define BLK_RQ_STAT_BATCH 64
15
Omar Sandoval34dbad52017-03-21 08:56:08 -070016struct blk_queue_stats {
17 struct list_head callbacks;
18 spinlock_t lock;
Shaohua Lib9147dd2017-03-27 15:19:42 -070019 bool enable_accounting;
Omar Sandoval34dbad52017-03-21 08:56:08 -070020};
21
Omar Sandoval34dbad52017-03-21 08:56:08 -070022static void blk_stat_init(struct blk_rq_stat *stat)
23{
24 stat->min = -1ULL;
25 stat->max = stat->nr_samples = stat->mean = 0;
26 stat->batch = stat->nr_batch = 0;
27}
28
Jens Axboecf43e6b2016-11-07 21:32:37 -070029static void blk_stat_flush_batch(struct blk_rq_stat *stat)
30{
31 const s32 nr_batch = READ_ONCE(stat->nr_batch);
Shaohua Li209200e2016-12-02 17:13:09 -080032 const s32 nr_samples = READ_ONCE(stat->nr_samples);
Jens Axboecf43e6b2016-11-07 21:32:37 -070033
34 if (!nr_batch)
35 return;
36 if (!nr_samples)
37 stat->mean = div64_s64(stat->batch, nr_batch);
38 else {
39 stat->mean = div64_s64((stat->mean * nr_samples) +
40 stat->batch,
41 nr_batch + nr_samples);
42 }
43
44 stat->nr_samples += nr_batch;
45 stat->nr_batch = stat->batch = 0;
46}
47
48static void blk_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src)
49{
Omar Sandoval7d8d0012017-03-16 09:46:14 -060050 blk_stat_flush_batch(src);
51
Jens Axboecf43e6b2016-11-07 21:32:37 -070052 if (!src->nr_samples)
53 return;
54
Jens Axboecf43e6b2016-11-07 21:32:37 -070055 dst->min = min(dst->min, src->min);
56 dst->max = max(dst->max, src->max);
57
58 if (!dst->nr_samples)
59 dst->mean = src->mean;
60 else {
61 dst->mean = div64_s64((src->mean * src->nr_samples) +
62 (dst->mean * dst->nr_samples),
63 dst->nr_samples + src->nr_samples);
64 }
65 dst->nr_samples += src->nr_samples;
66}
67
Omar Sandoval34dbad52017-03-21 08:56:08 -070068static void __blk_stat_add(struct blk_rq_stat *stat, u64 value)
Jens Axboecf43e6b2016-11-07 21:32:37 -070069{
Omar Sandoval34dbad52017-03-21 08:56:08 -070070 stat->min = min(stat->min, value);
71 stat->max = max(stat->max, value);
Jens Axboecf43e6b2016-11-07 21:32:37 -070072
73 if (stat->batch + value < stat->batch ||
74 stat->nr_batch + 1 == BLK_RQ_STAT_BATCH)
75 blk_stat_flush_batch(stat);
76
77 stat->batch += value;
78 stat->nr_batch++;
79}
80
Omar Sandoval34dbad52017-03-21 08:56:08 -070081void blk_stat_add(struct request *rq)
Jens Axboecf43e6b2016-11-07 21:32:37 -070082{
Omar Sandoval34dbad52017-03-21 08:56:08 -070083 struct request_queue *q = rq->q;
84 struct blk_stat_callback *cb;
85 struct blk_rq_stat *stat;
86 int bucket;
87 s64 now, value;
Jens Axboecf43e6b2016-11-07 21:32:37 -070088
Omar Sandoval34dbad52017-03-21 08:56:08 -070089 now = __blk_stat_time(ktime_to_ns(ktime_get()));
90 if (now < blk_stat_time(&rq->issue_stat))
91 return;
92
93 value = now - blk_stat_time(&rq->issue_stat);
94
Shaohua Lib9147dd2017-03-27 15:19:42 -070095 blk_throtl_stat_add(rq, value);
96
Omar Sandoval34dbad52017-03-21 08:56:08 -070097 rcu_read_lock();
98 list_for_each_entry_rcu(cb, &q->stats->callbacks, list) {
99 if (blk_stat_is_active(cb)) {
100 bucket = cb->bucket_fn(rq);
Stephen Batesa37244e2017-04-20 15:29:16 -0600101 if (bucket < 0)
102 continue;
Omar Sandoval34dbad52017-03-21 08:56:08 -0700103 stat = &this_cpu_ptr(cb->cpu_stat)[bucket];
104 __blk_stat_add(stat, value);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700105 }
Jens Axboecf43e6b2016-11-07 21:32:37 -0700106 }
Omar Sandoval34dbad52017-03-21 08:56:08 -0700107 rcu_read_unlock();
Jens Axboecf43e6b2016-11-07 21:32:37 -0700108}
109
Omar Sandoval34dbad52017-03-21 08:56:08 -0700110static void blk_stat_timer_fn(unsigned long data)
Jens Axboecf43e6b2016-11-07 21:32:37 -0700111{
Omar Sandoval34dbad52017-03-21 08:56:08 -0700112 struct blk_stat_callback *cb = (void *)data;
113 unsigned int bucket;
114 int cpu;
Jens Axboecf43e6b2016-11-07 21:32:37 -0700115
Omar Sandoval34dbad52017-03-21 08:56:08 -0700116 for (bucket = 0; bucket < cb->buckets; bucket++)
117 blk_stat_init(&cb->stat[bucket]);
118
119 for_each_online_cpu(cpu) {
120 struct blk_rq_stat *cpu_stat;
121
122 cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
123 for (bucket = 0; bucket < cb->buckets; bucket++) {
124 blk_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]);
125 blk_stat_init(&cpu_stat[bucket]);
126 }
Jens Axboecf43e6b2016-11-07 21:32:37 -0700127 }
128
Omar Sandoval34dbad52017-03-21 08:56:08 -0700129 cb->timer_fn(cb);
130}
131
132struct blk_stat_callback *
133blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
Stephen Batesa37244e2017-04-20 15:29:16 -0600134 int (*bucket_fn)(const struct request *),
Omar Sandoval34dbad52017-03-21 08:56:08 -0700135 unsigned int buckets, void *data)
136{
137 struct blk_stat_callback *cb;
138
139 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
140 if (!cb)
141 return NULL;
142
143 cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat),
144 GFP_KERNEL);
145 if (!cb->stat) {
146 kfree(cb);
147 return NULL;
148 }
149 cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat),
150 __alignof__(struct blk_rq_stat));
151 if (!cb->cpu_stat) {
152 kfree(cb->stat);
153 kfree(cb);
154 return NULL;
155 }
156
157 cb->timer_fn = timer_fn;
158 cb->bucket_fn = bucket_fn;
159 cb->data = data;
160 cb->buckets = buckets;
161 setup_timer(&cb->timer, blk_stat_timer_fn, (unsigned long)cb);
162
163 return cb;
164}
165EXPORT_SYMBOL_GPL(blk_stat_alloc_callback);
166
167void blk_stat_add_callback(struct request_queue *q,
168 struct blk_stat_callback *cb)
169{
170 unsigned int bucket;
171 int cpu;
172
173 for_each_possible_cpu(cpu) {
174 struct blk_rq_stat *cpu_stat;
175
176 cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
177 for (bucket = 0; bucket < cb->buckets; bucket++)
178 blk_stat_init(&cpu_stat[bucket]);
179 }
180
181 spin_lock(&q->stats->lock);
182 list_add_tail_rcu(&cb->list, &q->stats->callbacks);
183 set_bit(QUEUE_FLAG_STATS, &q->queue_flags);
184 spin_unlock(&q->stats->lock);
185}
186EXPORT_SYMBOL_GPL(blk_stat_add_callback);
187
188void blk_stat_remove_callback(struct request_queue *q,
189 struct blk_stat_callback *cb)
190{
191 spin_lock(&q->stats->lock);
192 list_del_rcu(&cb->list);
Shaohua Lib9147dd2017-03-27 15:19:42 -0700193 if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting)
Omar Sandoval34dbad52017-03-21 08:56:08 -0700194 clear_bit(QUEUE_FLAG_STATS, &q->queue_flags);
195 spin_unlock(&q->stats->lock);
196
197 del_timer_sync(&cb->timer);
198}
199EXPORT_SYMBOL_GPL(blk_stat_remove_callback);
200
201static void blk_stat_free_callback_rcu(struct rcu_head *head)
202{
203 struct blk_stat_callback *cb;
204
205 cb = container_of(head, struct blk_stat_callback, rcu);
206 free_percpu(cb->cpu_stat);
207 kfree(cb->stat);
208 kfree(cb);
209}
210
211void blk_stat_free_callback(struct blk_stat_callback *cb)
212{
Jens Axboea83b5762017-03-21 17:20:01 -0600213 if (cb)
214 call_rcu(&cb->rcu, blk_stat_free_callback_rcu);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700215}
216EXPORT_SYMBOL_GPL(blk_stat_free_callback);
217
Shaohua Lib9147dd2017-03-27 15:19:42 -0700218void blk_stat_enable_accounting(struct request_queue *q)
219{
220 spin_lock(&q->stats->lock);
221 q->stats->enable_accounting = true;
222 set_bit(QUEUE_FLAG_STATS, &q->queue_flags);
223 spin_unlock(&q->stats->lock);
224}
225
Omar Sandoval34dbad52017-03-21 08:56:08 -0700226struct blk_queue_stats *blk_alloc_queue_stats(void)
227{
228 struct blk_queue_stats *stats;
229
230 stats = kmalloc(sizeof(*stats), GFP_KERNEL);
231 if (!stats)
232 return NULL;
233
234 INIT_LIST_HEAD(&stats->callbacks);
235 spin_lock_init(&stats->lock);
Shaohua Lib9147dd2017-03-27 15:19:42 -0700236 stats->enable_accounting = false;
Omar Sandoval34dbad52017-03-21 08:56:08 -0700237
238 return stats;
239}
240
241void blk_free_queue_stats(struct blk_queue_stats *stats)
242{
243 if (!stats)
244 return;
245
246 WARN_ON(!list_empty(&stats->callbacks));
247
248 kfree(stats);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700249}