blk-stat.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. void blk_stat_add(struct request *rq, u64 now);
  57. /* record time/size info in request but not add a callback */
  58. void blk_stat_enable_accounting(struct request_queue *q);
  59. void blk_stat_disable_accounting(struct request_queue *q);
  60. /**
  61. * blk_stat_alloc_callback() - Allocate a block statistics callback.
  62. * @timer_fn: Timer callback function.
  63. * @bucket_fn: Bucket callback function.
  64. * @buckets: Number of statistics buckets.
  65. * @data: Value for the @data field of the &struct blk_stat_callback.
  66. *
  67. * See &struct blk_stat_callback for details on the callback functions.
  68. *
  69. * Return: &struct blk_stat_callback on success or NULL on ENOMEM.
  70. */
  71. struct blk_stat_callback *
  72. blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
  73. int (*bucket_fn)(const struct request *),
  74. unsigned int buckets, void *data);
  75. /**
  76. * blk_stat_add_callback() - Add a block statistics callback to be run on a
  77. * request queue.
  78. * @q: The request queue.
  79. * @cb: The callback.
  80. *
  81. * Note that a single &struct blk_stat_callback can only be added to a single
  82. * &struct request_queue.
  83. */
  84. void blk_stat_add_callback(struct request_queue *q,
  85. struct blk_stat_callback *cb);
  86. /**
  87. * blk_stat_remove_callback() - Remove a block statistics callback from a
  88. * request queue.
  89. * @q: The request queue.
  90. * @cb: The callback.
  91. *
  92. * When this returns, the callback is not running on any CPUs and will not be
  93. * called again unless readded.
  94. */
  95. void blk_stat_remove_callback(struct request_queue *q,
  96. struct blk_stat_callback *cb);
  97. /**
  98. * blk_stat_free_callback() - Free a block statistics callback.
  99. * @cb: The callback.
  100. *
  101. * @cb may be NULL, in which case this does nothing. If it is not NULL, @cb must
  102. * not be associated with a request queue. I.e., if it was previously added with
  103. * blk_stat_add_callback(), it must also have been removed since then with
  104. * blk_stat_remove_callback().
  105. */
  106. void blk_stat_free_callback(struct blk_stat_callback *cb);
  107. /**
  108. * blk_stat_is_active() - Check if a block statistics callback is currently
  109. * gathering statistics.
  110. * @cb: The callback.
  111. */
  112. static inline bool blk_stat_is_active(struct blk_stat_callback *cb)
  113. {
  114. return timer_pending(&cb->timer);
  115. }
  116. /**
  117. * blk_stat_activate_nsecs() - Gather block statistics during a time window in
  118. * nanoseconds.
  119. * @cb: The callback.
  120. * @nsecs: Number of nanoseconds to gather statistics for.
  121. *
  122. * The timer callback will be called when the window expires.
  123. */
  124. static inline void blk_stat_activate_nsecs(struct blk_stat_callback *cb,
  125. u64 nsecs)
  126. {
  127. mod_timer(&cb->timer, jiffies + nsecs_to_jiffies(nsecs));
  128. }
  129. static inline void blk_stat_deactivate(struct blk_stat_callback *cb)
  130. {
  131. del_timer_sync(&cb->timer);
  132. }
  133. /**
  134. * blk_stat_activate_msecs() - Gather block statistics during a time window in
  135. * milliseconds.
  136. * @cb: The callback.
  137. * @msecs: Number of milliseconds to gather statistics for.
  138. *
  139. * The timer callback will be called when the window expires.
  140. */
  141. static inline void blk_stat_activate_msecs(struct blk_stat_callback *cb,
  142. unsigned int msecs)
  143. {
  144. mod_timer(&cb->timer, jiffies + msecs_to_jiffies(msecs));
  145. }
  146. void blk_rq_stat_add(struct blk_rq_stat *, u64);
  147. void blk_rq_stat_sum(struct blk_rq_stat *, struct blk_rq_stat *);
  148. void blk_rq_stat_init(struct blk_rq_stat *);
  149. #endif