sched_debug.c 12 KB

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