sched_debug.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * kernel/time/sched_debug.c
  3. *
  4. * Print the CFS rbtree
  5. *
  6. * Copyright(C) 2007, Red Hat, Inc., Ingo Molnar
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/proc_fs.h>
  13. #include <linux/sched.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/utsname.h>
  17. typedef void (*print_fn_t)(struct seq_file *m, unsigned int *classes);
  18. /*
  19. * This allows printing both to /proc/sched_debug and
  20. * to the console
  21. */
  22. #define SEQ_printf(m, x...) \
  23. do { \
  24. if (m) \
  25. seq_printf(m, x); \
  26. else \
  27. printk(x); \
  28. } while (0)
  29. static void
  30. print_task(struct seq_file *m, struct rq *rq, struct task_struct *p, u64 now)
  31. {
  32. if (rq->curr == p)
  33. SEQ_printf(m, "R");
  34. else
  35. SEQ_printf(m, " ");
  36. SEQ_printf(m, "%15s %5d %15Ld %13Ld %13Ld %9Ld %5d "
  37. "%15Ld %15Ld %15Ld %15Ld %15Ld\n",
  38. p->comm, p->pid,
  39. (long long)p->se.fair_key,
  40. (long long)(p->se.fair_key - rq->cfs.fair_clock),
  41. (long long)p->se.wait_runtime,
  42. (long long)(p->nvcsw + p->nivcsw),
  43. p->prio,
  44. (long long)p->se.sum_exec_runtime,
  45. (long long)p->se.sum_wait_runtime,
  46. (long long)p->se.sum_sleep_runtime,
  47. (long long)p->se.wait_runtime_overruns,
  48. (long long)p->se.wait_runtime_underruns);
  49. }
  50. static void print_rq(struct seq_file *m, struct rq *rq, int rq_cpu, u64 now)
  51. {
  52. struct task_struct *g, *p;
  53. SEQ_printf(m,
  54. "\nrunnable tasks:\n"
  55. " task PID tree-key delta waiting"
  56. " switches prio"
  57. " sum-exec sum-wait sum-sleep"
  58. " wait-overrun wait-underrun\n"
  59. "------------------------------------------------------------------"
  60. "----------------"
  61. "------------------------------------------------"
  62. "--------------------------------\n");
  63. read_lock_irq(&tasklist_lock);
  64. do_each_thread(g, p) {
  65. if (!p->se.on_rq || task_cpu(p) != rq_cpu)
  66. continue;
  67. print_task(m, rq, p, now);
  68. } while_each_thread(g, p);
  69. read_unlock_irq(&tasklist_lock);
  70. }
  71. static void
  72. print_cfs_rq_runtime_sum(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
  73. {
  74. s64 wait_runtime_rq_sum = 0;
  75. struct task_struct *p;
  76. struct rb_node *curr;
  77. unsigned long flags;
  78. struct rq *rq = &per_cpu(runqueues, cpu);
  79. spin_lock_irqsave(&rq->lock, flags);
  80. curr = first_fair(cfs_rq);
  81. while (curr) {
  82. p = rb_entry(curr, struct task_struct, se.run_node);
  83. wait_runtime_rq_sum += p->se.wait_runtime;
  84. curr = rb_next(curr);
  85. }
  86. spin_unlock_irqrestore(&rq->lock, flags);
  87. SEQ_printf(m, " .%-30s: %Ld\n", "wait_runtime_rq_sum",
  88. (long long)wait_runtime_rq_sum);
  89. }
  90. void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq, u64 now)
  91. {
  92. SEQ_printf(m, "\ncfs_rq %p\n", cfs_rq);
  93. #define P(x) \
  94. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(cfs_rq->x))
  95. P(fair_clock);
  96. P(exec_clock);
  97. P(wait_runtime);
  98. P(wait_runtime_overruns);
  99. P(wait_runtime_underruns);
  100. P(sleeper_bonus);
  101. #undef P
  102. print_cfs_rq_runtime_sum(m, cpu, cfs_rq);
  103. }
  104. static void print_cpu(struct seq_file *m, int cpu, u64 now)
  105. {
  106. struct rq *rq = &per_cpu(runqueues, cpu);
  107. #ifdef CONFIG_X86
  108. {
  109. unsigned int freq = cpu_khz ? : 1;
  110. SEQ_printf(m, "\ncpu#%d, %u.%03u MHz\n",
  111. cpu, freq / 1000, (freq % 1000));
  112. }
  113. #else
  114. SEQ_printf(m, "\ncpu#%d\n", cpu);
  115. #endif
  116. #define P(x) \
  117. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rq->x))
  118. P(nr_running);
  119. SEQ_printf(m, " .%-30s: %lu\n", "load",
  120. rq->ls.load.weight);
  121. P(ls.delta_fair);
  122. P(ls.delta_exec);
  123. P(nr_switches);
  124. P(nr_load_updates);
  125. P(nr_uninterruptible);
  126. SEQ_printf(m, " .%-30s: %lu\n", "jiffies", jiffies);
  127. P(next_balance);
  128. P(curr->pid);
  129. P(clock);
  130. P(prev_clock_raw);
  131. P(clock_warps);
  132. P(clock_overflows);
  133. P(clock_unstable_events);
  134. P(clock_max_delta);
  135. P(cpu_load[0]);
  136. P(cpu_load[1]);
  137. P(cpu_load[2]);
  138. P(cpu_load[3]);
  139. P(cpu_load[4]);
  140. #undef P
  141. print_cfs_stats(m, cpu, now);
  142. print_rq(m, rq, cpu, now);
  143. }
  144. static int sched_debug_show(struct seq_file *m, void *v)
  145. {
  146. u64 now = ktime_to_ns(ktime_get());
  147. int cpu;
  148. SEQ_printf(m, "Sched Debug Version: v0.04, cfs-v19, %s %.*s\n",
  149. init_utsname()->release,
  150. (int)strcspn(init_utsname()->version, " "),
  151. init_utsname()->version);
  152. SEQ_printf(m, "now at %Lu nsecs\n", (unsigned long long)now);
  153. for_each_online_cpu(cpu)
  154. print_cpu(m, cpu, now);
  155. SEQ_printf(m, "\n");
  156. return 0;
  157. }
  158. void sysrq_sched_debug_show(void)
  159. {
  160. sched_debug_show(NULL, NULL);
  161. }
  162. static int sched_debug_open(struct inode *inode, struct file *filp)
  163. {
  164. return single_open(filp, sched_debug_show, NULL);
  165. }
  166. static struct file_operations sched_debug_fops = {
  167. .open = sched_debug_open,
  168. .read = seq_read,
  169. .llseek = seq_lseek,
  170. .release = seq_release,
  171. };
  172. static int __init init_sched_debug_procfs(void)
  173. {
  174. struct proc_dir_entry *pe;
  175. pe = create_proc_entry("sched_debug", 0644, NULL);
  176. if (!pe)
  177. return -ENOMEM;
  178. pe->proc_fops = &sched_debug_fops;
  179. return 0;
  180. }
  181. __initcall(init_sched_debug_procfs);
  182. void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  183. {
  184. unsigned long flags;
  185. int num_threads = 1;
  186. rcu_read_lock();
  187. if (lock_task_sighand(p, &flags)) {
  188. num_threads = atomic_read(&p->signal->count);
  189. unlock_task_sighand(p, &flags);
  190. }
  191. rcu_read_unlock();
  192. SEQ_printf(m, "%s (%d, #threads: %d)\n", p->comm, p->pid, num_threads);
  193. SEQ_printf(m, "----------------------------------------------\n");
  194. #define P(F) \
  195. SEQ_printf(m, "%-25s:%20Ld\n", #F, (long long)p->F)
  196. P(se.wait_start);
  197. P(se.wait_start_fair);
  198. P(se.exec_start);
  199. P(se.sleep_start);
  200. P(se.sleep_start_fair);
  201. P(se.block_start);
  202. P(se.sleep_max);
  203. P(se.block_max);
  204. P(se.exec_max);
  205. P(se.wait_max);
  206. P(se.wait_runtime);
  207. P(se.wait_runtime_overruns);
  208. P(se.wait_runtime_underruns);
  209. P(se.sum_wait_runtime);
  210. P(se.sum_exec_runtime);
  211. SEQ_printf(m, "%-25s:%20Ld\n",
  212. "nr_switches", (long long)(p->nvcsw + p->nivcsw));
  213. P(se.load.weight);
  214. P(policy);
  215. P(prio);
  216. #undef P
  217. {
  218. u64 t0, t1;
  219. t0 = sched_clock();
  220. t1 = sched_clock();
  221. SEQ_printf(m, "%-25s:%20Ld\n", "clock-delta", (long long)(t1-t0));
  222. }
  223. }
  224. void proc_sched_set_task(struct task_struct *p)
  225. {
  226. p->se.sleep_max = p->se.block_max = p->se.exec_max = p->se.wait_max = 0;
  227. p->se.wait_runtime_overruns = p->se.wait_runtime_underruns = 0;
  228. p->se.sum_exec_runtime = 0;
  229. }