debug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. cgroup_path(tg->css.cgroup, group_path, PATH_MAX);
  97. return group_path;
  98. }
  99. #endif
  100. static void
  101. print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
  102. {
  103. if (rq->curr == p)
  104. SEQ_printf(m, "R");
  105. else
  106. SEQ_printf(m, " ");
  107. SEQ_printf(m, "%15s %5d %9Ld.%06ld %9Ld %5d ",
  108. p->comm, task_pid_nr(p),
  109. SPLIT_NS(p->se.vruntime),
  110. (long long)(p->nvcsw + p->nivcsw),
  111. p->prio);
  112. #ifdef CONFIG_SCHEDSTATS
  113. SEQ_printf(m, "%9Ld.%06ld %9Ld.%06ld %9Ld.%06ld",
  114. SPLIT_NS(p->se.vruntime),
  115. SPLIT_NS(p->se.sum_exec_runtime),
  116. SPLIT_NS(p->se.statistics.sum_sleep_runtime));
  117. #else
  118. SEQ_printf(m, "%15Ld %15Ld %15Ld.%06ld %15Ld.%06ld %15Ld.%06ld",
  119. 0LL, 0LL, 0LL, 0L, 0LL, 0L, 0LL, 0L);
  120. #endif
  121. #ifdef CONFIG_CGROUP_SCHED
  122. SEQ_printf(m, " %s", task_group_path(task_group(p)));
  123. #endif
  124. SEQ_printf(m, "\n");
  125. }
  126. static void print_rq(struct seq_file *m, struct rq *rq, int rq_cpu)
  127. {
  128. struct task_struct *g, *p;
  129. unsigned long flags;
  130. SEQ_printf(m,
  131. "\nrunnable tasks:\n"
  132. " task PID tree-key switches prio"
  133. " exec-runtime sum-exec sum-sleep\n"
  134. "------------------------------------------------------"
  135. "----------------------------------------------------\n");
  136. read_lock_irqsave(&tasklist_lock, flags);
  137. do_each_thread(g, p) {
  138. if (!p->on_rq || task_cpu(p) != rq_cpu)
  139. continue;
  140. print_task(m, rq, p);
  141. } while_each_thread(g, p);
  142. read_unlock_irqrestore(&tasklist_lock, flags);
  143. }
  144. void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
  145. {
  146. s64 MIN_vruntime = -1, min_vruntime, max_vruntime = -1,
  147. spread, rq0_min_vruntime, spread0;
  148. struct rq *rq = cpu_rq(cpu);
  149. struct sched_entity *last;
  150. unsigned long flags;
  151. #ifdef CONFIG_FAIR_GROUP_SCHED
  152. SEQ_printf(m, "\ncfs_rq[%d]:%s\n", cpu, task_group_path(cfs_rq->tg));
  153. #else
  154. SEQ_printf(m, "\ncfs_rq[%d]:\n", cpu);
  155. #endif
  156. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "exec_clock",
  157. SPLIT_NS(cfs_rq->exec_clock));
  158. raw_spin_lock_irqsave(&rq->lock, flags);
  159. if (cfs_rq->rb_leftmost)
  160. MIN_vruntime = (__pick_first_entity(cfs_rq))->vruntime;
  161. last = __pick_last_entity(cfs_rq);
  162. if (last)
  163. max_vruntime = last->vruntime;
  164. min_vruntime = cfs_rq->min_vruntime;
  165. rq0_min_vruntime = cpu_rq(0)->cfs.min_vruntime;
  166. raw_spin_unlock_irqrestore(&rq->lock, flags);
  167. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "MIN_vruntime",
  168. SPLIT_NS(MIN_vruntime));
  169. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "min_vruntime",
  170. SPLIT_NS(min_vruntime));
  171. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "max_vruntime",
  172. SPLIT_NS(max_vruntime));
  173. spread = max_vruntime - MIN_vruntime;
  174. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread",
  175. SPLIT_NS(spread));
  176. spread0 = min_vruntime - rq0_min_vruntime;
  177. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread0",
  178. SPLIT_NS(spread0));
  179. SEQ_printf(m, " .%-30s: %d\n", "nr_spread_over",
  180. cfs_rq->nr_spread_over);
  181. SEQ_printf(m, " .%-30s: %d\n", "nr_running", cfs_rq->nr_running);
  182. SEQ_printf(m, " .%-30s: %ld\n", "load", cfs_rq->load.weight);
  183. #ifdef CONFIG_SMP
  184. SEQ_printf(m, " .%-30s: %ld\n", "runnable_load_avg",
  185. cfs_rq->runnable_load_avg);
  186. SEQ_printf(m, " .%-30s: %ld\n", "blocked_load_avg",
  187. cfs_rq->blocked_load_avg);
  188. #ifdef CONFIG_FAIR_GROUP_SCHED
  189. SEQ_printf(m, " .%-30s: %ld\n", "tg_load_contrib",
  190. cfs_rq->tg_load_contrib);
  191. SEQ_printf(m, " .%-30s: %d\n", "tg_runnable_contrib",
  192. cfs_rq->tg_runnable_contrib);
  193. SEQ_printf(m, " .%-30s: %ld\n", "tg_load_avg",
  194. atomic_long_read(&cfs_rq->tg->load_avg));
  195. SEQ_printf(m, " .%-30s: %d\n", "tg->runnable_avg",
  196. atomic_read(&cfs_rq->tg->runnable_avg));
  197. #endif
  198. #endif
  199. #ifdef CONFIG_FAIR_GROUP_SCHED
  200. print_cfs_group_stats(m, cpu, cfs_rq->tg);
  201. #endif
  202. }
  203. void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
  204. {
  205. #ifdef CONFIG_RT_GROUP_SCHED
  206. SEQ_printf(m, "\nrt_rq[%d]:%s\n", cpu, task_group_path(rt_rq->tg));
  207. #else
  208. SEQ_printf(m, "\nrt_rq[%d]:\n", cpu);
  209. #endif
  210. #define P(x) \
  211. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rt_rq->x))
  212. #define PN(x) \
  213. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rt_rq->x))
  214. P(rt_nr_running);
  215. P(rt_throttled);
  216. PN(rt_time);
  217. PN(rt_runtime);
  218. #undef PN
  219. #undef P
  220. }
  221. extern __read_mostly int sched_clock_running;
  222. static void print_cpu(struct seq_file *m, int cpu)
  223. {
  224. struct rq *rq = cpu_rq(cpu);
  225. unsigned long flags;
  226. #ifdef CONFIG_X86
  227. {
  228. unsigned int freq = cpu_khz ? : 1;
  229. SEQ_printf(m, "cpu#%d, %u.%03u MHz\n",
  230. cpu, freq / 1000, (freq % 1000));
  231. }
  232. #else
  233. SEQ_printf(m, "cpu#%d\n", cpu);
  234. #endif
  235. #define P(x) \
  236. do { \
  237. if (sizeof(rq->x) == 4) \
  238. SEQ_printf(m, " .%-30s: %ld\n", #x, (long)(rq->x)); \
  239. else \
  240. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rq->x));\
  241. } while (0)
  242. #define PN(x) \
  243. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rq->x))
  244. P(nr_running);
  245. SEQ_printf(m, " .%-30s: %lu\n", "load",
  246. rq->load.weight);
  247. P(nr_switches);
  248. P(nr_load_updates);
  249. P(nr_uninterruptible);
  250. PN(next_balance);
  251. SEQ_printf(m, " .%-30s: %ld\n", "curr->pid", (long)(task_pid_nr(rq->curr)));
  252. PN(clock);
  253. P(cpu_load[0]);
  254. P(cpu_load[1]);
  255. P(cpu_load[2]);
  256. P(cpu_load[3]);
  257. P(cpu_load[4]);
  258. #undef P
  259. #undef PN
  260. #ifdef CONFIG_SCHEDSTATS
  261. #define P(n) SEQ_printf(m, " .%-30s: %d\n", #n, rq->n);
  262. #define P64(n) SEQ_printf(m, " .%-30s: %Ld\n", #n, rq->n);
  263. P(yld_count);
  264. P(sched_count);
  265. P(sched_goidle);
  266. #ifdef CONFIG_SMP
  267. P64(avg_idle);
  268. #endif
  269. P(ttwu_count);
  270. P(ttwu_local);
  271. #undef P
  272. #undef P64
  273. #endif
  274. spin_lock_irqsave(&sched_debug_lock, flags);
  275. print_cfs_stats(m, cpu);
  276. print_rt_stats(m, cpu);
  277. rcu_read_lock();
  278. print_rq(m, rq, cpu);
  279. rcu_read_unlock();
  280. spin_unlock_irqrestore(&sched_debug_lock, flags);
  281. SEQ_printf(m, "\n");
  282. }
  283. static const char *sched_tunable_scaling_names[] = {
  284. "none",
  285. "logaritmic",
  286. "linear"
  287. };
  288. static void sched_debug_header(struct seq_file *m)
  289. {
  290. u64 ktime, sched_clk, cpu_clk;
  291. unsigned long flags;
  292. local_irq_save(flags);
  293. ktime = ktime_to_ns(ktime_get());
  294. sched_clk = sched_clock();
  295. cpu_clk = local_clock();
  296. local_irq_restore(flags);
  297. SEQ_printf(m, "Sched Debug Version: v0.10, %s %.*s\n",
  298. init_utsname()->release,
  299. (int)strcspn(init_utsname()->version, " "),
  300. init_utsname()->version);
  301. #define P(x) \
  302. SEQ_printf(m, "%-40s: %Ld\n", #x, (long long)(x))
  303. #define PN(x) \
  304. SEQ_printf(m, "%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
  305. PN(ktime);
  306. PN(sched_clk);
  307. PN(cpu_clk);
  308. P(jiffies);
  309. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  310. P(sched_clock_stable);
  311. #endif
  312. #undef PN
  313. #undef P
  314. SEQ_printf(m, "\n");
  315. SEQ_printf(m, "sysctl_sched\n");
  316. #define P(x) \
  317. SEQ_printf(m, " .%-40s: %Ld\n", #x, (long long)(x))
  318. #define PN(x) \
  319. SEQ_printf(m, " .%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
  320. PN(sysctl_sched_latency);
  321. PN(sysctl_sched_min_granularity);
  322. PN(sysctl_sched_wakeup_granularity);
  323. P(sysctl_sched_child_runs_first);
  324. P(sysctl_sched_features);
  325. #undef PN
  326. #undef P
  327. SEQ_printf(m, " .%-40s: %d (%s)\n",
  328. "sysctl_sched_tunable_scaling",
  329. sysctl_sched_tunable_scaling,
  330. sched_tunable_scaling_names[sysctl_sched_tunable_scaling]);
  331. SEQ_printf(m, "\n");
  332. }
  333. static int sched_debug_show(struct seq_file *m, void *v)
  334. {
  335. int cpu = (unsigned long)(v - 2);
  336. if (cpu != -1)
  337. print_cpu(m, cpu);
  338. else
  339. sched_debug_header(m);
  340. return 0;
  341. }
  342. void sysrq_sched_debug_show(void)
  343. {
  344. int cpu;
  345. sched_debug_header(NULL);
  346. for_each_online_cpu(cpu)
  347. print_cpu(NULL, cpu);
  348. }
  349. /*
  350. * This itererator needs some explanation.
  351. * It returns 1 for the header position.
  352. * This means 2 is cpu 0.
  353. * In a hotplugged system some cpus, including cpu 0, may be missing so we have
  354. * to use cpumask_* to iterate over the cpus.
  355. */
  356. static void *sched_debug_start(struct seq_file *file, loff_t *offset)
  357. {
  358. unsigned long n = *offset;
  359. if (n == 0)
  360. return (void *) 1;
  361. n--;
  362. if (n > 0)
  363. n = cpumask_next(n - 1, cpu_online_mask);
  364. else
  365. n = cpumask_first(cpu_online_mask);
  366. *offset = n + 1;
  367. if (n < nr_cpu_ids)
  368. return (void *)(unsigned long)(n + 2);
  369. return NULL;
  370. }
  371. static void *sched_debug_next(struct seq_file *file, void *data, loff_t *offset)
  372. {
  373. (*offset)++;
  374. return sched_debug_start(file, offset);
  375. }
  376. static void sched_debug_stop(struct seq_file *file, void *data)
  377. {
  378. }
  379. static const struct seq_operations sched_debug_sops = {
  380. .start = sched_debug_start,
  381. .next = sched_debug_next,
  382. .stop = sched_debug_stop,
  383. .show = sched_debug_show,
  384. };
  385. static int sched_debug_release(struct inode *inode, struct file *file)
  386. {
  387. seq_release(inode, file);
  388. return 0;
  389. }
  390. static int sched_debug_open(struct inode *inode, struct file *filp)
  391. {
  392. int ret = 0;
  393. ret = seq_open(filp, &sched_debug_sops);
  394. return ret;
  395. }
  396. static const struct file_operations sched_debug_fops = {
  397. .open = sched_debug_open,
  398. .read = seq_read,
  399. .llseek = seq_lseek,
  400. .release = sched_debug_release,
  401. };
  402. static int __init init_sched_debug_procfs(void)
  403. {
  404. struct proc_dir_entry *pe;
  405. pe = proc_create("sched_debug", 0444, NULL, &sched_debug_fops);
  406. if (!pe)
  407. return -ENOMEM;
  408. return 0;
  409. }
  410. __initcall(init_sched_debug_procfs);
  411. void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  412. {
  413. unsigned long nr_switches;
  414. SEQ_printf(m, "%s (%d, #threads: %d)\n", p->comm, task_pid_nr(p),
  415. get_nr_threads(p));
  416. SEQ_printf(m,
  417. "---------------------------------------------------------"
  418. "----------\n");
  419. #define __P(F) \
  420. SEQ_printf(m, "%-45s:%21Ld\n", #F, (long long)F)
  421. #define P(F) \
  422. SEQ_printf(m, "%-45s:%21Ld\n", #F, (long long)p->F)
  423. #define __PN(F) \
  424. SEQ_printf(m, "%-45s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)F))
  425. #define PN(F) \
  426. SEQ_printf(m, "%-45s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)p->F))
  427. PN(se.exec_start);
  428. PN(se.vruntime);
  429. PN(se.sum_exec_runtime);
  430. nr_switches = p->nvcsw + p->nivcsw;
  431. #ifdef CONFIG_SCHEDSTATS
  432. PN(se.statistics.wait_start);
  433. PN(se.statistics.sleep_start);
  434. PN(se.statistics.block_start);
  435. PN(se.statistics.sleep_max);
  436. PN(se.statistics.block_max);
  437. PN(se.statistics.exec_max);
  438. PN(se.statistics.slice_max);
  439. PN(se.statistics.wait_max);
  440. PN(se.statistics.wait_sum);
  441. P(se.statistics.wait_count);
  442. PN(se.statistics.iowait_sum);
  443. P(se.statistics.iowait_count);
  444. P(se.nr_migrations);
  445. P(se.statistics.nr_migrations_cold);
  446. P(se.statistics.nr_failed_migrations_affine);
  447. P(se.statistics.nr_failed_migrations_running);
  448. P(se.statistics.nr_failed_migrations_hot);
  449. P(se.statistics.nr_forced_migrations);
  450. P(se.statistics.nr_wakeups);
  451. P(se.statistics.nr_wakeups_sync);
  452. P(se.statistics.nr_wakeups_migrate);
  453. P(se.statistics.nr_wakeups_local);
  454. P(se.statistics.nr_wakeups_remote);
  455. P(se.statistics.nr_wakeups_affine);
  456. P(se.statistics.nr_wakeups_affine_attempts);
  457. P(se.statistics.nr_wakeups_passive);
  458. P(se.statistics.nr_wakeups_idle);
  459. {
  460. u64 avg_atom, avg_per_cpu;
  461. avg_atom = p->se.sum_exec_runtime;
  462. if (nr_switches)
  463. do_div(avg_atom, nr_switches);
  464. else
  465. avg_atom = -1LL;
  466. avg_per_cpu = p->se.sum_exec_runtime;
  467. if (p->se.nr_migrations) {
  468. avg_per_cpu = div64_u64(avg_per_cpu,
  469. p->se.nr_migrations);
  470. } else {
  471. avg_per_cpu = -1LL;
  472. }
  473. __PN(avg_atom);
  474. __PN(avg_per_cpu);
  475. }
  476. #endif
  477. __P(nr_switches);
  478. SEQ_printf(m, "%-45s:%21Ld\n",
  479. "nr_voluntary_switches", (long long)p->nvcsw);
  480. SEQ_printf(m, "%-45s:%21Ld\n",
  481. "nr_involuntary_switches", (long long)p->nivcsw);
  482. P(se.load.weight);
  483. #ifdef CONFIG_SMP
  484. P(se.avg.runnable_avg_sum);
  485. P(se.avg.runnable_avg_period);
  486. P(se.avg.load_avg_contrib);
  487. P(se.avg.decay_count);
  488. #endif
  489. P(policy);
  490. P(prio);
  491. #undef PN
  492. #undef __PN
  493. #undef P
  494. #undef __P
  495. {
  496. unsigned int this_cpu = raw_smp_processor_id();
  497. u64 t0, t1;
  498. t0 = cpu_clock(this_cpu);
  499. t1 = cpu_clock(this_cpu);
  500. SEQ_printf(m, "%-45s:%21Ld\n",
  501. "clock-delta", (long long)(t1-t0));
  502. }
  503. }
  504. void proc_sched_set_task(struct task_struct *p)
  505. {
  506. #ifdef CONFIG_SCHEDSTATS
  507. memset(&p->se.statistics, 0, sizeof(p->se.statistics));
  508. #endif
  509. }