Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1322244 > unrolled thread

[PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals

Started byriel@redhat.com
First post2016-01-30 04:40 +0100
Last post2016-02-01 12:40 +0100
Articles 5 — 5 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals riel@redhat.com - 2016-01-30 04:40 +0100
    Re: [PATCH 1/4] sched,time: remove non-power-of-two divides from  __acct_update_integrals kbuild test robot <lkp@intel.com> - 2016-01-30 06:00 +0100
    Re: [PATCH 1/4] sched,time: remove non-power-of-two divides from  __acct_update_integrals Frederic Weisbecker <fweisbec@gmail.com> - 2016-01-30 15:50 +0100
      Re: [PATCH 1/4] sched,time: remove non-power-of-two divides from  __acct_update_integrals Rik van Riel <riel@redhat.com> - 2016-01-30 19:00 +0100
        Re: [PATCH 1/4] sched,time: remove non-power-of-two divides from  __acct_update_integrals Peter Zijlstra <peterz@infradead.org> - 2016-02-01 12:40 +0100

#1322244 — [PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals

Fromriel@redhat.com
Date2016-01-30 04:40 +0100
Subject[PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals
Message-ID<qWuyd-51K-13@gated-at.bofh.it>
From: Rik van Riel <riel@redhat.com>

When running a microbenchmark calling an invalid syscall number
in a loop, on a nohz_full CPU, we spend a full 9% of our CPU
time in __acct_update_integrals.

This function converts cputime_t to jiffies, to a timeval, only to
convert the timeval back to microseconds before discarding it.

This patch leaves __acct_update_integrals functionally equivalent,
but speeds things up by about 12%, with 10 million calls to an
invalid syscall number dropping from 3.7 to 3.25 seconds.

Signed-off-by: Rik van Riel <riel@redhat.com>
---
 kernel/tsacct.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/kernel/tsacct.c b/kernel/tsacct.c
index 975cb49e32bf..41667b23dbd0 100644
--- a/kernel/tsacct.c
+++ b/kernel/tsacct.c
@@ -93,9 +93,9 @@ void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
 {
 	struct mm_struct *mm;
 
-	/* convert pages-usec to Mbyte-usec */
-	stats->coremem = p->acct_rss_mem1 * PAGE_SIZE / MB;
-	stats->virtmem = p->acct_vm_mem1 * PAGE_SIZE / MB;
+	/* convert pages-nsec/KB to Mbyte-usec, see __acct_update_integrals */
+	stats->coremem = p->acct_rss_mem1 * PAGE_SIZE / (1000 * KB);
+	stats->virtmem = p->acct_vm_mem1 * PAGE_SIZE / (1000 * KB);
 	mm = get_task_mm(p);
 	if (mm) {
 		/* adjust to KB unit */
@@ -125,22 +125,21 @@ static void __acct_update_integrals(struct task_struct *tsk,
 {
 	if (likely(tsk->mm)) {
 		cputime_t time, dtime;
-		struct timeval value;
 		unsigned long flags;
 		u64 delta;
 
 		local_irq_save(flags);
 		time = stime + utime;
 		dtime = time - tsk->acct_timexpd;
-		jiffies_to_timeval(cputime_to_jiffies(dtime), &value);
-		delta = value.tv_sec;
-		delta = delta * USEC_PER_SEC + value.tv_usec;
+		delta = cputime_to_nsecs(dtime);
 
-		if (delta == 0)
+		if (delta < TICK_NSEC)
 			goto out;
+
 		tsk->acct_timexpd = time;
-		tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm);
-		tsk->acct_vm_mem1 += delta * tsk->mm->total_vm;
+		/* The final unit will be Mbyte-usecs, see xacct_add_tsk */
+		tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm) / 1024;
+		tsk->acct_vm_mem1 += delta * tsk->mm->total_vm / 1024;
 	out:
 		local_irq_restore(flags);
 	}
-- 
2.5.0

[toc] | [next] | [standalone]


#1322259 — Re: [PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals

Fromkbuild test robot <lkp@intel.com>
Date2016-01-30 06:00 +0100
SubjectRe: [PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals
Message-ID<qWvNE-5Qx-3@gated-at.bofh.it>
In reply to#1322244

[Multipart message — attachments visible in raw view] — view raw

Hi Rik,

[auto build test ERROR on tip/sched/core]
[also build test ERROR on v4.5-rc1 next-20160129]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/riel-redhat-com/sched-time-remove-non-power-of-two-divides-from-__acct_update_integrals/20160130-114019
config: i386-defconfig (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

   kernel/built-in.o: In function `xacct_add_tsk':
>> (.text+0x906b0): undefined reference to `__udivdi3'
   kernel/built-in.o: In function `xacct_add_tsk':
   (.text+0x906e3): undefined reference to `__udivdi3'

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[toc] | [prev] | [next] | [standalone]


#1322380 — Re: [PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals

FromFrederic Weisbecker <fweisbec@gmail.com>
Date2016-01-30 15:50 +0100
SubjectRe: [PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals
Message-ID<qWF0C-4FU-11@gated-at.bofh.it>
In reply to#1322244
On Fri, Jan 29, 2016 at 10:36:02PM -0500, riel@redhat.com wrote:
> From: Rik van Riel <riel@redhat.com>
> 
> When running a microbenchmark calling an invalid syscall number
> in a loop, on a nohz_full CPU, we spend a full 9% of our CPU
> time in __acct_update_integrals.
> 
> This function converts cputime_t to jiffies, to a timeval, only to
> convert the timeval back to microseconds before discarding it.
> 
> This patch leaves __acct_update_integrals functionally equivalent,
> but speeds things up by about 12%, with 10 million calls to an
> invalid syscall number dropping from 3.7 to 3.25 seconds.
> 
> Signed-off-by: Rik van Riel <riel@redhat.com>
> ---
>  kernel/tsacct.c | 19 +++++++++----------
>  1 file changed, 9 insertions(+), 10 deletions(-)
> 
> diff --git a/kernel/tsacct.c b/kernel/tsacct.c
> index 975cb49e32bf..41667b23dbd0 100644
> --- a/kernel/tsacct.c
> +++ b/kernel/tsacct.c
> @@ -93,9 +93,9 @@ void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
>  {
>  	struct mm_struct *mm;
>  
> -	/* convert pages-usec to Mbyte-usec */
> -	stats->coremem = p->acct_rss_mem1 * PAGE_SIZE / MB;
> -	stats->virtmem = p->acct_vm_mem1 * PAGE_SIZE / MB;
> +	/* convert pages-nsec/KB to Mbyte-usec, see __acct_update_integrals */
> +	stats->coremem = p->acct_rss_mem1 * PAGE_SIZE / (1000 * KB);
> +	stats->virtmem = p->acct_vm_mem1 * PAGE_SIZE / (1000 * KB);
>  	mm = get_task_mm(p);
>  	if (mm) {
>  		/* adjust to KB unit */
> @@ -125,22 +125,21 @@ static void __acct_update_integrals(struct task_struct *tsk,
>  {
>  	if (likely(tsk->mm)) {
>  		cputime_t time, dtime;
> -		struct timeval value;
>  		unsigned long flags;
>  		u64 delta;
>  
>  		local_irq_save(flags);
>  		time = stime + utime;
>  		dtime = time - tsk->acct_timexpd;
> -		jiffies_to_timeval(cputime_to_jiffies(dtime), &value);
> -		delta = value.tv_sec;
> -		delta = delta * USEC_PER_SEC + value.tv_usec;
> +		delta = cputime_to_nsecs(dtime);

You might want to add a comment specifying why we don't call cputime_to_usecs()
directly (because we optimize if delta < TICK_NSEC).

Although this has a good impact on nohz_full, it might have a tiny bad one on !nohz_full
because now we first convert jiffies to nsecs (which implies a multiplication by 1000)
that we later divide again by 1000. Now this is ok because I plan to convert tsk->utime/stime
to nsecs and thus remove most of the cputime_t use and conversions everywhere.

>  
> -		if (delta == 0)
> +		if (delta < TICK_NSEC)
>  			goto out;


> +
>  		tsk->acct_timexpd = time;
> -		tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm);
> -		tsk->acct_vm_mem1 += delta * tsk->mm->total_vm;
> +		/* The final unit will be Mbyte-usecs, see xacct_add_tsk */
> +		tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm) / 1024;
> +		tsk->acct_vm_mem1 += delta * tsk->mm->total_vm / 1024;

The use of 1024 and the change on MB above are confusing me. Why are we doing that?

Thanks.

>  	out:
>  		local_irq_restore(flags);
>  	}
> -- 
> 2.5.0
> 

[toc] | [prev] | [next] | [standalone]


#1322475 — Re: [PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals

FromRik van Riel <riel@redhat.com>
Date2016-01-30 19:00 +0100
SubjectRe: [PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals
Message-ID<qWHYv-7qu-25@gated-at.bofh.it>
In reply to#1322380
On 01/30/2016 09:44 AM, Frederic Weisbecker wrote:
> On Fri, Jan 29, 2016 at 10:36:02PM -0500, riel@redhat.com wrote:
>> From: Rik van Riel <riel@redhat.com>
>>
>> When running a microbenchmark calling an invalid syscall number
>> in a loop, on a nohz_full CPU, we spend a full 9% of our CPU
>> time in __acct_update_integrals.
>>
>> This function converts cputime_t to jiffies, to a timeval, only to
>> convert the timeval back to microseconds before discarding it.
>>
>> This patch leaves __acct_update_integrals functionally equivalent,
>> but speeds things up by about 12%, with 10 million calls to an
>> invalid syscall number dropping from 3.7 to 3.25 seconds.
>>
>> Signed-off-by: Rik van Riel <riel@redhat.com>
>> ---
>>  kernel/tsacct.c | 19 +++++++++----------
>>  1 file changed, 9 insertions(+), 10 deletions(-)
>>
>> diff --git a/kernel/tsacct.c b/kernel/tsacct.c
>> index 975cb49e32bf..41667b23dbd0 100644
>> --- a/kernel/tsacct.c
>> +++ b/kernel/tsacct.c
>> @@ -93,9 +93,9 @@ void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
>>  {
>>  	struct mm_struct *mm;
>>  
>> -	/* convert pages-usec to Mbyte-usec */
>> -	stats->coremem = p->acct_rss_mem1 * PAGE_SIZE / MB;
>> -	stats->virtmem = p->acct_vm_mem1 * PAGE_SIZE / MB;
>> +	/* convert pages-nsec/KB to Mbyte-usec, see __acct_update_integrals */
>> +	stats->coremem = p->acct_rss_mem1 * PAGE_SIZE / (1000 * KB);
>> +	stats->virtmem = p->acct_vm_mem1 * PAGE_SIZE / (1000 * KB);
>>  	mm = get_task_mm(p);
>>  	if (mm) {
>>  		/* adjust to KB unit */
>> @@ -125,22 +125,21 @@ static void __acct_update_integrals(struct task_struct *tsk,
>>  {
>>  	if (likely(tsk->mm)) {
>>  		cputime_t time, dtime;
>> -		struct timeval value;
>>  		unsigned long flags;
>>  		u64 delta;
>>  
>>  		local_irq_save(flags);
>>  		time = stime + utime;
>>  		dtime = time - tsk->acct_timexpd;
>> -		jiffies_to_timeval(cputime_to_jiffies(dtime), &value);
>> -		delta = value.tv_sec;
>> -		delta = delta * USEC_PER_SEC + value.tv_usec;
>> +		delta = cputime_to_nsecs(dtime);
> 
> You might want to add a comment specifying why we don't call cputime_to_usecs()
> directly (because we optimize if delta < TICK_NSEC).
> 
> Although this has a good impact on nohz_full, it might have a tiny bad one on !nohz_full
> because now we first convert jiffies to nsecs (which implies a multiplication by 1000)
> that we later divide again by 1000. Now this is ok because I plan to convert tsk->utime/stime
> to nsecs and thus remove most of the cputime_t use and conversions everywhere.

Isn't cputime_t in nanoseconds even on !nohz_full systems nowadays,
due to sched_clock?

Also, a multiplication is essentially instantaneous compared to
a division, which is why Peter suggested going this way around.

>>  
>> -		if (delta == 0)
>> +		if (delta < TICK_NSEC)
>>  			goto out;
> 
> 
>> +
>>  		tsk->acct_timexpd = time;
>> -		tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm);
>> -		tsk->acct_vm_mem1 += delta * tsk->mm->total_vm;
>> +		/* The final unit will be Mbyte-usecs, see xacct_add_tsk */
>> +		tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm) / 1024;
>> +		tsk->acct_vm_mem1 += delta * tsk->mm->total_vm / 1024;
> 
> The use of 1024 and the change on MB above are confusing me. Why are we doing that?
> 
> Thanks.

So the compiler can just do a right shift in the frequently called
code, and have no divide at all left in __acct_update_integrals.
However, reducing the value here does seem useful for the prevention
of overflows.

The divide is saved for when the statistics are read out to
userspace.

-- 
All rights reversed

[toc] | [prev] | [next] | [standalone]


#1323012 — Re: [PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals

FromPeter Zijlstra <peterz@infradead.org>
Date2016-02-01 12:40 +0100
SubjectRe: [PATCH 1/4] sched,time: remove non-power-of-two divides from __acct_update_integrals
Message-ID<qXkZP-1UH-9@gated-at.bofh.it>
In reply to#1322475
On Sat, Jan 30, 2016 at 12:53:09PM -0500, Rik van Riel wrote:
> >> +		delta = cputime_to_nsecs(dtime);
> > 
> > You might want to add a comment specifying why we don't call cputime_to_usecs()
> > directly (because we optimize if delta < TICK_NSEC).
> > 
> > Although this has a good impact on nohz_full, it might have a tiny bad one on !nohz_full
> > because now we first convert jiffies to nsecs (which implies a multiplication by 1000)
> > that we later divide again by 1000. Now this is ok because I plan to convert tsk->utime/stime
> > to nsecs and thus remove most of the cputime_t use and conversions everywhere.
> 
> Isn't cputime_t in nanoseconds even on !nohz_full systems nowadays,
> due to sched_clock?

Don't think so, we still use jiffy accounting for !NOHZ_FULL.

> Also, a multiplication is essentially instantaneous compared to
> a division, which is why Peter suggested going this way around.

Yep.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web