xref: /openbmc/linux/block/blk-stat.c (revision 544ccc8dc904db55d4576c27a1eb66a888ffacea)
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 
58*544ccc8dSOmar Sandoval 	now = ktime_get_ns();
59*544ccc8dSOmar Sandoval 	value = (now >= rq->io_start_time_ns) ? now - rq->io_start_time_ns : 0;
6034dbad5dSOmar Sandoval 
61b9147dd1SShaohua Li 	blk_throtl_stat_add(rq, value);
62b9147dd1SShaohua Li 
6334dbad5dSOmar Sandoval 	rcu_read_lock();
6434dbad5dSOmar Sandoval 	list_for_each_entry_rcu(cb, &q->stats->callbacks, list) {
65d3738123SJens Axboe 		if (!blk_stat_is_active(cb))
66d3738123SJens Axboe 			continue;
67d3738123SJens Axboe 
6834dbad5dSOmar Sandoval 		bucket = cb->bucket_fn(rq);
69a37244e4SStephen Bates 		if (bucket < 0)
70a37244e4SStephen Bates 			continue;
71d3738123SJens Axboe 
72d3738123SJens Axboe 		stat = &get_cpu_ptr(cb->cpu_stat)[bucket];
7334dbad5dSOmar Sandoval 		__blk_stat_add(stat, value);
74d3738123SJens Axboe 		put_cpu_ptr(cb->cpu_stat);
75cf43e6beSJens Axboe 	}
7634dbad5dSOmar Sandoval 	rcu_read_unlock();
7734dbad5dSOmar Sandoval }
7834dbad5dSOmar Sandoval 
79e99e88a9SKees Cook static void blk_stat_timer_fn(struct timer_list *t)
8034dbad5dSOmar Sandoval {
81e99e88a9SKees Cook 	struct blk_stat_callback *cb = from_timer(cb, t, timer);
8234dbad5dSOmar Sandoval 	unsigned int bucket;
8334dbad5dSOmar Sandoval 	int cpu;
8434dbad5dSOmar Sandoval 
8534dbad5dSOmar Sandoval 	for (bucket = 0; bucket < cb->buckets; bucket++)
8634dbad5dSOmar Sandoval 		blk_stat_init(&cb->stat[bucket]);
8734dbad5dSOmar Sandoval 
8834dbad5dSOmar Sandoval 	for_each_online_cpu(cpu) {
8934dbad5dSOmar Sandoval 		struct blk_rq_stat *cpu_stat;
9034dbad5dSOmar Sandoval 
9134dbad5dSOmar Sandoval 		cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
9234dbad5dSOmar Sandoval 		for (bucket = 0; bucket < cb->buckets; bucket++) {
9334dbad5dSOmar Sandoval 			blk_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]);
9434dbad5dSOmar Sandoval 			blk_stat_init(&cpu_stat[bucket]);
95cf43e6beSJens Axboe 		}
96cf43e6beSJens Axboe 	}
97cf43e6beSJens Axboe 
9834dbad5dSOmar Sandoval 	cb->timer_fn(cb);
99cf43e6beSJens Axboe }
100cf43e6beSJens Axboe 
10134dbad5dSOmar Sandoval struct blk_stat_callback *
10234dbad5dSOmar Sandoval blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
103a37244e4SStephen Bates 			int (*bucket_fn)(const struct request *),
10434dbad5dSOmar Sandoval 			unsigned int buckets, void *data)
105cf43e6beSJens Axboe {
10634dbad5dSOmar Sandoval 	struct blk_stat_callback *cb;
10734dbad5dSOmar Sandoval 
10834dbad5dSOmar Sandoval 	cb = kmalloc(sizeof(*cb), GFP_KERNEL);
10934dbad5dSOmar Sandoval 	if (!cb)
11034dbad5dSOmar Sandoval 		return NULL;
11134dbad5dSOmar Sandoval 
11234dbad5dSOmar Sandoval 	cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat),
11334dbad5dSOmar Sandoval 				 GFP_KERNEL);
11434dbad5dSOmar Sandoval 	if (!cb->stat) {
11534dbad5dSOmar Sandoval 		kfree(cb);
11634dbad5dSOmar Sandoval 		return NULL;
11734dbad5dSOmar Sandoval 	}
11834dbad5dSOmar Sandoval 	cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat),
11934dbad5dSOmar Sandoval 				      __alignof__(struct blk_rq_stat));
12034dbad5dSOmar Sandoval 	if (!cb->cpu_stat) {
12134dbad5dSOmar Sandoval 		kfree(cb->stat);
12234dbad5dSOmar Sandoval 		kfree(cb);
12334dbad5dSOmar Sandoval 		return NULL;
12434dbad5dSOmar Sandoval 	}
12534dbad5dSOmar Sandoval 
12634dbad5dSOmar Sandoval 	cb->timer_fn = timer_fn;
12734dbad5dSOmar Sandoval 	cb->bucket_fn = bucket_fn;
12834dbad5dSOmar Sandoval 	cb->data = data;
12934dbad5dSOmar Sandoval 	cb->buckets = buckets;
130e99e88a9SKees Cook 	timer_setup(&cb->timer, blk_stat_timer_fn, 0);
13134dbad5dSOmar Sandoval 
13234dbad5dSOmar Sandoval 	return cb;
13334dbad5dSOmar Sandoval }
13434dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_alloc_callback);
13534dbad5dSOmar Sandoval 
13634dbad5dSOmar Sandoval void blk_stat_add_callback(struct request_queue *q,
13734dbad5dSOmar Sandoval 			   struct blk_stat_callback *cb)
13834dbad5dSOmar Sandoval {
13934dbad5dSOmar Sandoval 	unsigned int bucket;
14034dbad5dSOmar Sandoval 	int cpu;
14134dbad5dSOmar Sandoval 
14234dbad5dSOmar Sandoval 	for_each_possible_cpu(cpu) {
14334dbad5dSOmar Sandoval 		struct blk_rq_stat *cpu_stat;
14434dbad5dSOmar Sandoval 
14534dbad5dSOmar Sandoval 		cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
14634dbad5dSOmar Sandoval 		for (bucket = 0; bucket < cb->buckets; bucket++)
14734dbad5dSOmar Sandoval 			blk_stat_init(&cpu_stat[bucket]);
14834dbad5dSOmar Sandoval 	}
14934dbad5dSOmar Sandoval 
15034dbad5dSOmar Sandoval 	spin_lock(&q->stats->lock);
15134dbad5dSOmar Sandoval 	list_add_tail_rcu(&cb->list, &q->stats->callbacks);
1527dfdbc73SBart Van Assche 	blk_queue_flag_set(QUEUE_FLAG_STATS, q);
15334dbad5dSOmar Sandoval 	spin_unlock(&q->stats->lock);
15434dbad5dSOmar Sandoval }
15534dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_add_callback);
15634dbad5dSOmar Sandoval 
15734dbad5dSOmar Sandoval void blk_stat_remove_callback(struct request_queue *q,
15834dbad5dSOmar Sandoval 			      struct blk_stat_callback *cb)
15934dbad5dSOmar Sandoval {
16034dbad5dSOmar Sandoval 	spin_lock(&q->stats->lock);
16134dbad5dSOmar Sandoval 	list_del_rcu(&cb->list);
162b9147dd1SShaohua Li 	if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting)
1637dfdbc73SBart Van Assche 		blk_queue_flag_clear(QUEUE_FLAG_STATS, q);
16434dbad5dSOmar Sandoval 	spin_unlock(&q->stats->lock);
16534dbad5dSOmar Sandoval 
16634dbad5dSOmar Sandoval 	del_timer_sync(&cb->timer);
16734dbad5dSOmar Sandoval }
16834dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_remove_callback);
16934dbad5dSOmar Sandoval 
17034dbad5dSOmar Sandoval static void blk_stat_free_callback_rcu(struct rcu_head *head)
17134dbad5dSOmar Sandoval {
17234dbad5dSOmar Sandoval 	struct blk_stat_callback *cb;
17334dbad5dSOmar Sandoval 
17434dbad5dSOmar Sandoval 	cb = container_of(head, struct blk_stat_callback, rcu);
17534dbad5dSOmar Sandoval 	free_percpu(cb->cpu_stat);
17634dbad5dSOmar Sandoval 	kfree(cb->stat);
17734dbad5dSOmar Sandoval 	kfree(cb);
178cf43e6beSJens Axboe }
179cf43e6beSJens Axboe 
18034dbad5dSOmar Sandoval void blk_stat_free_callback(struct blk_stat_callback *cb)
18134dbad5dSOmar Sandoval {
182a83b576cSJens Axboe 	if (cb)
18334dbad5dSOmar Sandoval 		call_rcu(&cb->rcu, blk_stat_free_callback_rcu);
18434dbad5dSOmar Sandoval }
18534dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_free_callback);
18634dbad5dSOmar Sandoval 
187b9147dd1SShaohua Li void blk_stat_enable_accounting(struct request_queue *q)
188b9147dd1SShaohua Li {
189b9147dd1SShaohua Li 	spin_lock(&q->stats->lock);
190b9147dd1SShaohua Li 	q->stats->enable_accounting = true;
1917dfdbc73SBart Van Assche 	blk_queue_flag_set(QUEUE_FLAG_STATS, q);
192b9147dd1SShaohua Li 	spin_unlock(&q->stats->lock);
193b9147dd1SShaohua Li }
194b9147dd1SShaohua Li 
19534dbad5dSOmar Sandoval struct blk_queue_stats *blk_alloc_queue_stats(void)
19634dbad5dSOmar Sandoval {
19734dbad5dSOmar Sandoval 	struct blk_queue_stats *stats;
19834dbad5dSOmar Sandoval 
19934dbad5dSOmar Sandoval 	stats = kmalloc(sizeof(*stats), GFP_KERNEL);
20034dbad5dSOmar Sandoval 	if (!stats)
20134dbad5dSOmar Sandoval 		return NULL;
20234dbad5dSOmar Sandoval 
20334dbad5dSOmar Sandoval 	INIT_LIST_HEAD(&stats->callbacks);
20434dbad5dSOmar Sandoval 	spin_lock_init(&stats->lock);
205b9147dd1SShaohua Li 	stats->enable_accounting = false;
20634dbad5dSOmar Sandoval 
20734dbad5dSOmar Sandoval 	return stats;
20834dbad5dSOmar Sandoval }
20934dbad5dSOmar Sandoval 
21034dbad5dSOmar Sandoval void blk_free_queue_stats(struct blk_queue_stats *stats)
21134dbad5dSOmar Sandoval {
21234dbad5dSOmar Sandoval 	if (!stats)
21334dbad5dSOmar Sandoval 		return;
21434dbad5dSOmar Sandoval 
21534dbad5dSOmar Sandoval 	WARN_ON(!list_empty(&stats->callbacks));
21634dbad5dSOmar Sandoval 
21734dbad5dSOmar Sandoval 	kfree(stats);
218cf43e6beSJens Axboe }
219