drivers/net/wan/lapbether: Added needed_tailroom
[linux/fpc-iii.git] / block / blk-stat.c
blob940f15d600f8a7dc32a3bd77f650e6e53d9f66fc
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Block stat tracking code
5 * Copyright (C) 2016 Jens Axboe
6 */
7 #include <linux/kernel.h>
8 #include <linux/rculist.h>
9 #include <linux/blk-mq.h>
11 #include "blk-stat.h"
12 #include "blk-mq.h"
13 #include "blk.h"
15 struct blk_queue_stats {
16 struct list_head callbacks;
17 spinlock_t lock;
18 bool enable_accounting;
21 void blk_rq_stat_init(struct blk_rq_stat *stat)
23 stat->min = -1ULL;
24 stat->max = stat->nr_samples = stat->mean = 0;
25 stat->batch = 0;
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)
31 if (!src->nr_samples)
32 return;
34 dst->min = min(dst->min, src->min);
35 dst->max = max(dst->max, src->max);
37 dst->mean = div_u64(src->batch + dst->mean * dst->nr_samples,
38 dst->nr_samples + src->nr_samples);
40 dst->nr_samples += src->nr_samples;
43 void blk_rq_stat_add(struct blk_rq_stat *stat, u64 value)
45 stat->min = min(stat->min, value);
46 stat->max = max(stat->max, value);
47 stat->batch += value;
48 stat->nr_samples++;
51 void blk_stat_add(struct request *rq, u64 now)
53 struct request_queue *q = rq->q;
54 struct blk_stat_callback *cb;
55 struct blk_rq_stat *stat;
56 int bucket;
57 u64 value;
59 value = (now >= rq->io_start_time_ns) ? now - rq->io_start_time_ns : 0;
61 blk_throtl_stat_add(rq, value);
63 rcu_read_lock();
64 list_for_each_entry_rcu(cb, &q->stats->callbacks, list) {
65 if (!blk_stat_is_active(cb))
66 continue;
68 bucket = cb->bucket_fn(rq);
69 if (bucket < 0)
70 continue;
72 stat = &get_cpu_ptr(cb->cpu_stat)[bucket];
73 blk_rq_stat_add(stat, value);
74 put_cpu_ptr(cb->cpu_stat);
76 rcu_read_unlock();
79 static void blk_stat_timer_fn(struct timer_list *t)
81 struct blk_stat_callback *cb = from_timer(cb, t, timer);
82 unsigned int bucket;
83 int cpu;
85 for (bucket = 0; bucket < cb->buckets; bucket++)
86 blk_rq_stat_init(&cb->stat[bucket]);
88 for_each_online_cpu(cpu) {
89 struct blk_rq_stat *cpu_stat;
91 cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
92 for (bucket = 0; bucket < cb->buckets; bucket++) {
93 blk_rq_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]);
94 blk_rq_stat_init(&cpu_stat[bucket]);
98 cb->timer_fn(cb);
101 struct blk_stat_callback *
102 blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
103 int (*bucket_fn)(const struct request *),
104 unsigned int buckets, void *data)
106 struct blk_stat_callback *cb;
108 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
109 if (!cb)
110 return NULL;
112 cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat),
113 GFP_KERNEL);
114 if (!cb->stat) {
115 kfree(cb);
116 return NULL;
118 cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat),
119 __alignof__(struct blk_rq_stat));
120 if (!cb->cpu_stat) {
121 kfree(cb->stat);
122 kfree(cb);
123 return NULL;
126 cb->timer_fn = timer_fn;
127 cb->bucket_fn = bucket_fn;
128 cb->data = data;
129 cb->buckets = buckets;
130 timer_setup(&cb->timer, blk_stat_timer_fn, 0);
132 return cb;
135 void blk_stat_add_callback(struct request_queue *q,
136 struct blk_stat_callback *cb)
138 unsigned int bucket;
139 int cpu;
141 for_each_possible_cpu(cpu) {
142 struct blk_rq_stat *cpu_stat;
144 cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
145 for (bucket = 0; bucket < cb->buckets; bucket++)
146 blk_rq_stat_init(&cpu_stat[bucket]);
149 spin_lock(&q->stats->lock);
150 list_add_tail_rcu(&cb->list, &q->stats->callbacks);
151 blk_queue_flag_set(QUEUE_FLAG_STATS, q);
152 spin_unlock(&q->stats->lock);
155 void blk_stat_remove_callback(struct request_queue *q,
156 struct blk_stat_callback *cb)
158 spin_lock(&q->stats->lock);
159 list_del_rcu(&cb->list);
160 if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting)
161 blk_queue_flag_clear(QUEUE_FLAG_STATS, q);
162 spin_unlock(&q->stats->lock);
164 del_timer_sync(&cb->timer);
167 static void blk_stat_free_callback_rcu(struct rcu_head *head)
169 struct blk_stat_callback *cb;
171 cb = container_of(head, struct blk_stat_callback, rcu);
172 free_percpu(cb->cpu_stat);
173 kfree(cb->stat);
174 kfree(cb);
177 void blk_stat_free_callback(struct blk_stat_callback *cb)
179 if (cb)
180 call_rcu(&cb->rcu, blk_stat_free_callback_rcu);
183 void blk_stat_enable_accounting(struct request_queue *q)
185 spin_lock(&q->stats->lock);
186 q->stats->enable_accounting = true;
187 blk_queue_flag_set(QUEUE_FLAG_STATS, q);
188 spin_unlock(&q->stats->lock);
190 EXPORT_SYMBOL_GPL(blk_stat_enable_accounting);
192 struct blk_queue_stats *blk_alloc_queue_stats(void)
194 struct blk_queue_stats *stats;
196 stats = kmalloc(sizeof(*stats), GFP_KERNEL);
197 if (!stats)
198 return NULL;
200 INIT_LIST_HEAD(&stats->callbacks);
201 spin_lock_init(&stats->lock);
202 stats->enable_accounting = false;
204 return stats;
207 void blk_free_queue_stats(struct blk_queue_stats *stats)
209 if (!stats)
210 return;
212 WARN_ON(!list_empty(&stats->callbacks));
214 kfree(stats);