sched_stats.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #ifdef CONFIG_SCHEDSTATS
  2. /*
  3. * bump this up when changing the output format or the meaning of an existing
  4. * format, so that tools can adapt (or abort)
  5. */
  6. #define SCHEDSTAT_VERSION 14
  7. static int show_schedstat(struct seq_file *seq, void *v)
  8. {
  9. int cpu;
  10. int mask_len = DIV_ROUND_UP(NR_CPUS, 32) * 9;
  11. char *mask_str = kmalloc(mask_len, GFP_KERNEL);
  12. if (mask_str == NULL)
  13. return -ENOMEM;
  14. seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION);
  15. seq_printf(seq, "timestamp %lu\n", jiffies);
  16. for_each_online_cpu(cpu) {
  17. struct rq *rq = cpu_rq(cpu);
  18. #ifdef CONFIG_SMP
  19. struct sched_domain *sd;
  20. int dcount = 0;
  21. #endif
  22. /* runqueue-specific stats */
  23. seq_printf(seq,
  24. "cpu%d %u %u %u %u %u %u %u %u %u %llu %llu %lu",
  25. cpu, rq->yld_both_empty,
  26. rq->yld_act_empty, rq->yld_exp_empty, rq->yld_count,
  27. rq->sched_switch, rq->sched_count, rq->sched_goidle,
  28. rq->ttwu_count, rq->ttwu_local,
  29. rq->rq_cpu_time,
  30. rq->rq_sched_info.run_delay, rq->rq_sched_info.pcount);
  31. seq_printf(seq, "\n");
  32. #ifdef CONFIG_SMP
  33. /* domain-specific stats */
  34. preempt_disable();
  35. for_each_domain(cpu, sd) {
  36. enum cpu_idle_type itype;
  37. cpumask_scnprintf(mask_str, mask_len,
  38. sched_domain_span(sd));
  39. seq_printf(seq, "domain%d %s", dcount++, mask_str);
  40. for (itype = CPU_IDLE; itype < CPU_MAX_IDLE_TYPES;
  41. itype++) {
  42. seq_printf(seq, " %u %u %u %u %u %u %u %u",
  43. sd->lb_count[itype],
  44. sd->lb_balanced[itype],
  45. sd->lb_failed[itype],
  46. sd->lb_imbalance[itype],
  47. sd->lb_gained[itype],
  48. sd->lb_hot_gained[itype],
  49. sd->lb_nobusyq[itype],
  50. sd->lb_nobusyg[itype]);
  51. }
  52. seq_printf(seq,
  53. " %u %u %u %u %u %u %u %u %u %u %u %u\n",
  54. sd->alb_count, sd->alb_failed, sd->alb_pushed,
  55. sd->sbe_count, sd->sbe_balanced, sd->sbe_pushed,
  56. sd->sbf_count, sd->sbf_balanced, sd->sbf_pushed,
  57. sd->ttwu_wake_remote, sd->ttwu_move_affine,
  58. sd->ttwu_move_balance);
  59. }
  60. preempt_enable();
  61. #endif
  62. }
  63. kfree(mask_str);
  64. return 0;
  65. }
  66. static int schedstat_open(struct inode *inode, struct file *file)
  67. {
  68. unsigned int size = PAGE_SIZE * (1 + num_online_cpus() / 32);
  69. char *buf = kmalloc(size, GFP_KERNEL);
  70. struct seq_file *m;
  71. int res;
  72. if (!buf)
  73. return -ENOMEM;
  74. res = single_open(file, show_schedstat, NULL);
  75. if (!res) {
  76. m = file->private_data;
  77. m->buf = buf;
  78. m->size = size;
  79. } else
  80. kfree(buf);
  81. return res;
  82. }
  83. static const struct file_operations proc_schedstat_operations = {
  84. .open = schedstat_open,
  85. .read = seq_read,
  86. .llseek = seq_lseek,
  87. .release = single_release,
  88. };
  89. static int __init proc_schedstat_init(void)
  90. {
  91. proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
  92. return 0;
  93. }
  94. module_init(proc_schedstat_init);
  95. /*
  96. * Expects runqueue lock to be held for atomicity of update
  97. */
  98. static inline void
  99. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  100. {
  101. if (rq) {
  102. rq->rq_sched_info.run_delay += delta;
  103. rq->rq_sched_info.pcount++;
  104. }
  105. }
  106. /*
  107. * Expects runqueue lock to be held for atomicity of update
  108. */
  109. static inline void
  110. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  111. {
  112. if (rq)
  113. rq->rq_cpu_time += delta;
  114. }
  115. static inline void
  116. rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
  117. {
  118. if (rq)
  119. rq->rq_sched_info.run_delay += delta;
  120. }
  121. # define schedstat_inc(rq, field) do { (rq)->field++; } while (0)
  122. # define schedstat_add(rq, field, amt) do { (rq)->field += (amt); } while (0)
  123. # define schedstat_set(var, val) do { var = (val); } while (0)
  124. #else /* !CONFIG_SCHEDSTATS */
  125. static inline void
  126. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  127. {}
  128. static inline void
  129. rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
  130. {}
  131. static inline void
  132. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  133. {}
  134. # define schedstat_inc(rq, field) do { } while (0)
  135. # define schedstat_add(rq, field, amt) do { } while (0)
  136. # define schedstat_set(var, val) do { } while (0)
  137. #endif
  138. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  139. static inline void sched_info_reset_dequeued(struct task_struct *t)
  140. {
  141. t->sched_info.last_queued = 0;
  142. }
  143. /*
  144. * Called when a process is dequeued from the active array and given
  145. * the cpu. We should note that with the exception of interactive
  146. * tasks, the expired queue will become the active queue after the active
  147. * queue is empty, without explicitly dequeuing and requeuing tasks in the
  148. * expired queue. (Interactive tasks may be requeued directly to the
  149. * active queue, thus delaying tasks in the expired queue from running;
  150. * see scheduler_tick()).
  151. *
  152. * Though we are interested in knowing how long it was from the *first* time a
  153. * task was queued to the time that it finally hit a cpu, we call this routine
  154. * from dequeue_task() to account for possible rq->clock skew across cpus. The
  155. * delta taken on each cpu would annul the skew.
  156. */
  157. static inline void sched_info_dequeued(struct task_struct *t)
  158. {
  159. unsigned long long now = task_rq(t)->clock, delta = 0;
  160. if (unlikely(sched_info_on()))
  161. if (t->sched_info.last_queued)
  162. delta = now - t->sched_info.last_queued;
  163. sched_info_reset_dequeued(t);
  164. t->sched_info.run_delay += delta;
  165. rq_sched_info_dequeued(task_rq(t), delta);
  166. }
  167. /*
  168. * Called when a task finally hits the cpu. We can now calculate how
  169. * long it was waiting to run. We also note when it began so that we
  170. * can keep stats on how long its timeslice is.
  171. */
  172. static void sched_info_arrive(struct task_struct *t)
  173. {
  174. unsigned long long now = task_rq(t)->clock, delta = 0;
  175. if (t->sched_info.last_queued)
  176. delta = now - t->sched_info.last_queued;
  177. sched_info_reset_dequeued(t);
  178. t->sched_info.run_delay += delta;
  179. t->sched_info.last_arrival = now;
  180. t->sched_info.pcount++;
  181. rq_sched_info_arrive(task_rq(t), delta);
  182. }
  183. /*
  184. * Called when a process is queued into either the active or expired
  185. * array. The time is noted and later used to determine how long we
  186. * had to wait for us to reach the cpu. Since the expired queue will
  187. * become the active queue after active queue is empty, without dequeuing
  188. * and requeuing any tasks, we are interested in queuing to either. It
  189. * is unusual but not impossible for tasks to be dequeued and immediately
  190. * requeued in the same or another array: this can happen in sched_yield(),
  191. * set_user_nice(), and even load_balance() as it moves tasks from runqueue
  192. * to runqueue.
  193. *
  194. * This function is only called from enqueue_task(), but also only updates
  195. * the timestamp if it is already not set. It's assumed that
  196. * sched_info_dequeued() will clear that stamp when appropriate.
  197. */
  198. static inline void sched_info_queued(struct task_struct *t)
  199. {
  200. if (unlikely(sched_info_on()))
  201. if (!t->sched_info.last_queued)
  202. t->sched_info.last_queued = task_rq(t)->clock;
  203. }
  204. /*
  205. * Called when a process ceases being the active-running process, either
  206. * voluntarily or involuntarily. Now we can calculate how long we ran.
  207. * Also, if the process is still in the TASK_RUNNING state, call
  208. * sched_info_queued() to mark that it has now again started waiting on
  209. * the runqueue.
  210. */
  211. static inline void sched_info_depart(struct task_struct *t)
  212. {
  213. unsigned long long delta = task_rq(t)->clock -
  214. t->sched_info.last_arrival;
  215. rq_sched_info_depart(task_rq(t), delta);
  216. if (t->state == TASK_RUNNING)
  217. sched_info_queued(t);
  218. }
  219. /*
  220. * Called when tasks are switched involuntarily due, typically, to expiring
  221. * their time slice. (This may also be called when switching to or from
  222. * the idle task.) We are only called when prev != next.
  223. */
  224. static inline void
  225. __sched_info_switch(struct task_struct *prev, struct task_struct *next)
  226. {
  227. struct rq *rq = task_rq(prev);
  228. /*
  229. * prev now departs the cpu. It's not interesting to record
  230. * stats about how efficient we were at scheduling the idle
  231. * process, however.
  232. */
  233. if (prev != rq->idle)
  234. sched_info_depart(prev);
  235. if (next != rq->idle)
  236. sched_info_arrive(next);
  237. }
  238. static inline void
  239. sched_info_switch(struct task_struct *prev, struct task_struct *next)
  240. {
  241. if (unlikely(sched_info_on()))
  242. __sched_info_switch(prev, next);
  243. }
  244. #else
  245. #define sched_info_queued(t) do { } while (0)
  246. #define sched_info_reset_dequeued(t) do { } while (0)
  247. #define sched_info_dequeued(t) do { } while (0)
  248. #define sched_info_switch(t, next) do { } while (0)
  249. #endif /* CONFIG_SCHEDSTATS || CONFIG_TASK_DELAY_ACCT */
  250. /*
  251. * The following are functions that support scheduler-internal time accounting.
  252. * These functions are generally called at the timer tick. None of this depends
  253. * on CONFIG_SCHEDSTATS.
  254. */
  255. /**
  256. * account_group_user_time - Maintain utime for a thread group.
  257. *
  258. * @tsk: Pointer to task structure.
  259. * @cputime: Time value by which to increment the utime field of the
  260. * thread_group_cputime structure.
  261. *
  262. * If thread group time is being maintained, get the structure for the
  263. * running CPU and update the utime field there.
  264. */
  265. static inline void account_group_user_time(struct task_struct *tsk,
  266. cputime_t cputime)
  267. {
  268. struct thread_group_cputimer *cputimer;
  269. /* tsk == current, ensure it is safe to use ->signal */
  270. if (unlikely(tsk->exit_state))
  271. return;
  272. cputimer = &tsk->signal->cputimer;
  273. if (!cputimer->running)
  274. return;
  275. spin_lock(&cputimer->lock);
  276. cputimer->cputime.utime =
  277. cputime_add(cputimer->cputime.utime, cputime);
  278. spin_unlock(&cputimer->lock);
  279. }
  280. /**
  281. * account_group_system_time - Maintain stime for a thread group.
  282. *
  283. * @tsk: Pointer to task structure.
  284. * @cputime: Time value by which to increment the stime field of the
  285. * thread_group_cputime structure.
  286. *
  287. * If thread group time is being maintained, get the structure for the
  288. * running CPU and update the stime field there.
  289. */
  290. static inline void account_group_system_time(struct task_struct *tsk,
  291. cputime_t cputime)
  292. {
  293. struct thread_group_cputimer *cputimer;
  294. /* tsk == current, ensure it is safe to use ->signal */
  295. if (unlikely(tsk->exit_state))
  296. return;
  297. cputimer = &tsk->signal->cputimer;
  298. if (!cputimer->running)
  299. return;
  300. spin_lock(&cputimer->lock);
  301. cputimer->cputime.stime =
  302. cputime_add(cputimer->cputime.stime, cputime);
  303. spin_unlock(&cputimer->lock);
  304. }
  305. /**
  306. * account_group_exec_runtime - Maintain exec runtime for a thread group.
  307. *
  308. * @tsk: Pointer to task structure.
  309. * @ns: Time value by which to increment the sum_exec_runtime field
  310. * of the thread_group_cputime structure.
  311. *
  312. * If thread group time is being maintained, get the structure for the
  313. * running CPU and update the sum_exec_runtime field there.
  314. */
  315. static inline void account_group_exec_runtime(struct task_struct *tsk,
  316. unsigned long long ns)
  317. {
  318. struct thread_group_cputimer *cputimer;
  319. struct signal_struct *sig;
  320. sig = tsk->signal;
  321. /* see __exit_signal()->task_rq_unlock_wait() */
  322. barrier();
  323. if (unlikely(!sig))
  324. return;
  325. cputimer = &sig->cputimer;
  326. if (!cputimer->running)
  327. return;
  328. spin_lock(&cputimer->lock);
  329. cputimer->cputime.sum_exec_runtime += ns;
  330. spin_unlock(&cputimer->lock);
  331. }