stats.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 = task_rq(t)->clock, 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 = task_rq(t)->clock, 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 = task_rq(t)->clock;
  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 = task_rq(t)->clock -
  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. * account_group_user_time - Maintain utime for a thread group.
  146. *
  147. * @tsk: Pointer to task structure.
  148. * @cputime: Time value by which to increment the utime field of the
  149. * thread_group_cputime structure.
  150. *
  151. * If thread group time is being maintained, get the structure for the
  152. * running CPU and update the utime field there.
  153. */
  154. static inline void account_group_user_time(struct task_struct *tsk,
  155. cputime_t cputime)
  156. {
  157. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  158. if (!cputimer->running)
  159. return;
  160. raw_spin_lock(&cputimer->lock);
  161. cputimer->cputime.utime += cputime;
  162. raw_spin_unlock(&cputimer->lock);
  163. }
  164. /**
  165. * account_group_system_time - Maintain stime for a thread group.
  166. *
  167. * @tsk: Pointer to task structure.
  168. * @cputime: Time value by which to increment the stime field of the
  169. * thread_group_cputime structure.
  170. *
  171. * If thread group time is being maintained, get the structure for the
  172. * running CPU and update the stime field there.
  173. */
  174. static inline void account_group_system_time(struct task_struct *tsk,
  175. cputime_t cputime)
  176. {
  177. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  178. if (!cputimer->running)
  179. return;
  180. raw_spin_lock(&cputimer->lock);
  181. cputimer->cputime.stime += cputime;
  182. raw_spin_unlock(&cputimer->lock);
  183. }
  184. /**
  185. * account_group_exec_runtime - Maintain exec runtime for a thread group.
  186. *
  187. * @tsk: Pointer to task structure.
  188. * @ns: Time value by which to increment the sum_exec_runtime field
  189. * of the thread_group_cputime structure.
  190. *
  191. * If thread group time is being maintained, get the structure for the
  192. * running CPU and update the sum_exec_runtime field there.
  193. */
  194. static inline void account_group_exec_runtime(struct task_struct *tsk,
  195. unsigned long long ns)
  196. {
  197. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  198. if (!cputimer->running)
  199. return;
  200. raw_spin_lock(&cputimer->lock);
  201. cputimer->cputime.sum_exec_runtime += ns;
  202. raw_spin_unlock(&cputimer->lock);
  203. }