2020-01-02 10:09:14

by Alex Shi

[permalink] [raw]
Subject: [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

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: Anna-Maria Gleixner <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: [email protected]
---
kernel/sched/cputime.c | 15 ++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index d43318a489f2..cff3e656566d 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -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);
--
1.8.3.1


2020-01-02 10:09:30

by Alex Shi

[permalink] [raw]
Subject: [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.

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: Anna-Maria Gleixner <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: [email protected]
---
kernel/sched/cputime.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index cff3e656566d..46b837e94fce 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -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)
--
1.8.3.1

2020-01-02 10:10:18

by Alex Shi

[permalink] [raw]
Subject: [PATCH 3/3] sched/cputime: cleanup account_process_tick/account_idle_ticks

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.

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: Anna-Maria Gleixner <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: [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(-)

diff --git a/include/linux/kernel_stat.h b/include/linux/kernel_stat.h
index 89f0745c096d..e924bdb8c874 100644
--- a/include/linux/kernel_stat.h
+++ b/include/linux/kernel_stat.h
@@ -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 */
diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index 46b837e94fce..0e0c74c1b3c9 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -332,7 +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
@@ -354,7 +353,7 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
* 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,
+void irqtime_account_process_tick(struct task_struct *p, int user_tick,
int ticks)
{
u64 other, cputime = TICK_NSEC * ticks;
@@ -387,16 +386,6 @@ static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
account_idle_time(cputime);
}

-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 */
-
/*
* Use precise platform statistics if available:
*/
@@ -467,53 +456,10 @@ 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;
-
if (vtime_accounting_enabled_this_cpu())
return;

- if (sched_clock_irqtime) {
- irqtime_account_process_tick(p, user_tick, 1);
- return;
- }
-
- cputime = TICK_NSEC;
- steal = steal_account_process_time(ULONG_MAX);
-
- if (steal >= cputime)
- return;
-
- cputime -= steal;
-
- if (user_tick)
- account_user_time(p, cputime);
- else if ((p != this_rq()->idle) || (irq_count() != HARDIRQ_OFFSET))
- account_system_time(p, HARDIRQ_OFFSET, cputime);
- else
- account_idle_time(cputime);
-}
-
-/*
- * Account multiple ticks of idle time.
- * @ticks: number of stolen ticks
- */
-void account_idle_ticks(unsigned long ticks)
-{
- u64 cputime, steal;
-
- if (sched_clock_irqtime) {
- irqtime_account_idle_ticks(ticks);
- return;
- }
-
- cputime = ticks * TICK_NSEC;
- steal = steal_account_process_time(ULONG_MAX);
-
- if (steal >= cputime)
- return;
-
- cputime -= steal;
- account_idle_time(cputime);
+ irqtime_account_process_tick(p, user_tick, 1);
}

/*
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 8b192e67aabc..92a0979633e9 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -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
}

--
1.8.3.1

2020-01-06 14:56:20

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH 1/3] sched/cputime: move rq parameter in irqtime_account_process_tick


Thanks, I think this looks like.

Frederic, if you have time, a second set of eyes would be appreciated.

2020-01-06 15:54:51

by Frederic Weisbecker

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

On Thu, Jan 02, 2020 at 06:07:53PM +0800, Alex Shi wrote:
> 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.
>
> 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: Anna-Maria Gleixner <[email protected]>
> Cc: Thomas Gleixner <[email protected]>
> Cc: [email protected]
> ---
> kernel/sched/cputime.c | 9 +++------
> 1 file changed, 3 insertions(+), 6 deletions(-)
>
> diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
> index cff3e656566d..46b837e94fce 100644
> --- a/kernel/sched/cputime.c
> +++ b/kernel/sched/cputime.c
> @@ -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.


> 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)
> --
> 1.8.3.1
>

2020-01-07 09:15:47

by Peter Zijlstra

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

On Mon, Jan 06, 2020 at 04:53:51PM +0100, Frederic Weisbecker wrote:
> On Thu, Jan 02, 2020 at 06:07:53PM +0800, Alex Shi wrote:
> > 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.
> >
> > 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: Anna-Maria Gleixner <[email protected]>
> > Cc: Thomas Gleixner <[email protected]>
> > Cc: [email protected]
> > ---
> > kernel/sched/cputime.c | 9 +++------
> > 1 file changed, 3 insertions(+), 6 deletions(-)
> >
> > diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
> > index cff3e656566d..46b837e94fce 100644
> > --- a/kernel/sched/cputime.c
> > +++ b/kernel/sched/cputime.c
> > @@ -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.

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

2020-01-09 13:41:26

by Alex Shi

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


>> 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.

Signed-off-by: Alex Shi <[email protected]>
Cc: Frederic Weisbecker <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: [email protected]
---
kernel/sched/cputime.c | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index cff3e656566d..17640d145e44 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -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;
}

--
1.8.3.1

Subject: [tip: sched/core] sched/cputime: move rq parameter in irqtime_account_process_tick

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

Signed-off-by: Alex Shi <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
---
kernel/sched/cputime.c | 15 ++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index d43318a..cff3e65 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -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);