sched_debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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->wait_start);
  64. PN(se->sleep_start);
  65. PN(se->block_start);
  66. PN(se->sleep_max);
  67. PN(se->block_max);
  68. PN(se->exec_max);
  69. PN(se->slice_max);
  70. PN(se->wait_max);
  71. PN(se->wait_sum);
  72. P(se->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.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. void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
  128. {
  129. s64 MIN_vruntime = -1, min_vruntime, max_vruntime = -1,
  130. spread, rq0_min_vruntime, spread0;
  131. struct rq *rq = &per_cpu(runqueues, cpu);
  132. struct sched_entity *last;
  133. unsigned long flags;
  134. #if defined(CONFIG_CGROUP_SCHED) && defined(CONFIG_FAIR_GROUP_SCHED)
  135. char path[128] = "";
  136. struct task_group *tg = cfs_rq->tg;
  137. cgroup_path(tg->css.cgroup, path, sizeof(path));
  138. SEQ_printf(m, "\ncfs_rq[%d]:%s\n", cpu, path);
  139. #else
  140. SEQ_printf(m, "\ncfs_rq[%d]:\n", cpu);
  141. #endif
  142. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "exec_clock",
  143. SPLIT_NS(cfs_rq->exec_clock));
  144. spin_lock_irqsave(&rq->lock, flags);
  145. if (cfs_rq->rb_leftmost)
  146. MIN_vruntime = (__pick_next_entity(cfs_rq))->vruntime;
  147. last = __pick_last_entity(cfs_rq);
  148. if (last)
  149. max_vruntime = last->vruntime;
  150. min_vruntime = cfs_rq->min_vruntime;
  151. rq0_min_vruntime = per_cpu(runqueues, 0).cfs.min_vruntime;
  152. spin_unlock_irqrestore(&rq->lock, flags);
  153. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "MIN_vruntime",
  154. SPLIT_NS(MIN_vruntime));
  155. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "min_vruntime",
  156. SPLIT_NS(min_vruntime));
  157. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "max_vruntime",
  158. SPLIT_NS(max_vruntime));
  159. spread = max_vruntime - MIN_vruntime;
  160. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread",
  161. SPLIT_NS(spread));
  162. spread0 = min_vruntime - rq0_min_vruntime;
  163. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread0",
  164. SPLIT_NS(spread0));
  165. SEQ_printf(m, " .%-30s: %ld\n", "nr_running", cfs_rq->nr_running);
  166. SEQ_printf(m, " .%-30s: %ld\n", "load", cfs_rq->load.weight);
  167. SEQ_printf(m, " .%-30s: %d\n", "nr_spread_over",
  168. cfs_rq->nr_spread_over);
  169. #ifdef CONFIG_FAIR_GROUP_SCHED
  170. #ifdef CONFIG_SMP
  171. SEQ_printf(m, " .%-30s: %lu\n", "shares", cfs_rq->shares);
  172. #endif
  173. print_cfs_group_stats(m, cpu, cfs_rq->tg);
  174. #endif
  175. }
  176. void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
  177. {
  178. #if defined(CONFIG_CGROUP_SCHED) && defined(CONFIG_RT_GROUP_SCHED)
  179. char path[128] = "";
  180. struct task_group *tg = rt_rq->tg;
  181. cgroup_path(tg->css.cgroup, path, sizeof(path));
  182. SEQ_printf(m, "\nrt_rq[%d]:%s\n", cpu, path);
  183. #else
  184. SEQ_printf(m, "\nrt_rq[%d]:\n", cpu);
  185. #endif
  186. #define P(x) \
  187. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rt_rq->x))
  188. #define PN(x) \
  189. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rt_rq->x))
  190. P(rt_nr_running);
  191. P(rt_throttled);
  192. PN(rt_time);
  193. PN(rt_runtime);
  194. #undef PN
  195. #undef P
  196. }
  197. static void print_cpu(struct seq_file *m, int cpu)
  198. {
  199. struct rq *rq = &per_cpu(runqueues, cpu);
  200. #ifdef CONFIG_X86
  201. {
  202. unsigned int freq = cpu_khz ? : 1;
  203. SEQ_printf(m, "\ncpu#%d, %u.%03u MHz\n",
  204. cpu, freq / 1000, (freq % 1000));
  205. }
  206. #else
  207. SEQ_printf(m, "\ncpu#%d\n", cpu);
  208. #endif
  209. #define P(x) \
  210. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rq->x))
  211. #define PN(x) \
  212. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rq->x))
  213. P(nr_running);
  214. SEQ_printf(m, " .%-30s: %lu\n", "load",
  215. rq->load.weight);
  216. P(nr_switches);
  217. P(nr_load_updates);
  218. P(nr_uninterruptible);
  219. SEQ_printf(m, " .%-30s: %lu\n", "jiffies", jiffies);
  220. PN(next_balance);
  221. P(curr->pid);
  222. PN(clock);
  223. P(cpu_load[0]);
  224. P(cpu_load[1]);
  225. P(cpu_load[2]);
  226. P(cpu_load[3]);
  227. P(cpu_load[4]);
  228. #undef P
  229. #undef PN
  230. #ifdef CONFIG_SCHEDSTATS
  231. #define P(n) SEQ_printf(m, " .%-30s: %d\n", #n, rq->n);
  232. P(yld_exp_empty);
  233. P(yld_act_empty);
  234. P(yld_both_empty);
  235. P(yld_count);
  236. P(sched_switch);
  237. P(sched_count);
  238. P(sched_goidle);
  239. P(ttwu_count);
  240. P(ttwu_local);
  241. P(bkl_count);
  242. #undef P
  243. #endif
  244. print_cfs_stats(m, cpu);
  245. print_rt_stats(m, cpu);
  246. print_rq(m, rq, cpu);
  247. }
  248. static int sched_debug_show(struct seq_file *m, void *v)
  249. {
  250. u64 now = ktime_to_ns(ktime_get());
  251. int cpu;
  252. SEQ_printf(m, "Sched Debug Version: v0.08, %s %.*s\n",
  253. init_utsname()->release,
  254. (int)strcspn(init_utsname()->version, " "),
  255. init_utsname()->version);
  256. SEQ_printf(m, "now at %Lu.%06ld msecs\n", SPLIT_NS(now));
  257. #define P(x) \
  258. SEQ_printf(m, " .%-40s: %Ld\n", #x, (long long)(x))
  259. #define PN(x) \
  260. SEQ_printf(m, " .%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
  261. PN(sysctl_sched_latency);
  262. PN(sysctl_sched_min_granularity);
  263. PN(sysctl_sched_wakeup_granularity);
  264. PN(sysctl_sched_child_runs_first);
  265. P(sysctl_sched_features);
  266. #undef PN
  267. #undef P
  268. for_each_online_cpu(cpu)
  269. print_cpu(m, cpu);
  270. SEQ_printf(m, "\n");
  271. return 0;
  272. }
  273. static void sysrq_sched_debug_show(void)
  274. {
  275. sched_debug_show(NULL, NULL);
  276. }
  277. static int sched_debug_open(struct inode *inode, struct file *filp)
  278. {
  279. return single_open(filp, sched_debug_show, NULL);
  280. }
  281. static const struct file_operations sched_debug_fops = {
  282. .open = sched_debug_open,
  283. .read = seq_read,
  284. .llseek = seq_lseek,
  285. .release = single_release,
  286. };
  287. static int __init init_sched_debug_procfs(void)
  288. {
  289. struct proc_dir_entry *pe;
  290. pe = proc_create("sched_debug", 0444, NULL, &sched_debug_fops);
  291. if (!pe)
  292. return -ENOMEM;
  293. return 0;
  294. }
  295. __initcall(init_sched_debug_procfs);
  296. void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  297. {
  298. unsigned long nr_switches;
  299. unsigned long flags;
  300. int num_threads = 1;
  301. if (lock_task_sighand(p, &flags)) {
  302. num_threads = atomic_read(&p->signal->count);
  303. unlock_task_sighand(p, &flags);
  304. }
  305. SEQ_printf(m, "%s (%d, #threads: %d)\n", p->comm, p->pid, num_threads);
  306. SEQ_printf(m,
  307. "---------------------------------------------------------\n");
  308. #define __P(F) \
  309. SEQ_printf(m, "%-35s:%21Ld\n", #F, (long long)F)
  310. #define P(F) \
  311. SEQ_printf(m, "%-35s:%21Ld\n", #F, (long long)p->F)
  312. #define __PN(F) \
  313. SEQ_printf(m, "%-35s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)F))
  314. #define PN(F) \
  315. SEQ_printf(m, "%-35s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)p->F))
  316. PN(se.exec_start);
  317. PN(se.vruntime);
  318. PN(se.sum_exec_runtime);
  319. PN(se.avg_overlap);
  320. nr_switches = p->nvcsw + p->nivcsw;
  321. #ifdef CONFIG_SCHEDSTATS
  322. PN(se.wait_start);
  323. PN(se.sleep_start);
  324. PN(se.block_start);
  325. PN(se.sleep_max);
  326. PN(se.block_max);
  327. PN(se.exec_max);
  328. PN(se.slice_max);
  329. PN(se.wait_max);
  330. PN(se.wait_sum);
  331. P(se.wait_count);
  332. P(sched_info.bkl_count);
  333. P(se.nr_migrations);
  334. P(se.nr_migrations_cold);
  335. P(se.nr_failed_migrations_affine);
  336. P(se.nr_failed_migrations_running);
  337. P(se.nr_failed_migrations_hot);
  338. P(se.nr_forced_migrations);
  339. P(se.nr_forced2_migrations);
  340. P(se.nr_wakeups);
  341. P(se.nr_wakeups_sync);
  342. P(se.nr_wakeups_migrate);
  343. P(se.nr_wakeups_local);
  344. P(se.nr_wakeups_remote);
  345. P(se.nr_wakeups_affine);
  346. P(se.nr_wakeups_affine_attempts);
  347. P(se.nr_wakeups_passive);
  348. P(se.nr_wakeups_idle);
  349. {
  350. u64 avg_atom, avg_per_cpu;
  351. avg_atom = p->se.sum_exec_runtime;
  352. if (nr_switches)
  353. do_div(avg_atom, nr_switches);
  354. else
  355. avg_atom = -1LL;
  356. avg_per_cpu = p->se.sum_exec_runtime;
  357. if (p->se.nr_migrations) {
  358. avg_per_cpu = div64_u64(avg_per_cpu,
  359. p->se.nr_migrations);
  360. } else {
  361. avg_per_cpu = -1LL;
  362. }
  363. __PN(avg_atom);
  364. __PN(avg_per_cpu);
  365. }
  366. #endif
  367. __P(nr_switches);
  368. SEQ_printf(m, "%-35s:%21Ld\n",
  369. "nr_voluntary_switches", (long long)p->nvcsw);
  370. SEQ_printf(m, "%-35s:%21Ld\n",
  371. "nr_involuntary_switches", (long long)p->nivcsw);
  372. P(se.load.weight);
  373. P(policy);
  374. P(prio);
  375. #undef PN
  376. #undef __PN
  377. #undef P
  378. #undef __P
  379. {
  380. unsigned int this_cpu = raw_smp_processor_id();
  381. u64 t0, t1;
  382. t0 = cpu_clock(this_cpu);
  383. t1 = cpu_clock(this_cpu);
  384. SEQ_printf(m, "%-35s:%21Ld\n",
  385. "clock-delta", (long long)(t1-t0));
  386. }
  387. }
  388. void proc_sched_set_task(struct task_struct *p)
  389. {
  390. #ifdef CONFIG_SCHEDSTATS
  391. p->se.wait_max = 0;
  392. p->se.wait_sum = 0;
  393. p->se.wait_count = 0;
  394. p->se.sleep_max = 0;
  395. p->se.sum_sleep_runtime = 0;
  396. p->se.block_max = 0;
  397. p->se.exec_max = 0;
  398. p->se.slice_max = 0;
  399. p->se.nr_migrations = 0;
  400. p->se.nr_migrations_cold = 0;
  401. p->se.nr_failed_migrations_affine = 0;
  402. p->se.nr_failed_migrations_running = 0;
  403. p->se.nr_failed_migrations_hot = 0;
  404. p->se.nr_forced_migrations = 0;
  405. p->se.nr_forced2_migrations = 0;
  406. p->se.nr_wakeups = 0;
  407. p->se.nr_wakeups_sync = 0;
  408. p->se.nr_wakeups_migrate = 0;
  409. p->se.nr_wakeups_local = 0;
  410. p->se.nr_wakeups_remote = 0;
  411. p->se.nr_wakeups_affine = 0;
  412. p->se.nr_wakeups_affine_attempts = 0;
  413. p->se.nr_wakeups_passive = 0;
  414. p->se.nr_wakeups_idle = 0;
  415. p->sched_info.bkl_count = 0;
  416. #endif
  417. p->se.sum_exec_runtime = 0;
  418. p->se.prev_sum_exec_runtime = 0;
  419. p->nvcsw = 0;
  420. p->nivcsw = 0;
  421. }