1 /* SPDX-License-Identifier: GPL-2.0 */
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>
12 * struct blk_stat_callback - Block statistics callback.
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.
19 struct blk_stat_callback {
21 * @list: RCU list of callbacks for a &struct request_queue.
23 struct list_head list;
26 * @timer: Timer for the next callback invocation.
28 struct timer_list timer;
31 * @cpu_stat: Per-cpu statistics buckets.
33 struct blk_rq_stat __percpu *cpu_stat;
36 * @bucket_fn: Given a request, returns which statistics bucket it
37 * should be accounted under. Return -1 for no bucket for this
40 int (*bucket_fn)(const struct request *);
43 * @buckets: Number of statistics buckets.
48 * @stat: Array of statistics buckets.
50 struct blk_rq_stat *stat;
53 * @fn: Callback function.
55 void (*timer_fn)(struct blk_stat_callback *);
58 * @data: Private pointer for the user.
65 struct blk_queue_stats *blk_alloc_queue_stats(void);
66 void blk_free_queue_stats(struct blk_queue_stats *);
68 void blk_stat_add(struct request *rq, u64 now);
70 /* record time/size info in request but not add a callback */
71 void blk_stat_enable_accounting(struct request_queue *q);
74 * blk_stat_alloc_callback() - Allocate a block statistics callback.
75 * @timer_fn: Timer callback function.
76 * @bucket_fn: Bucket callback function.
77 * @buckets: Number of statistics buckets.
78 * @data: Value for the @data field of the &struct blk_stat_callback.
80 * See &struct blk_stat_callback for details on the callback functions.
82 * Return: &struct blk_stat_callback on success or NULL on ENOMEM.
84 struct blk_stat_callback *
85 blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
86 int (*bucket_fn)(const struct request *),
87 unsigned int buckets, void *data);
90 * blk_stat_add_callback() - Add a block statistics callback to be run on a
92 * @q: The request queue.
95 * Note that a single &struct blk_stat_callback can only be added to a single
96 * &struct request_queue.
98 void blk_stat_add_callback(struct request_queue *q,
99 struct blk_stat_callback *cb);
102 * blk_stat_remove_callback() - Remove a block statistics callback from a
104 * @q: The request queue.
107 * When this returns, the callback is not running on any CPUs and will not be
108 * called again unless readded.
110 void blk_stat_remove_callback(struct request_queue *q,
111 struct blk_stat_callback *cb);
114 * blk_stat_free_callback() - Free a block statistics callback.
117 * @cb may be NULL, in which case this does nothing. If it is not NULL, @cb must
118 * not be associated with a request queue. I.e., if it was previously added with
119 * blk_stat_add_callback(), it must also have been removed since then with
120 * blk_stat_remove_callback().
122 void blk_stat_free_callback(struct blk_stat_callback *cb);
125 * blk_stat_is_active() - Check if a block statistics callback is currently
126 * gathering statistics.
129 static inline bool blk_stat_is_active(struct blk_stat_callback *cb)
131 return timer_pending(&cb->timer);
135 * blk_stat_activate_nsecs() - Gather block statistics during a time window in
138 * @nsecs: Number of nanoseconds to gather statistics for.
140 * The timer callback will be called when the window expires.
142 static inline void blk_stat_activate_nsecs(struct blk_stat_callback *cb,
145 mod_timer(&cb->timer, jiffies + nsecs_to_jiffies(nsecs));
149 * blk_stat_activate_msecs() - Gather block statistics during a time window in
152 * @msecs: Number of milliseconds to gather statistics for.
154 * The timer callback will be called when the window expires.
156 static inline void blk_stat_activate_msecs(struct blk_stat_callback *cb,
159 mod_timer(&cb->timer, jiffies + msecs_to_jiffies(msecs));