debug.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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/mm.h>
  14. #include <linux/sched/task.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/utsname.h>
  18. #include <linux/mempolicy.h>
  19. #include <linux/debugfs.h>
  20. #include "sched.h"
  21. static DEFINE_SPINLOCK(sched_debug_lock);
  22. /*
  23. * This allows printing both to /proc/sched_debug and
  24. * to the console
  25. */
  26. #define SEQ_printf(m, x...) \
  27. do { \
  28. if (m) \
  29. seq_printf(m, x); \
  30. else \
  31. pr_cont(x); \
  32. } while (0)
  33. /*
  34. * Ease the printing of nsec fields:
  35. */
  36. static long long nsec_high(unsigned long long nsec)
  37. {
  38. if ((long long)nsec < 0) {
  39. nsec = -nsec;
  40. do_div(nsec, 1000000);
  41. return -nsec;
  42. }
  43. do_div(nsec, 1000000);
  44. return nsec;
  45. }
  46. static unsigned long nsec_low(unsigned long long nsec)
  47. {
  48. if ((long long)nsec < 0)
  49. nsec = -nsec;
  50. return do_div(nsec, 1000000);
  51. }
  52. #define SPLIT_NS(x) nsec_high(x), nsec_low(x)
  53. #define SCHED_FEAT(name, enabled) \
  54. #name ,
  55. static const char * const sched_feat_names[] = {
  56. #include "features.h"
  57. };
  58. #undef SCHED_FEAT
  59. static int sched_feat_show(struct seq_file *m, void *v)
  60. {
  61. int i;
  62. for (i = 0; i < __SCHED_FEAT_NR; i++) {
  63. if (!(sysctl_sched_features & (1UL << i)))
  64. seq_puts(m, "NO_");
  65. seq_printf(m, "%s ", sched_feat_names[i]);
  66. }
  67. seq_puts(m, "\n");
  68. return 0;
  69. }
  70. #ifdef HAVE_JUMP_LABEL
  71. #define jump_label_key__true STATIC_KEY_INIT_TRUE
  72. #define jump_label_key__false STATIC_KEY_INIT_FALSE
  73. #define SCHED_FEAT(name, enabled) \
  74. jump_label_key__##enabled ,
  75. struct static_key sched_feat_keys[__SCHED_FEAT_NR] = {
  76. #include "features.h"
  77. };
  78. #undef SCHED_FEAT
  79. static void sched_feat_disable(int i)
  80. {
  81. static_key_disable(&sched_feat_keys[i]);
  82. }
  83. static void sched_feat_enable(int i)
  84. {
  85. static_key_enable(&sched_feat_keys[i]);
  86. }
  87. #else
  88. static void sched_feat_disable(int i) { };
  89. static void sched_feat_enable(int i) { };
  90. #endif /* HAVE_JUMP_LABEL */
  91. static int sched_feat_set(char *cmp)
  92. {
  93. int i;
  94. int neg = 0;
  95. if (strncmp(cmp, "NO_", 3) == 0) {
  96. neg = 1;
  97. cmp += 3;
  98. }
  99. for (i = 0; i < __SCHED_FEAT_NR; i++) {
  100. if (strcmp(cmp, sched_feat_names[i]) == 0) {
  101. if (neg) {
  102. sysctl_sched_features &= ~(1UL << i);
  103. sched_feat_disable(i);
  104. } else {
  105. sysctl_sched_features |= (1UL << i);
  106. sched_feat_enable(i);
  107. }
  108. break;
  109. }
  110. }
  111. return i;
  112. }
  113. static ssize_t
  114. sched_feat_write(struct file *filp, const char __user *ubuf,
  115. size_t cnt, loff_t *ppos)
  116. {
  117. char buf[64];
  118. char *cmp;
  119. int i;
  120. struct inode *inode;
  121. if (cnt > 63)
  122. cnt = 63;
  123. if (copy_from_user(&buf, ubuf, cnt))
  124. return -EFAULT;
  125. buf[cnt] = 0;
  126. cmp = strstrip(buf);
  127. /* Ensure the static_key remains in a consistent state */
  128. inode = file_inode(filp);
  129. inode_lock(inode);
  130. i = sched_feat_set(cmp);
  131. inode_unlock(inode);
  132. if (i == __SCHED_FEAT_NR)
  133. return -EINVAL;
  134. *ppos += cnt;
  135. return cnt;
  136. }
  137. static int sched_feat_open(struct inode *inode, struct file *filp)
  138. {
  139. return single_open(filp, sched_feat_show, NULL);
  140. }
  141. static const struct file_operations sched_feat_fops = {
  142. .open = sched_feat_open,
  143. .write = sched_feat_write,
  144. .read = seq_read,
  145. .llseek = seq_lseek,
  146. .release = single_release,
  147. };
  148. __read_mostly bool sched_debug_enabled;
  149. static __init int sched_init_debug(void)
  150. {
  151. debugfs_create_file("sched_features", 0644, NULL, NULL,
  152. &sched_feat_fops);
  153. debugfs_create_bool("sched_debug", 0644, NULL,
  154. &sched_debug_enabled);
  155. return 0;
  156. }
  157. late_initcall(sched_init_debug);
  158. #ifdef CONFIG_SMP
  159. #ifdef CONFIG_SYSCTL
  160. static struct ctl_table sd_ctl_dir[] = {
  161. {
  162. .procname = "sched_domain",
  163. .mode = 0555,
  164. },
  165. {}
  166. };
  167. static struct ctl_table sd_ctl_root[] = {
  168. {
  169. .procname = "kernel",
  170. .mode = 0555,
  171. .child = sd_ctl_dir,
  172. },
  173. {}
  174. };
  175. static struct ctl_table *sd_alloc_ctl_entry(int n)
  176. {
  177. struct ctl_table *entry =
  178. kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
  179. return entry;
  180. }
  181. static void sd_free_ctl_entry(struct ctl_table **tablep)
  182. {
  183. struct ctl_table *entry;
  184. /*
  185. * In the intermediate directories, both the child directory and
  186. * procname are dynamically allocated and could fail but the mode
  187. * will always be set. In the lowest directory the names are
  188. * static strings and all have proc handlers.
  189. */
  190. for (entry = *tablep; entry->mode; entry++) {
  191. if (entry->child)
  192. sd_free_ctl_entry(&entry->child);
  193. if (entry->proc_handler == NULL)
  194. kfree(entry->procname);
  195. }
  196. kfree(*tablep);
  197. *tablep = NULL;
  198. }
  199. static int min_load_idx = 0;
  200. static int max_load_idx = CPU_LOAD_IDX_MAX-1;
  201. static void
  202. set_table_entry(struct ctl_table *entry,
  203. const char *procname, void *data, int maxlen,
  204. umode_t mode, proc_handler *proc_handler,
  205. bool load_idx)
  206. {
  207. entry->procname = procname;
  208. entry->data = data;
  209. entry->maxlen = maxlen;
  210. entry->mode = mode;
  211. entry->proc_handler = proc_handler;
  212. if (load_idx) {
  213. entry->extra1 = &min_load_idx;
  214. entry->extra2 = &max_load_idx;
  215. }
  216. }
  217. static struct ctl_table *
  218. sd_alloc_ctl_domain_table(struct sched_domain *sd)
  219. {
  220. struct ctl_table *table = sd_alloc_ctl_entry(14);
  221. if (table == NULL)
  222. return NULL;
  223. set_table_entry(&table[0], "min_interval", &sd->min_interval,
  224. sizeof(long), 0644, proc_doulongvec_minmax, false);
  225. set_table_entry(&table[1], "max_interval", &sd->max_interval,
  226. sizeof(long), 0644, proc_doulongvec_minmax, false);
  227. set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
  228. sizeof(int), 0644, proc_dointvec_minmax, true);
  229. set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
  230. sizeof(int), 0644, proc_dointvec_minmax, true);
  231. set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
  232. sizeof(int), 0644, proc_dointvec_minmax, true);
  233. set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
  234. sizeof(int), 0644, proc_dointvec_minmax, true);
  235. set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
  236. sizeof(int), 0644, proc_dointvec_minmax, true);
  237. set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
  238. sizeof(int), 0644, proc_dointvec_minmax, false);
  239. set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
  240. sizeof(int), 0644, proc_dointvec_minmax, false);
  241. set_table_entry(&table[9], "cache_nice_tries",
  242. &sd->cache_nice_tries,
  243. sizeof(int), 0644, proc_dointvec_minmax, false);
  244. set_table_entry(&table[10], "flags", &sd->flags,
  245. sizeof(int), 0644, proc_dointvec_minmax, false);
  246. set_table_entry(&table[11], "max_newidle_lb_cost",
  247. &sd->max_newidle_lb_cost,
  248. sizeof(long), 0644, proc_doulongvec_minmax, false);
  249. set_table_entry(&table[12], "name", sd->name,
  250. CORENAME_MAX_SIZE, 0444, proc_dostring, false);
  251. /* &table[13] is terminator */
  252. return table;
  253. }
  254. static struct ctl_table *sd_alloc_ctl_cpu_table(int cpu)
  255. {
  256. struct ctl_table *entry, *table;
  257. struct sched_domain *sd;
  258. int domain_num = 0, i;
  259. char buf[32];
  260. for_each_domain(cpu, sd)
  261. domain_num++;
  262. entry = table = sd_alloc_ctl_entry(domain_num + 1);
  263. if (table == NULL)
  264. return NULL;
  265. i = 0;
  266. for_each_domain(cpu, sd) {
  267. snprintf(buf, 32, "domain%d", i);
  268. entry->procname = kstrdup(buf, GFP_KERNEL);
  269. entry->mode = 0555;
  270. entry->child = sd_alloc_ctl_domain_table(sd);
  271. entry++;
  272. i++;
  273. }
  274. return table;
  275. }
  276. static cpumask_var_t sd_sysctl_cpus;
  277. static struct ctl_table_header *sd_sysctl_header;
  278. void register_sched_domain_sysctl(void)
  279. {
  280. static struct ctl_table *cpu_entries;
  281. static struct ctl_table **cpu_idx;
  282. char buf[32];
  283. int i;
  284. if (!cpu_entries) {
  285. cpu_entries = sd_alloc_ctl_entry(num_possible_cpus() + 1);
  286. if (!cpu_entries)
  287. return;
  288. WARN_ON(sd_ctl_dir[0].child);
  289. sd_ctl_dir[0].child = cpu_entries;
  290. }
  291. if (!cpu_idx) {
  292. struct ctl_table *e = cpu_entries;
  293. cpu_idx = kcalloc(nr_cpu_ids, sizeof(struct ctl_table*), GFP_KERNEL);
  294. if (!cpu_idx)
  295. return;
  296. /* deal with sparse possible map */
  297. for_each_possible_cpu(i) {
  298. cpu_idx[i] = e;
  299. e++;
  300. }
  301. }
  302. if (!cpumask_available(sd_sysctl_cpus)) {
  303. if (!alloc_cpumask_var(&sd_sysctl_cpus, GFP_KERNEL))
  304. return;
  305. /* init to possible to not have holes in @cpu_entries */
  306. cpumask_copy(sd_sysctl_cpus, cpu_possible_mask);
  307. }
  308. for_each_cpu(i, sd_sysctl_cpus) {
  309. struct ctl_table *e = cpu_idx[i];
  310. if (e->child)
  311. sd_free_ctl_entry(&e->child);
  312. if (!e->procname) {
  313. snprintf(buf, 32, "cpu%d", i);
  314. e->procname = kstrdup(buf, GFP_KERNEL);
  315. }
  316. e->mode = 0555;
  317. e->child = sd_alloc_ctl_cpu_table(i);
  318. __cpumask_clear_cpu(i, sd_sysctl_cpus);
  319. }
  320. WARN_ON(sd_sysctl_header);
  321. sd_sysctl_header = register_sysctl_table(sd_ctl_root);
  322. }
  323. void dirty_sched_domain_sysctl(int cpu)
  324. {
  325. if (cpumask_available(sd_sysctl_cpus))
  326. __cpumask_set_cpu(cpu, sd_sysctl_cpus);
  327. }
  328. /* may be called multiple times per register */
  329. void unregister_sched_domain_sysctl(void)
  330. {
  331. unregister_sysctl_table(sd_sysctl_header);
  332. sd_sysctl_header = NULL;
  333. }
  334. #endif /* CONFIG_SYSCTL */
  335. #endif /* CONFIG_SMP */
  336. #ifdef CONFIG_FAIR_GROUP_SCHED
  337. static void print_cfs_group_stats(struct seq_file *m, int cpu, struct task_group *tg)
  338. {
  339. struct sched_entity *se = tg->se[cpu];
  340. #define P(F) \
  341. SEQ_printf(m, " .%-30s: %lld\n", #F, (long long)F)
  342. #define P_SCHEDSTAT(F) \
  343. SEQ_printf(m, " .%-30s: %lld\n", #F, (long long)schedstat_val(F))
  344. #define PN(F) \
  345. SEQ_printf(m, " .%-30s: %lld.%06ld\n", #F, SPLIT_NS((long long)F))
  346. #define PN_SCHEDSTAT(F) \
  347. SEQ_printf(m, " .%-30s: %lld.%06ld\n", #F, SPLIT_NS((long long)schedstat_val(F)))
  348. if (!se)
  349. return;
  350. PN(se->exec_start);
  351. PN(se->vruntime);
  352. PN(se->sum_exec_runtime);
  353. if (schedstat_enabled()) {
  354. PN_SCHEDSTAT(se->statistics.wait_start);
  355. PN_SCHEDSTAT(se->statistics.sleep_start);
  356. PN_SCHEDSTAT(se->statistics.block_start);
  357. PN_SCHEDSTAT(se->statistics.sleep_max);
  358. PN_SCHEDSTAT(se->statistics.block_max);
  359. PN_SCHEDSTAT(se->statistics.exec_max);
  360. PN_SCHEDSTAT(se->statistics.slice_max);
  361. PN_SCHEDSTAT(se->statistics.wait_max);
  362. PN_SCHEDSTAT(se->statistics.wait_sum);
  363. P_SCHEDSTAT(se->statistics.wait_count);
  364. }
  365. P(se->load.weight);
  366. P(se->runnable_weight);
  367. #ifdef CONFIG_SMP
  368. P(se->avg.load_avg);
  369. P(se->avg.util_avg);
  370. P(se->avg.runnable_load_avg);
  371. #endif
  372. #undef PN_SCHEDSTAT
  373. #undef PN
  374. #undef P_SCHEDSTAT
  375. #undef P
  376. }
  377. #endif
  378. #ifdef CONFIG_CGROUP_SCHED
  379. static char group_path[PATH_MAX];
  380. static char *task_group_path(struct task_group *tg)
  381. {
  382. if (autogroup_path(tg, group_path, PATH_MAX))
  383. return group_path;
  384. cgroup_path(tg->css.cgroup, group_path, PATH_MAX);
  385. return group_path;
  386. }
  387. #endif
  388. static void
  389. print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
  390. {
  391. if (rq->curr == p)
  392. SEQ_printf(m, ">R");
  393. else
  394. SEQ_printf(m, " %c", task_state_to_char(p));
  395. SEQ_printf(m, "%15s %5d %9Ld.%06ld %9Ld %5d ",
  396. p->comm, task_pid_nr(p),
  397. SPLIT_NS(p->se.vruntime),
  398. (long long)(p->nvcsw + p->nivcsw),
  399. p->prio);
  400. SEQ_printf(m, "%9Ld.%06ld %9Ld.%06ld %9Ld.%06ld",
  401. SPLIT_NS(schedstat_val_or_zero(p->se.statistics.wait_sum)),
  402. SPLIT_NS(p->se.sum_exec_runtime),
  403. SPLIT_NS(schedstat_val_or_zero(p->se.statistics.sum_sleep_runtime)));
  404. #ifdef CONFIG_NUMA_BALANCING
  405. SEQ_printf(m, " %d %d", task_node(p), task_numa_group_id(p));
  406. #endif
  407. #ifdef CONFIG_CGROUP_SCHED
  408. SEQ_printf(m, " %s", task_group_path(task_group(p)));
  409. #endif
  410. SEQ_printf(m, "\n");
  411. }
  412. static void print_rq(struct seq_file *m, struct rq *rq, int rq_cpu)
  413. {
  414. struct task_struct *g, *p;
  415. SEQ_printf(m,
  416. "\nrunnable tasks:\n"
  417. " S task PID tree-key switches prio"
  418. " wait-time sum-exec sum-sleep\n"
  419. "-------------------------------------------------------"
  420. "----------------------------------------------------\n");
  421. rcu_read_lock();
  422. for_each_process_thread(g, p) {
  423. if (task_cpu(p) != rq_cpu)
  424. continue;
  425. print_task(m, rq, p);
  426. }
  427. rcu_read_unlock();
  428. }
  429. void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
  430. {
  431. s64 MIN_vruntime = -1, min_vruntime, max_vruntime = -1,
  432. spread, rq0_min_vruntime, spread0;
  433. struct rq *rq = cpu_rq(cpu);
  434. struct sched_entity *last;
  435. unsigned long flags;
  436. #ifdef CONFIG_FAIR_GROUP_SCHED
  437. SEQ_printf(m, "\ncfs_rq[%d]:%s\n", cpu, task_group_path(cfs_rq->tg));
  438. #else
  439. SEQ_printf(m, "\ncfs_rq[%d]:\n", cpu);
  440. #endif
  441. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "exec_clock",
  442. SPLIT_NS(cfs_rq->exec_clock));
  443. raw_spin_lock_irqsave(&rq->lock, flags);
  444. if (rb_first_cached(&cfs_rq->tasks_timeline))
  445. MIN_vruntime = (__pick_first_entity(cfs_rq))->vruntime;
  446. last = __pick_last_entity(cfs_rq);
  447. if (last)
  448. max_vruntime = last->vruntime;
  449. min_vruntime = cfs_rq->min_vruntime;
  450. rq0_min_vruntime = cpu_rq(0)->cfs.min_vruntime;
  451. raw_spin_unlock_irqrestore(&rq->lock, flags);
  452. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "MIN_vruntime",
  453. SPLIT_NS(MIN_vruntime));
  454. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "min_vruntime",
  455. SPLIT_NS(min_vruntime));
  456. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "max_vruntime",
  457. SPLIT_NS(max_vruntime));
  458. spread = max_vruntime - MIN_vruntime;
  459. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread",
  460. SPLIT_NS(spread));
  461. spread0 = min_vruntime - rq0_min_vruntime;
  462. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "spread0",
  463. SPLIT_NS(spread0));
  464. SEQ_printf(m, " .%-30s: %d\n", "nr_spread_over",
  465. cfs_rq->nr_spread_over);
  466. SEQ_printf(m, " .%-30s: %d\n", "nr_running", cfs_rq->nr_running);
  467. SEQ_printf(m, " .%-30s: %ld\n", "load", cfs_rq->load.weight);
  468. #ifdef CONFIG_SMP
  469. SEQ_printf(m, " .%-30s: %ld\n", "runnable_weight", cfs_rq->runnable_weight);
  470. SEQ_printf(m, " .%-30s: %lu\n", "load_avg",
  471. cfs_rq->avg.load_avg);
  472. SEQ_printf(m, " .%-30s: %lu\n", "runnable_load_avg",
  473. cfs_rq->avg.runnable_load_avg);
  474. SEQ_printf(m, " .%-30s: %lu\n", "util_avg",
  475. cfs_rq->avg.util_avg);
  476. SEQ_printf(m, " .%-30s: %ld\n", "removed.load_avg",
  477. cfs_rq->removed.load_avg);
  478. SEQ_printf(m, " .%-30s: %ld\n", "removed.util_avg",
  479. cfs_rq->removed.util_avg);
  480. SEQ_printf(m, " .%-30s: %ld\n", "removed.runnable_sum",
  481. cfs_rq->removed.runnable_sum);
  482. #ifdef CONFIG_FAIR_GROUP_SCHED
  483. SEQ_printf(m, " .%-30s: %lu\n", "tg_load_avg_contrib",
  484. cfs_rq->tg_load_avg_contrib);
  485. SEQ_printf(m, " .%-30s: %ld\n", "tg_load_avg",
  486. atomic_long_read(&cfs_rq->tg->load_avg));
  487. #endif
  488. #endif
  489. #ifdef CONFIG_CFS_BANDWIDTH
  490. SEQ_printf(m, " .%-30s: %d\n", "throttled",
  491. cfs_rq->throttled);
  492. SEQ_printf(m, " .%-30s: %d\n", "throttle_count",
  493. cfs_rq->throttle_count);
  494. #endif
  495. #ifdef CONFIG_FAIR_GROUP_SCHED
  496. print_cfs_group_stats(m, cpu, cfs_rq->tg);
  497. #endif
  498. }
  499. void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
  500. {
  501. #ifdef CONFIG_RT_GROUP_SCHED
  502. SEQ_printf(m, "\nrt_rq[%d]:%s\n", cpu, task_group_path(rt_rq->tg));
  503. #else
  504. SEQ_printf(m, "\nrt_rq[%d]:\n", cpu);
  505. #endif
  506. #define P(x) \
  507. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rt_rq->x))
  508. #define PU(x) \
  509. SEQ_printf(m, " .%-30s: %lu\n", #x, (unsigned long)(rt_rq->x))
  510. #define PN(x) \
  511. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rt_rq->x))
  512. PU(rt_nr_running);
  513. #ifdef CONFIG_SMP
  514. PU(rt_nr_migratory);
  515. #endif
  516. P(rt_throttled);
  517. PN(rt_time);
  518. PN(rt_runtime);
  519. #undef PN
  520. #undef PU
  521. #undef P
  522. }
  523. void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq)
  524. {
  525. struct dl_bw *dl_bw;
  526. SEQ_printf(m, "\ndl_rq[%d]:\n", cpu);
  527. #define PU(x) \
  528. SEQ_printf(m, " .%-30s: %lu\n", #x, (unsigned long)(dl_rq->x))
  529. PU(dl_nr_running);
  530. #ifdef CONFIG_SMP
  531. PU(dl_nr_migratory);
  532. dl_bw = &cpu_rq(cpu)->rd->dl_bw;
  533. #else
  534. dl_bw = &dl_rq->dl_bw;
  535. #endif
  536. SEQ_printf(m, " .%-30s: %lld\n", "dl_bw->bw", dl_bw->bw);
  537. SEQ_printf(m, " .%-30s: %lld\n", "dl_bw->total_bw", dl_bw->total_bw);
  538. #undef PU
  539. }
  540. extern __read_mostly int sched_clock_running;
  541. static void print_cpu(struct seq_file *m, int cpu)
  542. {
  543. struct rq *rq = cpu_rq(cpu);
  544. unsigned long flags;
  545. #ifdef CONFIG_X86
  546. {
  547. unsigned int freq = cpu_khz ? : 1;
  548. SEQ_printf(m, "cpu#%d, %u.%03u MHz\n",
  549. cpu, freq / 1000, (freq % 1000));
  550. }
  551. #else
  552. SEQ_printf(m, "cpu#%d\n", cpu);
  553. #endif
  554. #define P(x) \
  555. do { \
  556. if (sizeof(rq->x) == 4) \
  557. SEQ_printf(m, " .%-30s: %ld\n", #x, (long)(rq->x)); \
  558. else \
  559. SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rq->x));\
  560. } while (0)
  561. #define PN(x) \
  562. SEQ_printf(m, " .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rq->x))
  563. P(nr_running);
  564. SEQ_printf(m, " .%-30s: %lu\n", "load",
  565. rq->load.weight);
  566. P(nr_switches);
  567. P(nr_load_updates);
  568. P(nr_uninterruptible);
  569. PN(next_balance);
  570. SEQ_printf(m, " .%-30s: %ld\n", "curr->pid", (long)(task_pid_nr(rq->curr)));
  571. PN(clock);
  572. PN(clock_task);
  573. P(cpu_load[0]);
  574. P(cpu_load[1]);
  575. P(cpu_load[2]);
  576. P(cpu_load[3]);
  577. P(cpu_load[4]);
  578. #undef P
  579. #undef PN
  580. #ifdef CONFIG_SMP
  581. #define P64(n) SEQ_printf(m, " .%-30s: %Ld\n", #n, rq->n);
  582. P64(avg_idle);
  583. P64(max_idle_balance_cost);
  584. #undef P64
  585. #endif
  586. #define P(n) SEQ_printf(m, " .%-30s: %d\n", #n, schedstat_val(rq->n));
  587. if (schedstat_enabled()) {
  588. P(yld_count);
  589. P(sched_count);
  590. P(sched_goidle);
  591. P(ttwu_count);
  592. P(ttwu_local);
  593. }
  594. #undef P
  595. spin_lock_irqsave(&sched_debug_lock, flags);
  596. print_cfs_stats(m, cpu);
  597. print_rt_stats(m, cpu);
  598. print_dl_stats(m, cpu);
  599. print_rq(m, rq, cpu);
  600. spin_unlock_irqrestore(&sched_debug_lock, flags);
  601. SEQ_printf(m, "\n");
  602. }
  603. static const char *sched_tunable_scaling_names[] = {
  604. "none",
  605. "logaritmic",
  606. "linear"
  607. };
  608. static void sched_debug_header(struct seq_file *m)
  609. {
  610. u64 ktime, sched_clk, cpu_clk;
  611. unsigned long flags;
  612. local_irq_save(flags);
  613. ktime = ktime_to_ns(ktime_get());
  614. sched_clk = sched_clock();
  615. cpu_clk = local_clock();
  616. local_irq_restore(flags);
  617. SEQ_printf(m, "Sched Debug Version: v0.11, %s %.*s\n",
  618. init_utsname()->release,
  619. (int)strcspn(init_utsname()->version, " "),
  620. init_utsname()->version);
  621. #define P(x) \
  622. SEQ_printf(m, "%-40s: %Ld\n", #x, (long long)(x))
  623. #define PN(x) \
  624. SEQ_printf(m, "%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
  625. PN(ktime);
  626. PN(sched_clk);
  627. PN(cpu_clk);
  628. P(jiffies);
  629. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  630. P(sched_clock_stable());
  631. #endif
  632. #undef PN
  633. #undef P
  634. SEQ_printf(m, "\n");
  635. SEQ_printf(m, "sysctl_sched\n");
  636. #define P(x) \
  637. SEQ_printf(m, " .%-40s: %Ld\n", #x, (long long)(x))
  638. #define PN(x) \
  639. SEQ_printf(m, " .%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
  640. PN(sysctl_sched_latency);
  641. PN(sysctl_sched_min_granularity);
  642. PN(sysctl_sched_wakeup_granularity);
  643. P(sysctl_sched_child_runs_first);
  644. P(sysctl_sched_features);
  645. #undef PN
  646. #undef P
  647. SEQ_printf(m, " .%-40s: %d (%s)\n",
  648. "sysctl_sched_tunable_scaling",
  649. sysctl_sched_tunable_scaling,
  650. sched_tunable_scaling_names[sysctl_sched_tunable_scaling]);
  651. SEQ_printf(m, "\n");
  652. }
  653. static int sched_debug_show(struct seq_file *m, void *v)
  654. {
  655. int cpu = (unsigned long)(v - 2);
  656. if (cpu != -1)
  657. print_cpu(m, cpu);
  658. else
  659. sched_debug_header(m);
  660. return 0;
  661. }
  662. void sysrq_sched_debug_show(void)
  663. {
  664. int cpu;
  665. sched_debug_header(NULL);
  666. for_each_online_cpu(cpu)
  667. print_cpu(NULL, cpu);
  668. }
  669. /*
  670. * This itererator needs some explanation.
  671. * It returns 1 for the header position.
  672. * This means 2 is cpu 0.
  673. * In a hotplugged system some cpus, including cpu 0, may be missing so we have
  674. * to use cpumask_* to iterate over the cpus.
  675. */
  676. static void *sched_debug_start(struct seq_file *file, loff_t *offset)
  677. {
  678. unsigned long n = *offset;
  679. if (n == 0)
  680. return (void *) 1;
  681. n--;
  682. if (n > 0)
  683. n = cpumask_next(n - 1, cpu_online_mask);
  684. else
  685. n = cpumask_first(cpu_online_mask);
  686. *offset = n + 1;
  687. if (n < nr_cpu_ids)
  688. return (void *)(unsigned long)(n + 2);
  689. return NULL;
  690. }
  691. static void *sched_debug_next(struct seq_file *file, void *data, loff_t *offset)
  692. {
  693. (*offset)++;
  694. return sched_debug_start(file, offset);
  695. }
  696. static void sched_debug_stop(struct seq_file *file, void *data)
  697. {
  698. }
  699. static const struct seq_operations sched_debug_sops = {
  700. .start = sched_debug_start,
  701. .next = sched_debug_next,
  702. .stop = sched_debug_stop,
  703. .show = sched_debug_show,
  704. };
  705. static int sched_debug_release(struct inode *inode, struct file *file)
  706. {
  707. seq_release(inode, file);
  708. return 0;
  709. }
  710. static int sched_debug_open(struct inode *inode, struct file *filp)
  711. {
  712. int ret = 0;
  713. ret = seq_open(filp, &sched_debug_sops);
  714. return ret;
  715. }
  716. static const struct file_operations sched_debug_fops = {
  717. .open = sched_debug_open,
  718. .read = seq_read,
  719. .llseek = seq_lseek,
  720. .release = sched_debug_release,
  721. };
  722. static int __init init_sched_debug_procfs(void)
  723. {
  724. struct proc_dir_entry *pe;
  725. pe = proc_create("sched_debug", 0444, NULL, &sched_debug_fops);
  726. if (!pe)
  727. return -ENOMEM;
  728. return 0;
  729. }
  730. __initcall(init_sched_debug_procfs);
  731. #define __P(F) \
  732. SEQ_printf(m, "%-45s:%21Ld\n", #F, (long long)F)
  733. #define P(F) \
  734. SEQ_printf(m, "%-45s:%21Ld\n", #F, (long long)p->F)
  735. #define __PN(F) \
  736. SEQ_printf(m, "%-45s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)F))
  737. #define PN(F) \
  738. SEQ_printf(m, "%-45s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)p->F))
  739. #ifdef CONFIG_NUMA_BALANCING
  740. void print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
  741. unsigned long tpf, unsigned long gsf, unsigned long gpf)
  742. {
  743. SEQ_printf(m, "numa_faults node=%d ", node);
  744. SEQ_printf(m, "task_private=%lu task_shared=%lu ", tsf, tpf);
  745. SEQ_printf(m, "group_private=%lu group_shared=%lu\n", gsf, gpf);
  746. }
  747. #endif
  748. static void sched_show_numa(struct task_struct *p, struct seq_file *m)
  749. {
  750. #ifdef CONFIG_NUMA_BALANCING
  751. struct mempolicy *pol;
  752. if (p->mm)
  753. P(mm->numa_scan_seq);
  754. task_lock(p);
  755. pol = p->mempolicy;
  756. if (pol && !(pol->flags & MPOL_F_MORON))
  757. pol = NULL;
  758. mpol_get(pol);
  759. task_unlock(p);
  760. P(numa_pages_migrated);
  761. P(numa_preferred_nid);
  762. P(total_numa_faults);
  763. SEQ_printf(m, "current_node=%d, numa_group_id=%d\n",
  764. task_node(p), task_numa_group_id(p));
  765. show_numa_stats(p, m);
  766. mpol_put(pol);
  767. #endif
  768. }
  769. void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,
  770. struct seq_file *m)
  771. {
  772. unsigned long nr_switches;
  773. SEQ_printf(m, "%s (%d, #threads: %d)\n", p->comm, task_pid_nr_ns(p, ns),
  774. get_nr_threads(p));
  775. SEQ_printf(m,
  776. "---------------------------------------------------------"
  777. "----------\n");
  778. #define __P(F) \
  779. SEQ_printf(m, "%-45s:%21Ld\n", #F, (long long)F)
  780. #define P(F) \
  781. SEQ_printf(m, "%-45s:%21Ld\n", #F, (long long)p->F)
  782. #define P_SCHEDSTAT(F) \
  783. SEQ_printf(m, "%-45s:%21Ld\n", #F, (long long)schedstat_val(p->F))
  784. #define __PN(F) \
  785. SEQ_printf(m, "%-45s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)F))
  786. #define PN(F) \
  787. SEQ_printf(m, "%-45s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)p->F))
  788. #define PN_SCHEDSTAT(F) \
  789. SEQ_printf(m, "%-45s:%14Ld.%06ld\n", #F, SPLIT_NS((long long)schedstat_val(p->F)))
  790. PN(se.exec_start);
  791. PN(se.vruntime);
  792. PN(se.sum_exec_runtime);
  793. nr_switches = p->nvcsw + p->nivcsw;
  794. P(se.nr_migrations);
  795. if (schedstat_enabled()) {
  796. u64 avg_atom, avg_per_cpu;
  797. PN_SCHEDSTAT(se.statistics.sum_sleep_runtime);
  798. PN_SCHEDSTAT(se.statistics.wait_start);
  799. PN_SCHEDSTAT(se.statistics.sleep_start);
  800. PN_SCHEDSTAT(se.statistics.block_start);
  801. PN_SCHEDSTAT(se.statistics.sleep_max);
  802. PN_SCHEDSTAT(se.statistics.block_max);
  803. PN_SCHEDSTAT(se.statistics.exec_max);
  804. PN_SCHEDSTAT(se.statistics.slice_max);
  805. PN_SCHEDSTAT(se.statistics.wait_max);
  806. PN_SCHEDSTAT(se.statistics.wait_sum);
  807. P_SCHEDSTAT(se.statistics.wait_count);
  808. PN_SCHEDSTAT(se.statistics.iowait_sum);
  809. P_SCHEDSTAT(se.statistics.iowait_count);
  810. P_SCHEDSTAT(se.statistics.nr_migrations_cold);
  811. P_SCHEDSTAT(se.statistics.nr_failed_migrations_affine);
  812. P_SCHEDSTAT(se.statistics.nr_failed_migrations_running);
  813. P_SCHEDSTAT(se.statistics.nr_failed_migrations_hot);
  814. P_SCHEDSTAT(se.statistics.nr_forced_migrations);
  815. P_SCHEDSTAT(se.statistics.nr_wakeups);
  816. P_SCHEDSTAT(se.statistics.nr_wakeups_sync);
  817. P_SCHEDSTAT(se.statistics.nr_wakeups_migrate);
  818. P_SCHEDSTAT(se.statistics.nr_wakeups_local);
  819. P_SCHEDSTAT(se.statistics.nr_wakeups_remote);
  820. P_SCHEDSTAT(se.statistics.nr_wakeups_affine);
  821. P_SCHEDSTAT(se.statistics.nr_wakeups_affine_attempts);
  822. P_SCHEDSTAT(se.statistics.nr_wakeups_passive);
  823. P_SCHEDSTAT(se.statistics.nr_wakeups_idle);
  824. avg_atom = p->se.sum_exec_runtime;
  825. if (nr_switches)
  826. avg_atom = div64_ul(avg_atom, nr_switches);
  827. else
  828. avg_atom = -1LL;
  829. avg_per_cpu = p->se.sum_exec_runtime;
  830. if (p->se.nr_migrations) {
  831. avg_per_cpu = div64_u64(avg_per_cpu,
  832. p->se.nr_migrations);
  833. } else {
  834. avg_per_cpu = -1LL;
  835. }
  836. __PN(avg_atom);
  837. __PN(avg_per_cpu);
  838. }
  839. __P(nr_switches);
  840. SEQ_printf(m, "%-45s:%21Ld\n",
  841. "nr_voluntary_switches", (long long)p->nvcsw);
  842. SEQ_printf(m, "%-45s:%21Ld\n",
  843. "nr_involuntary_switches", (long long)p->nivcsw);
  844. P(se.load.weight);
  845. P(se.runnable_weight);
  846. #ifdef CONFIG_SMP
  847. P(se.avg.load_sum);
  848. P(se.avg.runnable_load_sum);
  849. P(se.avg.util_sum);
  850. P(se.avg.load_avg);
  851. P(se.avg.runnable_load_avg);
  852. P(se.avg.util_avg);
  853. P(se.avg.last_update_time);
  854. #endif
  855. P(policy);
  856. P(prio);
  857. if (p->policy == SCHED_DEADLINE) {
  858. P(dl.runtime);
  859. P(dl.deadline);
  860. }
  861. #undef PN_SCHEDSTAT
  862. #undef PN
  863. #undef __PN
  864. #undef P_SCHEDSTAT
  865. #undef P
  866. #undef __P
  867. {
  868. unsigned int this_cpu = raw_smp_processor_id();
  869. u64 t0, t1;
  870. t0 = cpu_clock(this_cpu);
  871. t1 = cpu_clock(this_cpu);
  872. SEQ_printf(m, "%-45s:%21Ld\n",
  873. "clock-delta", (long long)(t1-t0));
  874. }
  875. sched_show_numa(p, m);
  876. }
  877. void proc_sched_set_task(struct task_struct *p)
  878. {
  879. #ifdef CONFIG_SCHEDSTATS
  880. memset(&p->se.statistics, 0, sizeof(p->se.statistics));
  881. #endif
  882. }