stats.h 7.7 KB

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