sched_debug.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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(long long nsec)
  32. {
  33. if (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(long long nsec)
  42. {
  43. if (nsec < 0)
  44. nsec = -nsec;
  45. return do_div(nsec, 1000000);
  46. }
  47. #define SPLIT_NS(x) nsec_high(x), nsec_low(x)
  48. static void
  49. print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
  50. {
  51. if (rq->curr == p)
  52. SEQ_printf(m, "R");
  53. else
  54. SEQ_printf(m, " ");
  55. SEQ_printf(m, "%15s %5d %9Ld.%06ld %9Ld %5d ",
  56. p->comm, p->pid,
  57. SPLIT_NS(p->se.vruntime),
  58. (long long)(p->nvcsw + p->nivcsw),
  59. p->prio);
  60. #ifdef CONFIG_SCHEDSTATS
  61. SEQ_printf(m, "%9Ld.%06ld %9Ld.%06ld %9Ld.%06ld\n",
  62. SPLIT_NS(p->se.vruntime),
  63. SPLIT_NS(p->se.sum_exec_runtime),
  64. SPLIT_NS(p->se.sum_sleep_runtime));
  65. #else
  66. SEQ_printf(m, "%15Ld %15Ld %15Ld.%06ld %15Ld.%06ld %15Ld.%06ld\n",
  67. 0LL, 0LL, 0LL, 0L, 0LL, 0L, 0LL, 0L);
  68. #endif
  69. }
  70. static void print_rq(struct seq_file *m, struct rq *rq, int rq_cpu)
  71. {
  72. struct task_struct *g, *p;
  73. SEQ_printf(m,
  74. "\nrunnable tasks:\n"
  75. " task PID tree-key switches prio"
  76. " exec-runtime sum-exec sum-sleep\n"
  77. "------------------------------------------------------"
  78. "----------------------------------------------------\n");
  79. read_lock_irq(&tasklist_lock);
  80. do_each_thread(g, p) {
  81. if (!p->se.on_rq || task_cpu(p) != rq_cpu)
  82. continue;
  83. print_task(m, rq, p);
  84. } while_each_thread(g, p);
  85. read_unlock_irq(&tasklist_lock);
  86. }
  87. void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
  88. {
  89. s64 MIN_vruntime = -1, min_vruntime, max_vruntime = -1,
  90. spread, rq0_min_vruntime, spread0;
  91. struct rq *rq = &per_cpu(runqueues, cpu);
  92. struct sched_entity *last;
  93. unsigned long flags;
  94. SEQ_printf(m, "\ncfs_rq\n");
  95. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "exec_clock",
  96. SPLIT_NS(cfs_rq->exec_clock));
  97. spin_lock_irqsave(&rq->lock, flags);
  98. if (cfs_rq->rb_leftmost)
  99. MIN_vruntime = (__pick_next_entity(cfs_rq))->vruntime;
  100. last = __pick_last_entity(cfs_rq);
  101. if (last)
  102. max_vruntime = last->vruntime;
  103. min_vruntime = rq->cfs.min_vruntime;
  104. rq0_min_vruntime = per_cpu(runqueues, 0).cfs.min_vruntime;
  105. spin_unlock_irqrestore(&rq->lock, flags);
  106. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "MIN_vruntime",
  107. SPLIT_NS(MIN_vruntime));
  108. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "min_vruntime",
  109. SPLIT_NS(min_vruntime));
  110. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "max_vruntime",
  111. SPLIT_NS(max_vruntime));
  112. spread = max_vruntime - MIN_vruntime;
  113. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread",
  114. SPLIT_NS(spread));
  115. spread0 = min_vruntime - rq0_min_vruntime;
  116. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread0",
  117. SPLIT_NS(spread0));
  118. SEQ_printf(m, " .%-30s: %ld\n", "nr_running", cfs_rq->nr_running);
  119. SEQ_printf(m, " .%-30s: %ld\n", "load", cfs_rq->load.weight);
  120. #ifdef CONFIG_SCHEDSTATS
  121. SEQ_printf(m, " .%-30s: %ld\n", "bkl_cnt",
  122. rq->bkl_cnt);
  123. #endif
  124. }
  125. static void print_cpu(struct seq_file *m, int cpu)
  126. {
  127. struct rq *rq = &per_cpu(runqueues, cpu);
  128. #ifdef CONFIG_X86
  129. {
  130. unsigned int freq = cpu_khz ? : 1;
  131. SEQ_printf(m, "\ncpu#%d, %u.%03u MHz\n",
  132. cpu, freq / 1000, (freq % 1000));
  133. }
  134. #else
  135. SEQ_printf(m, "\ncpu#%d\n", cpu);
  136. #endif
  137. #define P(x) \
  138. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rq->x))
  139. #define PN(x) \
  140. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rq->x))
  141. P(nr_running);
  142. SEQ_printf(m, " .%-30s: %lu\n", "load",
  143. rq->load.weight);
  144. P(nr_switches);
  145. P(nr_load_updates);
  146. P(nr_uninterruptible);
  147. SEQ_printf(m, " .%-30s: %lu\n", "jiffies", jiffies);
  148. PN(next_balance);
  149. P(curr->pid);
  150. PN(clock);
  151. PN(idle_clock);
  152. PN(prev_clock_raw);
  153. P(clock_warps);
  154. P(clock_overflows);
  155. P(clock_deep_idle_events);
  156. PN(clock_max_delta);
  157. P(cpu_load[0]);
  158. P(cpu_load[1]);
  159. P(cpu_load[2]);
  160. P(cpu_load[3]);
  161. P(cpu_load[4]);
  162. #undef P
  163. #undef PN
  164. print_cfs_stats(m, cpu);
  165. print_rq(m, rq, cpu);
  166. }
  167. static int sched_debug_show(struct seq_file *m, void *v)
  168. {
  169. u64 now = ktime_to_ns(ktime_get());
  170. int cpu;
  171. SEQ_printf(m, "Sched Debug Version: v0.05-v20, %s %.*s\n",
  172. init_utsname()->release,
  173. (int)strcspn(init_utsname()->version, " "),
  174. init_utsname()->version);
  175. SEQ_printf(m, "now at %Lu.%06ld msecs\n", SPLIT_NS(now));
  176. #define P(x) \
  177. SEQ_printf(m, " .%-40s: %Ld\n", #x, (long long)(x))
  178. #define PN(x) \
  179. SEQ_printf(m, " .%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
  180. PN(sysctl_sched_latency);
  181. PN(sysctl_sched_min_granularity);
  182. PN(sysctl_sched_wakeup_granularity);
  183. PN(sysctl_sched_batch_wakeup_granularity);
  184. PN(sysctl_sched_child_runs_first);
  185. P(sysctl_sched_features);
  186. #undef PN
  187. #undef P
  188. for_each_online_cpu(cpu)
  189. print_cpu(m, cpu);
  190. SEQ_printf(m, "\n");
  191. return 0;
  192. }
  193. static void sysrq_sched_debug_show(void)
  194. {
  195. sched_debug_show(NULL, NULL);
  196. }
  197. #ifdef CONFIG_FAIR_USER_SCHED
  198. static DEFINE_MUTEX(root_user_share_mutex);
  199. static int
  200. root_user_share_read_proc(char *page, char **start, off_t off, int count,
  201. int *eof, void *data)
  202. {
  203. int len;
  204. len = sprintf(page, "%d\n", init_task_grp_load);
  205. return len;
  206. }
  207. static int
  208. root_user_share_write_proc(struct file *file, const char __user *buffer,
  209. unsigned long count, void *data)
  210. {
  211. unsigned long shares;
  212. char kbuf[sizeof(unsigned long)+1];
  213. int rc = 0;
  214. if (copy_from_user(kbuf, buffer, sizeof(kbuf)))
  215. return -EFAULT;
  216. shares = simple_strtoul(kbuf, NULL, 0);
  217. if (!shares)
  218. shares = NICE_0_LOAD;
  219. mutex_lock(&root_user_share_mutex);
  220. init_task_grp_load = shares;
  221. rc = sched_group_set_shares(&init_task_grp, shares);
  222. mutex_unlock(&root_user_share_mutex);
  223. return (rc < 0 ? rc : count);
  224. }
  225. #endif /* CONFIG_FAIR_USER_SCHED */
  226. static int sched_debug_open(struct inode *inode, struct file *filp)
  227. {
  228. return single_open(filp, sched_debug_show, NULL);
  229. }
  230. static struct file_operations sched_debug_fops = {
  231. .open = sched_debug_open,
  232. .read = seq_read,
  233. .llseek = seq_lseek,
  234. .release = single_release,
  235. };
  236. static int __init init_sched_debug_procfs(void)
  237. {
  238. struct proc_dir_entry *pe;
  239. pe = create_proc_entry("sched_debug", 0644, NULL);
  240. if (!pe)
  241. return -ENOMEM;
  242. pe->proc_fops = &sched_debug_fops;
  243. #ifdef CONFIG_FAIR_USER_SCHED
  244. pe = create_proc_entry("root_user_share", 0644, NULL);
  245. if (!pe)
  246. return -ENOMEM;
  247. pe->read_proc = root_user_share_read_proc;
  248. pe->write_proc = root_user_share_write_proc;
  249. #endif
  250. return 0;
  251. }
  252. __initcall(init_sched_debug_procfs);
  253. void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  254. {
  255. unsigned long flags;
  256. int num_threads = 1;
  257. rcu_read_lock();
  258. if (lock_task_sighand(p, &flags)) {
  259. num_threads = atomic_read(&p->signal->count);
  260. unlock_task_sighand(p, &flags);
  261. }
  262. rcu_read_unlock();
  263. SEQ_printf(m, "%s (%d, #threads: %d)\n", p->comm, p->pid, num_threads);
  264. SEQ_printf(m, "----------------------------------------------\n");
  265. #define P(F) \
  266. SEQ_printf(m, "%-25s:%20Ld\n", #F, (long long)p->F)
  267. #define PN(F) \
  268. SEQ_printf(m, "%-25s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)p->F))
  269. PN(se.exec_start);
  270. PN(se.vruntime);
  271. PN(se.sum_exec_runtime);
  272. #ifdef CONFIG_SCHEDSTATS
  273. PN(se.wait_start);
  274. PN(se.sleep_start);
  275. PN(se.block_start);
  276. PN(se.sleep_max);
  277. PN(se.block_max);
  278. PN(se.exec_max);
  279. PN(se.slice_max);
  280. PN(se.wait_max);
  281. P(sched_info.bkl_cnt);
  282. #endif
  283. SEQ_printf(m, "%-25s:%20Ld\n",
  284. "nr_switches", (long long)(p->nvcsw + p->nivcsw));
  285. P(se.load.weight);
  286. P(policy);
  287. P(prio);
  288. #undef P
  289. #undef PN
  290. {
  291. u64 t0, t1;
  292. t0 = sched_clock();
  293. t1 = sched_clock();
  294. SEQ_printf(m, "%-25s:%20Ld\n",
  295. "clock-delta", (long long)(t1-t0));
  296. }
  297. }
  298. void proc_sched_set_task(struct task_struct *p)
  299. {
  300. #ifdef CONFIG_SCHEDSTATS
  301. p->se.sleep_max = 0;
  302. p->se.block_max = 0;
  303. p->se.exec_max = 0;
  304. p->se.slice_max = 0;
  305. p->se.wait_max = 0;
  306. p->sched_info.bkl_cnt = 0;
  307. #endif
  308. p->se.sum_exec_runtime = 0;
  309. p->se.prev_sum_exec_runtime = 0;
  310. }