blk-stat.h 4.6 KB

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