stats.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #ifdef CONFIG_SCHEDSTATS
  2. /*
  3. * Expects runqueue lock to be held for atomicity of update
  4. */
  5. static inline void
  6. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  7. {
  8. if (rq) {
  9. rq->rq_sched_info.run_delay += delta;
  10. rq->rq_sched_info.pcount++;
  11. }
  12. }
  13. /*
  14. * Expects runqueue lock to be held for atomicity of update
  15. */
  16. static inline void
  17. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  18. {
  19. if (rq)
  20. rq->rq_cpu_time += delta;
  21. }
  22. static inline void
  23. rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
  24. {
  25. if (rq)
  26. rq->rq_sched_info.run_delay += delta;
  27. }
  28. # define schedstat_enabled() static_branch_unlikely(&sched_schedstats)
  29. # define schedstat_inc(rq, field) do { if (schedstat_enabled()) { (rq)->field++; } } while (0)
  30. # define schedstat_add(rq, field, amt) do { if (schedstat_enabled()) { (rq)->field += (amt); } } while (0)
  31. # define schedstat_set(var, val) do { if (schedstat_enabled()) { var = (val); } } while (0)
  32. # define schedstat_val(rq, field) ((schedstat_enabled()) ? (rq)->field : 0)
  33. #else /* !CONFIG_SCHEDSTATS */
  34. static inline void
  35. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  36. {}
  37. static inline void
  38. rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
  39. {}
  40. static inline void
  41. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  42. {}
  43. # define schedstat_enabled() 0
  44. # define schedstat_inc(rq, field) do { } while (0)
  45. # define schedstat_add(rq, field, amt) do { } while (0)
  46. # define schedstat_set(var, val) do { } while (0)
  47. # define schedstat_val(rq, field) 0
  48. #endif
  49. #ifdef CONFIG_SCHED_INFO
  50. static inline void sched_info_reset_dequeued(struct task_struct *t)
  51. {
  52. t->sched_info.last_queued = 0;
  53. }
  54. /*
  55. * We are interested in knowing how long it was from the *first* time a
  56. * task was queued to the time that it finally hit a cpu, we call this routine
  57. * from dequeue_task() to account for possible rq->clock skew across cpus. The
  58. * delta taken on each cpu would annul the skew.
  59. */
  60. static inline void sched_info_dequeued(struct rq *rq, struct task_struct *t)
  61. {
  62. unsigned long long now = rq_clock(rq), delta = 0;
  63. if (unlikely(sched_info_on()))
  64. if (t->sched_info.last_queued)
  65. delta = now - t->sched_info.last_queued;
  66. sched_info_reset_dequeued(t);
  67. t->sched_info.run_delay += delta;
  68. rq_sched_info_dequeued(rq, delta);
  69. }
  70. /*
  71. * Called when a task finally hits the cpu. We can now calculate how
  72. * long it was waiting to run. We also note when it began so that we
  73. * can keep stats on how long its timeslice is.
  74. */
  75. static void sched_info_arrive(struct rq *rq, struct task_struct *t)
  76. {
  77. unsigned long long now = rq_clock(rq), delta = 0;
  78. if (t->sched_info.last_queued)
  79. delta = now - t->sched_info.last_queued;
  80. sched_info_reset_dequeued(t);
  81. t->sched_info.run_delay += delta;
  82. t->sched_info.last_arrival = now;
  83. t->sched_info.pcount++;
  84. rq_sched_info_arrive(rq, delta);
  85. }
  86. /*
  87. * This function is only called from enqueue_task(), but also only updates
  88. * the timestamp if it is already not set. It's assumed that
  89. * sched_info_dequeued() will clear that stamp when appropriate.
  90. */
  91. static inline void sched_info_queued(struct rq *rq, struct task_struct *t)
  92. {
  93. if (unlikely(sched_info_on()))
  94. if (!t->sched_info.last_queued)
  95. t->sched_info.last_queued = rq_clock(rq);
  96. }
  97. /*
  98. * Called when a process ceases being the active-running process involuntarily
  99. * due, typically, to expiring its time slice (this may also be called when
  100. * switching to the idle task). Now we can calculate how long we ran.
  101. * Also, if the process is still in the TASK_RUNNING state, call
  102. * sched_info_queued() to mark that it has now again started waiting on
  103. * the runqueue.
  104. */
  105. static inline void sched_info_depart(struct rq *rq, struct task_struct *t)
  106. {
  107. unsigned long long delta = rq_clock(rq) -
  108. t->sched_info.last_arrival;
  109. rq_sched_info_depart(rq, delta);
  110. if (t->state == TASK_RUNNING)
  111. sched_info_queued(rq, t);
  112. }
  113. /*
  114. * Called when tasks are switched involuntarily due, typically, to expiring
  115. * their time slice. (This may also be called when switching to or from
  116. * the idle task.) We are only called when prev != next.
  117. */
  118. static inline void
  119. __sched_info_switch(struct rq *rq,
  120. struct task_struct *prev, struct task_struct *next)
  121. {
  122. /*
  123. * prev now departs the cpu. It's not interesting to record
  124. * stats about how efficient we were at scheduling the idle
  125. * process, however.
  126. */
  127. if (prev != rq->idle)
  128. sched_info_depart(rq, prev);
  129. if (next != rq->idle)
  130. sched_info_arrive(rq, next);
  131. }
  132. static inline void
  133. sched_info_switch(struct rq *rq,
  134. struct task_struct *prev, struct task_struct *next)
  135. {
  136. if (unlikely(sched_info_on()))
  137. __sched_info_switch(rq, prev, next);
  138. }
  139. #else
  140. #define sched_info_queued(rq, t) do { } while (0)
  141. #define sched_info_reset_dequeued(t) do { } while (0)
  142. #define sched_info_dequeued(rq, t) do { } while (0)
  143. #define sched_info_depart(rq, t) do { } while (0)
  144. #define sched_info_arrive(rq, next) do { } while (0)
  145. #define sched_info_switch(rq, t, next) do { } while (0)
  146. #endif /* CONFIG_SCHED_INFO */
  147. /*
  148. * The following are functions that support scheduler-internal time accounting.
  149. * These functions are generally called at the timer tick. None of this depends
  150. * on CONFIG_SCHEDSTATS.
  151. */
  152. /**
  153. * cputimer_running - return true if cputimer is running
  154. *
  155. * @tsk: Pointer to target task.
  156. */
  157. static inline bool cputimer_running(struct task_struct *tsk)
  158. {
  159. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  160. /* Check if cputimer isn't running. This is accessed without locking. */
  161. if (!READ_ONCE(cputimer->running))
  162. return false;
  163. /*
  164. * After we flush the task's sum_exec_runtime to sig->sum_sched_runtime
  165. * in __exit_signal(), we won't account to the signal struct further
  166. * cputime consumed by that task, even though the task can still be
  167. * ticking after __exit_signal().
  168. *
  169. * In order to keep a consistent behaviour between thread group cputime
  170. * and thread group cputimer accounting, lets also ignore the cputime
  171. * elapsing after __exit_signal() in any thread group timer running.
  172. *
  173. * This makes sure that POSIX CPU clocks and timers are synchronized, so
  174. * that a POSIX CPU timer won't expire while the corresponding POSIX CPU
  175. * clock delta is behind the expiring timer value.
  176. */
  177. if (unlikely(!tsk->sighand))
  178. return false;
  179. return true;
  180. }
  181. /**
  182. * account_group_user_time - Maintain utime for a thread group.
  183. *
  184. * @tsk: Pointer to task structure.
  185. * @cputime: Time value by which to increment the utime field of the
  186. * thread_group_cputime structure.
  187. *
  188. * If thread group time is being maintained, get the structure for the
  189. * running CPU and update the utime field there.
  190. */
  191. static inline void account_group_user_time(struct task_struct *tsk,
  192. cputime_t cputime)
  193. {
  194. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  195. if (!cputimer_running(tsk))
  196. return;
  197. atomic64_add(cputime, &cputimer->cputime_atomic.utime);
  198. }
  199. /**
  200. * account_group_system_time - Maintain stime for a thread group.
  201. *
  202. * @tsk: Pointer to task structure.
  203. * @cputime: Time value by which to increment the stime field of the
  204. * thread_group_cputime structure.
  205. *
  206. * If thread group time is being maintained, get the structure for the
  207. * running CPU and update the stime field there.
  208. */
  209. static inline void account_group_system_time(struct task_struct *tsk,
  210. cputime_t cputime)
  211. {
  212. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  213. if (!cputimer_running(tsk))
  214. return;
  215. atomic64_add(cputime, &cputimer->cputime_atomic.stime);
  216. }
  217. /**
  218. * account_group_exec_runtime - Maintain exec runtime for a thread group.
  219. *
  220. * @tsk: Pointer to task structure.
  221. * @ns: Time value by which to increment the sum_exec_runtime field
  222. * of the thread_group_cputime structure.
  223. *
  224. * If thread group time is being maintained, get the structure for the
  225. * running CPU and update the sum_exec_runtime field there.
  226. */
  227. static inline void account_group_exec_runtime(struct task_struct *tsk,
  228. unsigned long long ns)
  229. {
  230. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  231. if (!cputimer_running(tsk))
  232. return;
  233. atomic64_add(ns, &cputimer->cputime_atomic.sum_exec_runtime);
  234. }