sched/cpuacct: Use loop to consolidate code in cpuacct_stats_show()
authorZhao Lei <zhaolei@cn.fujitsu.com>
Mon, 20 Jun 2016 09:37:19 +0000 (17:37 +0800)
committerIngo Molnar <mingo@kernel.org>
Sat, 9 Jul 2016 11:56:15 +0000 (13:56 +0200)
In cpuacct_stats_show() we currently we have copies of similar code,
for each cpustat(system/user) variant.

Use a loop instead to consolidate the code. This will also work better
if we extend the CPUACCT_STAT_NSTATS type.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/b0597d4224655e9f333f1a6224ed9654c7d7d36a.1466415271.git.zhaolei@cn.fujitsu.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
kernel/sched/cpuacct.c

index 74241eb5f3ff5eb4e42a69fbe96f7edadbad42e1..677cd1ab33b742838ad44b546a4688068de309df 100644 (file)
@@ -243,27 +243,26 @@ static int cpuacct_percpu_seq_show(struct seq_file *m, void *V)
 static int cpuacct_stats_show(struct seq_file *sf, void *v)
 {
        struct cpuacct *ca = css_ca(seq_css(sf));
+       s64 val[CPUACCT_STAT_NSTATS];
        int cpu;
-       s64 val = 0;
+       int stat;
 
+       memset(val, 0, sizeof(val));
        for_each_possible_cpu(cpu) {
-               struct kernel_cpustat *kcpustat = per_cpu_ptr(ca->cpustat, cpu);
-               val += kcpustat->cpustat[CPUTIME_USER];
-               val += kcpustat->cpustat[CPUTIME_NICE];
-       }
-       val = cputime64_to_clock_t(val);
-       seq_printf(sf, "%s %lld\n", cpuacct_stat_desc[CPUACCT_STAT_USER], val);
+               u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat;
 
-       val = 0;
-       for_each_possible_cpu(cpu) {
-               struct kernel_cpustat *kcpustat = per_cpu_ptr(ca->cpustat, cpu);
-               val += kcpustat->cpustat[CPUTIME_SYSTEM];
-               val += kcpustat->cpustat[CPUTIME_IRQ];
-               val += kcpustat->cpustat[CPUTIME_SOFTIRQ];
+               val[CPUACCT_STAT_USER]   += cpustat[CPUTIME_USER];
+               val[CPUACCT_STAT_USER]   += cpustat[CPUTIME_NICE];
+               val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_SYSTEM];
+               val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_IRQ];
+               val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_SOFTIRQ];
        }
 
-       val = cputime64_to_clock_t(val);
-       seq_printf(sf, "%s %lld\n", cpuacct_stat_desc[CPUACCT_STAT_SYSTEM], val);
+       for (stat = 0; stat < CPUACCT_STAT_NSTATS; stat++) {
+               seq_printf(sf, "%s %lld\n",
+                          cpuacct_stat_desc[stat],
+                          cputime64_to_clock_t(val[stat]));
+       }
 
        return 0;
 }