sched_stats.h 7.4 KB

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