xref: /openbmc/linux/block/blk-stat.c (revision b9147dd1bae2b15d6931ecd42f8606c775fecbc9)
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"
12*b9147dd1SShaohua Li #include "blk.h"
13cf43e6beSJens Axboe 
144875253fSOmar Sandoval #define BLK_RQ_STAT_BATCH	64
154875253fSOmar Sandoval 
1634dbad5dSOmar Sandoval struct blk_queue_stats {
1734dbad5dSOmar Sandoval 	struct list_head callbacks;
1834dbad5dSOmar Sandoval 	spinlock_t lock;
19*b9147dd1SShaohua Li 	bool enable_accounting;
2034dbad5dSOmar Sandoval };
2134dbad5dSOmar Sandoval 
2234dbad5dSOmar Sandoval unsigned int blk_stat_rq_ddir(const struct request *rq)
2334dbad5dSOmar Sandoval {
2434dbad5dSOmar Sandoval 	return rq_data_dir(rq);
2534dbad5dSOmar Sandoval }
2634dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_rq_ddir);
2734dbad5dSOmar Sandoval 
2834dbad5dSOmar Sandoval static void blk_stat_init(struct blk_rq_stat *stat)
2934dbad5dSOmar Sandoval {
3034dbad5dSOmar Sandoval 	stat->min = -1ULL;
3134dbad5dSOmar Sandoval 	stat->max = stat->nr_samples = stat->mean = 0;
3234dbad5dSOmar Sandoval 	stat->batch = stat->nr_batch = 0;
3334dbad5dSOmar Sandoval }
3434dbad5dSOmar Sandoval 
35cf43e6beSJens Axboe static void blk_stat_flush_batch(struct blk_rq_stat *stat)
36cf43e6beSJens Axboe {
37cf43e6beSJens Axboe 	const s32 nr_batch = READ_ONCE(stat->nr_batch);
38209200efSShaohua Li 	const s32 nr_samples = READ_ONCE(stat->nr_samples);
39cf43e6beSJens Axboe 
40cf43e6beSJens Axboe 	if (!nr_batch)
41cf43e6beSJens Axboe 		return;
42cf43e6beSJens Axboe 	if (!nr_samples)
43cf43e6beSJens Axboe 		stat->mean = div64_s64(stat->batch, nr_batch);
44cf43e6beSJens Axboe 	else {
45cf43e6beSJens Axboe 		stat->mean = div64_s64((stat->mean * nr_samples) +
46cf43e6beSJens Axboe 					stat->batch,
47cf43e6beSJens Axboe 					nr_batch + nr_samples);
48cf43e6beSJens Axboe 	}
49cf43e6beSJens Axboe 
50cf43e6beSJens Axboe 	stat->nr_samples += nr_batch;
51cf43e6beSJens Axboe 	stat->nr_batch = stat->batch = 0;
52cf43e6beSJens Axboe }
53cf43e6beSJens Axboe 
54cf43e6beSJens Axboe static void blk_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src)
55cf43e6beSJens Axboe {
567d8d0014SOmar Sandoval 	blk_stat_flush_batch(src);
577d8d0014SOmar Sandoval 
58cf43e6beSJens Axboe 	if (!src->nr_samples)
59cf43e6beSJens Axboe 		return;
60cf43e6beSJens Axboe 
61cf43e6beSJens Axboe 	dst->min = min(dst->min, src->min);
62cf43e6beSJens Axboe 	dst->max = max(dst->max, src->max);
63cf43e6beSJens Axboe 
64cf43e6beSJens Axboe 	if (!dst->nr_samples)
65cf43e6beSJens Axboe 		dst->mean = src->mean;
66cf43e6beSJens Axboe 	else {
67cf43e6beSJens Axboe 		dst->mean = div64_s64((src->mean * src->nr_samples) +
68cf43e6beSJens Axboe 					(dst->mean * dst->nr_samples),
69cf43e6beSJens Axboe 					dst->nr_samples + src->nr_samples);
70cf43e6beSJens Axboe 	}
71cf43e6beSJens Axboe 	dst->nr_samples += src->nr_samples;
72cf43e6beSJens Axboe }
73cf43e6beSJens Axboe 
7434dbad5dSOmar Sandoval static void __blk_stat_add(struct blk_rq_stat *stat, u64 value)
75cf43e6beSJens Axboe {
7634dbad5dSOmar Sandoval 	stat->min = min(stat->min, value);
7734dbad5dSOmar Sandoval 	stat->max = max(stat->max, value);
78cf43e6beSJens Axboe 
79cf43e6beSJens Axboe 	if (stat->batch + value < stat->batch ||
80cf43e6beSJens Axboe 	    stat->nr_batch + 1 == BLK_RQ_STAT_BATCH)
81cf43e6beSJens Axboe 		blk_stat_flush_batch(stat);
82cf43e6beSJens Axboe 
83cf43e6beSJens Axboe 	stat->batch += value;
84cf43e6beSJens Axboe 	stat->nr_batch++;
85cf43e6beSJens Axboe }
86cf43e6beSJens Axboe 
8734dbad5dSOmar Sandoval void blk_stat_add(struct request *rq)
88cf43e6beSJens Axboe {
8934dbad5dSOmar Sandoval 	struct request_queue *q = rq->q;
9034dbad5dSOmar Sandoval 	struct blk_stat_callback *cb;
9134dbad5dSOmar Sandoval 	struct blk_rq_stat *stat;
9234dbad5dSOmar Sandoval 	int bucket;
9334dbad5dSOmar Sandoval 	s64 now, value;
94cf43e6beSJens Axboe 
9534dbad5dSOmar Sandoval 	now = __blk_stat_time(ktime_to_ns(ktime_get()));
9634dbad5dSOmar Sandoval 	if (now < blk_stat_time(&rq->issue_stat))
9734dbad5dSOmar Sandoval 		return;
9834dbad5dSOmar Sandoval 
9934dbad5dSOmar Sandoval 	value = now - blk_stat_time(&rq->issue_stat);
10034dbad5dSOmar Sandoval 
101*b9147dd1SShaohua Li 	blk_throtl_stat_add(rq, value);
102*b9147dd1SShaohua Li 
10334dbad5dSOmar Sandoval 	rcu_read_lock();
10434dbad5dSOmar Sandoval 	list_for_each_entry_rcu(cb, &q->stats->callbacks, list) {
10534dbad5dSOmar Sandoval 		if (blk_stat_is_active(cb)) {
10634dbad5dSOmar Sandoval 			bucket = cb->bucket_fn(rq);
10734dbad5dSOmar Sandoval 			stat = &this_cpu_ptr(cb->cpu_stat)[bucket];
10834dbad5dSOmar Sandoval 			__blk_stat_add(stat, value);
109cf43e6beSJens Axboe 		}
110cf43e6beSJens Axboe 	}
11134dbad5dSOmar Sandoval 	rcu_read_unlock();
11234dbad5dSOmar Sandoval }
11334dbad5dSOmar Sandoval 
11434dbad5dSOmar Sandoval static void blk_stat_timer_fn(unsigned long data)
11534dbad5dSOmar Sandoval {
11634dbad5dSOmar Sandoval 	struct blk_stat_callback *cb = (void *)data;
11734dbad5dSOmar Sandoval 	unsigned int bucket;
11834dbad5dSOmar Sandoval 	int cpu;
11934dbad5dSOmar Sandoval 
12034dbad5dSOmar Sandoval 	for (bucket = 0; bucket < cb->buckets; bucket++)
12134dbad5dSOmar Sandoval 		blk_stat_init(&cb->stat[bucket]);
12234dbad5dSOmar Sandoval 
12334dbad5dSOmar Sandoval 	for_each_online_cpu(cpu) {
12434dbad5dSOmar Sandoval 		struct blk_rq_stat *cpu_stat;
12534dbad5dSOmar Sandoval 
12634dbad5dSOmar Sandoval 		cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
12734dbad5dSOmar Sandoval 		for (bucket = 0; bucket < cb->buckets; bucket++) {
12834dbad5dSOmar Sandoval 			blk_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]);
12934dbad5dSOmar Sandoval 			blk_stat_init(&cpu_stat[bucket]);
130cf43e6beSJens Axboe 		}
131cf43e6beSJens Axboe 	}
132cf43e6beSJens Axboe 
13334dbad5dSOmar Sandoval 	cb->timer_fn(cb);
134cf43e6beSJens Axboe }
135cf43e6beSJens Axboe 
13634dbad5dSOmar Sandoval struct blk_stat_callback *
13734dbad5dSOmar Sandoval blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
13834dbad5dSOmar Sandoval 			unsigned int (*bucket_fn)(const struct request *),
13934dbad5dSOmar Sandoval 			unsigned int buckets, void *data)
140cf43e6beSJens Axboe {
14134dbad5dSOmar Sandoval 	struct blk_stat_callback *cb;
14234dbad5dSOmar Sandoval 
14334dbad5dSOmar Sandoval 	cb = kmalloc(sizeof(*cb), GFP_KERNEL);
14434dbad5dSOmar Sandoval 	if (!cb)
14534dbad5dSOmar Sandoval 		return NULL;
14634dbad5dSOmar Sandoval 
14734dbad5dSOmar Sandoval 	cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat),
14834dbad5dSOmar Sandoval 				 GFP_KERNEL);
14934dbad5dSOmar Sandoval 	if (!cb->stat) {
15034dbad5dSOmar Sandoval 		kfree(cb);
15134dbad5dSOmar Sandoval 		return NULL;
15234dbad5dSOmar Sandoval 	}
15334dbad5dSOmar Sandoval 	cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat),
15434dbad5dSOmar Sandoval 				      __alignof__(struct blk_rq_stat));
15534dbad5dSOmar Sandoval 	if (!cb->cpu_stat) {
15634dbad5dSOmar Sandoval 		kfree(cb->stat);
15734dbad5dSOmar Sandoval 		kfree(cb);
15834dbad5dSOmar Sandoval 		return NULL;
15934dbad5dSOmar Sandoval 	}
16034dbad5dSOmar Sandoval 
16134dbad5dSOmar Sandoval 	cb->timer_fn = timer_fn;
16234dbad5dSOmar Sandoval 	cb->bucket_fn = bucket_fn;
16334dbad5dSOmar Sandoval 	cb->data = data;
16434dbad5dSOmar Sandoval 	cb->buckets = buckets;
16534dbad5dSOmar Sandoval 	setup_timer(&cb->timer, blk_stat_timer_fn, (unsigned long)cb);
16634dbad5dSOmar Sandoval 
16734dbad5dSOmar Sandoval 	return cb;
16834dbad5dSOmar Sandoval }
16934dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_alloc_callback);
17034dbad5dSOmar Sandoval 
17134dbad5dSOmar Sandoval void blk_stat_add_callback(struct request_queue *q,
17234dbad5dSOmar Sandoval 			   struct blk_stat_callback *cb)
17334dbad5dSOmar Sandoval {
17434dbad5dSOmar Sandoval 	unsigned int bucket;
17534dbad5dSOmar Sandoval 	int cpu;
17634dbad5dSOmar Sandoval 
17734dbad5dSOmar Sandoval 	for_each_possible_cpu(cpu) {
17834dbad5dSOmar Sandoval 		struct blk_rq_stat *cpu_stat;
17934dbad5dSOmar Sandoval 
18034dbad5dSOmar Sandoval 		cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
18134dbad5dSOmar Sandoval 		for (bucket = 0; bucket < cb->buckets; bucket++)
18234dbad5dSOmar Sandoval 			blk_stat_init(&cpu_stat[bucket]);
18334dbad5dSOmar Sandoval 	}
18434dbad5dSOmar Sandoval 
18534dbad5dSOmar Sandoval 	spin_lock(&q->stats->lock);
18634dbad5dSOmar Sandoval 	list_add_tail_rcu(&cb->list, &q->stats->callbacks);
187cf43e6beSJens Axboe 	set_bit(QUEUE_FLAG_STATS, &q->queue_flags);
18834dbad5dSOmar Sandoval 	spin_unlock(&q->stats->lock);
18934dbad5dSOmar Sandoval }
19034dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_add_callback);
19134dbad5dSOmar Sandoval 
19234dbad5dSOmar Sandoval void blk_stat_remove_callback(struct request_queue *q,
19334dbad5dSOmar Sandoval 			      struct blk_stat_callback *cb)
19434dbad5dSOmar Sandoval {
19534dbad5dSOmar Sandoval 	spin_lock(&q->stats->lock);
19634dbad5dSOmar Sandoval 	list_del_rcu(&cb->list);
197*b9147dd1SShaohua Li 	if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting)
19834dbad5dSOmar Sandoval 		clear_bit(QUEUE_FLAG_STATS, &q->queue_flags);
19934dbad5dSOmar Sandoval 	spin_unlock(&q->stats->lock);
20034dbad5dSOmar Sandoval 
20134dbad5dSOmar Sandoval 	del_timer_sync(&cb->timer);
20234dbad5dSOmar Sandoval }
20334dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_remove_callback);
20434dbad5dSOmar Sandoval 
20534dbad5dSOmar Sandoval static void blk_stat_free_callback_rcu(struct rcu_head *head)
20634dbad5dSOmar Sandoval {
20734dbad5dSOmar Sandoval 	struct blk_stat_callback *cb;
20834dbad5dSOmar Sandoval 
20934dbad5dSOmar Sandoval 	cb = container_of(head, struct blk_stat_callback, rcu);
21034dbad5dSOmar Sandoval 	free_percpu(cb->cpu_stat);
21134dbad5dSOmar Sandoval 	kfree(cb->stat);
21234dbad5dSOmar Sandoval 	kfree(cb);
213cf43e6beSJens Axboe }
214cf43e6beSJens Axboe 
21534dbad5dSOmar Sandoval void blk_stat_free_callback(struct blk_stat_callback *cb)
21634dbad5dSOmar Sandoval {
217a83b576cSJens Axboe 	if (cb)
21834dbad5dSOmar Sandoval 		call_rcu(&cb->rcu, blk_stat_free_callback_rcu);
21934dbad5dSOmar Sandoval }
22034dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_free_callback);
22134dbad5dSOmar Sandoval 
222*b9147dd1SShaohua Li void blk_stat_enable_accounting(struct request_queue *q)
223*b9147dd1SShaohua Li {
224*b9147dd1SShaohua Li 	spin_lock(&q->stats->lock);
225*b9147dd1SShaohua Li 	q->stats->enable_accounting = true;
226*b9147dd1SShaohua Li 	set_bit(QUEUE_FLAG_STATS, &q->queue_flags);
227*b9147dd1SShaohua Li 	spin_unlock(&q->stats->lock);
228*b9147dd1SShaohua Li }
229*b9147dd1SShaohua Li 
23034dbad5dSOmar Sandoval struct blk_queue_stats *blk_alloc_queue_stats(void)
23134dbad5dSOmar Sandoval {
23234dbad5dSOmar Sandoval 	struct blk_queue_stats *stats;
23334dbad5dSOmar Sandoval 
23434dbad5dSOmar Sandoval 	stats = kmalloc(sizeof(*stats), GFP_KERNEL);
23534dbad5dSOmar Sandoval 	if (!stats)
23634dbad5dSOmar Sandoval 		return NULL;
23734dbad5dSOmar Sandoval 
23834dbad5dSOmar Sandoval 	INIT_LIST_HEAD(&stats->callbacks);
23934dbad5dSOmar Sandoval 	spin_lock_init(&stats->lock);
240*b9147dd1SShaohua Li 	stats->enable_accounting = false;
24134dbad5dSOmar Sandoval 
24234dbad5dSOmar Sandoval 	return stats;
24334dbad5dSOmar Sandoval }
24434dbad5dSOmar Sandoval 
24534dbad5dSOmar Sandoval void blk_free_queue_stats(struct blk_queue_stats *stats)
24634dbad5dSOmar Sandoval {
24734dbad5dSOmar Sandoval 	if (!stats)
24834dbad5dSOmar Sandoval 		return;
24934dbad5dSOmar Sandoval 
25034dbad5dSOmar Sandoval 	WARN_ON(!list_empty(&stats->callbacks));
25134dbad5dSOmar Sandoval 
25234dbad5dSOmar Sandoval 	kfree(stats);
253cf43e6beSJens Axboe }
254