sched_stats.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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, " %u %u %u %u %u %u %u %u %u %u %u %u\n",
  49. sd->alb_count, sd->alb_failed, sd->alb_pushed,
  50. sd->sbe_count, sd->sbe_balanced, sd->sbe_pushed,
  51. sd->sbf_count, sd->sbf_balanced, sd->sbf_pushed,
  52. sd->ttwu_wake_remote, sd->ttwu_move_affine,
  53. sd->ttwu_move_balance);
  54. }
  55. preempt_enable();
  56. #endif
  57. }
  58. return 0;
  59. }
  60. static int schedstat_open(struct inode *inode, struct file *file)
  61. {
  62. unsigned int size = PAGE_SIZE * (1 + num_online_cpus() / 32);
  63. char *buf = kmalloc(size, GFP_KERNEL);
  64. struct seq_file *m;
  65. int res;
  66. if (!buf)
  67. return -ENOMEM;
  68. res = single_open(file, show_schedstat, NULL);
  69. if (!res) {
  70. m = file->private_data;
  71. m->buf = buf;
  72. m->size = size;
  73. } else
  74. kfree(buf);
  75. return res;
  76. }
  77. const struct file_operations proc_schedstat_operations = {
  78. .open = schedstat_open,
  79. .read = seq_read,
  80. .llseek = seq_lseek,
  81. .release = single_release,
  82. };
  83. /*
  84. * Expects runqueue lock to be held for atomicity of update
  85. */
  86. static inline void
  87. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  88. {
  89. if (rq) {
  90. rq->rq_sched_info.run_delay += delta;
  91. rq->rq_sched_info.pcount++;
  92. }
  93. }
  94. /*
  95. * Expects runqueue lock to be held for atomicity of update
  96. */
  97. static inline void
  98. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  99. {
  100. if (rq)
  101. rq->rq_sched_info.cpu_time += delta;
  102. }
  103. # define schedstat_inc(rq, field) do { (rq)->field++; } while (0)
  104. # define schedstat_add(rq, field, amt) do { (rq)->field += (amt); } while (0)
  105. # define schedstat_set(var, val) do { var = (val); } while (0)
  106. #else /* !CONFIG_SCHEDSTATS */
  107. static inline void
  108. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  109. {}
  110. static inline void
  111. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  112. {}
  113. # define schedstat_inc(rq, field) do { } while (0)
  114. # define schedstat_add(rq, field, amt) do { } while (0)
  115. # define schedstat_set(var, val) 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 = task_rq(t)->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.pcount++;
  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 = task_rq(t)->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 = task_rq(t)->clock -
  181. t->sched_info.last_arrival;
  182. t->sched_info.cpu_time += delta;
  183. rq_sched_info_depart(task_rq(t), delta);
  184. }
  185. /*
  186. * Called when tasks are switched involuntarily due, typically, to expiring
  187. * their time slice. (This may also be called when switching to or from
  188. * the idle task.) We are only called when prev != next.
  189. */
  190. static inline void
  191. __sched_info_switch(struct task_struct *prev, struct task_struct *next)
  192. {
  193. struct rq *rq = task_rq(prev);
  194. /*
  195. * prev now departs the cpu. It's not interesting to record
  196. * stats about how efficient we were at scheduling the idle
  197. * process, however.
  198. */
  199. if (prev != rq->idle)
  200. sched_info_depart(prev);
  201. if (next != rq->idle)
  202. sched_info_arrive(next);
  203. }
  204. static inline void
  205. sched_info_switch(struct task_struct *prev, struct task_struct *next)
  206. {
  207. if (unlikely(sched_info_on()))
  208. __sched_info_switch(prev, next);
  209. }
  210. #else
  211. #define sched_info_queued(t) do { } while (0)
  212. #define sched_info_switch(t, next) do { } while (0)
  213. #endif /* CONFIG_SCHEDSTATS || CONFIG_TASK_DELAY_ACCT */