Skip to contentSkip to conversation

sched/cputime: move rq parameter in irqtime_account_process_tick

PATCH Applied Alex Shi 4 participants 8 messages Jan 2, 2020 – Jan 17, 2020

Subsystems (2)
lore
Share & tools
lore

Alex Shi

author
#0 lore raw

PATCH 1/3 sched/cputime: move rq parameter in irqtime_account_process_tick

Every time we call irqtime_account_process_tick() is in a interrupt, Every caller will get and assign a parameter rq = this_rq(), This is unnecessary and increase the code size a little bit. Move the rq getting action to irqtime_account_process_tick internally is better.

base with this patch
cputime.o 578792 bytes 577888 bytes

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Wanpeng Li <[email protected]>
Cc Anna-Maria Gleixner <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 kernel/sched/cputime.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
kernel/sched/cputime.c +6 -9
index d43318a489f2..cff3e656566d 100644
@@ -355,7 +355,7 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
  * 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)
+					 int ticks)
 {
 	u64 other, cputime = TICK_NSEC * ticks;
 
@@ -381,7 +381,7 @@ static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
 		account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
 	} else if (user_tick) {
 		account_user_time(p, cputime);
-	} else if (p == rq->idle) {
+	} else if (p == this_rq()->idle) {
 		account_idle_time(cputime);
 	} else if (p->flags & PF_VCPU) { /* System time or guest time */
 		account_guest_time(p, cputime);
@@ -392,14 +392,12 @@ static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
 
 static void irqtime_account_idle_ticks(int ticks)
 {
-	struct rq *rq = this_rq();
-
-	irqtime_account_process_tick(current, 0, rq, ticks);
+	irqtime_account_process_tick(current, 0, 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) { }
+						int nr_ticks) { }
 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
 
 /*
@@ -473,13 +471,12 @@ 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;
-	struct rq *rq = this_rq();
 
 	if (vtime_accounting_enabled_this_cpu())
 		return;
 
 	if (sched_clock_irqtime) {
-		irqtime_account_process_tick(p, user_tick, rq, 1);
+		irqtime_account_process_tick(p, user_tick, 1);
 		return;
 	}
 
@@ -493,7 +490,7 @@ void account_process_tick(struct task_struct *p, int user_tick)
 
 	if (user_tick)
 		account_user_time(p, cputime);
-	else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
+	else if ((p != this_rq()->idle) || (irq_count() != HARDIRQ_OFFSET))
 		account_system_time(p, HARDIRQ_OFFSET, cputime);
 	else
 		account_idle_time(cputime);
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 2/3 sched/cputime: code cleanup in irqtime_account_process_tick

In this func, since account_system_time() considers guest time account and other system time. we could fold the account_guest_time into account_system_time() to simply the code.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Wanpeng Li <[email protected]>
Cc Anna-Maria Gleixner <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 kernel/sched/cputime.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/sched/cputime.c +3 -6
index cff3e656566d..46b837e94fce 100644
@@ -381,13 +381,10 @@ static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
 		account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
 	} else if (user_tick) {
 		account_user_time(p, cputime);
-	} else if (p == this_rq()->idle) {
+	} else if ((p != this_rq()->idle) || (irq_count() != HARDIRQ_OFFSET))
+		account_system_time(p, HARDIRQ_OFFSET, cputime);
+	else
 		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);
-	}
 }
 
 static void irqtime_account_idle_ticks(int ticks)
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH 2/3] sched/cputime: code cleanup in irqtime_account_process_tick

Quoted 21 lines · Alex Shi

In this func, since account_system_time() considers guest time account and other system time. we could fold the account_guest_time into account_system_time() to simply the code.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Wanpeng Li <[email protected]>
Cc Anna-Maria Gleixner <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 kernel/sched/cputime.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/sched/cputime.c +0 -0
index cff3e656566d..46b837e94fce 100644

kernel/sched/cputime.c static void irqtime_account_process_tick(struct task_struct *p, int user_tick, · line 386

Quoted 7 lines
kernel/sched/cputime.c +3 -1
@@ -381,13 +381,10 @@ static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
 		account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
 	} else if (user_tick) {
 		account_user_time(p, cputime);
-	} else if (p == this_rq()->idle) {
+	} else if ((p != this_rq()->idle) || (irq_count() != HARDIRQ_OFFSET))
+		account_system_time(p, HARDIRQ_OFFSET, cputime);
+	else

I fear we can't really play the exact same game as account_process_tick() here. Since this is irqtime precise accounting, we have already computed the irqtime delta in account_other_time() (or we will at some point in the future) and substracted it from the ticks to account. This means that the remaining cputime to account has to be either utime/stime/gtime/idle-time but not interrupt time, or we may account interrupt time twice. And account_system_time() tries to account irq time, for example if we interrupt a softirq.

Thanks.

Quoted 12 lines
kernel/sched/cputime.c +0 -5
 		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);
-	}
 }
 
 static void irqtime_account_idle_ticks(int ticks)
Signature · 1 line
1.8.3.1
maintainer
#3 lore raw

Re: [PATCH 2/3] sched/cputime: code cleanup in irqtime_account_process_tick

Quoted 30 lines · Frederic Weisbecker
Quoted 21 lines · Alex Shi

In this func, since account_system_time() considers guest time account and other system time. we could fold the account_guest_time into account_system_time() to simply the code.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Wanpeng Li <[email protected]>
Cc Anna-Maria Gleixner <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 kernel/sched/cputime.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/sched/cputime.c +0 -0
index cff3e656566d..46b837e94fce 100644

kernel/sched/cputime.c static void irqtime_account_process_tick(struct task_struct *p, int user_tick, · line 386

Quoted 7 lines
kernel/sched/cputime.c +3 -1
@@ -381,13 +381,10 @@ static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
 		account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
 	} else if (user_tick) {
 		account_user_time(p, cputime);
-	} else if (p == this_rq()->idle) {
+	} else if ((p != this_rq()->idle) || (irq_count() != HARDIRQ_OFFSET))
+		account_system_time(p, HARDIRQ_OFFSET, cputime);
+	else
Quoted 7 lines

I fear we can't really play the exact same game as account_process_tick() here. Since this is irqtime precise accounting, we have already computed the irqtime delta in account_other_time() (or we will at some point in the future) and substracted it from the ticks to account. This means that the remaining cputime to account has to be either utime/stime/gtime/idle-time but not interrupt time, or we may account interrupt time twice. And account_system_time() tries to account irq time, for example if we interrupt a softirq.

OK, I've dropped 2 and 3. Thanks Frederic!

author
#4 lore raw

Re: [PATCH 2/3] sched/cputime: code cleanup in irqtime_account_process_tick

Quoted 11 lines
Quoted 8 lines

I fear we can't really play the exact same game as account_process_tick() here. Since this is irqtime precise accounting, we have already computed the irqtime delta in account_other_time() (or we will at some point in the future) and substracted it from the ticks to account. This means that the remaining cputime to account has to be either utime/stime/gtime/idle-time but not interrupt time, or we may account interrupt time twice. And account_system_time() tries to account irq time, for example if we interrupt a softirq.

OK, I've dropped 2 and 3. Thanks Frederic!

Hi Frederic & Peter,

Thanks a lot for the comments and review!
It's my fault to mess up the account_system_time details. And seems there is no easy way to replace irqtime_account_process_tick or account_process_tick with each other.

but on the other side, the account_idle_ticks could be replaced by irqtime_account_process_tick, or at least to remove irqtime_account_idle_ticks function. Any comments?

Thanks
Alex

---

From 7073e60babc3b42a987b4e89f380956887734233 Mon Sep 17 00:00:00 2001
From: Alex Shi <[email protected]>
Date: Thu, 9 Jan 2020 20:32:55 +0800
Subject: [PATCH] sched/cputime: remove irqtime_account_idle_ticks

irqtime_account_idle_ticks and irqtime_account_process_tick use in same condition. We don't bother to name and use a irqtime_account_idle_ticks for only one calling. Remove the function to simply code and reduce a bit object size of kernel.

And further more, we could replace account_idle_ticks by irqtime_account_process_tick too. But feed and check 'current' looks weird. So this is ok.

Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/sched/cputime.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
kernel/sched/cputime.c +1 -6
index cff3e656566d..17640d145e44 100644
@@ -390,12 +390,7 @@ static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
 	}
 }
 
-static void irqtime_account_idle_ticks(int ticks)
-{
-	irqtime_account_process_tick(current, 0, 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,
 						int nr_ticks) { }
 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
@@ -505,7 +500,7 @@ void account_idle_ticks(unsigned long ticks)
 	u64 cputime, steal;
 
 	if (sched_clock_irqtime) {
-		irqtime_account_idle_ticks(ticks);
+		irqtime_account_process_tick(current, 0, ticks);
 		return;
 	}
 
Signature · 1 line
1.8.3.1
author
#5 lore raw

PATCH 3/3 sched/cputime: cleanup account_process_tick/account_idle_ticks

In reply to Alex Shi

The irqtime_account_process_tick() was introduced for precise ns irq time account from commit abb74cefa9c6 ("sched: Export ns irqtimes through /proc/stat") which is necessary since account_process_tick() still use jiffes at that time.

Now account_process_tick/account_idle_ticks functions do the actual same actions as irqtime_account_process_tick() to account ns precision cputime when !sched_clock_irqtime. which could be replaced by irqtime_account_process_tick();

So we move out irqtime_account_process_tick from IRQ_TIME_ACCOUNTING config, let it work w/o IRQ_TIME_ACCOUNTING. and remove the duplicated code in account_process_tick/account_idle_ticks.
And furthmore I removed the function account_idle_ticks by a directly call.

It can simplify the code, also reduce a bit object size.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Wanpeng Li <[email protected]>
Cc Anna-Maria Gleixner <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 include/linux/kernel_stat.h |  2 +-
 kernel/sched/cputime.c      | 58 ++-------------------------------------------
 kernel/time/tick-sched.c    |  2 +-
 3 files changed, 4 insertions(+), 58 deletions(-)
include/linux/kernel_stat.h +1 -1
index 89f0745c096d..e924bdb8c874 100644
@@ -113,6 +113,6 @@ static inline void account_process_tick(struct task_struct *tsk, int user)
 extern void account_process_tick(struct task_struct *, int user);
 #endif
 
-extern void account_idle_ticks(unsigned long ticks);
+extern void irqtime_account_process_tick(struct task_struct *, int , int);
 
 #endif /* _LINUX_KERNEL_STAT_H */
kernel/sched/cputime.c +2 -56

85 lines not on this page · raw message

kernel/time/tick-sched.c +1 -1
index 8b192e67aabc..92a0979633e9 100644
@@ -1142,7 +1142,7 @@ static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
 	 * We might be one off. Do not randomly account a huge number of ticks!
 	 */
 	if (ticks && ticks < LONG_MAX)
-		account_idle_ticks(ticks);
+		irqtime_account_process_tick(current, 0, ticks);
 #endif
 }
 
Signature · 1 line
1.8.3.1
merge bot Applied
#7 lore raw
In reply to Alex Shi
The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     9dec1b6949ae9509cdc3edb2d75fda39c9db9fa2
Gitweb:        https://git.kernel.org/tip/9dec1b6949ae9509cdc3edb2d75fda39c9db9fa2
Author:        Alex Shi <[email protected]>
AuthorDate:    Thu, 02 Jan 2020 18:07:52 +08:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 17 Jan 2020 10:19:21 +01:00

sched/cputime: move rq parameter in irqtime_account_process_tick

Every time we call irqtime_account_process_tick() is in a interrupt, Every caller will get and assign a parameter rq = this_rq(), This is unnecessary and increase the code size a little bit. Move the rq getting action to irqtime_account_process_tick internally is better.

base with this patch
cputime.o 578792 bytes 577888 bytes

---
 kernel/sched/cputime.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
kernel/sched/cputime.c +6 -9
index d43318a..cff3e65 100644
@@ -355,7 +355,7 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
  * 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)
+					 int ticks)
 {
 	u64 other, cputime = TICK_NSEC * ticks;
 
@@ -381,7 +381,7 @@ static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
 		account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
 	} else if (user_tick) {
 		account_user_time(p, cputime);
-	} else if (p == rq->idle) {
+	} else if (p == this_rq()->idle) {
 		account_idle_time(cputime);
 	} else if (p->flags & PF_VCPU) { /* System time or guest time */
 		account_guest_time(p, cputime);
@@ -392,14 +392,12 @@ static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
 
 static void irqtime_account_idle_ticks(int ticks)
 {
-	struct rq *rq = this_rq();
-
-	irqtime_account_process_tick(current, 0, rq, ticks);
+	irqtime_account_process_tick(current, 0, 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) { }
+						int nr_ticks) { }
 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
 
 /*
@@ -473,13 +471,12 @@ 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;
-	struct rq *rq = this_rq();
 
 	if (vtime_accounting_enabled_this_cpu())
 		return;
 
 	if (sched_clock_irqtime) {
-		irqtime_account_process_tick(p, user_tick, rq, 1);
+		irqtime_account_process_tick(p, user_tick, 1);
 		return;
 	}
 
@@ -493,7 +490,7 @@ void account_process_tick(struct task_struct *p, int user_tick)
 
 	if (user_tick)
 		account_user_time(p, cputime);
-	else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
+	else if ((p != this_rq()->idle) || (irq_count() != HARDIRQ_OFFSET))
 		account_system_time(p, HARDIRQ_OFFSET, cputime);
 	else
 		account_idle_time(cputime);