blk-stat.c 4.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7
/*
 * Block stat tracking code
 *
 * Copyright (C) 2016 Jens Axboe
 */
#include <linux/kernel.h>
8
#include <linux/rculist.h>
9 10 11 12
#include <linux/blk-mq.h>

#include "blk-stat.h"
#include "blk-mq.h"
13
#include "blk.h"
14

15 16 17
struct blk_queue_stats {
	struct list_head callbacks;
	spinlock_t lock;
18
	bool enable_accounting;
19 20
};

21
void blk_rq_stat_init(struct blk_rq_stat *stat)
22 23 24
{
	stat->min = -1ULL;
	stat->max = stat->nr_samples = stat->mean = 0;
S
Shaohua Li 已提交
25
	stat->batch = 0;
26 27
}

S
Shaohua Li 已提交
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)
30 31 32 33 34 35 36
{
	if (!src->nr_samples)
		return;

	dst->min = min(dst->min, src->min);
	dst->max = max(dst->max, src->max);

S
Shaohua Li 已提交
37 38 39
	dst->mean = div_u64(src->batch + dst->mean * dst->nr_samples,
				dst->nr_samples + src->nr_samples);

40 41 42
	dst->nr_samples += src->nr_samples;
}

43
void blk_rq_stat_add(struct blk_rq_stat *stat, u64 value)
44
{
45 46 47
	stat->min = min(stat->min, value);
	stat->max = max(stat->max, value);
	stat->batch += value;
S
Shaohua Li 已提交
48
	stat->nr_samples++;
49 50
}

51
void blk_stat_add(struct request *rq, u64 now)
52
{
53 54 55
	struct request_queue *q = rq->q;
	struct blk_stat_callback *cb;
	struct blk_rq_stat *stat;
P
Pavel Begunkov 已提交
56
	int bucket, cpu;
57
	u64 value;
58

59
	value = (now >= rq->io_start_time_ns) ? now - rq->io_start_time_ns : 0;
60

61 62
	blk_throtl_stat_add(rq, value);

63
	rcu_read_lock();
P
Pavel Begunkov 已提交
64
	cpu = get_cpu();
65
	list_for_each_entry_rcu(cb, &q->stats->callbacks, list) {
66 67 68 69 70 71 72
		if (!blk_stat_is_active(cb))
			continue;

		bucket = cb->bucket_fn(rq);
		if (bucket < 0)
			continue;

P
Pavel Begunkov 已提交
73
		stat = &per_cpu_ptr(cb->cpu_stat, cpu)[bucket];
74
		blk_rq_stat_add(stat, value);
75
	}
P
Pavel Begunkov 已提交
76
	put_cpu();
77
	rcu_read_unlock();
78 79
}

80
static void blk_stat_timer_fn(struct timer_list *t)
81
{
82
	struct blk_stat_callback *cb = from_timer(cb, t, timer);
83 84
	unsigned int bucket;
	int cpu;
85

86
	for (bucket = 0; bucket < cb->buckets; bucket++)
87
		blk_rq_stat_init(&cb->stat[bucket]);
88

89 90
	for_each_online_cpu(cpu) {
		struct blk_rq_stat *cpu_stat;
91

92 93
		cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
		for (bucket = 0; bucket < cb->buckets; bucket++) {
94 95
			blk_rq_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]);
			blk_rq_stat_init(&cpu_stat[bucket]);
96
		}
97
	}
98

99
	cb->timer_fn(cb);
100 101
}

102 103
struct blk_stat_callback *
blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
104
			int (*bucket_fn)(const struct request *),
105
			unsigned int buckets, void *data)
106
{
107
	struct blk_stat_callback *cb;
108

109 110 111
	cb = kmalloc(sizeof(*cb), GFP_KERNEL);
	if (!cb)
		return NULL;
112

113 114 115 116 117 118 119 120 121 122 123 124 125
	cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat),
				 GFP_KERNEL);
	if (!cb->stat) {
		kfree(cb);
		return NULL;
	}
	cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat),
				      __alignof__(struct blk_rq_stat));
	if (!cb->cpu_stat) {
		kfree(cb->stat);
		kfree(cb);
		return NULL;
	}
126

127 128 129 130
	cb->timer_fn = timer_fn;
	cb->bucket_fn = bucket_fn;
	cb->data = data;
	cb->buckets = buckets;
131
	timer_setup(&cb->timer, blk_stat_timer_fn, 0);
132 133

	return cb;
134 135
}

136 137
void blk_stat_add_callback(struct request_queue *q,
			   struct blk_stat_callback *cb)
138
{
139 140
	unsigned int bucket;
	int cpu;
141

142 143
	for_each_possible_cpu(cpu) {
		struct blk_rq_stat *cpu_stat;
144

145 146
		cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
		for (bucket = 0; bucket < cb->buckets; bucket++)
147
			blk_rq_stat_init(&cpu_stat[bucket]);
148
	}
149

150 151
	spin_lock(&q->stats->lock);
	list_add_tail_rcu(&cb->list, &q->stats->callbacks);
152
	blk_queue_flag_set(QUEUE_FLAG_STATS, q);
153 154
	spin_unlock(&q->stats->lock);
}
155

156 157 158 159 160
void blk_stat_remove_callback(struct request_queue *q,
			      struct blk_stat_callback *cb)
{
	spin_lock(&q->stats->lock);
	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);
164

165
	del_timer_sync(&cb->timer);
166 167
}

168
static void blk_stat_free_callback_rcu(struct rcu_head *head)
169
{
170 171 172 173 174 175
	struct blk_stat_callback *cb;

	cb = container_of(head, struct blk_stat_callback, rcu);
	free_percpu(cb->cpu_stat);
	kfree(cb->stat);
	kfree(cb);
176 177
}

178
void blk_stat_free_callback(struct blk_stat_callback *cb)
179
{
180 181
	if (cb)
		call_rcu(&cb->rcu, blk_stat_free_callback_rcu);
182 183
}

184 185 186 187
void blk_stat_enable_accounting(struct request_queue *q)
{
	spin_lock(&q->stats->lock);
	q->stats->enable_accounting = true;
188
	blk_queue_flag_set(QUEUE_FLAG_STATS, q);
189 190
	spin_unlock(&q->stats->lock);
}
191
EXPORT_SYMBOL_GPL(blk_stat_enable_accounting);
192

193
struct blk_queue_stats *blk_alloc_queue_stats(void)
194
{
195 196 197 198 199 200 201 202
	struct blk_queue_stats *stats;

	stats = kmalloc(sizeof(*stats), GFP_KERNEL);
	if (!stats)
		return NULL;

	INIT_LIST_HEAD(&stats->callbacks);
	spin_lock_init(&stats->lock);
203
	stats->enable_accounting = false;
204 205 206 207 208 209 210 211 212 213

	return stats;
}

void blk_free_queue_stats(struct blk_queue_stats *stats)
{
	if (!stats)
		return;

	WARN_ON(!list_empty(&stats->callbacks));
214

215
	kfree(stats);
216
}