stats.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _KERNEL_STATS_H
  3. #define _KERNEL_STATS_H
  4. #ifdef CONFIG_SCHEDSTATS
  5. extern struct static_key_false sched_schedstats;
  6. /*
  7. * Expects runqueue lock to be held for atomicity of update
  8. */
  9. static inline void
  10. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  11. {
  12. if (rq) {
  13. rq->rq_sched_info.run_delay += delta;
  14. rq->rq_sched_info.pcount++;
  15. }
  16. }
  17. /*
  18. * Expects runqueue lock to be held for atomicity of update
  19. */
  20. static inline void
  21. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  22. {
  23. if (rq)
  24. rq->rq_cpu_time += delta;
  25. }
  26. static inline void
  27. rq_sched_info_dequeue(struct rq *rq, unsigned long long delta)
  28. {
  29. if (rq)
  30. rq->rq_sched_info.run_delay += delta;
  31. }
  32. #define schedstat_enabled() static_branch_unlikely(&sched_schedstats)
  33. #define __schedstat_inc(var) do { var++; } while (0)
  34. #define schedstat_inc(var) do { if (schedstat_enabled()) { var++; } } while (0)
  35. #define __schedstat_add(var, amt) do { var += (amt); } while (0)
  36. #define schedstat_add(var, amt) do { if (schedstat_enabled()) { var += (amt); } } while (0)
  37. #define __schedstat_set(var, val) do { var = (val); } while (0)
  38. #define schedstat_set(var, val) do { if (schedstat_enabled()) { var = (val); } } while (0)
  39. #define schedstat_val(var) (var)
  40. #define schedstat_val_or_zero(var) ((schedstat_enabled()) ? (var) : 0)
  41. void __update_stats_wait_start(struct rq *rq, struct task_struct *p,
  42. struct sched_statistics *stats);
  43. void __update_stats_wait_end(struct rq *rq, struct task_struct *p,
  44. struct sched_statistics *stats);
  45. void __update_stats_enqueue_sleeper(struct rq *rq, struct task_struct *p,
  46. struct sched_statistics *stats);
  47. static inline void
  48. check_schedstat_required(void)
  49. {
  50. if (schedstat_enabled())
  51. return;
  52. /* Force schedstat enabled if a dependent tracepoint is active */
  53. if (trace_sched_stat_wait_enabled() ||
  54. trace_sched_stat_sleep_enabled() ||
  55. trace_sched_stat_iowait_enabled() ||
  56. trace_sched_stat_blocked_enabled() ||
  57. trace_sched_stat_runtime_enabled())
  58. printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, stat_blocked and stat_runtime require the kernel parameter schedstats=enable or kernel.sched_schedstats=1\n");
  59. }
  60. #else /* !CONFIG_SCHEDSTATS: */
  61. static inline void rq_sched_info_arrive (struct rq *rq, unsigned long long delta) { }
  62. static inline void rq_sched_info_dequeue(struct rq *rq, unsigned long long delta) { }
  63. static inline void rq_sched_info_depart (struct rq *rq, unsigned long long delta) { }
  64. # define schedstat_enabled() 0
  65. # define __schedstat_inc(var) do { } while (0)
  66. # define schedstat_inc(var) do { } while (0)
  67. # define __schedstat_add(var, amt) do { } while (0)
  68. # define schedstat_add(var, amt) do { } while (0)
  69. # define __schedstat_set(var, val) do { } while (0)
  70. # define schedstat_set(var, val) do { } while (0)
  71. # define schedstat_val(var) 0
  72. # define schedstat_val_or_zero(var) 0
  73. # define __update_stats_wait_start(rq, p, stats) do { } while (0)
  74. # define __update_stats_wait_end(rq, p, stats) do { } while (0)
  75. # define __update_stats_enqueue_sleeper(rq, p, stats) do { } while (0)
  76. # define check_schedstat_required() do { } while (0)
  77. #endif /* CONFIG_SCHEDSTATS */
  78. #ifdef CONFIG_FAIR_GROUP_SCHED
  79. struct sched_entity_stats {
  80. struct sched_entity se;
  81. struct sched_statistics stats;
  82. } __no_randomize_layout;
  83. #endif
  84. static inline struct sched_statistics *
  85. __schedstats_from_se(struct sched_entity *se)
  86. {
  87. #ifdef CONFIG_FAIR_GROUP_SCHED
  88. if (!entity_is_task(se))
  89. return &container_of(se, struct sched_entity_stats, se)->stats;
  90. #endif
  91. return &task_of(se)->stats;
  92. }
  93. #ifdef CONFIG_PSI
  94. void psi_task_change(struct task_struct *task, int clear, int set);
  95. void psi_task_switch(struct task_struct *prev, struct task_struct *next,
  96. bool sleep);
  97. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  98. void psi_account_irqtime(struct rq *rq, struct task_struct *curr, struct task_struct *prev);
  99. #else
  100. static inline void psi_account_irqtime(struct rq *rq, struct task_struct *curr,
  101. struct task_struct *prev) {}
  102. #endif /*CONFIG_IRQ_TIME_ACCOUNTING */
  103. /*
  104. * PSI tracks state that persists across sleeps, such as iowaits and
  105. * memory stalls. As a result, it has to distinguish between sleeps,
  106. * where a task's runnable state changes, and migrations, where a task
  107. * and its runnable state are being moved between CPUs and runqueues.
  108. *
  109. * A notable case is a task whose dequeue is delayed. PSI considers
  110. * those sleeping, but because they are still on the runqueue they can
  111. * go through migration requeues. In this case, *sleeping* states need
  112. * to be transferred.
  113. */
  114. static inline void psi_enqueue(struct task_struct *p, int flags)
  115. {
  116. int clear = 0, set = 0;
  117. if (static_branch_likely(&psi_disabled))
  118. return;
  119. /* Same runqueue, nothing changed for psi */
  120. if (flags & ENQUEUE_RESTORE)
  121. return;
  122. /* psi_sched_switch() will handle the flags */
  123. if (task_on_cpu(task_rq(p), p))
  124. return;
  125. if (p->se.sched_delayed) {
  126. /* CPU migration of "sleeping" task */
  127. SCHED_WARN_ON(!(flags & ENQUEUE_MIGRATED));
  128. if (p->in_memstall)
  129. set |= TSK_MEMSTALL;
  130. if (p->in_iowait)
  131. set |= TSK_IOWAIT;
  132. } else if (flags & ENQUEUE_MIGRATED) {
  133. /* CPU migration of runnable task */
  134. set = TSK_RUNNING;
  135. if (p->in_memstall)
  136. set |= TSK_MEMSTALL | TSK_MEMSTALL_RUNNING;
  137. } else {
  138. /* Wakeup of new or sleeping task */
  139. if (p->in_iowait)
  140. clear |= TSK_IOWAIT;
  141. set = TSK_RUNNING;
  142. if (p->in_memstall)
  143. set |= TSK_MEMSTALL_RUNNING;
  144. }
  145. psi_task_change(p, clear, set);
  146. }
  147. static inline void psi_dequeue(struct task_struct *p, int flags)
  148. {
  149. if (static_branch_likely(&psi_disabled))
  150. return;
  151. /* Same runqueue, nothing changed for psi */
  152. if (flags & DEQUEUE_SAVE)
  153. return;
  154. /*
  155. * A voluntary sleep is a dequeue followed by a task switch. To
  156. * avoid walking all ancestors twice, psi_task_switch() handles
  157. * TSK_RUNNING and TSK_IOWAIT for us when it moves TSK_ONCPU.
  158. * Do nothing here.
  159. */
  160. if (flags & DEQUEUE_SLEEP)
  161. return;
  162. /*
  163. * When migrating a task to another CPU, clear all psi
  164. * state. The enqueue callback above will work it out.
  165. */
  166. psi_task_change(p, p->psi_flags, 0);
  167. }
  168. static inline void psi_ttwu_dequeue(struct task_struct *p)
  169. {
  170. if (static_branch_likely(&psi_disabled))
  171. return;
  172. /*
  173. * Is the task being migrated during a wakeup? Make sure to
  174. * deregister its sleep-persistent psi states from the old
  175. * queue, and let psi_enqueue() know it has to requeue.
  176. */
  177. if (unlikely(p->psi_flags)) {
  178. struct rq_flags rf;
  179. struct rq *rq;
  180. rq = __task_rq_lock(p, &rf);
  181. psi_task_change(p, p->psi_flags, 0);
  182. __task_rq_unlock(rq, &rf);
  183. }
  184. }
  185. static inline void psi_sched_switch(struct task_struct *prev,
  186. struct task_struct *next,
  187. bool sleep)
  188. {
  189. if (static_branch_likely(&psi_disabled))
  190. return;
  191. psi_task_switch(prev, next, sleep);
  192. }
  193. #else /* CONFIG_PSI */
  194. static inline void psi_enqueue(struct task_struct *p, bool migrate) {}
  195. static inline void psi_dequeue(struct task_struct *p, bool migrate) {}
  196. static inline void psi_ttwu_dequeue(struct task_struct *p) {}
  197. static inline void psi_sched_switch(struct task_struct *prev,
  198. struct task_struct *next,
  199. bool sleep) {}
  200. static inline void psi_account_irqtime(struct rq *rq, struct task_struct *curr,
  201. struct task_struct *prev) {}
  202. #endif /* CONFIG_PSI */
  203. #ifdef CONFIG_SCHED_INFO
  204. /*
  205. * We are interested in knowing how long it was from the *first* time a
  206. * task was queued to the time that it finally hit a CPU, we call this routine
  207. * from dequeue_task() to account for possible rq->clock skew across CPUs. The
  208. * delta taken on each CPU would annul the skew.
  209. */
  210. static inline void sched_info_dequeue(struct rq *rq, struct task_struct *t)
  211. {
  212. unsigned long long delta = 0;
  213. if (!t->sched_info.last_queued)
  214. return;
  215. delta = rq_clock(rq) - t->sched_info.last_queued;
  216. t->sched_info.last_queued = 0;
  217. t->sched_info.run_delay += delta;
  218. rq_sched_info_dequeue(rq, delta);
  219. }
  220. /*
  221. * Called when a task finally hits the CPU. We can now calculate how
  222. * long it was waiting to run. We also note when it began so that we
  223. * can keep stats on how long its time-slice is.
  224. */
  225. static void sched_info_arrive(struct rq *rq, struct task_struct *t)
  226. {
  227. unsigned long long now, delta = 0;
  228. if (!t->sched_info.last_queued)
  229. return;
  230. now = rq_clock(rq);
  231. delta = now - t->sched_info.last_queued;
  232. t->sched_info.last_queued = 0;
  233. t->sched_info.run_delay += delta;
  234. t->sched_info.last_arrival = now;
  235. t->sched_info.pcount++;
  236. rq_sched_info_arrive(rq, delta);
  237. }
  238. /*
  239. * This function is only called from enqueue_task(), but also only updates
  240. * the timestamp if it is already not set. It's assumed that
  241. * sched_info_dequeue() will clear that stamp when appropriate.
  242. */
  243. static inline void sched_info_enqueue(struct rq *rq, struct task_struct *t)
  244. {
  245. if (!t->sched_info.last_queued)
  246. t->sched_info.last_queued = rq_clock(rq);
  247. }
  248. /*
  249. * Called when a process ceases being the active-running process involuntarily
  250. * due, typically, to expiring its time slice (this may also be called when
  251. * switching to the idle task). Now we can calculate how long we ran.
  252. * Also, if the process is still in the TASK_RUNNING state, call
  253. * sched_info_enqueue() to mark that it has now again started waiting on
  254. * the runqueue.
  255. */
  256. static inline void sched_info_depart(struct rq *rq, struct task_struct *t)
  257. {
  258. unsigned long long delta = rq_clock(rq) - t->sched_info.last_arrival;
  259. rq_sched_info_depart(rq, delta);
  260. if (task_is_running(t))
  261. sched_info_enqueue(rq, t);
  262. }
  263. /*
  264. * Called when tasks are switched involuntarily due, typically, to expiring
  265. * their time slice. (This may also be called when switching to or from
  266. * the idle task.) We are only called when prev != next.
  267. */
  268. static inline void
  269. sched_info_switch(struct rq *rq, struct task_struct *prev, struct task_struct *next)
  270. {
  271. /*
  272. * prev now departs the CPU. It's not interesting to record
  273. * stats about how efficient we were at scheduling the idle
  274. * process, however.
  275. */
  276. if (prev != rq->idle)
  277. sched_info_depart(rq, prev);
  278. if (next != rq->idle)
  279. sched_info_arrive(rq, next);
  280. }
  281. #else /* !CONFIG_SCHED_INFO: */
  282. # define sched_info_enqueue(rq, t) do { } while (0)
  283. # define sched_info_dequeue(rq, t) do { } while (0)
  284. # define sched_info_switch(rq, t, next) do { } while (0)
  285. #endif /* CONFIG_SCHED_INFO */
  286. #endif /* _KERNEL_STATS_H */