Commit | Line | Data |
---|---|---|
df8106db AD |
1 | #include <linux/cpumask.h> |
2 | #include <linux/fs.h> | |
df8106db AD |
3 | #include <linux/init.h> |
4 | #include <linux/interrupt.h> | |
5 | #include <linux/kernel_stat.h> | |
6 | #include <linux/proc_fs.h> | |
7 | #include <linux/sched.h> | |
8 | #include <linux/seq_file.h> | |
9 | #include <linux/slab.h> | |
10 | #include <linux/time.h> | |
26ddd8d5 | 11 | #include <linux/irqnr.h> |
6fa3eb70 | 12 | #include <linux/vmalloc.h> |
df8106db | 13 | #include <asm/cputime.h> |
a25cac51 | 14 | #include <linux/tick.h> |
df8106db AD |
15 | |
16 | #ifndef arch_irq_stat_cpu | |
17 | #define arch_irq_stat_cpu(cpu) 0 | |
18 | #endif | |
19 | #ifndef arch_irq_stat | |
20 | #define arch_irq_stat() 0 | |
21 | #endif | |
cb85a6ed MS |
22 | |
23 | #ifdef arch_idle_time | |
24 | ||
25 | static cputime64_t get_idle_time(int cpu) | |
26 | { | |
27 | cputime64_t idle; | |
28 | ||
29 | idle = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE]; | |
30 | if (cpu_online(cpu) && !nr_iowait_cpu(cpu)) | |
31 | idle += arch_idle_time(cpu); | |
32 | return idle; | |
33 | } | |
34 | ||
35 | static cputime64_t get_iowait_time(int cpu) | |
36 | { | |
37 | cputime64_t iowait; | |
38 | ||
39 | iowait = kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT]; | |
40 | if (cpu_online(cpu) && nr_iowait_cpu(cpu)) | |
41 | iowait += arch_idle_time(cpu); | |
42 | return iowait; | |
43 | } | |
44 | ||
45 | #else | |
df8106db | 46 | |
3292beb3 | 47 | static u64 get_idle_time(int cpu) |
a25cac51 | 48 | { |
7386cdbf MH |
49 | u64 idle, idle_time = -1ULL; |
50 | ||
51 | if (cpu_online(cpu)) | |
52 | idle_time = get_cpu_idle_time_us(cpu, NULL); | |
a25cac51 | 53 | |
cb85a6ed | 54 | if (idle_time == -1ULL) |
7386cdbf | 55 | /* !NO_HZ or cpu offline so we can rely on cpustat.idle */ |
3292beb3 | 56 | idle = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE]; |
cb85a6ed | 57 | else |
34845636 | 58 | idle = usecs_to_cputime64(idle_time); |
a25cac51 MH |
59 | |
60 | return idle; | |
61 | } | |
62 | ||
3292beb3 | 63 | static u64 get_iowait_time(int cpu) |
a25cac51 | 64 | { |
7386cdbf MH |
65 | u64 iowait, iowait_time = -1ULL; |
66 | ||
67 | if (cpu_online(cpu)) | |
68 | iowait_time = get_cpu_iowait_time_us(cpu, NULL); | |
a25cac51 MH |
69 | |
70 | if (iowait_time == -1ULL) | |
7386cdbf | 71 | /* !NO_HZ or cpu offline so we can rely on cpustat.iowait */ |
3292beb3 | 72 | iowait = kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT]; |
a25cac51 | 73 | else |
34845636 | 74 | iowait = usecs_to_cputime64(iowait_time); |
a25cac51 MH |
75 | |
76 | return iowait; | |
77 | } | |
78 | ||
cb85a6ed MS |
79 | #endif |
80 | ||
df8106db AD |
81 | static int show_stat(struct seq_file *p, void *v) |
82 | { | |
83 | int i, j; | |
84 | unsigned long jif; | |
3292beb3 GC |
85 | u64 user, nice, system, idle, iowait, irq, softirq, steal; |
86 | u64 guest, guest_nice; | |
df8106db | 87 | u64 sum = 0; |
d3d64df2 KK |
88 | u64 sum_softirq = 0; |
89 | unsigned int per_softirq_sums[NR_SOFTIRQS] = {0}; | |
df8106db | 90 | struct timespec boottime; |
df8106db AD |
91 | |
92 | user = nice = system = idle = iowait = | |
3292beb3 GC |
93 | irq = softirq = steal = 0; |
94 | guest = guest_nice = 0; | |
df8106db AD |
95 | getboottime(&boottime); |
96 | jif = boottime.tv_sec; | |
97 | ||
98 | for_each_possible_cpu(i) { | |
3292beb3 GC |
99 | user += kcpustat_cpu(i).cpustat[CPUTIME_USER]; |
100 | nice += kcpustat_cpu(i).cpustat[CPUTIME_NICE]; | |
101 | system += kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]; | |
102 | idle += get_idle_time(i); | |
103 | iowait += get_iowait_time(i); | |
104 | irq += kcpustat_cpu(i).cpustat[CPUTIME_IRQ]; | |
105 | softirq += kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]; | |
106 | steal += kcpustat_cpu(i).cpustat[CPUTIME_STEAL]; | |
107 | guest += kcpustat_cpu(i).cpustat[CPUTIME_GUEST]; | |
108 | guest_nice += kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE]; | |
f7e6746e RK |
109 | sum += kstat_cpu_irqs_sum(i); |
110 | sum += arch_irq_stat_cpu(i); | |
d3d64df2 KK |
111 | |
112 | for (j = 0; j < NR_SOFTIRQS; j++) { | |
113 | unsigned int softirq_stat = kstat_softirqs_cpu(j, i); | |
114 | ||
115 | per_softirq_sums[j] += softirq_stat; | |
116 | sum_softirq += softirq_stat; | |
117 | } | |
df8106db AD |
118 | } |
119 | sum += arch_irq_stat(); | |
120 | ||
1ac101a5 KH |
121 | seq_puts(p, "cpu "); |
122 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(user)); | |
123 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(nice)); | |
124 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(system)); | |
125 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(idle)); | |
126 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(iowait)); | |
127 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(irq)); | |
128 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(softirq)); | |
129 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(steal)); | |
130 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest)); | |
131 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest_nice)); | |
132 | seq_putc(p, '\n'); | |
133 | ||
df8106db | 134 | for_each_online_cpu(i) { |
df8106db | 135 | /* Copy values here to work around gcc-2.95.3, gcc-2.96 */ |
3292beb3 GC |
136 | user = kcpustat_cpu(i).cpustat[CPUTIME_USER]; |
137 | nice = kcpustat_cpu(i).cpustat[CPUTIME_NICE]; | |
138 | system = kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]; | |
a25cac51 MH |
139 | idle = get_idle_time(i); |
140 | iowait = get_iowait_time(i); | |
3292beb3 GC |
141 | irq = kcpustat_cpu(i).cpustat[CPUTIME_IRQ]; |
142 | softirq = kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]; | |
143 | steal = kcpustat_cpu(i).cpustat[CPUTIME_STEAL]; | |
144 | guest = kcpustat_cpu(i).cpustat[CPUTIME_GUEST]; | |
145 | guest_nice = kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE]; | |
1ac101a5 KH |
146 | seq_printf(p, "cpu%d", i); |
147 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(user)); | |
148 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(nice)); | |
149 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(system)); | |
150 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(idle)); | |
151 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(iowait)); | |
152 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(irq)); | |
153 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(softirq)); | |
154 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(steal)); | |
155 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest)); | |
156 | seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest_nice)); | |
157 | seq_putc(p, '\n'); | |
df8106db AD |
158 | } |
159 | seq_printf(p, "intr %llu", (unsigned long long)sum); | |
160 | ||
161 | /* sum again ? it could be updated? */ | |
478735e3 | 162 | for_each_irq_nr(j) |
1ac101a5 | 163 | seq_put_decimal_ull(p, ' ', kstat_irqs(j)); |
df8106db AD |
164 | |
165 | seq_printf(p, | |
166 | "\nctxt %llu\n" | |
167 | "btime %lu\n" | |
168 | "processes %lu\n" | |
169 | "procs_running %lu\n" | |
170 | "procs_blocked %lu\n", | |
171 | nr_context_switches(), | |
172 | (unsigned long)jif, | |
173 | total_forks, | |
174 | nr_running(), | |
175 | nr_iowait()); | |
176 | ||
d3d64df2 KK |
177 | seq_printf(p, "softirq %llu", (unsigned long long)sum_softirq); |
178 | ||
179 | for (i = 0; i < NR_SOFTIRQS; i++) | |
1ac101a5 | 180 | seq_put_decimal_ull(p, ' ', per_softirq_sums[i]); |
9d6de12f | 181 | seq_putc(p, '\n'); |
d3d64df2 | 182 | |
df8106db AD |
183 | return 0; |
184 | } | |
185 | ||
186 | static int stat_open(struct inode *inode, struct file *file) | |
187 | { | |
9e5e8dec | 188 | size_t size = 1024 + 128 * num_possible_cpus(); |
df8106db AD |
189 | char *buf; |
190 | struct seq_file *m; | |
191 | int res; | |
192 | ||
59a32e2c ED |
193 | /* minimum size to display an interrupt count : 2 bytes */ |
194 | size += 2 * nr_irqs; | |
195 | ||
a4dbf0ec | 196 | /* don't ask for more than the kmalloc() max size */ |
6fa3eb70 S |
197 | //if (size > KMALLOC_MAX_SIZE) |
198 | // size = KMALLOC_MAX_SIZE; | |
199 | //buf = kmalloc(size, GFP_KERNEL); | |
200 | buf = vmalloc(size); | |
df8106db AD |
201 | if (!buf) |
202 | return -ENOMEM; | |
203 | ||
204 | res = single_open(file, show_stat, NULL); | |
205 | if (!res) { | |
206 | m = file->private_data; | |
207 | m->buf = buf; | |
6fa3eb70 S |
208 | //m->size = ksize(buf); |
209 | m->size = size; | |
df8106db | 210 | } else |
6fa3eb70 S |
211 | //kfree(buf); |
212 | vfree(buf); | |
df8106db AD |
213 | return res; |
214 | } | |
215 | ||
216 | static const struct file_operations proc_stat_operations = { | |
217 | .open = stat_open, | |
218 | .read = seq_read, | |
219 | .llseek = seq_lseek, | |
220 | .release = single_release, | |
221 | }; | |
222 | ||
223 | static int __init proc_stat_init(void) | |
224 | { | |
225 | proc_create("stat", 0, NULL, &proc_stat_operations); | |
226 | return 0; | |
227 | } | |
228 | module_init(proc_stat_init); |