debug.c 12 KB

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