sched_debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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",
  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",
  67. 0LL, 0LL, 0LL, 0L, 0LL, 0L, 0LL, 0L);
  68. #endif
  69. #ifdef CONFIG_CGROUP_SCHED
  70. {
  71. char path[64];
  72. cgroup_path(task_group(p)->css.cgroup, path, sizeof(path));
  73. SEQ_printf(m, " %s", path);
  74. }
  75. #endif
  76. SEQ_printf(m, "\n");
  77. }
  78. static void print_rq(struct seq_file *m, struct rq *rq, int rq_cpu)
  79. {
  80. struct task_struct *g, *p;
  81. unsigned long flags;
  82. SEQ_printf(m,
  83. "\nrunnable tasks:\n"
  84. " task PID tree-key switches prio"
  85. " exec-runtime sum-exec sum-sleep\n"
  86. "------------------------------------------------------"
  87. "----------------------------------------------------\n");
  88. read_lock_irqsave(&tasklist_lock, flags);
  89. do_each_thread(g, p) {
  90. if (!p->se.on_rq || task_cpu(p) != rq_cpu)
  91. continue;
  92. print_task(m, rq, p);
  93. } while_each_thread(g, p);
  94. read_unlock_irqrestore(&tasklist_lock, flags);
  95. }
  96. void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
  97. {
  98. s64 MIN_vruntime = -1, min_vruntime, max_vruntime = -1,
  99. spread, rq0_min_vruntime, spread0;
  100. struct rq *rq = &per_cpu(runqueues, cpu);
  101. struct sched_entity *last;
  102. unsigned long flags;
  103. #if defined(CONFIG_CGROUP_SCHED) && defined(CONFIG_FAIR_GROUP_SCHED)
  104. char path[128] = "";
  105. struct cgroup *cgroup = NULL;
  106. struct task_group *tg = cfs_rq->tg;
  107. if (tg)
  108. cgroup = tg->css.cgroup;
  109. if (cgroup)
  110. cgroup_path(cgroup, path, sizeof(path));
  111. SEQ_printf(m, "\ncfs_rq[%d]:%s\n", cpu, path);
  112. #else
  113. SEQ_printf(m, "\ncfs_rq[%d]:\n", cpu);
  114. #endif
  115. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "exec_clock",
  116. SPLIT_NS(cfs_rq->exec_clock));
  117. spin_lock_irqsave(&rq->lock, flags);
  118. if (cfs_rq->rb_leftmost)
  119. MIN_vruntime = (__pick_next_entity(cfs_rq))->vruntime;
  120. last = __pick_last_entity(cfs_rq);
  121. if (last)
  122. max_vruntime = last->vruntime;
  123. min_vruntime = rq->cfs.min_vruntime;
  124. rq0_min_vruntime = per_cpu(runqueues, 0).cfs.min_vruntime;
  125. spin_unlock_irqrestore(&rq->lock, flags);
  126. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "MIN_vruntime",
  127. SPLIT_NS(MIN_vruntime));
  128. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "min_vruntime",
  129. SPLIT_NS(min_vruntime));
  130. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "max_vruntime",
  131. SPLIT_NS(max_vruntime));
  132. spread = max_vruntime - MIN_vruntime;
  133. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread",
  134. SPLIT_NS(spread));
  135. spread0 = min_vruntime - rq0_min_vruntime;
  136. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread0",
  137. SPLIT_NS(spread0));
  138. SEQ_printf(m, " .%-30s: %ld\n", "nr_running", cfs_rq->nr_running);
  139. SEQ_printf(m, " .%-30s: %ld\n", "load", cfs_rq->load.weight);
  140. #ifdef CONFIG_SCHEDSTATS
  141. #define P(n) SEQ_printf(m, " .%-30s: %d\n", #n, rq->n);
  142. P(yld_exp_empty);
  143. P(yld_act_empty);
  144. P(yld_both_empty);
  145. P(yld_count);
  146. P(sched_switch);
  147. P(sched_count);
  148. P(sched_goidle);
  149. P(ttwu_count);
  150. P(ttwu_local);
  151. P(bkl_count);
  152. #undef P
  153. #endif
  154. SEQ_printf(m, " .%-30s: %ld\n", "nr_spread_over",
  155. cfs_rq->nr_spread_over);
  156. #ifdef CONFIG_FAIR_GROUP_SCHED
  157. #ifdef CONFIG_SMP
  158. SEQ_printf(m, " .%-30s: %lu\n", "shares", cfs_rq->shares);
  159. #endif
  160. #endif
  161. }
  162. void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
  163. {
  164. #if defined(CONFIG_CGROUP_SCHED) && defined(CONFIG_RT_GROUP_SCHED)
  165. char path[128] = "";
  166. struct cgroup *cgroup = NULL;
  167. struct task_group *tg = rt_rq->tg;
  168. if (tg)
  169. cgroup = tg->css.cgroup;
  170. if (cgroup)
  171. cgroup_path(cgroup, path, sizeof(path));
  172. SEQ_printf(m, "\nrt_rq[%d]:%s\n", cpu, path);
  173. #else
  174. SEQ_printf(m, "\nrt_rq[%d]:\n", cpu);
  175. #endif
  176. #define P(x) \
  177. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rt_rq->x))
  178. #define PN(x) \
  179. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rt_rq->x))
  180. P(rt_nr_running);
  181. P(rt_throttled);
  182. PN(rt_time);
  183. PN(rt_runtime);
  184. #undef PN
  185. #undef P
  186. }
  187. static void print_cpu(struct seq_file *m, int cpu)
  188. {
  189. struct rq *rq = &per_cpu(runqueues, cpu);
  190. #ifdef CONFIG_X86
  191. {
  192. unsigned int freq = cpu_khz ? : 1;
  193. SEQ_printf(m, "\ncpu#%d, %u.%03u MHz\n",
  194. cpu, freq / 1000, (freq % 1000));
  195. }
  196. #else
  197. SEQ_printf(m, "\ncpu#%d\n", cpu);
  198. #endif
  199. #define P(x) \
  200. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rq->x))
  201. #define PN(x) \
  202. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rq->x))
  203. P(nr_running);
  204. SEQ_printf(m, " .%-30s: %lu\n", "load",
  205. rq->load.weight);
  206. P(nr_switches);
  207. P(nr_load_updates);
  208. P(nr_uninterruptible);
  209. SEQ_printf(m, " .%-30s: %lu\n", "jiffies", jiffies);
  210. PN(next_balance);
  211. P(curr->pid);
  212. PN(clock);
  213. P(cpu_load[0]);
  214. P(cpu_load[1]);
  215. P(cpu_load[2]);
  216. P(cpu_load[3]);
  217. P(cpu_load[4]);
  218. #undef P
  219. #undef PN
  220. print_cfs_stats(m, cpu);
  221. print_rt_stats(m, cpu);
  222. print_rq(m, rq, cpu);
  223. }
  224. static int sched_debug_show(struct seq_file *m, void *v)
  225. {
  226. u64 now = ktime_to_ns(ktime_get());
  227. int cpu;
  228. SEQ_printf(m, "Sched Debug Version: v0.07, %s %.*s\n",
  229. init_utsname()->release,
  230. (int)strcspn(init_utsname()->version, " "),
  231. init_utsname()->version);
  232. SEQ_printf(m, "now at %Lu.%06ld msecs\n", SPLIT_NS(now));
  233. #define P(x) \
  234. SEQ_printf(m, " .%-40s: %Ld\n", #x, (long long)(x))
  235. #define PN(x) \
  236. SEQ_printf(m, " .%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
  237. PN(sysctl_sched_latency);
  238. PN(sysctl_sched_min_granularity);
  239. PN(sysctl_sched_wakeup_granularity);
  240. PN(sysctl_sched_child_runs_first);
  241. P(sysctl_sched_features);
  242. #undef PN
  243. #undef P
  244. for_each_online_cpu(cpu)
  245. print_cpu(m, cpu);
  246. SEQ_printf(m, "\n");
  247. return 0;
  248. }
  249. static void sysrq_sched_debug_show(void)
  250. {
  251. sched_debug_show(NULL, NULL);
  252. }
  253. static int sched_debug_open(struct inode *inode, struct file *filp)
  254. {
  255. return single_open(filp, sched_debug_show, NULL);
  256. }
  257. static const struct file_operations sched_debug_fops = {
  258. .open = sched_debug_open,
  259. .read = seq_read,
  260. .llseek = seq_lseek,
  261. .release = single_release,
  262. };
  263. static int __init init_sched_debug_procfs(void)
  264. {
  265. struct proc_dir_entry *pe;
  266. pe = proc_create("sched_debug", 0644, NULL, &sched_debug_fops);
  267. if (!pe)
  268. return -ENOMEM;
  269. return 0;
  270. }
  271. __initcall(init_sched_debug_procfs);
  272. void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  273. {
  274. unsigned long nr_switches;
  275. unsigned long flags;
  276. int num_threads = 1;
  277. rcu_read_lock();
  278. if (lock_task_sighand(p, &flags)) {
  279. num_threads = atomic_read(&p->signal->count);
  280. unlock_task_sighand(p, &flags);
  281. }
  282. rcu_read_unlock();
  283. SEQ_printf(m, "%s (%d, #threads: %d)\n", p->comm, p->pid, num_threads);
  284. SEQ_printf(m,
  285. "---------------------------------------------------------\n");
  286. #define __P(F) \
  287. SEQ_printf(m, "%-35s:%21Ld\n", #F, (long long)F)
  288. #define P(F) \
  289. SEQ_printf(m, "%-35s:%21Ld\n", #F, (long long)p->F)
  290. #define __PN(F) \
  291. SEQ_printf(m, "%-35s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)F))
  292. #define PN(F) \
  293. SEQ_printf(m, "%-35s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)p->F))
  294. PN(se.exec_start);
  295. PN(se.vruntime);
  296. PN(se.sum_exec_runtime);
  297. PN(se.avg_overlap);
  298. nr_switches = p->nvcsw + p->nivcsw;
  299. #ifdef CONFIG_SCHEDSTATS
  300. PN(se.wait_start);
  301. PN(se.sleep_start);
  302. PN(se.block_start);
  303. PN(se.sleep_max);
  304. PN(se.block_max);
  305. PN(se.exec_max);
  306. PN(se.slice_max);
  307. PN(se.wait_max);
  308. PN(se.wait_sum);
  309. P(se.wait_count);
  310. P(sched_info.bkl_count);
  311. P(se.nr_migrations);
  312. P(se.nr_migrations_cold);
  313. P(se.nr_failed_migrations_affine);
  314. P(se.nr_failed_migrations_running);
  315. P(se.nr_failed_migrations_hot);
  316. P(se.nr_forced_migrations);
  317. P(se.nr_forced2_migrations);
  318. P(se.nr_wakeups);
  319. P(se.nr_wakeups_sync);
  320. P(se.nr_wakeups_migrate);
  321. P(se.nr_wakeups_local);
  322. P(se.nr_wakeups_remote);
  323. P(se.nr_wakeups_affine);
  324. P(se.nr_wakeups_affine_attempts);
  325. P(se.nr_wakeups_passive);
  326. P(se.nr_wakeups_idle);
  327. {
  328. u64 avg_atom, avg_per_cpu;
  329. avg_atom = p->se.sum_exec_runtime;
  330. if (nr_switches)
  331. do_div(avg_atom, nr_switches);
  332. else
  333. avg_atom = -1LL;
  334. avg_per_cpu = p->se.sum_exec_runtime;
  335. if (p->se.nr_migrations) {
  336. avg_per_cpu = div64_u64(avg_per_cpu,
  337. p->se.nr_migrations);
  338. } else {
  339. avg_per_cpu = -1LL;
  340. }
  341. __PN(avg_atom);
  342. __PN(avg_per_cpu);
  343. }
  344. #endif
  345. __P(nr_switches);
  346. SEQ_printf(m, "%-35s:%21Ld\n",
  347. "nr_voluntary_switches", (long long)p->nvcsw);
  348. SEQ_printf(m, "%-35s:%21Ld\n",
  349. "nr_involuntary_switches", (long long)p->nivcsw);
  350. P(se.load.weight);
  351. P(policy);
  352. P(prio);
  353. #undef PN
  354. #undef __PN
  355. #undef P
  356. #undef __P
  357. {
  358. u64 t0, t1;
  359. t0 = sched_clock();
  360. t1 = sched_clock();
  361. SEQ_printf(m, "%-35s:%21Ld\n",
  362. "clock-delta", (long long)(t1-t0));
  363. }
  364. }
  365. void proc_sched_set_task(struct task_struct *p)
  366. {
  367. #ifdef CONFIG_SCHEDSTATS
  368. p->se.wait_max = 0;
  369. p->se.wait_sum = 0;
  370. p->se.wait_count = 0;
  371. p->se.sleep_max = 0;
  372. p->se.sum_sleep_runtime = 0;
  373. p->se.block_max = 0;
  374. p->se.exec_max = 0;
  375. p->se.slice_max = 0;
  376. p->se.nr_migrations = 0;
  377. p->se.nr_migrations_cold = 0;
  378. p->se.nr_failed_migrations_affine = 0;
  379. p->se.nr_failed_migrations_running = 0;
  380. p->se.nr_failed_migrations_hot = 0;
  381. p->se.nr_forced_migrations = 0;
  382. p->se.nr_forced2_migrations = 0;
  383. p->se.nr_wakeups = 0;
  384. p->se.nr_wakeups_sync = 0;
  385. p->se.nr_wakeups_migrate = 0;
  386. p->se.nr_wakeups_local = 0;
  387. p->se.nr_wakeups_remote = 0;
  388. p->se.nr_wakeups_affine = 0;
  389. p->se.nr_wakeups_affine_attempts = 0;
  390. p->se.nr_wakeups_passive = 0;
  391. p->se.nr_wakeups_idle = 0;
  392. p->sched_info.bkl_count = 0;
  393. #endif
  394. p->se.sum_exec_runtime = 0;
  395. p->se.prev_sum_exec_runtime = 0;
  396. p->nvcsw = 0;
  397. p->nivcsw = 0;
  398. }