1
2 #include <linux/slab.h>
3 #include <linux/fs.h>
4 #include <linux/seq_file.h>
5 #include <linux/proc_fs.h>
6
7 #include "sched.h"
8
9 /*
10 * bump this up when changing the output format or the meaning of an existing
11 * format, so that tools can adapt (or abort)
12 */
13 #define SCHEDSTAT_VERSION 15
14
15 #ifdef CONFIG_SMP
show_easstat(struct seq_file * seq,struct eas_stats * stats)16 static inline void show_easstat(struct seq_file *seq, struct eas_stats *stats)
17 {
18 /* eas-specific runqueue stats */
19 seq_printf(seq, "eas %llu %llu %llu %llu %llu %llu ",
20 stats->sis_attempts, stats->sis_idle, stats->sis_cache_affine,
21 stats->sis_suff_cap, stats->sis_idle_cpu, stats->sis_count);
22
23 seq_printf(seq, "%llu %llu %llu %llu %llu %llu %llu ",
24 stats->secb_attempts, stats->secb_sync, stats->secb_idle_bt,
25 stats->secb_insuff_cap, stats->secb_no_nrg_sav,
26 stats->secb_nrg_sav, stats->secb_count);
27
28 seq_printf(seq, "%llu %llu %llu %llu %llu ",
29 stats->fbt_attempts, stats->fbt_no_cpu, stats->fbt_no_sd,
30 stats->fbt_pref_idle, stats->fbt_count);
31
32 seq_printf(seq, "%llu %llu\n",
33 stats->cas_attempts, stats->cas_count);
34 }
35 #endif
36
show_schedstat(struct seq_file * seq,void * v)37 static int show_schedstat(struct seq_file *seq, void *v)
38 {
39 int cpu;
40
41 if (v == (void *)1) {
42 seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION);
43 seq_printf(seq, "timestamp %lu\n", jiffies);
44 } else {
45 struct rq *rq;
46 #ifdef CONFIG_SMP
47 struct sched_domain *sd;
48 int dcount = 0;
49 #endif
50 cpu = (unsigned long)(v - 2);
51 rq = cpu_rq(cpu);
52
53 /* runqueue-specific stats */
54 seq_printf(seq,
55 "cpu%d %u 0 %u %u %u %u %llu %llu %lu",
56 cpu, rq->yld_count,
57 rq->sched_count, rq->sched_goidle,
58 rq->ttwu_count, rq->ttwu_local,
59 rq->rq_cpu_time,
60 rq->rq_sched_info.run_delay, rq->rq_sched_info.pcount);
61
62 seq_printf(seq, "\n");
63
64 #ifdef CONFIG_SMP
65 show_easstat(seq, &rq->eas_stats);
66
67 /* domain-specific stats */
68 rcu_read_lock();
69 for_each_domain(cpu, sd) {
70 enum cpu_idle_type itype;
71
72 seq_printf(seq, "domain%d %*pb", dcount++,
73 cpumask_pr_args(sched_domain_span(sd)));
74 for (itype = CPU_IDLE; itype < CPU_MAX_IDLE_TYPES;
75 itype++) {
76 seq_printf(seq, " %u %u %u %u %u %u %u %u",
77 sd->lb_count[itype],
78 sd->lb_balanced[itype],
79 sd->lb_failed[itype],
80 sd->lb_imbalance[itype],
81 sd->lb_gained[itype],
82 sd->lb_hot_gained[itype],
83 sd->lb_nobusyq[itype],
84 sd->lb_nobusyg[itype]);
85 }
86 seq_printf(seq,
87 " %u %u %u %u %u %u %u %u %u %u %u %u\n",
88 sd->alb_count, sd->alb_failed, sd->alb_pushed,
89 sd->sbe_count, sd->sbe_balanced, sd->sbe_pushed,
90 sd->sbf_count, sd->sbf_balanced, sd->sbf_pushed,
91 sd->ttwu_wake_remote, sd->ttwu_move_affine,
92 sd->ttwu_move_balance);
93
94 show_easstat(seq, &sd->eas_stats);
95 }
96 rcu_read_unlock();
97 #endif
98 }
99 return 0;
100 }
101
102 /*
103 * This itererator needs some explanation.
104 * It returns 1 for the header position.
105 * This means 2 is cpu 0.
106 * In a hotplugged system some cpus, including cpu 0, may be missing so we have
107 * to use cpumask_* to iterate over the cpus.
108 */
schedstat_start(struct seq_file * file,loff_t * offset)109 static void *schedstat_start(struct seq_file *file, loff_t *offset)
110 {
111 unsigned long n = *offset;
112
113 if (n == 0)
114 return (void *) 1;
115
116 n--;
117
118 if (n > 0)
119 n = cpumask_next(n - 1, cpu_online_mask);
120 else
121 n = cpumask_first(cpu_online_mask);
122
123 *offset = n + 1;
124
125 if (n < nr_cpu_ids)
126 return (void *)(unsigned long)(n + 2);
127 return NULL;
128 }
129
schedstat_next(struct seq_file * file,void * data,loff_t * offset)130 static void *schedstat_next(struct seq_file *file, void *data, loff_t *offset)
131 {
132 (*offset)++;
133 return schedstat_start(file, offset);
134 }
135
schedstat_stop(struct seq_file * file,void * data)136 static void schedstat_stop(struct seq_file *file, void *data)
137 {
138 }
139
140 static const struct seq_operations schedstat_sops = {
141 .start = schedstat_start,
142 .next = schedstat_next,
143 .stop = schedstat_stop,
144 .show = show_schedstat,
145 };
146
schedstat_open(struct inode * inode,struct file * file)147 static int schedstat_open(struct inode *inode, struct file *file)
148 {
149 return seq_open(file, &schedstat_sops);
150 }
151
152 static const struct file_operations proc_schedstat_operations = {
153 .open = schedstat_open,
154 .read = seq_read,
155 .llseek = seq_lseek,
156 .release = seq_release,
157 };
158
proc_schedstat_init(void)159 static int __init proc_schedstat_init(void)
160 {
161 proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
162 return 0;
163 }
164 subsys_initcall(proc_schedstat_init);
165