projects/linux·blob master · refs

177 lines · 4.8 KiB

1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef BLK_STAT_H
3#define BLK_STAT_H
4
5#include <linux/kernel.h>
6#include <linux/blkdev.h>
7#include <linux/ktime.h>
8#include <linux/rcupdate.h>
9#include <linux/timer.h>
10
11/**
12 * struct blk_stat_callback - Block statistics callback.
13 *
14 * A &struct blk_stat_callback is associated with a &struct request_queue. While
15 * @timer is active, that queue's request completion latencies are sorted into
16 * buckets by @bucket_fn and added to a per-cpu buffer, @cpu_stat. When the
17 * timer fires, @cpu_stat is flushed to @stat and @timer_fn is invoked.
18 */
19struct blk_stat_callback {
20 /**
21 * @list: RCU list of callbacks for a &struct request_queue.
22 */
23 struct list_head list;
24
25 /**
26 * @timer: Timer for the next callback invocation.
27 */
28 struct timer_list timer;
29
30 /**
31 * @cpu_stat: Per-cpu statistics buckets.
32 */
33 struct blk_rq_stat __percpu *cpu_stat;
34
35 /**
36 * @bucket_fn: Given a request, returns which statistics bucket it
37 * should be accounted under. Return -1 for no bucket for this
38 * request.
39 */
40 int (*bucket_fn)(const struct request *);
41
42 /**
43 * @buckets: Number of statistics buckets.
44 */
45 unsigned int buckets;
46
47 /**
48 * @stat: Array of statistics buckets.
49 */
50 struct blk_rq_stat *stat;
51
52 /**
53 * @timer_fn: Callback function.
54 */
55 void (*timer_fn)(struct blk_stat_callback *);
56
57 /**
58 * @data: Private pointer for the user.
59 */
60 void *data;
61
62 /**
63 * @rcu: rcu list head
64 */
65 struct rcu_head rcu;
66};
67
68struct blk_queue_stats *blk_alloc_queue_stats(void);
69void blk_free_queue_stats(struct blk_queue_stats *);
70
71void blk_stat_add(struct request *rq, u64 now);
72
73/* record time/size info in request but not add a callback */
74void blk_stat_enable_accounting(struct request_queue *q);
75void blk_stat_disable_accounting(struct request_queue *q);
76
77/**
78 * blk_stat_alloc_callback() - Allocate a block statistics callback.
79 * @timer_fn: Timer callback function.
80 * @bucket_fn: Bucket callback function.
81 * @buckets: Number of statistics buckets.
82 * @data: Value for the @data field of the &struct blk_stat_callback.
83 *
84 * See &struct blk_stat_callback for details on the callback functions.
85 *
86 * Return: &struct blk_stat_callback on success or NULL on ENOMEM.
87 */
88struct blk_stat_callback *
89blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
90 int (*bucket_fn)(const struct request *),
91 unsigned int buckets, void *data);
92
93/**
94 * blk_stat_add_callback() - Add a block statistics callback to be run on a
95 * request queue.
96 * @q: The request queue.
97 * @cb: The callback.
98 *
99 * Note that a single &struct blk_stat_callback can only be added to a single
100 * &struct request_queue.
101 */
102void blk_stat_add_callback(struct request_queue *q,
103 struct blk_stat_callback *cb);
104
105/**
106 * blk_stat_remove_callback() - Remove a block statistics callback from a
107 * request queue.
108 * @q: The request queue.
109 * @cb: The callback.
110 *
111 * When this returns, the callback is not running on any CPUs and will not be
112 * called again unless readded.
113 */
114void blk_stat_remove_callback(struct request_queue *q,
115 struct blk_stat_callback *cb);
116
117/**
118 * blk_stat_free_callback() - Free a block statistics callback.
119 * @cb: The callback.
120 *
121 * @cb may be NULL, in which case this does nothing. If it is not NULL, @cb must
122 * not be associated with a request queue. I.e., if it was previously added with
123 * blk_stat_add_callback(), it must also have been removed since then with
124 * blk_stat_remove_callback().
125 */
126void blk_stat_free_callback(struct blk_stat_callback *cb);
127
128/**
129 * blk_stat_is_active() - Check if a block statistics callback is currently
130 * gathering statistics.
131 * @cb: The callback.
132 *
133 * Returns: %true iff the callback is active.
134 */
135static inline bool blk_stat_is_active(struct blk_stat_callback *cb)
136{
137 return timer_pending(&cb->timer);
138}
139
140/**
141 * blk_stat_activate_nsecs() - Gather block statistics during a time window in
142 * nanoseconds.
143 * @cb: The callback.
144 * @nsecs: Number of nanoseconds to gather statistics for.
145 *
146 * The timer callback will be called when the window expires.
147 */
148static inline void blk_stat_activate_nsecs(struct blk_stat_callback *cb,
149 u64 nsecs)
150{
151 mod_timer(&cb->timer, jiffies + nsecs_to_jiffies(nsecs));
152}
153
154static inline void blk_stat_deactivate(struct blk_stat_callback *cb)
155{
156 timer_delete_sync(&cb->timer);
157}
158
159/**
160 * blk_stat_activate_msecs() - Gather block statistics during a time window in
161 * milliseconds.
162 * @cb: The callback.
163 * @msecs: Number of milliseconds to gather statistics for.
164 *
165 * The timer callback will be called when the window expires.
166 */
167static inline void blk_stat_activate_msecs(struct blk_stat_callback *cb,
168 unsigned int msecs)
169{
170 mod_timer(&cb->timer, jiffies + msecs_to_jiffies(msecs));
171}
172
173void blk_rq_stat_add(struct blk_rq_stat *, u64);
174void blk_rq_stat_sum(struct blk_rq_stat *, struct blk_rq_stat *);
175void blk_rq_stat_init(struct blk_rq_stat *);
176
177#endif