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