]>
Commit | Line | Data |
---|---|---|
cf43e6be JA |
1 | /* |
2 | * Block stat tracking code | |
3 | * | |
4 | * Copyright (C) 2016 Jens Axboe | |
5 | */ | |
6 | #include <linux/kernel.h> | |
34dbad5d | 7 | #include <linux/rculist.h> |
cf43e6be JA |
8 | #include <linux/blk-mq.h> |
9 | ||
10 | #include "blk-stat.h" | |
11 | #include "blk-mq.h" | |
b9147dd1 | 12 | #include "blk.h" |
cf43e6be | 13 | |
4875253f OS |
14 | #define BLK_RQ_STAT_BATCH 64 |
15 | ||
34dbad5d OS |
16 | struct blk_queue_stats { |
17 | struct list_head callbacks; | |
18 | spinlock_t lock; | |
b9147dd1 | 19 | bool enable_accounting; |
34dbad5d OS |
20 | }; |
21 | ||
34dbad5d OS |
22 | static void blk_stat_init(struct blk_rq_stat *stat) |
23 | { | |
24 | stat->min = -1ULL; | |
25 | stat->max = stat->nr_samples = stat->mean = 0; | |
26 | stat->batch = stat->nr_batch = 0; | |
27 | } | |
28 | ||
cf43e6be JA |
29 | static void blk_stat_flush_batch(struct blk_rq_stat *stat) |
30 | { | |
31 | const s32 nr_batch = READ_ONCE(stat->nr_batch); | |
209200ef | 32 | const s32 nr_samples = READ_ONCE(stat->nr_samples); |
cf43e6be JA |
33 | |
34 | if (!nr_batch) | |
35 | return; | |
36 | if (!nr_samples) | |
37 | stat->mean = div64_s64(stat->batch, nr_batch); | |
38 | else { | |
39 | stat->mean = div64_s64((stat->mean * nr_samples) + | |
40 | stat->batch, | |
41 | nr_batch + nr_samples); | |
42 | } | |
43 | ||
44 | stat->nr_samples += nr_batch; | |
45 | stat->nr_batch = stat->batch = 0; | |
46 | } | |
47 | ||
48 | static void blk_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src) | |
49 | { | |
7d8d0014 OS |
50 | blk_stat_flush_batch(src); |
51 | ||
cf43e6be JA |
52 | if (!src->nr_samples) |
53 | return; | |
54 | ||
cf43e6be JA |
55 | dst->min = min(dst->min, src->min); |
56 | dst->max = max(dst->max, src->max); | |
57 | ||
58 | if (!dst->nr_samples) | |
59 | dst->mean = src->mean; | |
60 | else { | |
61 | dst->mean = div64_s64((src->mean * src->nr_samples) + | |
62 | (dst->mean * dst->nr_samples), | |
63 | dst->nr_samples + src->nr_samples); | |
64 | } | |
65 | dst->nr_samples += src->nr_samples; | |
66 | } | |
67 | ||
34dbad5d | 68 | static void __blk_stat_add(struct blk_rq_stat *stat, u64 value) |
cf43e6be | 69 | { |
34dbad5d OS |
70 | stat->min = min(stat->min, value); |
71 | stat->max = max(stat->max, value); | |
cf43e6be | 72 | |
34dbad5d OS |
73 | if (stat->batch + value < stat->batch || |
74 | stat->nr_batch + 1 == BLK_RQ_STAT_BATCH) | |
75 | blk_stat_flush_batch(stat); | |
cf43e6be | 76 | |
34dbad5d OS |
77 | stat->batch += value; |
78 | stat->nr_batch++; | |
cf43e6be JA |
79 | } |
80 | ||
34dbad5d | 81 | void blk_stat_add(struct request *rq) |
cf43e6be | 82 | { |
34dbad5d OS |
83 | struct request_queue *q = rq->q; |
84 | struct blk_stat_callback *cb; | |
85 | struct blk_rq_stat *stat; | |
86 | int bucket; | |
87 | s64 now, value; | |
88 | ||
89 | now = __blk_stat_time(ktime_to_ns(ktime_get())); | |
90 | if (now < blk_stat_time(&rq->issue_stat)) | |
91 | return; | |
92 | ||
93 | value = now - blk_stat_time(&rq->issue_stat); | |
94 | ||
b9147dd1 SL |
95 | blk_throtl_stat_add(rq, value); |
96 | ||
34dbad5d OS |
97 | rcu_read_lock(); |
98 | list_for_each_entry_rcu(cb, &q->stats->callbacks, list) { | |
d3738123 JA |
99 | if (!blk_stat_is_active(cb)) |
100 | continue; | |
101 | ||
102 | bucket = cb->bucket_fn(rq); | |
103 | if (bucket < 0) | |
104 | continue; | |
105 | ||
106 | stat = &get_cpu_ptr(cb->cpu_stat)[bucket]; | |
107 | __blk_stat_add(stat, value); | |
108 | put_cpu_ptr(cb->cpu_stat); | |
cf43e6be | 109 | } |
34dbad5d | 110 | rcu_read_unlock(); |
cf43e6be JA |
111 | } |
112 | ||
34dbad5d | 113 | static void blk_stat_timer_fn(unsigned long data) |
cf43e6be | 114 | { |
34dbad5d OS |
115 | struct blk_stat_callback *cb = (void *)data; |
116 | unsigned int bucket; | |
117 | int cpu; | |
cf43e6be | 118 | |
34dbad5d OS |
119 | for (bucket = 0; bucket < cb->buckets; bucket++) |
120 | blk_stat_init(&cb->stat[bucket]); | |
cf43e6be | 121 | |
34dbad5d OS |
122 | for_each_online_cpu(cpu) { |
123 | struct blk_rq_stat *cpu_stat; | |
7cd54aa8 | 124 | |
34dbad5d OS |
125 | cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu); |
126 | for (bucket = 0; bucket < cb->buckets; bucket++) { | |
127 | blk_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]); | |
128 | blk_stat_init(&cpu_stat[bucket]); | |
cf43e6be | 129 | } |
34dbad5d | 130 | } |
cf43e6be | 131 | |
34dbad5d | 132 | cb->timer_fn(cb); |
cf43e6be JA |
133 | } |
134 | ||
34dbad5d OS |
135 | struct blk_stat_callback * |
136 | blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *), | |
a37244e4 | 137 | int (*bucket_fn)(const struct request *), |
34dbad5d | 138 | unsigned int buckets, void *data) |
cf43e6be | 139 | { |
34dbad5d | 140 | struct blk_stat_callback *cb; |
cf43e6be | 141 | |
34dbad5d OS |
142 | cb = kmalloc(sizeof(*cb), GFP_KERNEL); |
143 | if (!cb) | |
144 | return NULL; | |
cf43e6be | 145 | |
34dbad5d OS |
146 | cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat), |
147 | GFP_KERNEL); | |
148 | if (!cb->stat) { | |
149 | kfree(cb); | |
150 | return NULL; | |
151 | } | |
152 | cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat), | |
153 | __alignof__(struct blk_rq_stat)); | |
154 | if (!cb->cpu_stat) { | |
155 | kfree(cb->stat); | |
156 | kfree(cb); | |
157 | return NULL; | |
158 | } | |
cf43e6be | 159 | |
34dbad5d OS |
160 | cb->timer_fn = timer_fn; |
161 | cb->bucket_fn = bucket_fn; | |
162 | cb->data = data; | |
163 | cb->buckets = buckets; | |
164 | setup_timer(&cb->timer, blk_stat_timer_fn, (unsigned long)cb); | |
165 | ||
166 | return cb; | |
cf43e6be | 167 | } |
34dbad5d | 168 | EXPORT_SYMBOL_GPL(blk_stat_alloc_callback); |
cf43e6be | 169 | |
34dbad5d OS |
170 | void blk_stat_add_callback(struct request_queue *q, |
171 | struct blk_stat_callback *cb) | |
cf43e6be | 172 | { |
34dbad5d OS |
173 | unsigned int bucket; |
174 | int cpu; | |
cf43e6be | 175 | |
34dbad5d OS |
176 | for_each_possible_cpu(cpu) { |
177 | struct blk_rq_stat *cpu_stat; | |
cf43e6be | 178 | |
34dbad5d OS |
179 | cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu); |
180 | for (bucket = 0; bucket < cb->buckets; bucket++) | |
181 | blk_stat_init(&cpu_stat[bucket]); | |
182 | } | |
cf43e6be | 183 | |
34dbad5d OS |
184 | spin_lock(&q->stats->lock); |
185 | list_add_tail_rcu(&cb->list, &q->stats->callbacks); | |
186 | set_bit(QUEUE_FLAG_STATS, &q->queue_flags); | |
187 | spin_unlock(&q->stats->lock); | |
188 | } | |
189 | EXPORT_SYMBOL_GPL(blk_stat_add_callback); | |
cf43e6be | 190 | |
34dbad5d OS |
191 | void blk_stat_remove_callback(struct request_queue *q, |
192 | struct blk_stat_callback *cb) | |
193 | { | |
194 | spin_lock(&q->stats->lock); | |
195 | list_del_rcu(&cb->list); | |
b9147dd1 | 196 | if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting) |
34dbad5d OS |
197 | clear_bit(QUEUE_FLAG_STATS, &q->queue_flags); |
198 | spin_unlock(&q->stats->lock); | |
cf43e6be | 199 | |
34dbad5d | 200 | del_timer_sync(&cb->timer); |
cf43e6be | 201 | } |
34dbad5d | 202 | EXPORT_SYMBOL_GPL(blk_stat_remove_callback); |
cf43e6be | 203 | |
34dbad5d | 204 | static void blk_stat_free_callback_rcu(struct rcu_head *head) |
cf43e6be | 205 | { |
34dbad5d OS |
206 | struct blk_stat_callback *cb; |
207 | ||
208 | cb = container_of(head, struct blk_stat_callback, rcu); | |
209 | free_percpu(cb->cpu_stat); | |
210 | kfree(cb->stat); | |
211 | kfree(cb); | |
cf43e6be JA |
212 | } |
213 | ||
34dbad5d | 214 | void blk_stat_free_callback(struct blk_stat_callback *cb) |
cf43e6be | 215 | { |
a83b576c JA |
216 | if (cb) |
217 | call_rcu(&cb->rcu, blk_stat_free_callback_rcu); | |
cf43e6be | 218 | } |
34dbad5d | 219 | EXPORT_SYMBOL_GPL(blk_stat_free_callback); |
cf43e6be | 220 | |
b9147dd1 SL |
221 | void blk_stat_enable_accounting(struct request_queue *q) |
222 | { | |
223 | spin_lock(&q->stats->lock); | |
224 | q->stats->enable_accounting = true; | |
225 | set_bit(QUEUE_FLAG_STATS, &q->queue_flags); | |
226 | spin_unlock(&q->stats->lock); | |
227 | } | |
228 | ||
34dbad5d | 229 | struct blk_queue_stats *blk_alloc_queue_stats(void) |
cf43e6be | 230 | { |
34dbad5d OS |
231 | struct blk_queue_stats *stats; |
232 | ||
233 | stats = kmalloc(sizeof(*stats), GFP_KERNEL); | |
234 | if (!stats) | |
235 | return NULL; | |
236 | ||
237 | INIT_LIST_HEAD(&stats->callbacks); | |
238 | spin_lock_init(&stats->lock); | |
b9147dd1 | 239 | stats->enable_accounting = false; |
34dbad5d OS |
240 | |
241 | return stats; | |
242 | } | |
243 | ||
244 | void blk_free_queue_stats(struct blk_queue_stats *stats) | |
245 | { | |
246 | if (!stats) | |
247 | return; | |
248 | ||
249 | WARN_ON(!list_empty(&stats->callbacks)); | |
cf43e6be | 250 | |
34dbad5d | 251 | kfree(stats); |
cf43e6be | 252 | } |