blob: 17e1eb4ec7e25dba273877006d82d901f5864bad [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Jens Axboecf43e6b2016-11-07 21:32:37 -07002#ifndef BLK_STAT_H
3#define BLK_STAT_H
4
Omar Sandoval34dbad52017-03-21 08:56:08 -07005#include <linux/kernel.h>
6#include <linux/blkdev.h>
7#include <linux/ktime.h>
8#include <linux/rcupdate.h>
9#include <linux/timer.h>
Jens Axboecf43e6b2016-11-07 21:32:37 -070010
Omar Sandoval34dbad52017-03-21 08:56:08 -070011/**
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
Stephen Batesa37244e2017-04-20 15:29:16 -060037 * should be accounted under. Return -1 for no bucket for this
38 * request.
Omar Sandoval34dbad52017-03-21 08:56:08 -070039 */
Stephen Batesa37244e2017-04-20 15:29:16 -060040 int (*bucket_fn)(const struct request *);
Omar Sandoval34dbad52017-03-21 08:56:08 -070041
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 * @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 struct rcu_head rcu;
63};
64
65struct blk_queue_stats *blk_alloc_queue_stats(void);
66void blk_free_queue_stats(struct blk_queue_stats *);
Jens Axboe48b5c1f2021-11-13 14:03:26 -070067bool blk_stats_alloc_enable(struct request_queue *q);
Omar Sandoval34dbad52017-03-21 08:56:08 -070068
Omar Sandoval522a7772018-05-09 02:08:53 -070069void blk_stat_add(struct request *rq, u64 now);
Omar Sandoval34dbad52017-03-21 08:56:08 -070070
Shaohua Lib9147dd2017-03-27 15:19:42 -070071/* record time/size info in request but not add a callback */
72void blk_stat_enable_accounting(struct request_queue *q);
Jens Axboe68497092021-12-14 17:23:05 -070073void blk_stat_disable_accounting(struct request_queue *q);
Shaohua Lib9147dd2017-03-27 15:19:42 -070074
Omar Sandoval34dbad52017-03-21 08:56:08 -070075/**
76 * blk_stat_alloc_callback() - Allocate a block statistics callback.
77 * @timer_fn: Timer callback function.
78 * @bucket_fn: Bucket callback function.
79 * @buckets: Number of statistics buckets.
80 * @data: Value for the @data field of the &struct blk_stat_callback.
81 *
82 * See &struct blk_stat_callback for details on the callback functions.
83 *
84 * Return: &struct blk_stat_callback on success or NULL on ENOMEM.
85 */
86struct blk_stat_callback *
87blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
Stephen Batesa37244e2017-04-20 15:29:16 -060088 int (*bucket_fn)(const struct request *),
Omar Sandoval34dbad52017-03-21 08:56:08 -070089 unsigned int buckets, void *data);
90
91/**
92 * blk_stat_add_callback() - Add a block statistics callback to be run on a
93 * request queue.
94 * @q: The request queue.
95 * @cb: The callback.
96 *
97 * Note that a single &struct blk_stat_callback can only be added to a single
98 * &struct request_queue.
99 */
100void blk_stat_add_callback(struct request_queue *q,
101 struct blk_stat_callback *cb);
102
103/**
104 * blk_stat_remove_callback() - Remove a block statistics callback from a
105 * request queue.
106 * @q: The request queue.
107 * @cb: The callback.
108 *
109 * When this returns, the callback is not running on any CPUs and will not be
110 * called again unless readded.
111 */
112void blk_stat_remove_callback(struct request_queue *q,
113 struct blk_stat_callback *cb);
114
115/**
116 * blk_stat_free_callback() - Free a block statistics callback.
117 * @cb: The callback.
118 *
119 * @cb may be NULL, in which case this does nothing. If it is not NULL, @cb must
120 * not be associated with a request queue. I.e., if it was previously added with
121 * blk_stat_add_callback(), it must also have been removed since then with
122 * blk_stat_remove_callback().
123 */
124void blk_stat_free_callback(struct blk_stat_callback *cb);
125
126/**
127 * blk_stat_is_active() - Check if a block statistics callback is currently
128 * gathering statistics.
129 * @cb: The callback.
130 */
131static inline bool blk_stat_is_active(struct blk_stat_callback *cb)
132{
133 return timer_pending(&cb->timer);
134}
135
136/**
137 * blk_stat_activate_nsecs() - Gather block statistics during a time window in
138 * nanoseconds.
139 * @cb: The callback.
140 * @nsecs: Number of nanoseconds to gather statistics for.
141 *
142 * The timer callback will be called when the window expires.
143 */
144static inline void blk_stat_activate_nsecs(struct blk_stat_callback *cb,
145 u64 nsecs)
146{
147 mod_timer(&cb->timer, jiffies + nsecs_to_jiffies(nsecs));
148}
149
Ming Lei544fbd12018-12-12 19:44:34 +0800150static inline void blk_stat_deactivate(struct blk_stat_callback *cb)
151{
152 del_timer_sync(&cb->timer);
153}
154
Omar Sandoval34dbad52017-03-21 08:56:08 -0700155/**
156 * blk_stat_activate_msecs() - Gather block statistics during a time window in
157 * milliseconds.
158 * @cb: The callback.
159 * @msecs: Number of milliseconds to gather statistics for.
160 *
161 * The timer callback will be called when the window expires.
162 */
163static inline void blk_stat_activate_msecs(struct blk_stat_callback *cb,
164 unsigned int msecs)
165{
166 mod_timer(&cb->timer, jiffies + msecs_to_jiffies(msecs));
167}
168
Josef Bacik2ecbf452018-07-03 11:14:57 -0400169void blk_rq_stat_add(struct blk_rq_stat *, u64);
170void blk_rq_stat_sum(struct blk_rq_stat *, struct blk_rq_stat *);
171void blk_rq_stat_init(struct blk_rq_stat *);
172
Jens Axboecf43e6b2016-11-07 21:32:37 -0700173#endif