sched_stats.h 7.1 KB

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