Skip to content

Commit ce0e7b2

Browse files
ozaki-rIngo Molnar
authored andcommitted
sched, cpuacct: Fix niced guest time accounting
CPU time of a guest is always accounted in 'user' time without concern for the nice value of its counterpart process although the guest is scheduled under the nice value. This patch fixes the defect and accounts cpu time of a niced guest in 'nice' time as same as a niced process. And also the patch adds 'guest_nice' to cpuacct. The value provides niced guest cpu time which is like 'nice' to 'user'. The original discussions can be found here: http://www.mail-archive.com/kvm@vger.kernel.org/msg23982.html http://www.mail-archive.com/kvm@vger.kernel.org/msg23860.html Signed-off-by: Ryota Ozaki <ozaki.ryota@gmail.com> Acked-by: Avi Kivity <avi@redhat.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> LKML-Reference: <1256314810-7897-1-git-send-email-ozaki.ryota@gmail.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 0b9e31e commit ce0e7b2

4 files changed

Lines changed: 23 additions & 9 deletions

File tree

‎Documentation/filesystems/proc.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,8 @@ second). The meanings of the columns are as follows, from left to right:
10721072
- irq: servicing interrupts
10731073
- softirq: servicing softirqs
10741074
- steal: involuntary wait
1075-
- guest: running a guest
1075+
- guest: running a normal guest
1076+
- guest_nice: running a niced guest
10761077

10771078
The "intr" line gives counts of interrupts serviced since boot time, for each
10781079
of the possible system interrupts. The first column is the total of all

‎fs/proc/stat.c‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static int show_stat(struct seq_file *p, void *v)
2727
int i, j;
2828
unsigned long jif;
2929
cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
30-
cputime64_t guest;
30+
cputime64_t guest, guest_nice;
3131
u64 sum = 0;
3232
u64 sum_softirq = 0;
3333
unsigned int per_softirq_sums[NR_SOFTIRQS] = {0};
@@ -36,7 +36,7 @@ static int show_stat(struct seq_file *p, void *v)
3636

3737
user = nice = system = idle = iowait =
3838
irq = softirq = steal = cputime64_zero;
39-
guest = cputime64_zero;
39+
guest = guest_nice = cputime64_zero;
4040
getboottime(&boottime);
4141
jif = boottime.tv_sec;
4242

@@ -51,6 +51,8 @@ static int show_stat(struct seq_file *p, void *v)
5151
softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
5252
steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
5353
guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
54+
guest_nice = cputime64_add(guest_nice,
55+
kstat_cpu(i).cpustat.guest_nice);
5456
for_each_irq_nr(j) {
5557
sum += kstat_irqs_cpu(j, i);
5658
}
@@ -65,7 +67,8 @@ static int show_stat(struct seq_file *p, void *v)
6567
}
6668
sum += arch_irq_stat();
6769

68-
seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
70+
seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu "
71+
"%llu\n",
6972
(unsigned long long)cputime64_to_clock_t(user),
7073
(unsigned long long)cputime64_to_clock_t(nice),
7174
(unsigned long long)cputime64_to_clock_t(system),
@@ -74,7 +77,8 @@ static int show_stat(struct seq_file *p, void *v)
7477
(unsigned long long)cputime64_to_clock_t(irq),
7578
(unsigned long long)cputime64_to_clock_t(softirq),
7679
(unsigned long long)cputime64_to_clock_t(steal),
77-
(unsigned long long)cputime64_to_clock_t(guest));
80+
(unsigned long long)cputime64_to_clock_t(guest),
81+
(unsigned long long)cputime64_to_clock_t(guest_nice));
7882
for_each_online_cpu(i) {
7983

8084
/* Copy values here to work around gcc-2.95.3, gcc-2.96 */
@@ -88,8 +92,10 @@ static int show_stat(struct seq_file *p, void *v)
8892
softirq = kstat_cpu(i).cpustat.softirq;
8993
steal = kstat_cpu(i).cpustat.steal;
9094
guest = kstat_cpu(i).cpustat.guest;
95+
guest_nice = kstat_cpu(i).cpustat.guest_nice;
9196
seq_printf(p,
92-
"cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
97+
"cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu "
98+
"%llu\n",
9399
i,
94100
(unsigned long long)cputime64_to_clock_t(user),
95101
(unsigned long long)cputime64_to_clock_t(nice),
@@ -99,7 +105,8 @@ static int show_stat(struct seq_file *p, void *v)
99105
(unsigned long long)cputime64_to_clock_t(irq),
100106
(unsigned long long)cputime64_to_clock_t(softirq),
101107
(unsigned long long)cputime64_to_clock_t(steal),
102-
(unsigned long long)cputime64_to_clock_t(guest));
108+
(unsigned long long)cputime64_to_clock_t(guest),
109+
(unsigned long long)cputime64_to_clock_t(guest_nice));
103110
}
104111
seq_printf(p, "intr %llu", (unsigned long long)sum);
105112

‎include/linux/kernel_stat.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct cpu_usage_stat {
2525
cputime64_t iowait;
2626
cputime64_t steal;
2727
cputime64_t guest;
28+
cputime64_t guest_nice;
2829
};
2930

3031
struct kernel_stat {

‎kernel/sched.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5017,8 +5017,13 @@ static void account_guest_time(struct task_struct *p, cputime_t cputime,
50175017
p->gtime = cputime_add(p->gtime, cputime);
50185018

50195019
/* Add guest time to cpustat. */
5020-
cpustat->user = cputime64_add(cpustat->user, tmp);
5021-
cpustat->guest = cputime64_add(cpustat->guest, tmp);
5020+
if (TASK_NICE(p) > 0) {
5021+
cpustat->nice = cputime64_add(cpustat->nice, tmp);
5022+
cpustat->guest_nice = cputime64_add(cpustat->guest_nice, tmp);
5023+
} else {
5024+
cpustat->user = cputime64_add(cpustat->user, tmp);
5025+
cpustat->guest = cputime64_add(cpustat->guest, tmp);
5026+
}
50225027
}
50235028

50245029
/*

0 commit comments

Comments
 (0)