stats.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifdef CONFIG_SCHEDSTATS
  3. /*
  4. * Expects runqueue lock to be held for atomicity of update
  5. */
  6. static inline void
  7. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  8. {
  9. if (rq) {
  10. rq->rq_sched_info.run_delay += delta;
  11. rq->rq_sched_info.pcount++;
  12. }
  13. }
  14. /*
  15. * Expects runqueue lock to be held for atomicity of update
  16. */
  17. static inline void
  18. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  19. {
  20. if (rq)
  21. rq->rq_cpu_time += delta;
  22. }
  23. static inline void
  24. rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
  25. {
  26. if (rq)
  27. rq->rq_sched_info.run_delay += delta;
  28. }
  29. #define schedstat_enabled() static_branch_unlikely(&sched_schedstats)
  30. #define __schedstat_inc(var) do { var++; } while (0)
  31. #define schedstat_inc(var) do { if (schedstat_enabled()) { var++; } } while (0)
  32. #define schedstat_add(var, amt) do { if (schedstat_enabled()) { var += (amt); } } while (0)
  33. #define schedstat_set(var, val) do { if (schedstat_enabled()) { var = (val); } } while (0)
  34. #define schedstat_val(var) (var)
  35. #define schedstat_val_or_zero(var) ((schedstat_enabled()) ? (var) : 0)
  36. #else /* !CONFIG_SCHEDSTATS */
  37. static inline void
  38. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  39. {}
  40. static inline void
  41. rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
  42. {}
  43. static inline void
  44. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  45. {}
  46. #define schedstat_enabled() 0
  47. #define __schedstat_inc(var) do { } while (0)
  48. #define schedstat_inc(var) do { } while (0)
  49. #define schedstat_add(var, amt) do { } while (0)
  50. #define schedstat_set(var, val) do { } while (0)
  51. #define schedstat_val(var) 0
  52. #define schedstat_val_or_zero(var) 0
  53. #endif /* CONFIG_SCHEDSTATS */
  54. #ifdef CONFIG_SCHED_INFO
  55. static inline void sched_info_reset_dequeued(struct task_struct *t)
  56. {
  57. t->sched_info.last_queued = 0;
  58. }
  59. /*
  60. * We are interested in knowing how long it was from the *first* time a
  61. * task was queued to the time that it finally hit a cpu, we call this routine
  62. * from dequeue_task() to account for possible rq->clock skew across cpus. The
  63. * delta taken on each cpu would annul the skew.
  64. */
  65. static inline void sched_info_dequeued(struct rq *rq, struct task_struct *t)
  66. {
  67. unsigned long long now = rq_clock(rq), delta = 0;
  68. if (unlikely(sched_info_on()))
  69. if (t->sched_info.last_queued)
  70. delta = now - t->sched_info.last_queued;
  71. sched_info_reset_dequeued(t);
  72. t->sched_info.run_delay += delta;
  73. rq_sched_info_dequeued(rq, delta);
  74. }
  75. /*
  76. * Called when a task finally hits the cpu. We can now calculate how
  77. * long it was waiting to run. We also note when it began so that we
  78. * can keep stats on how long its timeslice is.
  79. */
  80. static void sched_info_arrive(struct rq *rq, struct task_struct *t)
  81. {
  82. unsigned long long now = rq_clock(rq), delta = 0;
  83. if (t->sched_info.last_queued)
  84. delta = now - t->sched_info.last_queued;
  85. sched_info_reset_dequeued(t);
  86. t->sched_info.run_delay += delta;
  87. t->sched_info.last_arrival = now;
  88. t->sched_info.pcount++;
  89. rq_sched_info_arrive(rq, delta);
  90. }
  91. /*
  92. * This function is only called from enqueue_task(), but also only updates
  93. * the timestamp if it is already not set. It's assumed that
  94. * sched_info_dequeued() will clear that stamp when appropriate.
  95. */
  96. static inline void sched_info_queued(struct rq *rq, struct task_struct *t)
  97. {
  98. if (unlikely(sched_info_on()))
  99. if (!t->sched_info.last_queued)
  100. t->sched_info.last_queued = rq_clock(rq);
  101. }
  102. /*
  103. * Called when a process ceases being the active-running process involuntarily
  104. * due, typically, to expiring its time slice (this may also be called when
  105. * switching to the idle task). Now we can calculate how long we ran.
  106. * Also, if the process is still in the TASK_RUNNING state, call
  107. * sched_info_queued() to mark that it has now again started waiting on
  108. * the runqueue.
  109. */
  110. static inline void sched_info_depart(struct rq *rq, struct task_struct *t)
  111. {
  112. unsigned long long delta = rq_clock(rq) -
  113. t->sched_info.last_arrival;
  114. rq_sched_info_depart(rq, delta);
  115. if (t->state == TASK_RUNNING)
  116. sched_info_queued(rq, t);
  117. }
  118. /*
  119. * Called when tasks are switched involuntarily due, typically, to expiring
  120. * their time slice. (This may also be called when switching to or from
  121. * the idle task.) We are only called when prev != next.
  122. */
  123. static inline void
  124. __sched_info_switch(struct rq *rq,
  125. struct task_struct *prev, struct task_struct *next)
  126. {
  127. /*
  128. * prev now departs the cpu. It's not interesting to record
  129. * stats about how efficient we were at scheduling the idle
  130. * process, however.
  131. */
  132. if (prev != rq->idle)
  133. sched_info_depart(rq, prev);
  134. if (next != rq->idle)
  135. sched_info_arrive(rq, next);
  136. }
  137. static inline void
  138. sched_info_switch(struct rq *rq,
  139. struct task_struct *prev, struct task_struct *next)
  140. {
  141. if (unlikely(sched_info_on()))
  142. __sched_info_switch(rq, prev, next);
  143. }
  144. #else
  145. #define sched_info_queued(rq, t) do { } while (0)
  146. #define sched_info_reset_dequeued(t) do { } while (0)
  147. #define sched_info_dequeued(rq, t) do { } while (0)
  148. #define sched_info_depart(rq, t) do { } while (0)
  149. #define sched_info_arrive(rq, next) do { } while (0)
  150. #define sched_info_switch(rq, t, next) do { } while (0)
  151. #endif /* CONFIG_SCHED_INFO */