sched_debug.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. /*
  18. * This allows printing both to /proc/sched_debug and
  19. * to the console
  20. */
  21. #define SEQ_printf(m, x...) \
  22. do { \
  23. if (m) \
  24. seq_printf(m, x); \
  25. else \
  26. printk(x); \
  27. } while (0)
  28. static void
  29. print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
  30. {
  31. if (rq->curr == p)
  32. SEQ_printf(m, "R");
  33. else
  34. SEQ_printf(m, " ");
  35. SEQ_printf(m, "%15s %5d %15Ld %13Ld %13Ld %9Ld %5d ",
  36. p->comm, p->pid,
  37. (long long)p->se.fair_key,
  38. (long long)(p->se.fair_key - rq->cfs.fair_clock),
  39. (long long)p->se.wait_runtime,
  40. (long long)(p->nvcsw + p->nivcsw),
  41. p->prio);
  42. #ifdef CONFIG_SCHEDSTATS
  43. SEQ_printf(m, "%15Ld %15Ld %15Ld %15Ld %15Ld %15Ld\n",
  44. (long long)p->se.vruntime,
  45. (long long)p->se.sum_exec_runtime,
  46. (long long)p->se.sum_wait_runtime,
  47. (long long)p->se.sum_sleep_runtime,
  48. (long long)p->se.wait_runtime_overruns,
  49. (long long)p->se.wait_runtime_underruns);
  50. #else
  51. SEQ_printf(m, "%15Ld %15Ld %15Ld %15Ld %15Ld\n",
  52. 0LL, 0LL, 0LL, 0LL, 0LL);
  53. #endif
  54. }
  55. static void print_rq(struct seq_file *m, struct rq *rq, int rq_cpu)
  56. {
  57. struct task_struct *g, *p;
  58. SEQ_printf(m,
  59. "\nrunnable tasks:\n"
  60. " task PID tree-key delta waiting"
  61. " switches prio"
  62. " exec-runtime sum-exec sum-wait sum-sleep"
  63. " wait-overrun wait-underrun\n"
  64. "------------------------------------------------------------------"
  65. "--------------------------------"
  66. "------------------------------------------------"
  67. "--------------------------------\n");
  68. read_lock_irq(&tasklist_lock);
  69. do_each_thread(g, p) {
  70. if (!p->se.on_rq || task_cpu(p) != rq_cpu)
  71. continue;
  72. print_task(m, rq, p);
  73. } while_each_thread(g, p);
  74. read_unlock_irq(&tasklist_lock);
  75. }
  76. static void
  77. print_cfs_rq_runtime_sum(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
  78. {
  79. s64 wait_runtime_rq_sum = 0;
  80. struct task_struct *p;
  81. struct rb_node *curr;
  82. unsigned long flags;
  83. struct rq *rq = &per_cpu(runqueues, cpu);
  84. spin_lock_irqsave(&rq->lock, flags);
  85. curr = first_fair(cfs_rq);
  86. while (curr) {
  87. p = rb_entry(curr, struct task_struct, se.run_node);
  88. wait_runtime_rq_sum += p->se.wait_runtime;
  89. curr = rb_next(curr);
  90. }
  91. spin_unlock_irqrestore(&rq->lock, flags);
  92. SEQ_printf(m, " .%-30s: %Ld\n", "wait_runtime_rq_sum",
  93. (long long)wait_runtime_rq_sum);
  94. }
  95. void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
  96. {
  97. s64 MIN_vruntime = -1, min_vruntime, max_vruntime = -1,
  98. spread, rq0_min_vruntime, spread0;
  99. struct rq *rq = &per_cpu(runqueues, cpu);
  100. struct sched_entity *last;
  101. unsigned long flags;
  102. SEQ_printf(m, "\ncfs_rq\n");
  103. #define P(x) \
  104. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(cfs_rq->x))
  105. P(fair_clock);
  106. P(exec_clock);
  107. spin_lock_irqsave(&rq->lock, flags);
  108. if (cfs_rq->rb_leftmost)
  109. MIN_vruntime = (__pick_next_entity(cfs_rq))->vruntime;
  110. last = __pick_last_entity(cfs_rq);
  111. if (last)
  112. max_vruntime = last->vruntime;
  113. min_vruntime = rq->cfs.min_vruntime;
  114. rq0_min_vruntime = per_cpu(runqueues, 0).cfs.min_vruntime;
  115. spin_unlock_irqrestore(&rq->lock, flags);
  116. SEQ_printf(m, " .%-30s: %Ld\n", "MIN_vruntime",
  117. (long long)MIN_vruntime);
  118. SEQ_printf(m, " .%-30s: %Ld\n", "min_vruntime",
  119. (long long)min_vruntime);
  120. SEQ_printf(m, " .%-30s: %Ld\n", "max_vruntime",
  121. (long long)max_vruntime);
  122. spread = max_vruntime - MIN_vruntime;
  123. SEQ_printf(m, " .%-30s: %Ld\n", "spread",
  124. (long long)spread);
  125. spread0 = min_vruntime - rq0_min_vruntime;
  126. SEQ_printf(m, " .%-30s: %Ld\n", "spread0",
  127. (long long)spread0);
  128. P(wait_runtime);
  129. P(wait_runtime_overruns);
  130. P(wait_runtime_underruns);
  131. P(sleeper_bonus);
  132. #undef P
  133. print_cfs_rq_runtime_sum(m, cpu, cfs_rq);
  134. }
  135. static void print_cpu(struct seq_file *m, int cpu)
  136. {
  137. struct rq *rq = &per_cpu(runqueues, cpu);
  138. #ifdef CONFIG_X86
  139. {
  140. unsigned int freq = cpu_khz ? : 1;
  141. SEQ_printf(m, "\ncpu#%d, %u.%03u MHz\n",
  142. cpu, freq / 1000, (freq % 1000));
  143. }
  144. #else
  145. SEQ_printf(m, "\ncpu#%d\n", cpu);
  146. #endif
  147. #define P(x) \
  148. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rq->x))
  149. P(nr_running);
  150. SEQ_printf(m, " .%-30s: %lu\n", "load",
  151. rq->load.weight);
  152. P(nr_switches);
  153. P(nr_load_updates);
  154. P(nr_uninterruptible);
  155. SEQ_printf(m, " .%-30s: %lu\n", "jiffies", jiffies);
  156. P(next_balance);
  157. P(curr->pid);
  158. P(clock);
  159. P(idle_clock);
  160. P(prev_clock_raw);
  161. P(clock_warps);
  162. P(clock_overflows);
  163. P(clock_deep_idle_events);
  164. P(clock_max_delta);
  165. P(cpu_load[0]);
  166. P(cpu_load[1]);
  167. P(cpu_load[2]);
  168. P(cpu_load[3]);
  169. P(cpu_load[4]);
  170. #undef P
  171. print_cfs_stats(m, cpu);
  172. print_rq(m, rq, cpu);
  173. }
  174. static int sched_debug_show(struct seq_file *m, void *v)
  175. {
  176. u64 now = ktime_to_ns(ktime_get());
  177. int cpu;
  178. SEQ_printf(m, "Sched Debug Version: v0.05-v20, %s %.*s\n",
  179. init_utsname()->release,
  180. (int)strcspn(init_utsname()->version, " "),
  181. init_utsname()->version);
  182. SEQ_printf(m, "now at %Lu nsecs\n", (unsigned long long)now);
  183. for_each_online_cpu(cpu)
  184. print_cpu(m, cpu);
  185. SEQ_printf(m, "\n");
  186. return 0;
  187. }
  188. static void sysrq_sched_debug_show(void)
  189. {
  190. sched_debug_show(NULL, NULL);
  191. }
  192. static int sched_debug_open(struct inode *inode, struct file *filp)
  193. {
  194. return single_open(filp, sched_debug_show, NULL);
  195. }
  196. static struct file_operations sched_debug_fops = {
  197. .open = sched_debug_open,
  198. .read = seq_read,
  199. .llseek = seq_lseek,
  200. .release = single_release,
  201. };
  202. static int __init init_sched_debug_procfs(void)
  203. {
  204. struct proc_dir_entry *pe;
  205. pe = create_proc_entry("sched_debug", 0644, NULL);
  206. if (!pe)
  207. return -ENOMEM;
  208. pe->proc_fops = &sched_debug_fops;
  209. return 0;
  210. }
  211. __initcall(init_sched_debug_procfs);
  212. void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  213. {
  214. unsigned long flags;
  215. int num_threads = 1;
  216. rcu_read_lock();
  217. if (lock_task_sighand(p, &flags)) {
  218. num_threads = atomic_read(&p->signal->count);
  219. unlock_task_sighand(p, &flags);
  220. }
  221. rcu_read_unlock();
  222. SEQ_printf(m, "%s (%d, #threads: %d)\n", p->comm, p->pid, num_threads);
  223. SEQ_printf(m, "----------------------------------------------\n");
  224. #define P(F) \
  225. SEQ_printf(m, "%-25s:%20Ld\n", #F, (long long)p->F)
  226. P(se.wait_runtime);
  227. P(se.wait_start_fair);
  228. P(se.exec_start);
  229. P(se.sleep_start_fair);
  230. P(se.vruntime);
  231. P(se.sum_exec_runtime);
  232. #ifdef CONFIG_SCHEDSTATS
  233. P(se.wait_start);
  234. P(se.sleep_start);
  235. P(se.block_start);
  236. P(se.sleep_max);
  237. P(se.block_max);
  238. P(se.exec_max);
  239. P(se.slice_max);
  240. P(se.wait_max);
  241. P(se.wait_runtime_overruns);
  242. P(se.wait_runtime_underruns);
  243. P(se.sum_wait_runtime);
  244. #endif
  245. SEQ_printf(m, "%-25s:%20Ld\n",
  246. "nr_switches", (long long)(p->nvcsw + p->nivcsw));
  247. P(se.load.weight);
  248. P(policy);
  249. P(prio);
  250. #undef P
  251. {
  252. u64 t0, t1;
  253. t0 = sched_clock();
  254. t1 = sched_clock();
  255. SEQ_printf(m, "%-25s:%20Ld\n",
  256. "clock-delta", (long long)(t1-t0));
  257. }
  258. }
  259. void proc_sched_set_task(struct task_struct *p)
  260. {
  261. #ifdef CONFIG_SCHEDSTATS
  262. p->se.sleep_max = 0;
  263. p->se.block_max = 0;
  264. p->se.exec_max = 0;
  265. p->se.slice_max = 0;
  266. p->se.wait_max = 0;
  267. p->se.wait_runtime_overruns = 0;
  268. p->se.wait_runtime_underruns = 0;
  269. #endif
  270. p->se.sum_exec_runtime = 0;
  271. p->se.prev_sum_exec_runtime = 0;
  272. }