xref: /openbmc/linux/fs/proc/stat.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2df8106dbSAlexey Dobriyan #include <linux/cpumask.h>
3df8106dbSAlexey Dobriyan #include <linux/fs.h>
4df8106dbSAlexey Dobriyan #include <linux/init.h>
5df8106dbSAlexey Dobriyan #include <linux/interrupt.h>
6df8106dbSAlexey Dobriyan #include <linux/kernel_stat.h>
7df8106dbSAlexey Dobriyan #include <linux/proc_fs.h>
8df8106dbSAlexey Dobriyan #include <linux/sched.h>
903441a34SIngo Molnar #include <linux/sched/stat.h>
10df8106dbSAlexey Dobriyan #include <linux/seq_file.h>
11df8106dbSAlexey Dobriyan #include <linux/slab.h>
12df8106dbSAlexey Dobriyan #include <linux/time.h>
133ae700ecSMichael Weiß #include <linux/time_namespace.h>
1426ddd8d5SKOSAKI Motohiro #include <linux/irqnr.h>
1532ef5517SIngo Molnar #include <linux/sched/cputime.h>
16a25cac51SMichal Hocko #include <linux/tick.h>
17df8106dbSAlexey Dobriyan 
18df8106dbSAlexey Dobriyan #ifndef arch_irq_stat_cpu
19df8106dbSAlexey Dobriyan #define arch_irq_stat_cpu(cpu) 0
20df8106dbSAlexey Dobriyan #endif
21df8106dbSAlexey Dobriyan #ifndef arch_irq_stat
22df8106dbSAlexey Dobriyan #define arch_irq_stat() 0
23df8106dbSAlexey Dobriyan #endif
24cb85a6edSMartin Schwidefsky 
get_idle_time(struct kernel_cpustat * kcs,int cpu)25*a130e8fbSJosh Don u64 get_idle_time(struct kernel_cpustat *kcs, int cpu)
26a25cac51SMichal Hocko {
277fb1327eSFrederic Weisbecker 	u64 idle, idle_usecs = -1ULL;
287386cdbfSMichal Hocko 
297386cdbfSMichal Hocko 	if (cpu_online(cpu))
307fb1327eSFrederic Weisbecker 		idle_usecs = get_cpu_idle_time_us(cpu, NULL);
31a25cac51SMichal Hocko 
327fb1327eSFrederic Weisbecker 	if (idle_usecs == -1ULL)
337386cdbfSMichal Hocko 		/* !NO_HZ or cpu offline so we can rely on cpustat.idle */
345713f35cSAlexey Dobriyan 		idle = kcs->cpustat[CPUTIME_IDLE];
35cb85a6edSMartin Schwidefsky 	else
367fb1327eSFrederic Weisbecker 		idle = idle_usecs * NSEC_PER_USEC;
37a25cac51SMichal Hocko 
38a25cac51SMichal Hocko 	return idle;
39a25cac51SMichal Hocko }
40a25cac51SMichal Hocko 
get_iowait_time(struct kernel_cpustat * kcs,int cpu)415713f35cSAlexey Dobriyan static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu)
42a25cac51SMichal Hocko {
437fb1327eSFrederic Weisbecker 	u64 iowait, iowait_usecs = -1ULL;
447386cdbfSMichal Hocko 
457386cdbfSMichal Hocko 	if (cpu_online(cpu))
467fb1327eSFrederic Weisbecker 		iowait_usecs = get_cpu_iowait_time_us(cpu, NULL);
47a25cac51SMichal Hocko 
487fb1327eSFrederic Weisbecker 	if (iowait_usecs == -1ULL)
497386cdbfSMichal Hocko 		/* !NO_HZ or cpu offline so we can rely on cpustat.iowait */
505713f35cSAlexey Dobriyan 		iowait = kcs->cpustat[CPUTIME_IOWAIT];
51a25cac51SMichal Hocko 	else
527fb1327eSFrederic Weisbecker 		iowait = iowait_usecs * NSEC_PER_USEC;
53a25cac51SMichal Hocko 
54a25cac51SMichal Hocko 	return iowait;
55a25cac51SMichal Hocko }
56a25cac51SMichal Hocko 
show_irq_gap(struct seq_file * p,unsigned int gap)57c2da3f1bSThomas Gleixner static void show_irq_gap(struct seq_file *p, unsigned int gap)
58c2da3f1bSThomas Gleixner {
59c2da3f1bSThomas Gleixner 	static const char zeros[] = " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0";
60c2da3f1bSThomas Gleixner 
61c2da3f1bSThomas Gleixner 	while (gap > 0) {
62c2da3f1bSThomas Gleixner 		unsigned int inc;
63c2da3f1bSThomas Gleixner 
64c2da3f1bSThomas Gleixner 		inc = min_t(unsigned int, gap, ARRAY_SIZE(zeros) / 2);
65c2da3f1bSThomas Gleixner 		seq_write(p, zeros, 2 * inc);
66c2da3f1bSThomas Gleixner 		gap -= inc;
67c2da3f1bSThomas Gleixner 	}
68c2da3f1bSThomas Gleixner }
69c2da3f1bSThomas Gleixner 
show_all_irqs(struct seq_file * p)70c2da3f1bSThomas Gleixner static void show_all_irqs(struct seq_file *p)
71c2da3f1bSThomas Gleixner {
72c2da3f1bSThomas Gleixner 	unsigned int i, next = 0;
73c2da3f1bSThomas Gleixner 
74c2da3f1bSThomas Gleixner 	for_each_active_irq(i) {
75c2da3f1bSThomas Gleixner 		show_irq_gap(p, i - next);
76c2da3f1bSThomas Gleixner 		seq_put_decimal_ull(p, " ", kstat_irqs_usr(i));
77c2da3f1bSThomas Gleixner 		next = i + 1;
78c2da3f1bSThomas Gleixner 	}
79c2da3f1bSThomas Gleixner 	show_irq_gap(p, nr_irqs - next);
80c2da3f1bSThomas Gleixner }
81c2da3f1bSThomas Gleixner 
show_stat(struct seq_file * p,void * v)82df8106dbSAlexey Dobriyan static int show_stat(struct seq_file *p, void *v)
83df8106dbSAlexey Dobriyan {
84df8106dbSAlexey Dobriyan 	int i, j;
853292beb3SGlauber Costa 	u64 user, nice, system, idle, iowait, irq, softirq, steal;
863292beb3SGlauber Costa 	u64 guest, guest_nice;
87df8106dbSAlexey Dobriyan 	u64 sum = 0;
88d3d64df2SKeika Kobayashi 	u64 sum_softirq = 0;
89d3d64df2SKeika Kobayashi 	unsigned int per_softirq_sums[NR_SOFTIRQS] = {0};
90519ded5aSArnd Bergmann 	struct timespec64 boottime;
91df8106dbSAlexey Dobriyan 
92df8106dbSAlexey Dobriyan 	user = nice = system = idle = iowait =
933292beb3SGlauber Costa 		irq = softirq = steal = 0;
943292beb3SGlauber Costa 	guest = guest_nice = 0;
95519ded5aSArnd Bergmann 	getboottime64(&boottime);
963ae700ecSMichael Weiß 	/* shift boot timestamp according to the timens offset */
973ae700ecSMichael Weiß 	timens_sub_boottime(&boottime);
98df8106dbSAlexey Dobriyan 
99df8106dbSAlexey Dobriyan 	for_each_possible_cpu(i) {
10026dae145SFrederic Weisbecker 		struct kernel_cpustat kcpustat;
10126dae145SFrederic Weisbecker 		u64 *cpustat = kcpustat.cpustat;
1025713f35cSAlexey Dobriyan 
10326dae145SFrederic Weisbecker 		kcpustat_cpu_fetch(&kcpustat, i);
10426dae145SFrederic Weisbecker 
10526dae145SFrederic Weisbecker 		user		+= cpustat[CPUTIME_USER];
10626dae145SFrederic Weisbecker 		nice		+= cpustat[CPUTIME_NICE];
10726dae145SFrederic Weisbecker 		system		+= cpustat[CPUTIME_SYSTEM];
10826dae145SFrederic Weisbecker 		idle		+= get_idle_time(&kcpustat, i);
10926dae145SFrederic Weisbecker 		iowait		+= get_iowait_time(&kcpustat, i);
11026dae145SFrederic Weisbecker 		irq		+= cpustat[CPUTIME_IRQ];
11126dae145SFrederic Weisbecker 		softirq		+= cpustat[CPUTIME_SOFTIRQ];
11226dae145SFrederic Weisbecker 		steal		+= cpustat[CPUTIME_STEAL];
11326dae145SFrederic Weisbecker 		guest		+= cpustat[CPUTIME_GUEST];
114346da4d2SFlavio Leitner 		guest_nice	+= cpustat[CPUTIME_GUEST_NICE];
115f7e6746eSRussell King 		sum		+= kstat_cpu_irqs_sum(i);
116f7e6746eSRussell King 		sum		+= arch_irq_stat_cpu(i);
117d3d64df2SKeika Kobayashi 
118d3d64df2SKeika Kobayashi 		for (j = 0; j < NR_SOFTIRQS; j++) {
119d3d64df2SKeika Kobayashi 			unsigned int softirq_stat = kstat_softirqs_cpu(j, i);
120d3d64df2SKeika Kobayashi 
121d3d64df2SKeika Kobayashi 			per_softirq_sums[j] += softirq_stat;
122d3d64df2SKeika Kobayashi 			sum_softirq += softirq_stat;
123d3d64df2SKeika Kobayashi 		}
124df8106dbSAlexey Dobriyan 	}
125df8106dbSAlexey Dobriyan 	sum += arch_irq_stat();
126df8106dbSAlexey Dobriyan 
1277fb1327eSFrederic Weisbecker 	seq_put_decimal_ull(p, "cpu  ", nsec_to_clock_t(user));
1287fb1327eSFrederic Weisbecker 	seq_put_decimal_ull(p, " ", nsec_to_clock_t(nice));
1297fb1327eSFrederic Weisbecker 	seq_put_decimal_ull(p, " ", nsec_to_clock_t(system));
1307fb1327eSFrederic Weisbecker 	seq_put_decimal_ull(p, " ", nsec_to_clock_t(idle));
1317fb1327eSFrederic Weisbecker 	seq_put_decimal_ull(p, " ", nsec_to_clock_t(iowait));
1327fb1327eSFrederic Weisbecker 	seq_put_decimal_ull(p, " ", nsec_to_clock_t(irq));
1337fb1327eSFrederic Weisbecker 	seq_put_decimal_ull(p, " ", nsec_to_clock_t(softirq));
1347fb1327eSFrederic Weisbecker 	seq_put_decimal_ull(p, " ", nsec_to_clock_t(steal));
1357fb1327eSFrederic Weisbecker 	seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest));
1367fb1327eSFrederic Weisbecker 	seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest_nice));
1371ac101a5SKAMEZAWA Hiroyuki 	seq_putc(p, '\n');
1381ac101a5SKAMEZAWA Hiroyuki 
139df8106dbSAlexey Dobriyan 	for_each_online_cpu(i) {
14026dae145SFrederic Weisbecker 		struct kernel_cpustat kcpustat;
14126dae145SFrederic Weisbecker 		u64 *cpustat = kcpustat.cpustat;
14226dae145SFrederic Weisbecker 
14326dae145SFrederic Weisbecker 		kcpustat_cpu_fetch(&kcpustat, i);
1445713f35cSAlexey Dobriyan 
145df8106dbSAlexey Dobriyan 		/* Copy values here to work around gcc-2.95.3, gcc-2.96 */
14626dae145SFrederic Weisbecker 		user		= cpustat[CPUTIME_USER];
14726dae145SFrederic Weisbecker 		nice		= cpustat[CPUTIME_NICE];
14826dae145SFrederic Weisbecker 		system		= cpustat[CPUTIME_SYSTEM];
14926dae145SFrederic Weisbecker 		idle		= get_idle_time(&kcpustat, i);
15026dae145SFrederic Weisbecker 		iowait		= get_iowait_time(&kcpustat, i);
15126dae145SFrederic Weisbecker 		irq		= cpustat[CPUTIME_IRQ];
15226dae145SFrederic Weisbecker 		softirq		= cpustat[CPUTIME_SOFTIRQ];
15326dae145SFrederic Weisbecker 		steal		= cpustat[CPUTIME_STEAL];
15426dae145SFrederic Weisbecker 		guest		= cpustat[CPUTIME_GUEST];
155346da4d2SFlavio Leitner 		guest_nice	= cpustat[CPUTIME_GUEST_NICE];
1561ac101a5SKAMEZAWA Hiroyuki 		seq_printf(p, "cpu%d", i);
1577fb1327eSFrederic Weisbecker 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(user));
1587fb1327eSFrederic Weisbecker 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(nice));
1597fb1327eSFrederic Weisbecker 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(system));
1607fb1327eSFrederic Weisbecker 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(idle));
1617fb1327eSFrederic Weisbecker 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(iowait));
1627fb1327eSFrederic Weisbecker 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(irq));
1637fb1327eSFrederic Weisbecker 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(softirq));
1647fb1327eSFrederic Weisbecker 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(steal));
1657fb1327eSFrederic Weisbecker 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest));
1667fb1327eSFrederic Weisbecker 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest_nice));
1671ac101a5SKAMEZAWA Hiroyuki 		seq_putc(p, '\n');
168df8106dbSAlexey Dobriyan 	}
16975ba1d07SJoe Perches 	seq_put_decimal_ull(p, "intr ", (unsigned long long)sum);
170df8106dbSAlexey Dobriyan 
171c2da3f1bSThomas Gleixner 	show_all_irqs(p);
172df8106dbSAlexey Dobriyan 
173df8106dbSAlexey Dobriyan 	seq_printf(p,
174df8106dbSAlexey Dobriyan 		"\nctxt %llu\n"
175519ded5aSArnd Bergmann 		"btime %llu\n"
176df8106dbSAlexey Dobriyan 		"processes %lu\n"
17701aee8fdSAlexey Dobriyan 		"procs_running %u\n"
17897455168SAlexey Dobriyan 		"procs_blocked %u\n",
179df8106dbSAlexey Dobriyan 		nr_context_switches(),
180519ded5aSArnd Bergmann 		(unsigned long long)boottime.tv_sec,
181df8106dbSAlexey Dobriyan 		total_forks,
182df8106dbSAlexey Dobriyan 		nr_running(),
183df8106dbSAlexey Dobriyan 		nr_iowait());
184df8106dbSAlexey Dobriyan 
18575ba1d07SJoe Perches 	seq_put_decimal_ull(p, "softirq ", (unsigned long long)sum_softirq);
186d3d64df2SKeika Kobayashi 
187d3d64df2SKeika Kobayashi 	for (i = 0; i < NR_SOFTIRQS; i++)
18875ba1d07SJoe Perches 		seq_put_decimal_ull(p, " ", per_softirq_sums[i]);
1899d6de12fSAlexey Dobriyan 	seq_putc(p, '\n');
190d3d64df2SKeika Kobayashi 
191df8106dbSAlexey Dobriyan 	return 0;
192df8106dbSAlexey Dobriyan }
193df8106dbSAlexey Dobriyan 
stat_open(struct inode * inode,struct file * file)194df8106dbSAlexey Dobriyan static int stat_open(struct inode *inode, struct file *file)
195df8106dbSAlexey Dobriyan {
1969a27e97aSAlexey Dobriyan 	unsigned int size = 1024 + 128 * num_online_cpus();
197df8106dbSAlexey Dobriyan 
19859a32e2cSEric Dumazet 	/* minimum size to display an interrupt count : 2 bytes */
19959a32e2cSEric Dumazet 	size += 2 * nr_irqs;
200f74373a5SHeiko Carstens 	return single_open_size(file, show_stat, NULL, size);
201df8106dbSAlexey Dobriyan }
202df8106dbSAlexey Dobriyan 
20397a32539SAlexey Dobriyan static const struct proc_ops stat_proc_ops = {
204d919b33dSAlexey Dobriyan 	.proc_flags	= PROC_ENTRY_PERMANENT,
20597a32539SAlexey Dobriyan 	.proc_open	= stat_open,
20628589f9eSChristoph Hellwig 	.proc_read_iter	= seq_read_iter,
20797a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
20897a32539SAlexey Dobriyan 	.proc_release	= single_release,
209df8106dbSAlexey Dobriyan };
210df8106dbSAlexey Dobriyan 
proc_stat_init(void)211df8106dbSAlexey Dobriyan static int __init proc_stat_init(void)
212df8106dbSAlexey Dobriyan {
21397a32539SAlexey Dobriyan 	proc_create("stat", 0, NULL, &stat_proc_ops);
214df8106dbSAlexey Dobriyan 	return 0;
215df8106dbSAlexey Dobriyan }
216abaf3787SPaul Gortmaker fs_initcall(proc_stat_init);
217