xref: /openbmc/linux/block/blk-stat.c (revision 7dfdbc7367f6f789715cab2cb484b78ab45e9f3e)
1cf43e6beSJens Axboe /*
2cf43e6beSJens Axboe  * Block stat tracking code
3cf43e6beSJens Axboe  *
4cf43e6beSJens Axboe  * Copyright (C) 2016 Jens Axboe
5cf43e6beSJens Axboe  */
6cf43e6beSJens Axboe #include <linux/kernel.h>
734dbad5dSOmar Sandoval #include <linux/rculist.h>
8cf43e6beSJens Axboe #include <linux/blk-mq.h>
9cf43e6beSJens Axboe 
10cf43e6beSJens Axboe #include "blk-stat.h"
11cf43e6beSJens Axboe #include "blk-mq.h"
12b9147dd1SShaohua Li #include "blk.h"
13cf43e6beSJens Axboe 
1434dbad5dSOmar Sandoval struct blk_queue_stats {
1534dbad5dSOmar Sandoval 	struct list_head callbacks;
1634dbad5dSOmar Sandoval 	spinlock_t lock;
17b9147dd1SShaohua Li 	bool enable_accounting;
1834dbad5dSOmar Sandoval };
1934dbad5dSOmar Sandoval 
2034dbad5dSOmar Sandoval static void blk_stat_init(struct blk_rq_stat *stat)
2134dbad5dSOmar Sandoval {
2234dbad5dSOmar Sandoval 	stat->min = -1ULL;
2334dbad5dSOmar Sandoval 	stat->max = stat->nr_samples = stat->mean = 0;
24eca8b53aSShaohua Li 	stat->batch = 0;
2534dbad5dSOmar Sandoval }
2634dbad5dSOmar Sandoval 
27eca8b53aSShaohua Li /* src is a per-cpu stat, mean isn't initialized */
28cf43e6beSJens Axboe static void blk_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src)
29cf43e6beSJens Axboe {
30cf43e6beSJens Axboe 	if (!src->nr_samples)
31cf43e6beSJens Axboe 		return;
32cf43e6beSJens Axboe 
33cf43e6beSJens Axboe 	dst->min = min(dst->min, src->min);
34cf43e6beSJens Axboe 	dst->max = max(dst->max, src->max);
35cf43e6beSJens Axboe 
36eca8b53aSShaohua Li 	dst->mean = div_u64(src->batch + dst->mean * dst->nr_samples,
37cf43e6beSJens Axboe 				dst->nr_samples + src->nr_samples);
38eca8b53aSShaohua Li 
39cf43e6beSJens Axboe 	dst->nr_samples += src->nr_samples;
40cf43e6beSJens Axboe }
41cf43e6beSJens Axboe 
4234dbad5dSOmar Sandoval static void __blk_stat_add(struct blk_rq_stat *stat, u64 value)
43cf43e6beSJens Axboe {
4434dbad5dSOmar Sandoval 	stat->min = min(stat->min, value);
4534dbad5dSOmar Sandoval 	stat->max = max(stat->max, value);
46cf43e6beSJens Axboe 	stat->batch += value;
47eca8b53aSShaohua Li 	stat->nr_samples++;
48cf43e6beSJens Axboe }
49cf43e6beSJens Axboe 
5034dbad5dSOmar Sandoval void blk_stat_add(struct request *rq)
51cf43e6beSJens Axboe {
5234dbad5dSOmar Sandoval 	struct request_queue *q = rq->q;
5334dbad5dSOmar Sandoval 	struct blk_stat_callback *cb;
5434dbad5dSOmar Sandoval 	struct blk_rq_stat *stat;
5534dbad5dSOmar Sandoval 	int bucket;
56eca8b53aSShaohua Li 	u64 now, value;
57cf43e6beSJens Axboe 
5834dbad5dSOmar Sandoval 	now = __blk_stat_time(ktime_to_ns(ktime_get()));
5934dbad5dSOmar Sandoval 	if (now < blk_stat_time(&rq->issue_stat))
6034dbad5dSOmar Sandoval 		return;
6134dbad5dSOmar Sandoval 
6234dbad5dSOmar Sandoval 	value = now - blk_stat_time(&rq->issue_stat);
6334dbad5dSOmar Sandoval 
64b9147dd1SShaohua Li 	blk_throtl_stat_add(rq, value);
65b9147dd1SShaohua Li 
6634dbad5dSOmar Sandoval 	rcu_read_lock();
6734dbad5dSOmar Sandoval 	list_for_each_entry_rcu(cb, &q->stats->callbacks, list) {
68d3738123SJens Axboe 		if (!blk_stat_is_active(cb))
69d3738123SJens Axboe 			continue;
70d3738123SJens Axboe 
7134dbad5dSOmar Sandoval 		bucket = cb->bucket_fn(rq);
72a37244e4SStephen Bates 		if (bucket < 0)
73a37244e4SStephen Bates 			continue;
74d3738123SJens Axboe 
75d3738123SJens Axboe 		stat = &get_cpu_ptr(cb->cpu_stat)[bucket];
7634dbad5dSOmar Sandoval 		__blk_stat_add(stat, value);
77d3738123SJens Axboe 		put_cpu_ptr(cb->cpu_stat);
78cf43e6beSJens Axboe 	}
7934dbad5dSOmar Sandoval 	rcu_read_unlock();
8034dbad5dSOmar Sandoval }
8134dbad5dSOmar Sandoval 
82e99e88a9SKees Cook static void blk_stat_timer_fn(struct timer_list *t)
8334dbad5dSOmar Sandoval {
84e99e88a9SKees Cook 	struct blk_stat_callback *cb = from_timer(cb, t, timer);
8534dbad5dSOmar Sandoval 	unsigned int bucket;
8634dbad5dSOmar Sandoval 	int cpu;
8734dbad5dSOmar Sandoval 
8834dbad5dSOmar Sandoval 	for (bucket = 0; bucket < cb->buckets; bucket++)
8934dbad5dSOmar Sandoval 		blk_stat_init(&cb->stat[bucket]);
9034dbad5dSOmar Sandoval 
9134dbad5dSOmar Sandoval 	for_each_online_cpu(cpu) {
9234dbad5dSOmar Sandoval 		struct blk_rq_stat *cpu_stat;
9334dbad5dSOmar Sandoval 
9434dbad5dSOmar Sandoval 		cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
9534dbad5dSOmar Sandoval 		for (bucket = 0; bucket < cb->buckets; bucket++) {
9634dbad5dSOmar Sandoval 			blk_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]);
9734dbad5dSOmar Sandoval 			blk_stat_init(&cpu_stat[bucket]);
98cf43e6beSJens Axboe 		}
99cf43e6beSJens Axboe 	}
100cf43e6beSJens Axboe 
10134dbad5dSOmar Sandoval 	cb->timer_fn(cb);
102cf43e6beSJens Axboe }
103cf43e6beSJens Axboe 
10434dbad5dSOmar Sandoval struct blk_stat_callback *
10534dbad5dSOmar Sandoval blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
106a37244e4SStephen Bates 			int (*bucket_fn)(const struct request *),
10734dbad5dSOmar Sandoval 			unsigned int buckets, void *data)
108cf43e6beSJens Axboe {
10934dbad5dSOmar Sandoval 	struct blk_stat_callback *cb;
11034dbad5dSOmar Sandoval 
11134dbad5dSOmar Sandoval 	cb = kmalloc(sizeof(*cb), GFP_KERNEL);
11234dbad5dSOmar Sandoval 	if (!cb)
11334dbad5dSOmar Sandoval 		return NULL;
11434dbad5dSOmar Sandoval 
11534dbad5dSOmar Sandoval 	cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat),
11634dbad5dSOmar Sandoval 				 GFP_KERNEL);
11734dbad5dSOmar Sandoval 	if (!cb->stat) {
11834dbad5dSOmar Sandoval 		kfree(cb);
11934dbad5dSOmar Sandoval 		return NULL;
12034dbad5dSOmar Sandoval 	}
12134dbad5dSOmar Sandoval 	cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat),
12234dbad5dSOmar Sandoval 				      __alignof__(struct blk_rq_stat));
12334dbad5dSOmar Sandoval 	if (!cb->cpu_stat) {
12434dbad5dSOmar Sandoval 		kfree(cb->stat);
12534dbad5dSOmar Sandoval 		kfree(cb);
12634dbad5dSOmar Sandoval 		return NULL;
12734dbad5dSOmar Sandoval 	}
12834dbad5dSOmar Sandoval 
12934dbad5dSOmar Sandoval 	cb->timer_fn = timer_fn;
13034dbad5dSOmar Sandoval 	cb->bucket_fn = bucket_fn;
13134dbad5dSOmar Sandoval 	cb->data = data;
13234dbad5dSOmar Sandoval 	cb->buckets = buckets;
133e99e88a9SKees Cook 	timer_setup(&cb->timer, blk_stat_timer_fn, 0);
13434dbad5dSOmar Sandoval 
13534dbad5dSOmar Sandoval 	return cb;
13634dbad5dSOmar Sandoval }
13734dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_alloc_callback);
13834dbad5dSOmar Sandoval 
13934dbad5dSOmar Sandoval void blk_stat_add_callback(struct request_queue *q,
14034dbad5dSOmar Sandoval 			   struct blk_stat_callback *cb)
14134dbad5dSOmar Sandoval {
14234dbad5dSOmar Sandoval 	unsigned int bucket;
14334dbad5dSOmar Sandoval 	int cpu;
14434dbad5dSOmar Sandoval 
14534dbad5dSOmar Sandoval 	for_each_possible_cpu(cpu) {
14634dbad5dSOmar Sandoval 		struct blk_rq_stat *cpu_stat;
14734dbad5dSOmar Sandoval 
14834dbad5dSOmar Sandoval 		cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
14934dbad5dSOmar Sandoval 		for (bucket = 0; bucket < cb->buckets; bucket++)
15034dbad5dSOmar Sandoval 			blk_stat_init(&cpu_stat[bucket]);
15134dbad5dSOmar Sandoval 	}
15234dbad5dSOmar Sandoval 
15334dbad5dSOmar Sandoval 	spin_lock(&q->stats->lock);
15434dbad5dSOmar Sandoval 	list_add_tail_rcu(&cb->list, &q->stats->callbacks);
155*7dfdbc73SBart Van Assche 	blk_queue_flag_set(QUEUE_FLAG_STATS, q);
15634dbad5dSOmar Sandoval 	spin_unlock(&q->stats->lock);
15734dbad5dSOmar Sandoval }
15834dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_add_callback);
15934dbad5dSOmar Sandoval 
16034dbad5dSOmar Sandoval void blk_stat_remove_callback(struct request_queue *q,
16134dbad5dSOmar Sandoval 			      struct blk_stat_callback *cb)
16234dbad5dSOmar Sandoval {
16334dbad5dSOmar Sandoval 	spin_lock(&q->stats->lock);
16434dbad5dSOmar Sandoval 	list_del_rcu(&cb->list);
165b9147dd1SShaohua Li 	if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting)
166*7dfdbc73SBart Van Assche 		blk_queue_flag_clear(QUEUE_FLAG_STATS, q);
16734dbad5dSOmar Sandoval 	spin_unlock(&q->stats->lock);
16834dbad5dSOmar Sandoval 
16934dbad5dSOmar Sandoval 	del_timer_sync(&cb->timer);
17034dbad5dSOmar Sandoval }
17134dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_remove_callback);
17234dbad5dSOmar Sandoval 
17334dbad5dSOmar Sandoval static void blk_stat_free_callback_rcu(struct rcu_head *head)
17434dbad5dSOmar Sandoval {
17534dbad5dSOmar Sandoval 	struct blk_stat_callback *cb;
17634dbad5dSOmar Sandoval 
17734dbad5dSOmar Sandoval 	cb = container_of(head, struct blk_stat_callback, rcu);
17834dbad5dSOmar Sandoval 	free_percpu(cb->cpu_stat);
17934dbad5dSOmar Sandoval 	kfree(cb->stat);
18034dbad5dSOmar Sandoval 	kfree(cb);
181cf43e6beSJens Axboe }
182cf43e6beSJens Axboe 
18334dbad5dSOmar Sandoval void blk_stat_free_callback(struct blk_stat_callback *cb)
18434dbad5dSOmar Sandoval {
185a83b576cSJens Axboe 	if (cb)
18634dbad5dSOmar Sandoval 		call_rcu(&cb->rcu, blk_stat_free_callback_rcu);
18734dbad5dSOmar Sandoval }
18834dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_free_callback);
18934dbad5dSOmar Sandoval 
190b9147dd1SShaohua Li void blk_stat_enable_accounting(struct request_queue *q)
191b9147dd1SShaohua Li {
192b9147dd1SShaohua Li 	spin_lock(&q->stats->lock);
193b9147dd1SShaohua Li 	q->stats->enable_accounting = true;
194*7dfdbc73SBart Van Assche 	blk_queue_flag_set(QUEUE_FLAG_STATS, q);
195b9147dd1SShaohua Li 	spin_unlock(&q->stats->lock);
196b9147dd1SShaohua Li }
197b9147dd1SShaohua Li 
19834dbad5dSOmar Sandoval struct blk_queue_stats *blk_alloc_queue_stats(void)
19934dbad5dSOmar Sandoval {
20034dbad5dSOmar Sandoval 	struct blk_queue_stats *stats;
20134dbad5dSOmar Sandoval 
20234dbad5dSOmar Sandoval 	stats = kmalloc(sizeof(*stats), GFP_KERNEL);
20334dbad5dSOmar Sandoval 	if (!stats)
20434dbad5dSOmar Sandoval 		return NULL;
20534dbad5dSOmar Sandoval 
20634dbad5dSOmar Sandoval 	INIT_LIST_HEAD(&stats->callbacks);
20734dbad5dSOmar Sandoval 	spin_lock_init(&stats->lock);
208b9147dd1SShaohua Li 	stats->enable_accounting = false;
20934dbad5dSOmar Sandoval 
21034dbad5dSOmar Sandoval 	return stats;
21134dbad5dSOmar Sandoval }
21234dbad5dSOmar Sandoval 
21334dbad5dSOmar Sandoval void blk_free_queue_stats(struct blk_queue_stats *stats)
21434dbad5dSOmar Sandoval {
21534dbad5dSOmar Sandoval 	if (!stats)
21634dbad5dSOmar Sandoval 		return;
21734dbad5dSOmar Sandoval 
21834dbad5dSOmar Sandoval 	WARN_ON(!list_empty(&stats->callbacks));
21934dbad5dSOmar Sandoval 
22034dbad5dSOmar Sandoval 	kfree(stats);
221cf43e6beSJens Axboe }
222