[PATCH 1/9] sched/cputime: Allow to pass cputime index on user/guest accounting

From: Frederic Weisbecker
Date: Tue Nov 05 2019 - 22:08:26 EST


When we account user or guest cputime, we decide to add the delta either
to the nice fields or the normal fields of kcpustat and this depends on
the nice value for the task passed in parameter.

Since we are going to track the nice-ness from vtime instead, we'll need
to be able to pass custom kcpustat destination index fields to the
accounting functions.

Signed-off-by: Frederic Weisbecker <frederic@xxxxxxxxxx>
Cc: Yauheni Kaliuta <yauheni.kaliuta@xxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Rik van Riel <riel@xxxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Wanpeng Li <wanpengli@xxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
---
kernel/sched/cputime.c | 50 +++++++++++++++++++++++++++---------------
1 file changed, 32 insertions(+), 18 deletions(-)

diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index e0cd20693ef5..738ed7db615e 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -109,6 +109,20 @@ static inline void task_group_account_field(struct task_struct *p, int index,
cgroup_account_cputime_field(p, index, tmp);
}

+static void account_user_time_index(struct task_struct *p,
+ u64 cputime, enum cpu_usage_stat index)
+{
+ /* Add user time to process. */
+ p->utime += cputime;
+ account_group_user_time(p, cputime);
+
+ /* Add user time to cpustat. */
+ task_group_account_field(p, index, cputime);
+
+ /* Account for user time used */
+ acct_account_cputime(p);
+}
+
/*
* Account user CPU time to a process.
* @p: the process that the CPU time gets accounted to
@@ -116,27 +130,14 @@ static inline void task_group_account_field(struct task_struct *p, int index,
*/
void account_user_time(struct task_struct *p, u64 cputime)
{
- int index;
-
- /* Add user time to process. */
- p->utime += cputime;
- account_group_user_time(p, cputime);
+ enum cpu_usage_stat index;

index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
-
- /* Add user time to cpustat. */
- task_group_account_field(p, index, cputime);
-
- /* Account for user time used */
- acct_account_cputime(p);
+ account_user_time_index(p, cputime, index);
}

-/*
- * Account guest CPU time to a process.
- * @p: the process that the CPU time gets accounted to
- * @cputime: the CPU time spent in virtual machine since the last update
- */
-void account_guest_time(struct task_struct *p, u64 cputime)
+static void account_guest_time_index(struct task_struct *p,
+ u64 cputime, enum cpu_usage_stat index)
{
u64 *cpustat = kcpustat_this_cpu->cpustat;

@@ -146,7 +147,7 @@ void account_guest_time(struct task_struct *p, u64 cputime)
p->gtime += cputime;

/* Add guest time to cpustat. */
- if (task_nice(p) > 0) {
+ if (index == CPUTIME_GUEST_NICE) {
cpustat[CPUTIME_NICE] += cputime;
cpustat[CPUTIME_GUEST_NICE] += cputime;
} else {
@@ -155,6 +156,19 @@ void account_guest_time(struct task_struct *p, u64 cputime)
}
}

+/*
+ * Account guest CPU time to a process.
+ * @p: the process that the CPU time gets accounted to
+ * @cputime: the CPU time spent in virtual machine since the last update
+ */
+void account_guest_time(struct task_struct *p, u64 cputime)
+{
+ enum cpu_usage_stat index;
+
+ index = (task_nice(p) > 0) ? CPUTIME_GUEST_NICE : CPUTIME_GUEST;
+ account_guest_time_index(p, cputime, index);
+}
+
/*
* Account system CPU time to a process and desired cpustat field
* @p: the process that the CPU time gets accounted to
--
2.23.0