0001
0002 #include <linux/cpumask.h>
0003 #include <linux/fs.h>
0004 #include <linux/init.h>
0005 #include <linux/interrupt.h>
0006 #include <linux/kernel_stat.h>
0007 #include <linux/proc_fs.h>
0008 #include <linux/sched.h>
0009 #include <linux/sched/stat.h>
0010 #include <linux/seq_file.h>
0011 #include <linux/slab.h>
0012 #include <linux/time.h>
0013 #include <linux/time_namespace.h>
0014 #include <linux/irqnr.h>
0015 #include <linux/sched/cputime.h>
0016 #include <linux/tick.h>
0017
0018 #ifndef arch_irq_stat_cpu
0019 #define arch_irq_stat_cpu(cpu) 0
0020 #endif
0021 #ifndef arch_irq_stat
0022 #define arch_irq_stat() 0
0023 #endif
0024
0025 #ifdef arch_idle_time
0026
0027 u64 get_idle_time(struct kernel_cpustat *kcs, int cpu)
0028 {
0029 u64 idle;
0030
0031 idle = kcs->cpustat[CPUTIME_IDLE];
0032 if (cpu_online(cpu) && !nr_iowait_cpu(cpu))
0033 idle += arch_idle_time(cpu);
0034 return idle;
0035 }
0036
0037 static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu)
0038 {
0039 u64 iowait;
0040
0041 iowait = kcs->cpustat[CPUTIME_IOWAIT];
0042 if (cpu_online(cpu) && nr_iowait_cpu(cpu))
0043 iowait += arch_idle_time(cpu);
0044 return iowait;
0045 }
0046
0047 #else
0048
0049 u64 get_idle_time(struct kernel_cpustat *kcs, int cpu)
0050 {
0051 u64 idle, idle_usecs = -1ULL;
0052
0053 if (cpu_online(cpu))
0054 idle_usecs = get_cpu_idle_time_us(cpu, NULL);
0055
0056 if (idle_usecs == -1ULL)
0057
0058 idle = kcs->cpustat[CPUTIME_IDLE];
0059 else
0060 idle = idle_usecs * NSEC_PER_USEC;
0061
0062 return idle;
0063 }
0064
0065 static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu)
0066 {
0067 u64 iowait, iowait_usecs = -1ULL;
0068
0069 if (cpu_online(cpu))
0070 iowait_usecs = get_cpu_iowait_time_us(cpu, NULL);
0071
0072 if (iowait_usecs == -1ULL)
0073
0074 iowait = kcs->cpustat[CPUTIME_IOWAIT];
0075 else
0076 iowait = iowait_usecs * NSEC_PER_USEC;
0077
0078 return iowait;
0079 }
0080
0081 #endif
0082
0083 static void show_irq_gap(struct seq_file *p, unsigned int gap)
0084 {
0085 static const char zeros[] = " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0";
0086
0087 while (gap > 0) {
0088 unsigned int inc;
0089
0090 inc = min_t(unsigned int, gap, ARRAY_SIZE(zeros) / 2);
0091 seq_write(p, zeros, 2 * inc);
0092 gap -= inc;
0093 }
0094 }
0095
0096 static void show_all_irqs(struct seq_file *p)
0097 {
0098 unsigned int i, next = 0;
0099
0100 for_each_active_irq(i) {
0101 show_irq_gap(p, i - next);
0102 seq_put_decimal_ull(p, " ", kstat_irqs_usr(i));
0103 next = i + 1;
0104 }
0105 show_irq_gap(p, nr_irqs - next);
0106 }
0107
0108 static int show_stat(struct seq_file *p, void *v)
0109 {
0110 int i, j;
0111 u64 user, nice, system, idle, iowait, irq, softirq, steal;
0112 u64 guest, guest_nice;
0113 u64 sum = 0;
0114 u64 sum_softirq = 0;
0115 unsigned int per_softirq_sums[NR_SOFTIRQS] = {0};
0116 struct timespec64 boottime;
0117
0118 user = nice = system = idle = iowait =
0119 irq = softirq = steal = 0;
0120 guest = guest_nice = 0;
0121 getboottime64(&boottime);
0122
0123 timens_sub_boottime(&boottime);
0124
0125 for_each_possible_cpu(i) {
0126 struct kernel_cpustat kcpustat;
0127 u64 *cpustat = kcpustat.cpustat;
0128
0129 kcpustat_cpu_fetch(&kcpustat, i);
0130
0131 user += cpustat[CPUTIME_USER];
0132 nice += cpustat[CPUTIME_NICE];
0133 system += cpustat[CPUTIME_SYSTEM];
0134 idle += get_idle_time(&kcpustat, i);
0135 iowait += get_iowait_time(&kcpustat, i);
0136 irq += cpustat[CPUTIME_IRQ];
0137 softirq += cpustat[CPUTIME_SOFTIRQ];
0138 steal += cpustat[CPUTIME_STEAL];
0139 guest += cpustat[CPUTIME_GUEST];
0140 guest_nice += cpustat[CPUTIME_GUEST_NICE];
0141 sum += kstat_cpu_irqs_sum(i);
0142 sum += arch_irq_stat_cpu(i);
0143
0144 for (j = 0; j < NR_SOFTIRQS; j++) {
0145 unsigned int softirq_stat = kstat_softirqs_cpu(j, i);
0146
0147 per_softirq_sums[j] += softirq_stat;
0148 sum_softirq += softirq_stat;
0149 }
0150 }
0151 sum += arch_irq_stat();
0152
0153 seq_put_decimal_ull(p, "cpu ", nsec_to_clock_t(user));
0154 seq_put_decimal_ull(p, " ", nsec_to_clock_t(nice));
0155 seq_put_decimal_ull(p, " ", nsec_to_clock_t(system));
0156 seq_put_decimal_ull(p, " ", nsec_to_clock_t(idle));
0157 seq_put_decimal_ull(p, " ", nsec_to_clock_t(iowait));
0158 seq_put_decimal_ull(p, " ", nsec_to_clock_t(irq));
0159 seq_put_decimal_ull(p, " ", nsec_to_clock_t(softirq));
0160 seq_put_decimal_ull(p, " ", nsec_to_clock_t(steal));
0161 seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest));
0162 seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest_nice));
0163 seq_putc(p, '\n');
0164
0165 for_each_online_cpu(i) {
0166 struct kernel_cpustat kcpustat;
0167 u64 *cpustat = kcpustat.cpustat;
0168
0169 kcpustat_cpu_fetch(&kcpustat, i);
0170
0171
0172 user = cpustat[CPUTIME_USER];
0173 nice = cpustat[CPUTIME_NICE];
0174 system = cpustat[CPUTIME_SYSTEM];
0175 idle = get_idle_time(&kcpustat, i);
0176 iowait = get_iowait_time(&kcpustat, i);
0177 irq = cpustat[CPUTIME_IRQ];
0178 softirq = cpustat[CPUTIME_SOFTIRQ];
0179 steal = cpustat[CPUTIME_STEAL];
0180 guest = cpustat[CPUTIME_GUEST];
0181 guest_nice = cpustat[CPUTIME_GUEST_NICE];
0182 seq_printf(p, "cpu%d", i);
0183 seq_put_decimal_ull(p, " ", nsec_to_clock_t(user));
0184 seq_put_decimal_ull(p, " ", nsec_to_clock_t(nice));
0185 seq_put_decimal_ull(p, " ", nsec_to_clock_t(system));
0186 seq_put_decimal_ull(p, " ", nsec_to_clock_t(idle));
0187 seq_put_decimal_ull(p, " ", nsec_to_clock_t(iowait));
0188 seq_put_decimal_ull(p, " ", nsec_to_clock_t(irq));
0189 seq_put_decimal_ull(p, " ", nsec_to_clock_t(softirq));
0190 seq_put_decimal_ull(p, " ", nsec_to_clock_t(steal));
0191 seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest));
0192 seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest_nice));
0193 seq_putc(p, '\n');
0194 }
0195 seq_put_decimal_ull(p, "intr ", (unsigned long long)sum);
0196
0197 show_all_irqs(p);
0198
0199 seq_printf(p,
0200 "\nctxt %llu\n"
0201 "btime %llu\n"
0202 "processes %lu\n"
0203 "procs_running %u\n"
0204 "procs_blocked %u\n",
0205 nr_context_switches(),
0206 (unsigned long long)boottime.tv_sec,
0207 total_forks,
0208 nr_running(),
0209 nr_iowait());
0210
0211 seq_put_decimal_ull(p, "softirq ", (unsigned long long)sum_softirq);
0212
0213 for (i = 0; i < NR_SOFTIRQS; i++)
0214 seq_put_decimal_ull(p, " ", per_softirq_sums[i]);
0215 seq_putc(p, '\n');
0216
0217 return 0;
0218 }
0219
0220 static int stat_open(struct inode *inode, struct file *file)
0221 {
0222 unsigned int size = 1024 + 128 * num_online_cpus();
0223
0224
0225 size += 2 * nr_irqs;
0226 return single_open_size(file, show_stat, NULL, size);
0227 }
0228
0229 static const struct proc_ops stat_proc_ops = {
0230 .proc_flags = PROC_ENTRY_PERMANENT,
0231 .proc_open = stat_open,
0232 .proc_read_iter = seq_read_iter,
0233 .proc_lseek = seq_lseek,
0234 .proc_release = single_release,
0235 };
0236
0237 static int __init proc_stat_init(void)
0238 {
0239 proc_create("stat", 0, NULL, &stat_proc_ops);
0240 return 0;
0241 }
0242 fs_initcall(proc_stat_init);