1 // SPDX-License-Identifier: GPL-2.0
3 * Block stat tracking code
5 * Copyright (C) 2016 Jens Axboe
7 #include <linux/kernel.h>
8 #include <linux/rculist.h>
9 #include <linux/blk-mq.h>
15 struct blk_queue_stats {
16 struct list_head callbacks;
18 bool enable_accounting;
21 void blk_rq_stat_init(struct blk_rq_stat *stat)
24 stat->max = stat->nr_samples = stat->mean = 0;
28 /* src is a per-cpu stat, mean isn't initialized */
29 void blk_rq_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src)
34 dst->min = min(dst->min, src->min);
35 dst->max = max(dst->max, src->max);
37 dst->mean = div_u64(src->batch + dst->mean * dst->nr_samples,
38 dst->nr_samples + src->nr_samples);
40 dst->nr_samples += src->nr_samples;
43 void blk_rq_stat_add(struct blk_rq_stat *stat, u64 value)
45 stat->min = min(stat->min, value);
46 stat->max = max(stat->max, value);
51 void blk_stat_add(struct request *rq, u64 now)
53 struct request_queue *q = rq->q;
54 struct blk_stat_callback *cb;
55 struct blk_rq_stat *stat;
59 value = (now >= rq->io_start_time_ns) ? now - rq->io_start_time_ns : 0;
61 blk_throtl_stat_add(rq, value);
65 list_for_each_entry_rcu(cb, &q->stats->callbacks, list) {
66 if (!blk_stat_is_active(cb))
69 bucket = cb->bucket_fn(rq);
73 stat = &per_cpu_ptr(cb->cpu_stat, cpu)[bucket];
74 blk_rq_stat_add(stat, value);
80 static void blk_stat_timer_fn(struct timer_list *t)
82 struct blk_stat_callback *cb = from_timer(cb, t, timer);
86 for (bucket = 0; bucket < cb->buckets; bucket++)
87 blk_rq_stat_init(&cb->stat[bucket]);
89 for_each_online_cpu(cpu) {
90 struct blk_rq_stat *cpu_stat;
92 cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
93 for (bucket = 0; bucket < cb->buckets; bucket++) {
94 blk_rq_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]);
95 blk_rq_stat_init(&cpu_stat[bucket]);
102 struct blk_stat_callback *
103 blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
104 int (*bucket_fn)(const struct request *),
105 unsigned int buckets, void *data)
107 struct blk_stat_callback *cb;
109 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
113 cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat),
119 cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat),
120 __alignof__(struct blk_rq_stat));
127 cb->timer_fn = timer_fn;
128 cb->bucket_fn = bucket_fn;
130 cb->buckets = buckets;
131 timer_setup(&cb->timer, blk_stat_timer_fn, 0);
136 void blk_stat_add_callback(struct request_queue *q,
137 struct blk_stat_callback *cb)
142 for_each_possible_cpu(cpu) {
143 struct blk_rq_stat *cpu_stat;
145 cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
146 for (bucket = 0; bucket < cb->buckets; bucket++)
147 blk_rq_stat_init(&cpu_stat[bucket]);
150 spin_lock(&q->stats->lock);
151 list_add_tail_rcu(&cb->list, &q->stats->callbacks);
152 blk_queue_flag_set(QUEUE_FLAG_STATS, q);
153 spin_unlock(&q->stats->lock);
156 void blk_stat_remove_callback(struct request_queue *q,
157 struct blk_stat_callback *cb)
159 spin_lock(&q->stats->lock);
160 list_del_rcu(&cb->list);
161 if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting)
162 blk_queue_flag_clear(QUEUE_FLAG_STATS, q);
163 spin_unlock(&q->stats->lock);
165 del_timer_sync(&cb->timer);
168 static void blk_stat_free_callback_rcu(struct rcu_head *head)
170 struct blk_stat_callback *cb;
172 cb = container_of(head, struct blk_stat_callback, rcu);
173 free_percpu(cb->cpu_stat);
178 void blk_stat_free_callback(struct blk_stat_callback *cb)
181 call_rcu(&cb->rcu, blk_stat_free_callback_rcu);
184 void blk_stat_enable_accounting(struct request_queue *q)
186 spin_lock(&q->stats->lock);
187 q->stats->enable_accounting = true;
188 blk_queue_flag_set(QUEUE_FLAG_STATS, q);
189 spin_unlock(&q->stats->lock);
191 EXPORT_SYMBOL_GPL(blk_stat_enable_accounting);
193 struct blk_queue_stats *blk_alloc_queue_stats(void)
195 struct blk_queue_stats *stats;
197 stats = kmalloc(sizeof(*stats), GFP_KERNEL);
201 INIT_LIST_HEAD(&stats->callbacks);
202 spin_lock_init(&stats->lock);
203 stats->enable_accounting = false;
208 void blk_free_queue_stats(struct blk_queue_stats *stats)
213 WARN_ON(!list_empty(&stats->callbacks));