2019-07-23 14:15:53

by Alex Shi

[permalink] [raw]
Subject: [RFC PATCH 1/3] cputime: fix a account error of softirq

According the comments before this line:
* ksoftirqd time do not get accounted in cpu_softirq_time.
And process in irqtime_account_irq()
I guess the original attempt is to account ksoftirqd into
system time instead of softirq time.

Signed-off-by: Alex Shi <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Frederic Weisbecker <[email protected]>
Cc: Wanpeng Li <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: [email protected]
---
kernel/sched/cputime.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index 2305ce89a26c..d78aee140957 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -378,7 +378,7 @@ static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
* So, we have to handle it separately here.
* Also, p->stime needs to be updated for ksoftirqd.
*/
- account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
+ account_system_index_time(p, cputime, CPUTIME_SYSTEM);
} else if (user_tick) {
account_user_time(p, cputime);
} else if (p == rq->idle) {
--
2.19.1.856.g8858448bb


2019-07-23 14:15:58

by Alex Shi

[permalink] [raw]
Subject: [RFC PATCH 2/3] cputime: unify account_idle_ticks

Check the 'current' task in account_idle_ticks is meaningless. So we
could remove irqtime_account_idle_ticks and unify this function.

Signed-off-by: Alex Shi <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Frederic Weisbecker <[email protected]>
Cc: Wanpeng Li <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: [email protected]
---
kernel/sched/cputime.c | 21 ++++-----------------
1 file changed, 4 insertions(+), 17 deletions(-)

diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index d78aee140957..3bf94eb7b7c6 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -389,15 +389,7 @@ static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
account_system_index_time(p, cputime, CPUTIME_SYSTEM);
}
}
-
-static void irqtime_account_idle_ticks(int ticks)
-{
- struct rq *rq = this_rq();
-
- irqtime_account_process_tick(current, 0, rq, ticks);
-}
#else /* CONFIG_IRQ_TIME_ACCOUNTING */
-static inline void irqtime_account_idle_ticks(int ticks) { }
static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
struct rq *rq, int nr_ticks) { }
#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
@@ -507,20 +499,15 @@ void account_process_tick(struct task_struct *p, int user_tick)
*/
void account_idle_ticks(unsigned long ticks)
{
- u64 cputime, steal;
-
- if (sched_clock_irqtime) {
- irqtime_account_idle_ticks(ticks);
- return;
- }
+ u64 cputime, other;

cputime = ticks * TICK_NSEC;
- steal = steal_account_process_time(ULONG_MAX);
+ other = account_other_time(ULONG_MAX);

- if (steal >= cputime)
+ if (other >= cputime)
return;

- cputime -= steal;
+ cputime -= other;
account_idle_time(cputime);
}

--
2.19.1.856.g8858448bb

2019-07-23 16:09:41

by Alex Shi

[permalink] [raw]
Subject: [RFC PATCH 3/3] cputime: unify account_process_tick func

The irqtime_account_process_tick path was introduced for precise ns irq
time account from commit abb74cefa9c6 ("sched: Export ns irqtimes
through /proc/stat") while account_process_tick still use jiffes. This
divide isn't necessary especially now both paths are ns precison.

Move out the irqtime_account_process_tick func from IRQ_TIME_ACCOUNTING.
and combine the code for both *account_process_tick funcs for 2 paths.
Then remove the useless irqtime_account_process_tick().

Signed-off-by: Alex Shi <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Frederic Weisbecker <[email protected]>
Cc: Wanpeng Li <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: [email protected]
---
kernel/sched/cputime.c | 84 ++++++------------------------------------
1 file changed, 12 insertions(+), 72 deletions(-)

diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index 3bf94eb7b7c6..3cc581409252 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -332,68 +332,6 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
rcu_read_unlock();
}

-#ifdef CONFIG_IRQ_TIME_ACCOUNTING
-/*
- * Account a tick to a process and cpustat
- * @p: the process that the CPU time gets accounted to
- * @user_tick: is the tick from userspace
- * @rq: the pointer to rq
- *
- * Tick demultiplexing follows the order
- * - pending hardirq update
- * - pending softirq update
- * - user_time
- * - idle_time
- * - system time
- * - check for guest_time
- * - else account as system_time
- *
- * Check for hardirq is done both for system and user time as there is
- * no timer going off while we are on hardirq and hence we may never get an
- * opportunity to update it solely in system time.
- * p->stime and friends are only updated on system time and not on irq
- * softirq as those do not count in task exec_runtime any more.
- */
-static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
- struct rq *rq, int ticks)
-{
- u64 other, cputime = TICK_NSEC * ticks;
-
- /*
- * When returning from idle, many ticks can get accounted at
- * once, including some ticks of steal, irq, and softirq time.
- * Subtract those ticks from the amount of time accounted to
- * idle, or potentially user or system time. Due to rounding,
- * other time can exceed ticks occasionally.
- */
- other = account_other_time(ULONG_MAX);
- if (other >= cputime)
- return;
-
- cputime -= other;
-
- if (this_cpu_ksoftirqd() == p) {
- /*
- * ksoftirqd time do not get accounted in cpu_softirq_time.
- * So, we have to handle it separately here.
- * Also, p->stime needs to be updated for ksoftirqd.
- */
- account_system_index_time(p, cputime, CPUTIME_SYSTEM);
- } else if (user_tick) {
- account_user_time(p, cputime);
- } else if (p == rq->idle) {
- account_idle_time(cputime);
- } else if (p->flags & PF_VCPU) { /* System time or guest time */
- account_guest_time(p, cputime);
- } else {
- account_system_index_time(p, cputime, CPUTIME_SYSTEM);
- }
-}
-#else /* CONFIG_IRQ_TIME_ACCOUNTING */
-static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
- struct rq *rq, int nr_ticks) { }
-#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
-
/*
* Use precise platform statistics if available:
*/
@@ -466,26 +404,28 @@ void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
*/
void account_process_tick(struct task_struct *p, int user_tick)
{
- u64 cputime, steal;
+ u64 cputime, other;
struct rq *rq = this_rq();

if (vtime_accounting_cpu_enabled())
return;

- if (sched_clock_irqtime) {
- irqtime_account_process_tick(p, user_tick, rq, 1);
- return;
- }
-
cputime = TICK_NSEC;
- steal = steal_account_process_time(ULONG_MAX);
+ other = account_other_time(ULONG_MAX);

- if (steal >= cputime)
+ if (other >= cputime)
return;

- cputime -= steal;
+ cputime -= other;

- if (user_tick)
+ if (this_cpu_ksoftirqd() == p)
+ /*
+ * ksoftirqd time do not get accounted in cpu_softirq_time.
+ * So, we have to handle it separately here.
+ * Also, p->stime needs to be updated for ksoftirqd.
+ */
+ account_system_index_time(p, cputime, CPUTIME_SYSTEM);
+ else if (user_tick)
account_user_time(p, cputime);
else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
account_system_time(p, HARDIRQ_OFFSET, cputime);
--
2.19.1.856.g8858448bb