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