blk-stat.h 5.5 KB

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