4 #include <linux/kernel.h>
5 #include <linux/blkdev.h>
6 #include <linux/ktime.h>
7 #include <linux/rcupdate.h>
8 #include <linux/timer.h>
11 * Upper 3 bits can be used elsewhere
13 #define BLK_STAT_RES_BITS 3
14 #define BLK_STAT_SHIFT (64 - BLK_STAT_RES_BITS)
15 #define BLK_STAT_TIME_MASK ((1ULL << BLK_STAT_SHIFT) - 1)
16 #define BLK_STAT_MASK ~BLK_STAT_TIME_MASK
19 * struct blk_stat_callback - Block statistics callback.
21 * A &struct blk_stat_callback is associated with a &struct request_queue. While
22 * @timer is active, that queue's request completion latencies are sorted into
23 * buckets by @bucket_fn and added to a per-cpu buffer, @cpu_stat. When the
24 * timer fires, @cpu_stat is flushed to @stat and @timer_fn is invoked.
26 struct blk_stat_callback {
28 * @list: RCU list of callbacks for a &struct request_queue.
30 struct list_head list;
33 * @timer: Timer for the next callback invocation.
35 struct timer_list timer;
38 * @cpu_stat: Per-cpu statistics buckets.
40 struct blk_rq_stat __percpu *cpu_stat;
43 * @bucket_fn: Given a request, returns which statistics bucket it
44 * should be accounted under.
46 unsigned int (*bucket_fn)(const struct request *);
49 * @buckets: Number of statistics buckets.
54 * @stat: Array of statistics buckets.
56 struct blk_rq_stat *stat;
59 * @fn: Callback function.
61 void (*timer_fn)(struct blk_stat_callback *);
64 * @data: Private pointer for the user.
71 struct blk_queue_stats *blk_alloc_queue_stats(void);
72 void blk_free_queue_stats(struct blk_queue_stats *);
74 void blk_stat_add(struct request *);
76 static inline void blk_stat_set_issue_time(struct blk_issue_stat *stat)
78 stat->time = ((stat->time & BLK_STAT_MASK) |
79 (ktime_to_ns(ktime_get()) & BLK_STAT_TIME_MASK));
82 static inline u64 __blk_stat_time(u64 time)
84 return time & BLK_STAT_TIME_MASK;
87 static inline u64 blk_stat_time(struct blk_issue_stat *stat)
89 return __blk_stat_time(stat->time);
93 * blk_stat_rq_ddir() - Bucket callback function for the request data direction.
96 * This is the same as rq_data_dir() but as a function so it can be used as
97 * @bucket_fn for blk_stat_alloc_callback().
99 * Return: Data direction of the request, either READ or WRITE.
101 unsigned int blk_stat_rq_ddir(const struct request *rq);
104 * blk_stat_alloc_callback() - Allocate a block statistics callback.
105 * @timer_fn: Timer callback function.
106 * @bucket_fn: Bucket callback function.
107 * @buckets: Number of statistics buckets.
108 * @data: Value for the @data field of the &struct blk_stat_callback.
110 * See &struct blk_stat_callback for details on the callback functions.
112 * Return: &struct blk_stat_callback on success or NULL on ENOMEM.
114 struct blk_stat_callback *
115 blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
116 unsigned int (*bucket_fn)(const struct request *),
117 unsigned int buckets, void *data);
120 * blk_stat_add_callback() - Add a block statistics callback to be run on a
122 * @q: The request queue.
125 * Note that a single &struct blk_stat_callback can only be added to a single
126 * &struct request_queue.
128 void blk_stat_add_callback(struct request_queue *q,
129 struct blk_stat_callback *cb);
132 * blk_stat_remove_callback() - Remove a block statistics callback from a
134 * @q: The request queue.
137 * When this returns, the callback is not running on any CPUs and will not be
138 * called again unless readded.
140 void blk_stat_remove_callback(struct request_queue *q,
141 struct blk_stat_callback *cb);
144 * blk_stat_free_callback() - Free a block statistics callback.
147 * @cb may be NULL, in which case this does nothing. If it is not NULL, @cb must
148 * not be associated with a request queue. I.e., if it was previously added with
149 * blk_stat_add_callback(), it must also have been removed since then with
150 * blk_stat_remove_callback().
152 void blk_stat_free_callback(struct blk_stat_callback *cb);
155 * blk_stat_is_active() - Check if a block statistics callback is currently
156 * gathering statistics.
159 static inline bool blk_stat_is_active(struct blk_stat_callback *cb)
161 return timer_pending(&cb->timer);
165 * blk_stat_activate_nsecs() - Gather block statistics during a time window in
168 * @nsecs: Number of nanoseconds to gather statistics for.
170 * The timer callback will be called when the window expires.
172 static inline void blk_stat_activate_nsecs(struct blk_stat_callback *cb,
175 mod_timer(&cb->timer, jiffies + nsecs_to_jiffies(nsecs));
179 * blk_stat_activate_msecs() - Gather block statistics during a time window in
182 * @msecs: Number of milliseconds to gather statistics for.
184 * The timer callback will be called when the window expires.
186 static inline void blk_stat_activate_msecs(struct blk_stat_callback *cb,
189 mod_timer(&cb->timer, jiffies + msecs_to_jiffies(msecs));