sched_stats.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION);
  11. seq_printf(seq, "timestamp %lu\n", jiffies);
  12. for_each_online_cpu(cpu) {
  13. struct rq *rq = cpu_rq(cpu);
  14. #ifdef CONFIG_SMP
  15. struct sched_domain *sd;
  16. int dcnt = 0;
  17. #endif
  18. /* runqueue-specific stats */
  19. seq_printf(seq,
  20. "cpu%d %lu %lu %lu %lu %lu %lu %lu %lu %lu %llu %llu %lu",
  21. cpu, rq->yld_both_empty,
  22. rq->yld_act_empty, rq->yld_exp_empty, rq->yld_cnt,
  23. rq->sched_switch, rq->sched_cnt, rq->sched_goidle,
  24. rq->ttwu_cnt, rq->ttwu_local,
  25. rq->rq_sched_info.cpu_time,
  26. rq->rq_sched_info.run_delay, rq->rq_sched_info.pcnt);
  27. seq_printf(seq, "\n");
  28. #ifdef CONFIG_SMP
  29. /* domain-specific stats */
  30. preempt_disable();
  31. for_each_domain(cpu, sd) {
  32. enum cpu_idle_type itype;
  33. char mask_str[NR_CPUS];
  34. cpumask_scnprintf(mask_str, NR_CPUS, sd->span);
  35. seq_printf(seq, "domain%d %s", dcnt++, mask_str);
  36. for (itype = CPU_IDLE; itype < CPU_MAX_IDLE_TYPES;
  37. itype++) {
  38. seq_printf(seq, " %lu %lu %lu %lu %lu %lu %lu "
  39. "%lu",
  40. sd->lb_cnt[itype],
  41. sd->lb_balanced[itype],
  42. sd->lb_failed[itype],
  43. sd->lb_imbalance[itype],
  44. sd->lb_gained[itype],
  45. sd->lb_hot_gained[itype],
  46. sd->lb_nobusyq[itype],
  47. sd->lb_nobusyg[itype]);
  48. }
  49. seq_printf(seq, " %lu %lu %lu %lu %lu %lu %lu %lu %lu"
  50. " %lu %lu %lu\n",
  51. sd->alb_cnt, sd->alb_failed, sd->alb_pushed,
  52. sd->sbe_cnt, sd->sbe_balanced, sd->sbe_pushed,
  53. sd->sbf_cnt, sd->sbf_balanced, sd->sbf_pushed,
  54. sd->ttwu_wake_remote, sd->ttwu_move_affine,
  55. sd->ttwu_move_balance);
  56. }
  57. preempt_enable();
  58. #endif
  59. }
  60. return 0;
  61. }
  62. static int schedstat_open(struct inode *inode, struct file *file)
  63. {
  64. unsigned int size = PAGE_SIZE * (1 + num_online_cpus() / 32);
  65. char *buf = kmalloc(size, GFP_KERNEL);
  66. struct seq_file *m;
  67. int res;
  68. if (!buf)
  69. return -ENOMEM;
  70. res = single_open(file, show_schedstat, NULL);
  71. if (!res) {
  72. m = file->private_data;
  73. m->buf = buf;
  74. m->size = size;
  75. } else
  76. kfree(buf);
  77. return res;
  78. }
  79. const struct file_operations proc_schedstat_operations = {
  80. .open = schedstat_open,
  81. .read = seq_read,
  82. .llseek = seq_lseek,
  83. .release = single_release,
  84. };
  85. /*
  86. * Expects runqueue lock to be held for atomicity of update
  87. */
  88. static inline void
  89. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  90. {
  91. if (rq) {
  92. rq->rq_sched_info.run_delay += delta;
  93. rq->rq_sched_info.pcnt++;
  94. }
  95. }
  96. /*
  97. * Expects runqueue lock to be held for atomicity of update
  98. */
  99. static inline void
  100. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  101. {
  102. if (rq)
  103. rq->rq_sched_info.cpu_time += delta;
  104. }
  105. # define schedstat_inc(rq, field) do { (rq)->field++; } while (0)
  106. # define schedstat_add(rq, field, amt) do { (rq)->field += (amt); } while (0)
  107. #else /* !CONFIG_SCHEDSTATS */
  108. static inline void
  109. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  110. {}
  111. static inline void
  112. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  113. {}
  114. # define schedstat_inc(rq, field) do { } while (0)
  115. # define schedstat_add(rq, field, amt) do { } while (0)
  116. #endif
  117. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  118. /*
  119. * Called when a process is dequeued from the active array and given
  120. * the cpu. We should note that with the exception of interactive
  121. * tasks, the expired queue will become the active queue after the active
  122. * queue is empty, without explicitly dequeuing and requeuing tasks in the
  123. * expired queue. (Interactive tasks may be requeued directly to the
  124. * active queue, thus delaying tasks in the expired queue from running;
  125. * see scheduler_tick()).
  126. *
  127. * This function is only called from sched_info_arrive(), rather than
  128. * dequeue_task(). Even though a task may be queued and dequeued multiple
  129. * times as it is shuffled about, we're really interested in knowing how
  130. * long it was from the *first* time it was queued to the time that it
  131. * finally hit a cpu.
  132. */
  133. static inline void sched_info_dequeued(struct task_struct *t)
  134. {
  135. t->sched_info.last_queued = 0;
  136. }
  137. /*
  138. * Called when a task finally hits the cpu. We can now calculate how
  139. * long it was waiting to run. We also note when it began so that we
  140. * can keep stats on how long its timeslice is.
  141. */
  142. static void sched_info_arrive(struct task_struct *t)
  143. {
  144. unsigned long long now = sched_clock(), delta = 0;
  145. if (t->sched_info.last_queued)
  146. delta = now - t->sched_info.last_queued;
  147. sched_info_dequeued(t);
  148. t->sched_info.run_delay += delta;
  149. t->sched_info.last_arrival = now;
  150. t->sched_info.pcnt++;
  151. rq_sched_info_arrive(task_rq(t), delta);
  152. }
  153. /*
  154. * Called when a process is queued into either the active or expired
  155. * array. The time is noted and later used to determine how long we
  156. * had to wait for us to reach the cpu. Since the expired queue will
  157. * become the active queue after active queue is empty, without dequeuing
  158. * and requeuing any tasks, we are interested in queuing to either. It
  159. * is unusual but not impossible for tasks to be dequeued and immediately
  160. * requeued in the same or another array: this can happen in sched_yield(),
  161. * set_user_nice(), and even load_balance() as it moves tasks from runqueue
  162. * to runqueue.
  163. *
  164. * This function is only called from enqueue_task(), but also only updates
  165. * the timestamp if it is already not set. It's assumed that
  166. * sched_info_dequeued() will clear that stamp when appropriate.
  167. */
  168. static inline void sched_info_queued(struct task_struct *t)
  169. {
  170. if (unlikely(sched_info_on()))
  171. if (!t->sched_info.last_queued)
  172. t->sched_info.last_queued = sched_clock();
  173. }
  174. /*
  175. * Called when a process ceases being the active-running process, either
  176. * voluntarily or involuntarily. Now we can calculate how long we ran.
  177. */
  178. static inline void sched_info_depart(struct task_struct *t)
  179. {
  180. unsigned long long delta = sched_clock() - t->sched_info.last_arrival;
  181. t->sched_info.cpu_time += delta;
  182. rq_sched_info_depart(task_rq(t), delta);
  183. }
  184. /*
  185. * Called when tasks are switched involuntarily due, typically, to expiring
  186. * their time slice. (This may also be called when switching to or from
  187. * the idle task.) We are only called when prev != next.
  188. */
  189. static inline void
  190. __sched_info_switch(struct task_struct *prev, struct task_struct *next)
  191. {
  192. struct rq *rq = task_rq(prev);
  193. /*
  194. * prev now departs the cpu. It's not interesting to record
  195. * stats about how efficient we were at scheduling the idle
  196. * process, however.
  197. */
  198. if (prev != rq->idle)
  199. sched_info_depart(prev);
  200. if (next != rq->idle)
  201. sched_info_arrive(next);
  202. }
  203. static inline void
  204. sched_info_switch(struct task_struct *prev, struct task_struct *next)
  205. {
  206. if (unlikely(sched_info_on()))
  207. __sched_info_switch(prev, next);
  208. }
  209. #else
  210. #define sched_info_queued(t) do { } while (0)
  211. #define sched_info_switch(t, next) do { } while (0)
  212. #endif /* CONFIG_SCHEDSTATS || CONFIG_TASK_DELAY_ACCT */