sched_debug.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. /*
  29. * Ease the printing of nsec fields:
  30. */
  31. static long long nsec_high(unsigned long long nsec)
  32. {
  33. if ((long long)nsec < 0) {
  34. nsec = -nsec;
  35. do_div(nsec, 1000000);
  36. return -nsec;
  37. }
  38. do_div(nsec, 1000000);
  39. return nsec;
  40. }
  41. static unsigned long nsec_low(unsigned long long nsec)
  42. {
  43. if ((long long)nsec < 0)
  44. nsec = -nsec;
  45. return do_div(nsec, 1000000);
  46. }
  47. #define SPLIT_NS(x) nsec_high(x), nsec_low(x)
  48. static void
  49. print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
  50. {
  51. if (rq->curr == p)
  52. SEQ_printf(m, "R");
  53. else
  54. SEQ_printf(m, " ");
  55. SEQ_printf(m, "%15s %5d %9Ld.%06ld %9Ld %5d ",
  56. p->comm, p->pid,
  57. SPLIT_NS(p->se.vruntime),
  58. (long long)(p->nvcsw + p->nivcsw),
  59. p->prio);
  60. #ifdef CONFIG_SCHEDSTATS
  61. SEQ_printf(m, "%9Ld.%06ld %9Ld.%06ld %9Ld.%06ld\n",
  62. SPLIT_NS(p->se.vruntime),
  63. SPLIT_NS(p->se.sum_exec_runtime),
  64. SPLIT_NS(p->se.sum_sleep_runtime));
  65. #else
  66. SEQ_printf(m, "%15Ld %15Ld %15Ld.%06ld %15Ld.%06ld %15Ld.%06ld\n",
  67. 0LL, 0LL, 0LL, 0L, 0LL, 0L, 0LL, 0L);
  68. #endif
  69. }
  70. static void print_rq(struct seq_file *m, struct rq *rq, int rq_cpu)
  71. {
  72. struct task_struct *g, *p;
  73. unsigned long flags;
  74. SEQ_printf(m,
  75. "\nrunnable tasks:\n"
  76. " task PID tree-key switches prio"
  77. " exec-runtime sum-exec sum-sleep\n"
  78. "------------------------------------------------------"
  79. "----------------------------------------------------\n");
  80. read_lock_irqsave(&tasklist_lock, flags);
  81. do_each_thread(g, p) {
  82. if (!p->se.on_rq || task_cpu(p) != rq_cpu)
  83. continue;
  84. print_task(m, rq, p);
  85. } while_each_thread(g, p);
  86. read_unlock_irqrestore(&tasklist_lock, flags);
  87. }
  88. void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
  89. {
  90. s64 MIN_vruntime = -1, min_vruntime, max_vruntime = -1,
  91. spread, rq0_min_vruntime, spread0;
  92. struct rq *rq = &per_cpu(runqueues, cpu);
  93. struct sched_entity *last;
  94. unsigned long flags;
  95. SEQ_printf(m, "\ncfs_rq\n");
  96. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "exec_clock",
  97. SPLIT_NS(cfs_rq->exec_clock));
  98. spin_lock_irqsave(&rq->lock, flags);
  99. if (cfs_rq->rb_leftmost)
  100. MIN_vruntime = (__pick_next_entity(cfs_rq))->vruntime;
  101. last = __pick_last_entity(cfs_rq);
  102. if (last)
  103. max_vruntime = last->vruntime;
  104. min_vruntime = rq->cfs.min_vruntime;
  105. rq0_min_vruntime = per_cpu(runqueues, 0).cfs.min_vruntime;
  106. spin_unlock_irqrestore(&rq->lock, flags);
  107. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "MIN_vruntime",
  108. SPLIT_NS(MIN_vruntime));
  109. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "min_vruntime",
  110. SPLIT_NS(min_vruntime));
  111. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "max_vruntime",
  112. SPLIT_NS(max_vruntime));
  113. spread = max_vruntime - MIN_vruntime;
  114. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread",
  115. SPLIT_NS(spread));
  116. spread0 = min_vruntime - rq0_min_vruntime;
  117. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread0",
  118. SPLIT_NS(spread0));
  119. SEQ_printf(m, " .%-30s: %ld\n", "nr_running", cfs_rq->nr_running);
  120. SEQ_printf(m, " .%-30s: %ld\n", "load", cfs_rq->load.weight);
  121. #ifdef CONFIG_SCHEDSTATS
  122. SEQ_printf(m, " .%-30s: %d\n", "bkl_count",
  123. rq->bkl_count);
  124. #endif
  125. SEQ_printf(m, " .%-30s: %ld\n", "nr_spread_over",
  126. cfs_rq->nr_spread_over);
  127. }
  128. static void print_cpu(struct seq_file *m, int cpu)
  129. {
  130. struct rq *rq = &per_cpu(runqueues, cpu);
  131. #ifdef CONFIG_X86
  132. {
  133. unsigned int freq = cpu_khz ? : 1;
  134. SEQ_printf(m, "\ncpu#%d, %u.%03u MHz\n",
  135. cpu, freq / 1000, (freq % 1000));
  136. }
  137. #else
  138. SEQ_printf(m, "\ncpu#%d\n", cpu);
  139. #endif
  140. #define P(x) \
  141. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rq->x))
  142. #define PN(x) \
  143. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rq->x))
  144. P(nr_running);
  145. SEQ_printf(m, " .%-30s: %lu\n", "load",
  146. rq->load.weight);
  147. P(nr_switches);
  148. P(nr_load_updates);
  149. P(nr_uninterruptible);
  150. SEQ_printf(m, " .%-30s: %lu\n", "jiffies", jiffies);
  151. PN(next_balance);
  152. P(curr->pid);
  153. PN(clock);
  154. PN(idle_clock);
  155. PN(prev_clock_raw);
  156. P(clock_warps);
  157. P(clock_overflows);
  158. P(clock_underflows);
  159. P(clock_deep_idle_events);
  160. PN(clock_max_delta);
  161. P(cpu_load[0]);
  162. P(cpu_load[1]);
  163. P(cpu_load[2]);
  164. P(cpu_load[3]);
  165. P(cpu_load[4]);
  166. #undef P
  167. #undef PN
  168. print_cfs_stats(m, cpu);
  169. print_rq(m, rq, cpu);
  170. }
  171. static int sched_debug_show(struct seq_file *m, void *v)
  172. {
  173. u64 now = ktime_to_ns(ktime_get());
  174. int cpu;
  175. SEQ_printf(m, "Sched Debug Version: v0.07, %s %.*s\n",
  176. init_utsname()->release,
  177. (int)strcspn(init_utsname()->version, " "),
  178. init_utsname()->version);
  179. SEQ_printf(m, "now at %Lu.%06ld msecs\n", SPLIT_NS(now));
  180. #define P(x) \
  181. SEQ_printf(m, " .%-40s: %Ld\n", #x, (long long)(x))
  182. #define PN(x) \
  183. SEQ_printf(m, " .%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
  184. PN(sysctl_sched_latency);
  185. PN(sysctl_sched_min_granularity);
  186. PN(sysctl_sched_wakeup_granularity);
  187. PN(sysctl_sched_batch_wakeup_granularity);
  188. PN(sysctl_sched_child_runs_first);
  189. P(sysctl_sched_features);
  190. #undef PN
  191. #undef P
  192. for_each_online_cpu(cpu)
  193. print_cpu(m, cpu);
  194. SEQ_printf(m, "\n");
  195. return 0;
  196. }
  197. static void sysrq_sched_debug_show(void)
  198. {
  199. sched_debug_show(NULL, NULL);
  200. }
  201. static int sched_debug_open(struct inode *inode, struct file *filp)
  202. {
  203. return single_open(filp, sched_debug_show, NULL);
  204. }
  205. static const struct file_operations sched_debug_fops = {
  206. .open = sched_debug_open,
  207. .read = seq_read,
  208. .llseek = seq_lseek,
  209. .release = single_release,
  210. };
  211. static int __init init_sched_debug_procfs(void)
  212. {
  213. struct proc_dir_entry *pe;
  214. pe = create_proc_entry("sched_debug", 0644, NULL);
  215. if (!pe)
  216. return -ENOMEM;
  217. pe->proc_fops = &sched_debug_fops;
  218. return 0;
  219. }
  220. __initcall(init_sched_debug_procfs);
  221. void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  222. {
  223. unsigned long nr_switches;
  224. unsigned long flags;
  225. int num_threads = 1;
  226. rcu_read_lock();
  227. if (lock_task_sighand(p, &flags)) {
  228. num_threads = atomic_read(&p->signal->count);
  229. unlock_task_sighand(p, &flags);
  230. }
  231. rcu_read_unlock();
  232. SEQ_printf(m, "%s (%d, #threads: %d)\n", p->comm, p->pid, num_threads);
  233. SEQ_printf(m,
  234. "---------------------------------------------------------\n");
  235. #define __P(F) \
  236. SEQ_printf(m, "%-35s:%21Ld\n", #F, (long long)F)
  237. #define P(F) \
  238. SEQ_printf(m, "%-35s:%21Ld\n", #F, (long long)p->F)
  239. #define __PN(F) \
  240. SEQ_printf(m, "%-35s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)F))
  241. #define PN(F) \
  242. SEQ_printf(m, "%-35s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)p->F))
  243. PN(se.exec_start);
  244. PN(se.vruntime);
  245. PN(se.sum_exec_runtime);
  246. PN(se.avg_overlap);
  247. nr_switches = p->nvcsw + p->nivcsw;
  248. #ifdef CONFIG_SCHEDSTATS
  249. PN(se.wait_start);
  250. PN(se.sleep_start);
  251. PN(se.block_start);
  252. PN(se.sleep_max);
  253. PN(se.block_max);
  254. PN(se.exec_max);
  255. PN(se.slice_max);
  256. PN(se.wait_max);
  257. PN(se.wait_sum);
  258. P(se.wait_count);
  259. P(sched_info.bkl_count);
  260. P(se.nr_migrations);
  261. P(se.nr_migrations_cold);
  262. P(se.nr_failed_migrations_affine);
  263. P(se.nr_failed_migrations_running);
  264. P(se.nr_failed_migrations_hot);
  265. P(se.nr_forced_migrations);
  266. P(se.nr_forced2_migrations);
  267. P(se.nr_wakeups);
  268. P(se.nr_wakeups_sync);
  269. P(se.nr_wakeups_migrate);
  270. P(se.nr_wakeups_local);
  271. P(se.nr_wakeups_remote);
  272. P(se.nr_wakeups_affine);
  273. P(se.nr_wakeups_affine_attempts);
  274. P(se.nr_wakeups_passive);
  275. P(se.nr_wakeups_idle);
  276. {
  277. u64 avg_atom, avg_per_cpu;
  278. avg_atom = p->se.sum_exec_runtime;
  279. if (nr_switches)
  280. do_div(avg_atom, nr_switches);
  281. else
  282. avg_atom = -1LL;
  283. avg_per_cpu = p->se.sum_exec_runtime;
  284. if (p->se.nr_migrations) {
  285. avg_per_cpu = div64_64(avg_per_cpu,
  286. p->se.nr_migrations);
  287. } else {
  288. avg_per_cpu = -1LL;
  289. }
  290. __PN(avg_atom);
  291. __PN(avg_per_cpu);
  292. }
  293. #endif
  294. __P(nr_switches);
  295. SEQ_printf(m, "%-35s:%21Ld\n",
  296. "nr_voluntary_switches", (long long)p->nvcsw);
  297. SEQ_printf(m, "%-35s:%21Ld\n",
  298. "nr_involuntary_switches", (long long)p->nivcsw);
  299. P(se.load.weight);
  300. P(policy);
  301. P(prio);
  302. #undef PN
  303. #undef __PN
  304. #undef P
  305. #undef __P
  306. {
  307. u64 t0, t1;
  308. t0 = sched_clock();
  309. t1 = sched_clock();
  310. SEQ_printf(m, "%-35s:%21Ld\n",
  311. "clock-delta", (long long)(t1-t0));
  312. }
  313. }
  314. void proc_sched_set_task(struct task_struct *p)
  315. {
  316. #ifdef CONFIG_SCHEDSTATS
  317. p->se.wait_max = 0;
  318. p->se.wait_sum = 0;
  319. p->se.wait_count = 0;
  320. p->se.sleep_max = 0;
  321. p->se.sum_sleep_runtime = 0;
  322. p->se.block_max = 0;
  323. p->se.exec_max = 0;
  324. p->se.slice_max = 0;
  325. p->se.nr_migrations = 0;
  326. p->se.nr_migrations_cold = 0;
  327. p->se.nr_failed_migrations_affine = 0;
  328. p->se.nr_failed_migrations_running = 0;
  329. p->se.nr_failed_migrations_hot = 0;
  330. p->se.nr_forced_migrations = 0;
  331. p->se.nr_forced2_migrations = 0;
  332. p->se.nr_wakeups = 0;
  333. p->se.nr_wakeups_sync = 0;
  334. p->se.nr_wakeups_migrate = 0;
  335. p->se.nr_wakeups_local = 0;
  336. p->se.nr_wakeups_remote = 0;
  337. p->se.nr_wakeups_affine = 0;
  338. p->se.nr_wakeups_affine_attempts = 0;
  339. p->se.nr_wakeups_passive = 0;
  340. p->se.nr_wakeups_idle = 0;
  341. p->sched_info.bkl_count = 0;
  342. #endif
  343. p->se.sum_exec_runtime = 0;
  344. p->se.prev_sum_exec_runtime = 0;
  345. p->nvcsw = 0;
  346. p->nivcsw = 0;
  347. }