sched_debug.c 12 KB

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