debug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * kernel/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. #include "sched.h"
  18. static DEFINE_SPINLOCK(sched_debug_lock);
  19. /*
  20. * This allows printing both to /proc/sched_debug and
  21. * to the console
  22. */
  23. #define SEQ_printf(m, x...) \
  24. do { \
  25. if (m) \
  26. seq_printf(m, x); \
  27. else \
  28. printk(x); \
  29. } while (0)
  30. /*
  31. * Ease the printing of nsec fields:
  32. */
  33. static long long nsec_high(unsigned long long nsec)
  34. {
  35. if ((long long)nsec < 0) {
  36. nsec = -nsec;
  37. do_div(nsec, 1000000);
  38. return -nsec;
  39. }
  40. do_div(nsec, 1000000);
  41. return nsec;
  42. }
  43. static unsigned long nsec_low(unsigned long long nsec)
  44. {
  45. if ((long long)nsec < 0)
  46. nsec = -nsec;
  47. return do_div(nsec, 1000000);
  48. }
  49. #define SPLIT_NS(x) nsec_high(x), nsec_low(x)
  50. #ifdef CONFIG_FAIR_GROUP_SCHED
  51. static void print_cfs_group_stats(struct seq_file *m, int cpu, struct task_group *tg)
  52. {
  53. struct sched_entity *se = tg->se[cpu];
  54. if (!se)
  55. return;
  56. #define P(F) \
  57. SEQ_printf(m, " .%-30s: %lld\n", #F, (long long)F)
  58. #define PN(F) \
  59. SEQ_printf(m, " .%-30s: %lld.%06ld\n", #F, SPLIT_NS((long long)F))
  60. PN(se->exec_start);
  61. PN(se->vruntime);
  62. PN(se->sum_exec_runtime);
  63. #ifdef CONFIG_SCHEDSTATS
  64. PN(se->statistics.wait_start);
  65. PN(se->statistics.sleep_start);
  66. PN(se->statistics.block_start);
  67. PN(se->statistics.sleep_max);
  68. PN(se->statistics.block_max);
  69. PN(se->statistics.exec_max);
  70. PN(se->statistics.slice_max);
  71. PN(se->statistics.wait_max);
  72. PN(se->statistics.wait_sum);
  73. P(se->statistics.wait_count);
  74. #endif
  75. P(se->load.weight);
  76. #undef PN
  77. #undef P
  78. }
  79. #endif
  80. #ifdef CONFIG_CGROUP_SCHED
  81. static char group_path[PATH_MAX];
  82. static char *task_group_path(struct task_group *tg)
  83. {
  84. if (autogroup_path(tg, group_path, PATH_MAX))
  85. return group_path;
  86. /*
  87. * May be NULL if the underlying cgroup isn't fully-created yet
  88. */
  89. if (!tg->css.cgroup) {
  90. group_path[0] = '\0';
  91. return group_path;
  92. }
  93. cgroup_path(tg->css.cgroup, group_path, PATH_MAX);
  94. return group_path;
  95. }
  96. #endif
  97. static void
  98. print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
  99. {
  100. if (rq->curr == p)
  101. SEQ_printf(m, "R");
  102. else
  103. SEQ_printf(m, " ");
  104. SEQ_printf(m, "%15s %5d %9Ld.%06ld %9Ld %5d ",
  105. p->comm, p->pid,
  106. SPLIT_NS(p->se.vruntime),
  107. (long long)(p->nvcsw + p->nivcsw),
  108. p->prio);
  109. #ifdef CONFIG_SCHEDSTATS
  110. SEQ_printf(m, "%9Ld.%06ld %9Ld.%06ld %9Ld.%06ld",
  111. SPLIT_NS(p->se.vruntime),
  112. SPLIT_NS(p->se.sum_exec_runtime),
  113. SPLIT_NS(p->se.statistics.sum_sleep_runtime));
  114. #else
  115. SEQ_printf(m, "%15Ld %15Ld %15Ld.%06ld %15Ld.%06ld %15Ld.%06ld",
  116. 0LL, 0LL, 0LL, 0L, 0LL, 0L, 0LL, 0L);
  117. #endif
  118. #ifdef CONFIG_CGROUP_SCHED
  119. SEQ_printf(m, " %s", task_group_path(task_group(p)));
  120. #endif
  121. SEQ_printf(m, "\n");
  122. }
  123. static void print_rq(struct seq_file *m, struct rq *rq, int rq_cpu)
  124. {
  125. struct task_struct *g, *p;
  126. unsigned long flags;
  127. SEQ_printf(m,
  128. "\nrunnable tasks:\n"
  129. " task PID tree-key switches prio"
  130. " exec-runtime sum-exec sum-sleep\n"
  131. "------------------------------------------------------"
  132. "----------------------------------------------------\n");
  133. read_lock_irqsave(&tasklist_lock, flags);
  134. do_each_thread(g, p) {
  135. if (!p->on_rq || task_cpu(p) != rq_cpu)
  136. continue;
  137. print_task(m, rq, p);
  138. } while_each_thread(g, p);
  139. read_unlock_irqrestore(&tasklist_lock, flags);
  140. }
  141. void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
  142. {
  143. s64 MIN_vruntime = -1, min_vruntime, max_vruntime = -1,
  144. spread, rq0_min_vruntime, spread0;
  145. struct rq *rq = cpu_rq(cpu);
  146. struct sched_entity *last;
  147. unsigned long flags;
  148. #ifdef CONFIG_FAIR_GROUP_SCHED
  149. SEQ_printf(m, "\ncfs_rq[%d]:%s\n", cpu, task_group_path(cfs_rq->tg));
  150. #else
  151. SEQ_printf(m, "\ncfs_rq[%d]:\n", cpu);
  152. #endif
  153. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "exec_clock",
  154. SPLIT_NS(cfs_rq->exec_clock));
  155. raw_spin_lock_irqsave(&rq->lock, flags);
  156. if (cfs_rq->rb_leftmost)
  157. MIN_vruntime = (__pick_first_entity(cfs_rq))->vruntime;
  158. last = __pick_last_entity(cfs_rq);
  159. if (last)
  160. max_vruntime = last->vruntime;
  161. min_vruntime = cfs_rq->min_vruntime;
  162. rq0_min_vruntime = cpu_rq(0)->cfs.min_vruntime;
  163. raw_spin_unlock_irqrestore(&rq->lock, flags);
  164. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "MIN_vruntime",
  165. SPLIT_NS(MIN_vruntime));
  166. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "min_vruntime",
  167. SPLIT_NS(min_vruntime));
  168. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "max_vruntime",
  169. SPLIT_NS(max_vruntime));
  170. spread = max_vruntime - MIN_vruntime;
  171. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread",
  172. SPLIT_NS(spread));
  173. spread0 = min_vruntime - rq0_min_vruntime;
  174. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread0",
  175. SPLIT_NS(spread0));
  176. SEQ_printf(m, " .%-30s: %d\n", "nr_spread_over",
  177. cfs_rq->nr_spread_over);
  178. SEQ_printf(m, " .%-30s: %ld\n", "nr_running", cfs_rq->nr_running);
  179. SEQ_printf(m, " .%-30s: %ld\n", "load", cfs_rq->load.weight);
  180. #ifdef CONFIG_FAIR_GROUP_SCHED
  181. #ifdef CONFIG_SMP
  182. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "load_avg",
  183. SPLIT_NS(cfs_rq->load_avg));
  184. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "load_period",
  185. SPLIT_NS(cfs_rq->load_period));
  186. SEQ_printf(m, " .%-30s: %ld\n", "load_contrib",
  187. cfs_rq->load_contribution);
  188. SEQ_printf(m, " .%-30s: %d\n", "load_tg",
  189. atomic_read(&cfs_rq->tg->load_weight));
  190. #endif
  191. #ifdef CONFIG_CFS_BANDWIDTH
  192. SEQ_printf(m, " .%-30s: %d\n", "tg->cfs_bandwidth.timer_active",
  193. cfs_rq->tg->cfs_bandwidth.timer_active);
  194. SEQ_printf(m, " .%-30s: %d\n", "throttled",
  195. cfs_rq->throttled);
  196. SEQ_printf(m, " .%-30s: %d\n", "throttle_count",
  197. cfs_rq->throttle_count);
  198. #endif
  199. print_cfs_group_stats(m, cpu, cfs_rq->tg);
  200. #endif
  201. }
  202. void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
  203. {
  204. #ifdef CONFIG_RT_GROUP_SCHED
  205. SEQ_printf(m, "\nrt_rq[%d]:%s\n", cpu, task_group_path(rt_rq->tg));
  206. #else
  207. SEQ_printf(m, "\nrt_rq[%d]:\n", cpu);
  208. #endif
  209. #define P(x) \
  210. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rt_rq->x))
  211. #define PN(x) \
  212. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rt_rq->x))
  213. P(rt_nr_running);
  214. P(rt_throttled);
  215. PN(rt_time);
  216. PN(rt_runtime);
  217. #undef PN
  218. #undef P
  219. }
  220. extern __read_mostly int sched_clock_running;
  221. static void print_cpu(struct seq_file *m, int cpu)
  222. {
  223. struct rq *rq = cpu_rq(cpu);
  224. unsigned long flags;
  225. #ifdef CONFIG_X86
  226. {
  227. unsigned int freq = cpu_khz ? : 1;
  228. SEQ_printf(m, "\ncpu#%d, %u.%03u MHz\n",
  229. cpu, freq / 1000, (freq % 1000));
  230. }
  231. #else
  232. SEQ_printf(m, "\ncpu#%d\n", cpu);
  233. #endif
  234. #define P(x) \
  235. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rq->x))
  236. #define PN(x) \
  237. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rq->x))
  238. P(nr_running);
  239. SEQ_printf(m, " .%-30s: %lu\n", "load",
  240. rq->load.weight);
  241. P(nr_switches);
  242. P(nr_load_updates);
  243. P(nr_uninterruptible);
  244. PN(next_balance);
  245. P(curr->pid);
  246. PN(clock);
  247. P(cpu_load[0]);
  248. P(cpu_load[1]);
  249. P(cpu_load[2]);
  250. P(cpu_load[3]);
  251. P(cpu_load[4]);
  252. #undef P
  253. #undef PN
  254. #ifdef CONFIG_SCHEDSTATS
  255. #define P(n) SEQ_printf(m, " .%-30s: %d\n", #n, rq->n);
  256. #define P64(n) SEQ_printf(m, " .%-30s: %Ld\n", #n, rq->n);
  257. P(yld_count);
  258. P(sched_count);
  259. P(sched_goidle);
  260. #ifdef CONFIG_SMP
  261. P64(avg_idle);
  262. #endif
  263. P(ttwu_count);
  264. P(ttwu_local);
  265. #undef P
  266. #undef P64
  267. #endif
  268. spin_lock_irqsave(&sched_debug_lock, flags);
  269. print_cfs_stats(m, cpu);
  270. print_rt_stats(m, cpu);
  271. rcu_read_lock();
  272. print_rq(m, rq, cpu);
  273. rcu_read_unlock();
  274. spin_unlock_irqrestore(&sched_debug_lock, flags);
  275. }
  276. static const char *sched_tunable_scaling_names[] = {
  277. "none",
  278. "logaritmic",
  279. "linear"
  280. };
  281. static int sched_debug_show(struct seq_file *m, void *v)
  282. {
  283. u64 ktime, sched_clk, cpu_clk;
  284. unsigned long flags;
  285. int cpu;
  286. local_irq_save(flags);
  287. ktime = ktime_to_ns(ktime_get());
  288. sched_clk = sched_clock();
  289. cpu_clk = local_clock();
  290. local_irq_restore(flags);
  291. SEQ_printf(m, "Sched Debug Version: v0.10, %s %.*s\n",
  292. init_utsname()->release,
  293. (int)strcspn(init_utsname()->version, " "),
  294. init_utsname()->version);
  295. #define P(x) \
  296. SEQ_printf(m, "%-40s: %Ld\n", #x, (long long)(x))
  297. #define PN(x) \
  298. SEQ_printf(m, "%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
  299. PN(ktime);
  300. PN(sched_clk);
  301. PN(cpu_clk);
  302. P(jiffies);
  303. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  304. P(sched_clock_stable);
  305. #endif
  306. #undef PN
  307. #undef P
  308. SEQ_printf(m, "\n");
  309. SEQ_printf(m, "sysctl_sched\n");
  310. #define P(x) \
  311. SEQ_printf(m, " .%-40s: %Ld\n", #x, (long long)(x))
  312. #define PN(x) \
  313. SEQ_printf(m, " .%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
  314. PN(sysctl_sched_latency);
  315. PN(sysctl_sched_min_granularity);
  316. PN(sysctl_sched_wakeup_granularity);
  317. P(sysctl_sched_child_runs_first);
  318. P(sysctl_sched_features);
  319. #undef PN
  320. #undef P
  321. SEQ_printf(m, " .%-40s: %d (%s)\n", "sysctl_sched_tunable_scaling",
  322. sysctl_sched_tunable_scaling,
  323. sched_tunable_scaling_names[sysctl_sched_tunable_scaling]);
  324. for_each_online_cpu(cpu)
  325. print_cpu(m, cpu);
  326. SEQ_printf(m, "\n");
  327. return 0;
  328. }
  329. #ifdef CONFIG_SYSRQ_SCHED_DEBUG
  330. void sysrq_sched_debug_show(void)
  331. {
  332. sched_debug_show(NULL, NULL);
  333. }
  334. #endif
  335. static int sched_debug_open(struct inode *inode, struct file *filp)
  336. {
  337. return single_open(filp, sched_debug_show, NULL);
  338. }
  339. static const struct file_operations sched_debug_fops = {
  340. .open = sched_debug_open,
  341. .read = seq_read,
  342. .llseek = seq_lseek,
  343. .release = single_release,
  344. };
  345. static int __init init_sched_debug_procfs(void)
  346. {
  347. struct proc_dir_entry *pe;
  348. pe = proc_create("sched_debug", 0444, NULL, &sched_debug_fops);
  349. if (!pe)
  350. return -ENOMEM;
  351. return 0;
  352. }
  353. __initcall(init_sched_debug_procfs);
  354. void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  355. {
  356. unsigned long nr_switches;
  357. SEQ_printf(m, "%s (%d, #threads: %d)\n", p->comm, p->pid,
  358. get_nr_threads(p));
  359. SEQ_printf(m,
  360. "---------------------------------------------------------\n");
  361. #define __P(F) \
  362. SEQ_printf(m, "%-35s:%21Ld\n", #F, (long long)F)
  363. #define P(F) \
  364. SEQ_printf(m, "%-35s:%21Ld\n", #F, (long long)p->F)
  365. #define __PN(F) \
  366. SEQ_printf(m, "%-35s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)F))
  367. #define PN(F) \
  368. SEQ_printf(m, "%-35s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)p->F))
  369. PN(se.exec_start);
  370. PN(se.vruntime);
  371. PN(se.sum_exec_runtime);
  372. nr_switches = p->nvcsw + p->nivcsw;
  373. #ifdef CONFIG_SCHEDSTATS
  374. PN(se.statistics.wait_start);
  375. PN(se.statistics.sleep_start);
  376. PN(se.statistics.block_start);
  377. PN(se.statistics.sleep_max);
  378. PN(se.statistics.block_max);
  379. PN(se.statistics.exec_max);
  380. PN(se.statistics.slice_max);
  381. PN(se.statistics.wait_max);
  382. PN(se.statistics.wait_sum);
  383. P(se.statistics.wait_count);
  384. PN(se.statistics.iowait_sum);
  385. P(se.statistics.iowait_count);
  386. P(se.nr_migrations);
  387. P(se.statistics.nr_migrations_cold);
  388. P(se.statistics.nr_failed_migrations_affine);
  389. P(se.statistics.nr_failed_migrations_running);
  390. P(se.statistics.nr_failed_migrations_hot);
  391. P(se.statistics.nr_forced_migrations);
  392. P(se.statistics.nr_wakeups);
  393. P(se.statistics.nr_wakeups_sync);
  394. P(se.statistics.nr_wakeups_migrate);
  395. P(se.statistics.nr_wakeups_local);
  396. P(se.statistics.nr_wakeups_remote);
  397. P(se.statistics.nr_wakeups_affine);
  398. P(se.statistics.nr_wakeups_affine_attempts);
  399. P(se.statistics.nr_wakeups_passive);
  400. P(se.statistics.nr_wakeups_idle);
  401. {
  402. u64 avg_atom, avg_per_cpu;
  403. avg_atom = p->se.sum_exec_runtime;
  404. if (nr_switches)
  405. do_div(avg_atom, nr_switches);
  406. else
  407. avg_atom = -1LL;
  408. avg_per_cpu = p->se.sum_exec_runtime;
  409. if (p->se.nr_migrations) {
  410. avg_per_cpu = div64_u64(avg_per_cpu,
  411. p->se.nr_migrations);
  412. } else {
  413. avg_per_cpu = -1LL;
  414. }
  415. __PN(avg_atom);
  416. __PN(avg_per_cpu);
  417. }
  418. #endif
  419. __P(nr_switches);
  420. SEQ_printf(m, "%-35s:%21Ld\n",
  421. "nr_voluntary_switches", (long long)p->nvcsw);
  422. SEQ_printf(m, "%-35s:%21Ld\n",
  423. "nr_involuntary_switches", (long long)p->nivcsw);
  424. P(se.load.weight);
  425. P(policy);
  426. P(prio);
  427. #undef PN
  428. #undef __PN
  429. #undef P
  430. #undef __P
  431. {
  432. unsigned int this_cpu = raw_smp_processor_id();
  433. u64 t0, t1;
  434. t0 = cpu_clock(this_cpu);
  435. t1 = cpu_clock(this_cpu);
  436. SEQ_printf(m, "%-35s:%21Ld\n",
  437. "clock-delta", (long long)(t1-t0));
  438. }
  439. }
  440. void proc_sched_set_task(struct task_struct *p)
  441. {
  442. #ifdef CONFIG_SCHEDSTATS
  443. memset(&p->se.statistics, 0, sizeof(p->se.statistics));
  444. #endif
  445. }