Skip to content

Commit 5713f35

Browse files
Alexey Dobriyantorvalds
authored andcommitted
proc: read kernel cpu stat pointer once
Help gcc generate better code: $ ./scripts/bloat-o-meter ../vmlinux-000 ../vmlinux-001 add/remove: 2/2 grow/shrink: 0/1 up/down: 92/-142 (-50) Function old new delta get_iowait_time.isra - 46 +46 get_idle_time.isra - 46 +46 show_stat 1489 1477 -12 get_iowait_time 65 - -65 get_idle_time 65 - -65 Link: http://lkml.kernel.org/r/20190114195907.GA9680@avx2 Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 867aacc commit 5713f35

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

fs/proc/stat.c

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,29 @@
2323

2424
#ifdef arch_idle_time
2525

26-
static u64 get_idle_time(int cpu)
26+
static u64 get_idle_time(struct kernel_cpustat *kcs, int cpu)
2727
{
2828
u64 idle;
2929

30-
idle = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE];
30+
idle = kcs->cpustat[CPUTIME_IDLE];
3131
if (cpu_online(cpu) && !nr_iowait_cpu(cpu))
3232
idle += arch_idle_time(cpu);
3333
return idle;
3434
}
3535

36-
static u64 get_iowait_time(int cpu)
36+
static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu)
3737
{
3838
u64 iowait;
3939

40-
iowait = kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT];
40+
iowait = kcs->cpustat[CPUTIME_IOWAIT];
4141
if (cpu_online(cpu) && nr_iowait_cpu(cpu))
4242
iowait += arch_idle_time(cpu);
4343
return iowait;
4444
}
4545

4646
#else
4747

48-
static u64 get_idle_time(int cpu)
48+
static u64 get_idle_time(struct kernel_cpustat *kcs, int cpu)
4949
{
5050
u64 idle, idle_usecs = -1ULL;
5151

@@ -54,14 +54,14 @@ static u64 get_idle_time(int cpu)
5454

5555
if (idle_usecs == -1ULL)
5656
/* !NO_HZ or cpu offline so we can rely on cpustat.idle */
57-
idle = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE];
57+
idle = kcs->cpustat[CPUTIME_IDLE];
5858
else
5959
idle = idle_usecs * NSEC_PER_USEC;
6060

6161
return idle;
6262
}
6363

64-
static u64 get_iowait_time(int cpu)
64+
static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu)
6565
{
6666
u64 iowait, iowait_usecs = -1ULL;
6767

@@ -70,7 +70,7 @@ static u64 get_iowait_time(int cpu)
7070

7171
if (iowait_usecs == -1ULL)
7272
/* !NO_HZ or cpu offline so we can rely on cpustat.iowait */
73-
iowait = kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT];
73+
iowait = kcs->cpustat[CPUTIME_IOWAIT];
7474
else
7575
iowait = iowait_usecs * NSEC_PER_USEC;
7676

@@ -95,16 +95,18 @@ static int show_stat(struct seq_file *p, void *v)
9595
getboottime64(&boottime);
9696

9797
for_each_possible_cpu(i) {
98-
user += kcpustat_cpu(i).cpustat[CPUTIME_USER];
99-
nice += kcpustat_cpu(i).cpustat[CPUTIME_NICE];
100-
system += kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM];
101-
idle += get_idle_time(i);
102-
iowait += get_iowait_time(i);
103-
irq += kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
104-
softirq += kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ];
105-
steal += kcpustat_cpu(i).cpustat[CPUTIME_STEAL];
106-
guest += kcpustat_cpu(i).cpustat[CPUTIME_GUEST];
107-
guest_nice += kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE];
98+
struct kernel_cpustat *kcs = &kcpustat_cpu(i);
99+
100+
user += kcs->cpustat[CPUTIME_USER];
101+
nice += kcs->cpustat[CPUTIME_NICE];
102+
system += kcs->cpustat[CPUTIME_SYSTEM];
103+
idle += get_idle_time(kcs, i);
104+
iowait += get_iowait_time(kcs, i);
105+
irq += kcs->cpustat[CPUTIME_IRQ];
106+
softirq += kcs->cpustat[CPUTIME_SOFTIRQ];
107+
steal += kcs->cpustat[CPUTIME_STEAL];
108+
guest += kcs->cpustat[CPUTIME_GUEST];
109+
guest_nice += kcs->cpustat[CPUTIME_GUEST_NICE];
108110
sum += kstat_cpu_irqs_sum(i);
109111
sum += arch_irq_stat_cpu(i);
110112

@@ -130,17 +132,19 @@ static int show_stat(struct seq_file *p, void *v)
130132
seq_putc(p, '\n');
131133

132134
for_each_online_cpu(i) {
135+
struct kernel_cpustat *kcs = &kcpustat_cpu(i);
136+
133137
/* Copy values here to work around gcc-2.95.3, gcc-2.96 */
134-
user = kcpustat_cpu(i).cpustat[CPUTIME_USER];
135-
nice = kcpustat_cpu(i).cpustat[CPUTIME_NICE];
136-
system = kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM];
137-
idle = get_idle_time(i);
138-
iowait = get_iowait_time(i);
139-
irq = kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
140-
softirq = kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ];
141-
steal = kcpustat_cpu(i).cpustat[CPUTIME_STEAL];
142-
guest = kcpustat_cpu(i).cpustat[CPUTIME_GUEST];
143-
guest_nice = kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE];
138+
user = kcs->cpustat[CPUTIME_USER];
139+
nice = kcs->cpustat[CPUTIME_NICE];
140+
system = kcs->cpustat[CPUTIME_SYSTEM];
141+
idle = get_idle_time(kcs, i);
142+
iowait = get_iowait_time(kcs, i);
143+
irq = kcs->cpustat[CPUTIME_IRQ];
144+
softirq = kcs->cpustat[CPUTIME_SOFTIRQ];
145+
steal = kcs->cpustat[CPUTIME_STEAL];
146+
guest = kcs->cpustat[CPUTIME_GUEST];
147+
guest_nice = kcs->cpustat[CPUTIME_GUEST_NICE];
144148
seq_printf(p, "cpu%d", i);
145149
seq_put_decimal_ull(p, " ", nsec_to_clock_t(user));
146150
seq_put_decimal_ull(p, " ", nsec_to_clock_t(nice));

0 commit comments

Comments
 (0)