blob: e77ec52f5bb51513fc9d7850301ab5bbafe51551 [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
22unsigned int blk_stat_rq_ddir(const struct request *rq)
23{
24 return rq_data_dir(rq);
25}
26EXPORT_SYMBOL_GPL(blk_stat_rq_ddir);
27
28static 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 Axboecf43e6b2016-11-07 21:32:37 -070035static void blk_stat_flush_batch(struct blk_rq_stat *stat)
36{
37 const s32 nr_batch = READ_ONCE(stat->nr_batch);
Shaohua Li209200e2016-12-02 17:13:09 -080038 const s32 nr_samples = READ_ONCE(stat->nr_samples);
Jens Axboecf43e6b2016-11-07 21:32:37 -070039
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
54static void blk_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src)
55{
Omar Sandoval7d8d0012017-03-16 09:46:14 -060056 blk_stat_flush_batch(src);
57
Jens Axboecf43e6b2016-11-07 21:32:37 -070058 if (!src->nr_samples)
59 return;
60
Jens Axboecf43e6b2016-11-07 21:32:37 -070061 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 Sandoval34dbad52017-03-21 08:56:08 -070074static void __blk_stat_add(struct blk_rq_stat *stat, u64 value)
Jens Axboecf43e6b2016-11-07 21:32:37 -070075{
Omar Sandoval34dbad52017-03-21 08:56:08 -070076 stat->min = min(stat->min, value);
77 stat->max = max(stat->max, value);
Jens Axboecf43e6b2016-11-07 21:32:37 -070078
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 Sandoval34dbad52017-03-21 08:56:08 -070087void blk_stat_add(struct request *rq)
Jens Axboecf43e6b2016-11-07 21:32:37 -070088{
Omar Sandoval34dbad52017-03-21 08:56:08 -070089 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 Axboecf43e6b2016-11-07 21:32:37 -070094
Omar Sandoval34dbad52017-03-21 08:56:08 -070095 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 Lib9147dd2017-03-27 15:19:42 -0700101 blk_throtl_stat_add(rq, value);
102
Omar Sandoval34dbad52017-03-21 08:56:08 -0700103 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 Axboecf43e6b2016-11-07 21:32:37 -0700109 }
Jens Axboecf43e6b2016-11-07 21:32:37 -0700110 }
Omar Sandoval34dbad52017-03-21 08:56:08 -0700111 rcu_read_unlock();
Jens Axboecf43e6b2016-11-07 21:32:37 -0700112}
113
Omar Sandoval34dbad52017-03-21 08:56:08 -0700114static void blk_stat_timer_fn(unsigned long data)
Jens Axboecf43e6b2016-11-07 21:32:37 -0700115{
Omar Sandoval34dbad52017-03-21 08:56:08 -0700116 struct blk_stat_callback *cb = (void *)data;
117 unsigned int bucket;
118 int cpu;
Jens Axboecf43e6b2016-11-07 21:32:37 -0700119
Omar Sandoval34dbad52017-03-21 08:56:08 -0700120 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 Axboecf43e6b2016-11-07 21:32:37 -0700131 }
132
Omar Sandoval34dbad52017-03-21 08:56:08 -0700133 cb->timer_fn(cb);
134}
135
136struct blk_stat_callback *
137blk_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}
169EXPORT_SYMBOL_GPL(blk_stat_alloc_callback);
170
171void 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}
190EXPORT_SYMBOL_GPL(blk_stat_add_callback);
191
192void 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 Lib9147dd2017-03-27 15:19:42 -0700197 if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting)
Omar Sandoval34dbad52017-03-21 08:56:08 -0700198 clear_bit(QUEUE_FLAG_STATS, &q->queue_flags);
199 spin_unlock(&q->stats->lock);
200
201 del_timer_sync(&cb->timer);
202}
203EXPORT_SYMBOL_GPL(blk_stat_remove_callback);
204
205static 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
215void blk_stat_free_callback(struct blk_stat_callback *cb)
216{
Jens Axboea83b5762017-03-21 17:20:01 -0600217 if (cb)
218 call_rcu(&cb->rcu, blk_stat_free_callback_rcu);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700219}
220EXPORT_SYMBOL_GPL(blk_stat_free_callback);
221
Shaohua Lib9147dd2017-03-27 15:19:42 -0700222void 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 Sandoval34dbad52017-03-21 08:56:08 -0700230struct 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 Lib9147dd2017-03-27 15:19:42 -0700240 stats->enable_accounting = false;
Omar Sandoval34dbad52017-03-21 08:56:08 -0700241
242 return stats;
243}
244
245void 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 Axboecf43e6b2016-11-07 21:32:37 -0700253}