blob: 940f15d600f8a7dc32a3bd77f650e6e53d9f66fc [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Jens Axboecf43e6b2016-11-07 21:32:37 -07002/*
3 * Block stat tracking code
4 *
5 * Copyright (C) 2016 Jens Axboe
6 */
7#include <linux/kernel.h>
Omar Sandoval34dbad52017-03-21 08:56:08 -07008#include <linux/rculist.h>
Jens Axboecf43e6b2016-11-07 21:32:37 -07009#include <linux/blk-mq.h>
10
11#include "blk-stat.h"
12#include "blk-mq.h"
Shaohua Lib9147dd2017-03-27 15:19:42 -070013#include "blk.h"
Jens Axboecf43e6b2016-11-07 21:32:37 -070014
Omar Sandoval34dbad52017-03-21 08:56:08 -070015struct blk_queue_stats {
16 struct list_head callbacks;
17 spinlock_t lock;
Shaohua Lib9147dd2017-03-27 15:19:42 -070018 bool enable_accounting;
Omar Sandoval34dbad52017-03-21 08:56:08 -070019};
20
Josef Bacik2ecbf452018-07-03 11:14:57 -040021void blk_rq_stat_init(struct blk_rq_stat *stat)
Omar Sandoval34dbad52017-03-21 08:56:08 -070022{
23 stat->min = -1ULL;
24 stat->max = stat->nr_samples = stat->mean = 0;
Shaohua Lieca8b532017-10-06 17:55:59 -070025 stat->batch = 0;
Omar Sandoval34dbad52017-03-21 08:56:08 -070026}
27
Shaohua Lieca8b532017-10-06 17:55:59 -070028/* src is a per-cpu stat, mean isn't initialized */
Josef Bacik2ecbf452018-07-03 11:14:57 -040029void blk_rq_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src)
Jens Axboecf43e6b2016-11-07 21:32:37 -070030{
31 if (!src->nr_samples)
32 return;
33
Jens Axboecf43e6b2016-11-07 21:32:37 -070034 dst->min = min(dst->min, src->min);
35 dst->max = max(dst->max, src->max);
36
Shaohua Lieca8b532017-10-06 17:55:59 -070037 dst->mean = div_u64(src->batch + dst->mean * dst->nr_samples,
38 dst->nr_samples + src->nr_samples);
39
Jens Axboecf43e6b2016-11-07 21:32:37 -070040 dst->nr_samples += src->nr_samples;
41}
42
Josef Bacik2ecbf452018-07-03 11:14:57 -040043void blk_rq_stat_add(struct blk_rq_stat *stat, u64 value)
Jens Axboecf43e6b2016-11-07 21:32:37 -070044{
Omar Sandoval34dbad52017-03-21 08:56:08 -070045 stat->min = min(stat->min, value);
46 stat->max = max(stat->max, value);
Jens Axboecf43e6b2016-11-07 21:32:37 -070047 stat->batch += value;
Shaohua Lieca8b532017-10-06 17:55:59 -070048 stat->nr_samples++;
Jens Axboecf43e6b2016-11-07 21:32:37 -070049}
50
Omar Sandoval522a7772018-05-09 02:08:53 -070051void blk_stat_add(struct request *rq, u64 now)
Jens Axboecf43e6b2016-11-07 21:32:37 -070052{
Omar Sandoval34dbad52017-03-21 08:56:08 -070053 struct request_queue *q = rq->q;
54 struct blk_stat_callback *cb;
55 struct blk_rq_stat *stat;
56 int bucket;
Omar Sandoval522a7772018-05-09 02:08:53 -070057 u64 value;
Jens Axboecf43e6b2016-11-07 21:32:37 -070058
Omar Sandoval544ccc8d2018-05-09 02:08:50 -070059 value = (now >= rq->io_start_time_ns) ? now - rq->io_start_time_ns : 0;
Omar Sandoval34dbad52017-03-21 08:56:08 -070060
Shaohua Lib9147dd2017-03-27 15:19:42 -070061 blk_throtl_stat_add(rq, value);
62
Omar Sandoval34dbad52017-03-21 08:56:08 -070063 rcu_read_lock();
64 list_for_each_entry_rcu(cb, &q->stats->callbacks, list) {
Jens Axboed3738122017-05-09 11:39:56 -060065 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];
Josef Bacik2ecbf452018-07-03 11:14:57 -040073 blk_rq_stat_add(stat, value);
Jens Axboed3738122017-05-09 11:39:56 -060074 put_cpu_ptr(cb->cpu_stat);
Jens Axboecf43e6b2016-11-07 21:32:37 -070075 }
Omar Sandoval34dbad52017-03-21 08:56:08 -070076 rcu_read_unlock();
Jens Axboecf43e6b2016-11-07 21:32:37 -070077}
78
Kees Cooke99e88a2017-10-16 14:43:17 -070079static void blk_stat_timer_fn(struct timer_list *t)
Jens Axboecf43e6b2016-11-07 21:32:37 -070080{
Kees Cooke99e88a2017-10-16 14:43:17 -070081 struct blk_stat_callback *cb = from_timer(cb, t, timer);
Omar Sandoval34dbad52017-03-21 08:56:08 -070082 unsigned int bucket;
83 int cpu;
Jens Axboecf43e6b2016-11-07 21:32:37 -070084
Omar Sandoval34dbad52017-03-21 08:56:08 -070085 for (bucket = 0; bucket < cb->buckets; bucket++)
Josef Bacik2ecbf452018-07-03 11:14:57 -040086 blk_rq_stat_init(&cb->stat[bucket]);
Omar Sandoval34dbad52017-03-21 08:56:08 -070087
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++) {
Josef Bacik2ecbf452018-07-03 11:14:57 -040093 blk_rq_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]);
94 blk_rq_stat_init(&cpu_stat[bucket]);
Omar Sandoval34dbad52017-03-21 08:56:08 -070095 }
Jens Axboecf43e6b2016-11-07 21:32:37 -070096 }
97
Omar Sandoval34dbad52017-03-21 08:56:08 -070098 cb->timer_fn(cb);
99}
100
101struct blk_stat_callback *
102blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
Stephen Batesa37244e2017-04-20 15:29:16 -0600103 int (*bucket_fn)(const struct request *),
Omar Sandoval34dbad52017-03-21 08:56:08 -0700104 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 Cooke99e88a2017-10-16 14:43:17 -0700130 timer_setup(&cb->timer, blk_stat_timer_fn, 0);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700131
132 return cb;
133}
Omar Sandoval34dbad52017-03-21 08:56:08 -0700134
135void blk_stat_add_callback(struct request_queue *q,
136 struct blk_stat_callback *cb)
137{
138 unsigned int bucket;
139 int cpu;
140
141 for_each_possible_cpu(cpu) {
142 struct blk_rq_stat *cpu_stat;
143
144 cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
145 for (bucket = 0; bucket < cb->buckets; bucket++)
Josef Bacik2ecbf452018-07-03 11:14:57 -0400146 blk_rq_stat_init(&cpu_stat[bucket]);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700147 }
148
149 spin_lock(&q->stats->lock);
150 list_add_tail_rcu(&cb->list, &q->stats->callbacks);
Bart Van Assche7dfdbc72018-03-07 17:10:05 -0800151 blk_queue_flag_set(QUEUE_FLAG_STATS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700152 spin_unlock(&q->stats->lock);
153}
Omar Sandoval34dbad52017-03-21 08:56:08 -0700154
155void blk_stat_remove_callback(struct request_queue *q,
156 struct blk_stat_callback *cb)
157{
158 spin_lock(&q->stats->lock);
159 list_del_rcu(&cb->list);
Shaohua Lib9147dd2017-03-27 15:19:42 -0700160 if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting)
Bart Van Assche7dfdbc72018-03-07 17:10:05 -0800161 blk_queue_flag_clear(QUEUE_FLAG_STATS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700162 spin_unlock(&q->stats->lock);
163
164 del_timer_sync(&cb->timer);
165}
Omar Sandoval34dbad52017-03-21 08:56:08 -0700166
167static void blk_stat_free_callback_rcu(struct rcu_head *head)
168{
169 struct blk_stat_callback *cb;
170
171 cb = container_of(head, struct blk_stat_callback, rcu);
172 free_percpu(cb->cpu_stat);
173 kfree(cb->stat);
174 kfree(cb);
175}
176
177void blk_stat_free_callback(struct blk_stat_callback *cb)
178{
Jens Axboea83b5762017-03-21 17:20:01 -0600179 if (cb)
180 call_rcu(&cb->rcu, blk_stat_free_callback_rcu);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700181}
Omar Sandoval34dbad52017-03-21 08:56:08 -0700182
Shaohua Lib9147dd2017-03-27 15:19:42 -0700183void blk_stat_enable_accounting(struct request_queue *q)
184{
185 spin_lock(&q->stats->lock);
186 q->stats->enable_accounting = true;
Bart Van Assche7dfdbc72018-03-07 17:10:05 -0800187 blk_queue_flag_set(QUEUE_FLAG_STATS, q);
Shaohua Lib9147dd2017-03-27 15:19:42 -0700188 spin_unlock(&q->stats->lock);
189}
Omar Sandovalf8232f22018-09-27 15:55:52 -0700190EXPORT_SYMBOL_GPL(blk_stat_enable_accounting);
Shaohua Lib9147dd2017-03-27 15:19:42 -0700191
Omar Sandoval34dbad52017-03-21 08:56:08 -0700192struct blk_queue_stats *blk_alloc_queue_stats(void)
193{
194 struct blk_queue_stats *stats;
195
196 stats = kmalloc(sizeof(*stats), GFP_KERNEL);
197 if (!stats)
198 return NULL;
199
200 INIT_LIST_HEAD(&stats->callbacks);
201 spin_lock_init(&stats->lock);
Shaohua Lib9147dd2017-03-27 15:19:42 -0700202 stats->enable_accounting = false;
Omar Sandoval34dbad52017-03-21 08:56:08 -0700203
204 return stats;
205}
206
207void blk_free_queue_stats(struct blk_queue_stats *stats)
208{
209 if (!stats)
210 return;
211
212 WARN_ON(!list_empty(&stats->callbacks));
213
214 kfree(stats);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700215}