blk-stat.h 4.8 KB

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