stats.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  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 task_struct *t)
  57. {
  58. unsigned long long now = rq_clock(task_rq(t)), 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(task_rq(t), 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 task_struct *t)
  72. {
  73. unsigned long long now = rq_clock(task_rq(t)), 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(task_rq(t), 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 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(task_rq(t));
  92. }
  93. /*
  94. * Called when a process ceases being the active-running process, either
  95. * voluntarily or involuntarily. Now we can calculate how long we ran.
  96. * Also, if the process is still in the TASK_RUNNING state, call
  97. * sched_info_queued() to mark that it has now again started waiting on
  98. * the runqueue.
  99. */
  100. static inline void sched_info_depart(struct task_struct *t)
  101. {
  102. unsigned long long delta = rq_clock(task_rq(t)) -
  103. t->sched_info.last_arrival;
  104. rq_sched_info_depart(task_rq(t), delta);
  105. if (t->state == TASK_RUNNING)
  106. sched_info_queued(t);
  107. }
  108. /*
  109. * Called when tasks are switched involuntarily due, typically, to expiring
  110. * their time slice. (This may also be called when switching to or from
  111. * the idle task.) We are only called when prev != next.
  112. */
  113. static inline void
  114. __sched_info_switch(struct task_struct *prev, struct task_struct *next)
  115. {
  116. struct rq *rq = task_rq(prev);
  117. /*
  118. * prev now departs the cpu. It's not interesting to record
  119. * stats about how efficient we were at scheduling the idle
  120. * process, however.
  121. */
  122. if (prev != rq->idle)
  123. sched_info_depart(prev);
  124. if (next != rq->idle)
  125. sched_info_arrive(next);
  126. }
  127. static inline void
  128. sched_info_switch(struct task_struct *prev, struct task_struct *next)
  129. {
  130. if (unlikely(sched_info_on()))
  131. __sched_info_switch(prev, next);
  132. }
  133. #else
  134. #define sched_info_queued(t) do { } while (0)
  135. #define sched_info_reset_dequeued(t) do { } while (0)
  136. #define sched_info_dequeued(t) do { } while (0)
  137. #define sched_info_switch(t, next) do { } while (0)
  138. #endif /* CONFIG_SCHEDSTATS || CONFIG_TASK_DELAY_ACCT */
  139. /*
  140. * The following are functions that support scheduler-internal time accounting.
  141. * These functions are generally called at the timer tick. None of this depends
  142. * on CONFIG_SCHEDSTATS.
  143. */
  144. /**
  145. * cputimer_running - return true if cputimer is running
  146. *
  147. * @tsk: Pointer to target task.
  148. */
  149. static inline bool cputimer_running(struct task_struct *tsk)
  150. {
  151. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  152. if (!cputimer->running)
  153. return false;
  154. /*
  155. * After we flush the task's sum_exec_runtime to sig->sum_sched_runtime
  156. * in __exit_signal(), we won't account to the signal struct further
  157. * cputime consumed by that task, even though the task can still be
  158. * ticking after __exit_signal().
  159. *
  160. * In order to keep a consistent behaviour between thread group cputime
  161. * and thread group cputimer accounting, lets also ignore the cputime
  162. * elapsing after __exit_signal() in any thread group timer running.
  163. *
  164. * This makes sure that POSIX CPU clocks and timers are synchronized, so
  165. * that a POSIX CPU timer won't expire while the corresponding POSIX CPU
  166. * clock delta is behind the expiring timer value.
  167. */
  168. if (unlikely(!tsk->sighand))
  169. return false;
  170. return true;
  171. }
  172. /**
  173. * account_group_user_time - Maintain utime for a thread group.
  174. *
  175. * @tsk: Pointer to task structure.
  176. * @cputime: Time value by which to increment the utime field of the
  177. * thread_group_cputime structure.
  178. *
  179. * If thread group time is being maintained, get the structure for the
  180. * running CPU and update the utime field there.
  181. */
  182. static inline void account_group_user_time(struct task_struct *tsk,
  183. cputime_t cputime)
  184. {
  185. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  186. if (!cputimer_running(tsk))
  187. return;
  188. raw_spin_lock(&cputimer->lock);
  189. cputimer->cputime.utime += cputime;
  190. raw_spin_unlock(&cputimer->lock);
  191. }
  192. /**
  193. * account_group_system_time - Maintain stime for a thread group.
  194. *
  195. * @tsk: Pointer to task structure.
  196. * @cputime: Time value by which to increment the stime field of the
  197. * thread_group_cputime structure.
  198. *
  199. * If thread group time is being maintained, get the structure for the
  200. * running CPU and update the stime field there.
  201. */
  202. static inline void account_group_system_time(struct task_struct *tsk,
  203. cputime_t cputime)
  204. {
  205. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  206. if (!cputimer_running(tsk))
  207. return;
  208. raw_spin_lock(&cputimer->lock);
  209. cputimer->cputime.stime += cputime;
  210. raw_spin_unlock(&cputimer->lock);
  211. }
  212. /**
  213. * account_group_exec_runtime - Maintain exec runtime for a thread group.
  214. *
  215. * @tsk: Pointer to task structure.
  216. * @ns: Time value by which to increment the sum_exec_runtime field
  217. * of the thread_group_cputime structure.
  218. *
  219. * If thread group time is being maintained, get the structure for the
  220. * running CPU and update the sum_exec_runtime field there.
  221. */
  222. static inline void account_group_exec_runtime(struct task_struct *tsk,
  223. unsigned long long ns)
  224. {
  225. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  226. if (!cputimer_running(tsk))
  227. return;
  228. raw_spin_lock(&cputimer->lock);
  229. cputimer->cputime.sum_exec_runtime += ns;
  230. raw_spin_unlock(&cputimer->lock);
  231. }