]> git.karo-electronics.de Git - karo-tx-linux.git/blob - block/blk-stat.h
block: make nr_iovecs unsigned in bio_alloc_bioset()
[karo-tx-linux.git] / block / blk-stat.h
1 #ifndef BLK_STAT_H
2 #define BLK_STAT_H
3
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>
9
10 /*
11  * Upper 3 bits can be used elsewhere
12  */
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
17
18 /**
19  * struct blk_stat_callback - Block statistics callback.
20  *
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.
25  */
26 struct blk_stat_callback {
27         /*
28          * @list: RCU list of callbacks for a &struct request_queue.
29          */
30         struct list_head list;
31
32         /**
33          * @timer: Timer for the next callback invocation.
34          */
35         struct timer_list timer;
36
37         /**
38          * @cpu_stat: Per-cpu statistics buckets.
39          */
40         struct blk_rq_stat __percpu *cpu_stat;
41
42         /**
43          * @bucket_fn: Given a request, returns which statistics bucket it
44          * should be accounted under.
45          */
46         unsigned int (*bucket_fn)(const struct request *);
47
48         /**
49          * @buckets: Number of statistics buckets.
50          */
51         unsigned int buckets;
52
53         /**
54          * @stat: Array of statistics buckets.
55          */
56         struct blk_rq_stat *stat;
57
58         /**
59          * @fn: Callback function.
60          */
61         void (*timer_fn)(struct blk_stat_callback *);
62
63         /**
64          * @data: Private pointer for the user.
65          */
66         void *data;
67
68         struct rcu_head rcu;
69 };
70
71 struct blk_queue_stats *blk_alloc_queue_stats(void);
72 void blk_free_queue_stats(struct blk_queue_stats *);
73
74 void blk_stat_add(struct request *);
75
76 static inline void blk_stat_set_issue_time(struct blk_issue_stat *stat)
77 {
78         stat->time = ((stat->time & BLK_STAT_MASK) |
79                       (ktime_to_ns(ktime_get()) & BLK_STAT_TIME_MASK));
80 }
81
82 static inline u64 __blk_stat_time(u64 time)
83 {
84         return time & BLK_STAT_TIME_MASK;
85 }
86
87 static inline u64 blk_stat_time(struct blk_issue_stat *stat)
88 {
89         return __blk_stat_time(stat->time);
90 }
91
92 /*
93  * blk_stat_rq_ddir() - Bucket callback function for the request data direction.
94  * @rq: Request.
95  *
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().
98  *
99  * Return: Data direction of the request, either READ or WRITE.
100  */
101 unsigned int blk_stat_rq_ddir(const struct request *rq);
102
103 /**
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.
109  *
110  * See &struct blk_stat_callback for details on the callback functions.
111  *
112  * Return: &struct blk_stat_callback on success or NULL on ENOMEM.
113  */
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);
118
119 /**
120  * blk_stat_add_callback() - Add a block statistics callback to be run on a
121  * request queue.
122  * @q: The request queue.
123  * @cb: The callback.
124  *
125  * Note that a single &struct blk_stat_callback can only be added to a single
126  * &struct request_queue.
127  */
128 void blk_stat_add_callback(struct request_queue *q,
129                            struct blk_stat_callback *cb);
130
131 /**
132  * blk_stat_remove_callback() - Remove a block statistics callback from a
133  * request queue.
134  * @q: The request queue.
135  * @cb: The callback.
136  *
137  * When this returns, the callback is not running on any CPUs and will not be
138  * called again unless readded.
139  */
140 void blk_stat_remove_callback(struct request_queue *q,
141                               struct blk_stat_callback *cb);
142
143 /**
144  * blk_stat_free_callback() - Free a block statistics callback.
145  * @cb: The callback.
146  *
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().
151  */
152 void blk_stat_free_callback(struct blk_stat_callback *cb);
153
154 /**
155  * blk_stat_is_active() - Check if a block statistics callback is currently
156  * gathering statistics.
157  * @cb: The callback.
158  */
159 static inline bool blk_stat_is_active(struct blk_stat_callback *cb)
160 {
161         return timer_pending(&cb->timer);
162 }
163
164 /**
165  * blk_stat_activate_nsecs() - Gather block statistics during a time window in
166  * nanoseconds.
167  * @cb: The callback.
168  * @nsecs: Number of nanoseconds to gather statistics for.
169  *
170  * The timer callback will be called when the window expires.
171  */
172 static inline void blk_stat_activate_nsecs(struct blk_stat_callback *cb,
173                                            u64 nsecs)
174 {
175         mod_timer(&cb->timer, jiffies + nsecs_to_jiffies(nsecs));
176 }
177
178 /**
179  * blk_stat_activate_msecs() - Gather block statistics during a time window in
180  * milliseconds.
181  * @cb: The callback.
182  * @msecs: Number of milliseconds to gather statistics for.
183  *
184  * The timer callback will be called when the window expires.
185  */
186 static inline void blk_stat_activate_msecs(struct blk_stat_callback *cb,
187                                            unsigned int msecs)
188 {
189         mod_timer(&cb->timer, jiffies + msecs_to_jiffies(msecs));
190 }
191
192 #endif