stats.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 { if (schedstat_enabled()) { var++; } } while (0)
  31. #define schedstat_add(var, amt) do { if (schedstat_enabled()) { var += (amt); } } while (0)
  32. #define schedstat_set(var, val) do { if (schedstat_enabled()) { var = (val); } } while (0)
  33. #define schedstat_val(var) (var)
  34. #define schedstat_val_or_zero(var) ((schedstat_enabled()) ? (var) : 0)
  35. #else /* !CONFIG_SCHEDSTATS */
  36. static inline void
  37. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  38. {}
  39. static inline void
  40. rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
  41. {}
  42. static inline void
  43. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  44. {}
  45. #define schedstat_enabled() 0
  46. #define schedstat_inc(var) do { } while (0)
  47. #define schedstat_add(var, amt) do { } while (0)
  48. #define schedstat_set(var, val) do { } while (0)
  49. #define schedstat_val(var) 0
  50. #define schedstat_val_or_zero(var) 0
  51. #endif /* CONFIG_SCHEDSTATS */
  52. #ifdef CONFIG_PSI
  53. /*
  54. * PSI tracks state that persists across sleeps, such as iowaits and
  55. * memory stalls. As a result, it has to distinguish between sleeps,
  56. * where a task's runnable state changes, and requeues, where a task
  57. * and its state are being moved between CPUs and runqueues.
  58. */
  59. static inline void psi_enqueue(struct task_struct *p, bool wakeup)
  60. {
  61. int clear = 0, set = TSK_RUNNING;
  62. if (static_branch_likely(&psi_disabled))
  63. return;
  64. if (!wakeup || p->sched_psi_wake_requeue) {
  65. if (p->flags & PF_MEMSTALL)
  66. set |= TSK_MEMSTALL;
  67. if (p->sched_psi_wake_requeue)
  68. p->sched_psi_wake_requeue = 0;
  69. } else {
  70. if (p->in_iowait)
  71. clear |= TSK_IOWAIT;
  72. }
  73. psi_task_change(p, clear, set);
  74. }
  75. static inline void psi_dequeue(struct task_struct *p, bool sleep)
  76. {
  77. int clear = TSK_RUNNING, set = 0;
  78. if (static_branch_likely(&psi_disabled))
  79. return;
  80. if (!sleep) {
  81. if (p->flags & PF_MEMSTALL)
  82. clear |= TSK_MEMSTALL;
  83. } else {
  84. if (p->in_iowait)
  85. set |= TSK_IOWAIT;
  86. }
  87. psi_task_change(p, clear, set);
  88. }
  89. static inline void psi_ttwu_dequeue(struct task_struct *p)
  90. {
  91. if (static_branch_likely(&psi_disabled))
  92. return;
  93. /*
  94. * Is the task being migrated during a wakeup? Make sure to
  95. * deregister its sleep-persistent psi states from the old
  96. * queue, and let psi_enqueue() know it has to requeue.
  97. */
  98. if (unlikely(p->in_iowait || (p->flags & PF_MEMSTALL))) {
  99. struct rq_flags rf;
  100. struct rq *rq;
  101. int clear = 0;
  102. if (p->in_iowait)
  103. clear |= TSK_IOWAIT;
  104. if (p->flags & PF_MEMSTALL)
  105. clear |= TSK_MEMSTALL;
  106. rq = __task_rq_lock(p, &rf);
  107. psi_task_change(p, clear, 0);
  108. p->sched_psi_wake_requeue = 1;
  109. __task_rq_unlock(rq, &rf);
  110. }
  111. }
  112. static inline void psi_task_tick(struct rq *rq)
  113. {
  114. if (static_branch_likely(&psi_disabled))
  115. return;
  116. if (unlikely(rq->curr->flags & PF_MEMSTALL))
  117. psi_memstall_tick(rq->curr, cpu_of(rq));
  118. }
  119. #else /* CONFIG_PSI */
  120. static inline void psi_enqueue(struct task_struct *p, bool wakeup) {}
  121. static inline void psi_dequeue(struct task_struct *p, bool sleep) {}
  122. static inline void psi_ttwu_dequeue(struct task_struct *p) {}
  123. static inline void psi_task_tick(struct rq *rq) {}
  124. #endif /* CONFIG_PSI */
  125. #ifdef CONFIG_SCHED_INFO
  126. static inline void sched_info_reset_dequeued(struct task_struct *t)
  127. {
  128. t->sched_info.last_queued = 0;
  129. }
  130. /*
  131. * We are interested in knowing how long it was from the *first* time a
  132. * task was queued to the time that it finally hit a cpu, we call this routine
  133. * from dequeue_task() to account for possible rq->clock skew across cpus. The
  134. * delta taken on each cpu would annul the skew.
  135. */
  136. static inline void sched_info_dequeued(struct rq *rq, struct task_struct *t)
  137. {
  138. unsigned long long now = rq_clock(rq), delta = 0;
  139. if (unlikely(sched_info_on()))
  140. if (t->sched_info.last_queued)
  141. delta = now - t->sched_info.last_queued;
  142. sched_info_reset_dequeued(t);
  143. t->sched_info.run_delay += delta;
  144. rq_sched_info_dequeued(rq, delta);
  145. }
  146. /*
  147. * Called when a task finally hits the cpu. We can now calculate how
  148. * long it was waiting to run. We also note when it began so that we
  149. * can keep stats on how long its timeslice is.
  150. */
  151. static void sched_info_arrive(struct rq *rq, struct task_struct *t)
  152. {
  153. unsigned long long now = rq_clock(rq), delta = 0;
  154. if (t->sched_info.last_queued)
  155. delta = now - t->sched_info.last_queued;
  156. sched_info_reset_dequeued(t);
  157. t->sched_info.run_delay += delta;
  158. t->sched_info.last_arrival = now;
  159. t->sched_info.pcount++;
  160. rq_sched_info_arrive(rq, delta);
  161. }
  162. /*
  163. * This function is only called from enqueue_task(), but also only updates
  164. * the timestamp if it is already not set. It's assumed that
  165. * sched_info_dequeued() will clear that stamp when appropriate.
  166. */
  167. static inline void sched_info_queued(struct rq *rq, struct task_struct *t)
  168. {
  169. if (unlikely(sched_info_on()))
  170. if (!t->sched_info.last_queued)
  171. t->sched_info.last_queued = rq_clock(rq);
  172. }
  173. /*
  174. * Called when a process ceases being the active-running process involuntarily
  175. * due, typically, to expiring its time slice (this may also be called when
  176. * switching to the idle task). Now we can calculate how long we ran.
  177. * Also, if the process is still in the TASK_RUNNING state, call
  178. * sched_info_queued() to mark that it has now again started waiting on
  179. * the runqueue.
  180. */
  181. static inline void sched_info_depart(struct rq *rq, struct task_struct *t)
  182. {
  183. unsigned long long delta = rq_clock(rq) -
  184. t->sched_info.last_arrival;
  185. rq_sched_info_depart(rq, delta);
  186. if (t->state == TASK_RUNNING)
  187. sched_info_queued(rq, t);
  188. }
  189. /*
  190. * Called when tasks are switched involuntarily due, typically, to expiring
  191. * their time slice. (This may also be called when switching to or from
  192. * the idle task.) We are only called when prev != next.
  193. */
  194. static inline void
  195. __sched_info_switch(struct rq *rq,
  196. struct task_struct *prev, struct task_struct *next)
  197. {
  198. /*
  199. * prev now departs the cpu. It's not interesting to record
  200. * stats about how efficient we were at scheduling the idle
  201. * process, however.
  202. */
  203. if (prev != rq->idle)
  204. sched_info_depart(rq, prev);
  205. if (next != rq->idle)
  206. sched_info_arrive(rq, next);
  207. }
  208. static inline void
  209. sched_info_switch(struct rq *rq,
  210. struct task_struct *prev, struct task_struct *next)
  211. {
  212. if (unlikely(sched_info_on()))
  213. __sched_info_switch(rq, prev, next);
  214. }
  215. #else
  216. #define sched_info_queued(rq, t) do { } while (0)
  217. #define sched_info_reset_dequeued(t) do { } while (0)
  218. #define sched_info_dequeued(rq, t) do { } while (0)
  219. #define sched_info_depart(rq, t) do { } while (0)
  220. #define sched_info_arrive(rq, next) do { } while (0)
  221. #define sched_info_switch(rq, t, next) do { } while (0)
  222. #endif /* CONFIG_SCHED_INFO */