mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Tim Chen <tim.c.chen@linux.intel.com>
Cc: linux-kernel@vger.kernel.org, Andi Kleen <ak@linux.intel.com>,
	Hugh Dickins <hughd@google.com>,
	yanmin.zhang@intel.com
Subject: Re: [PATCH v3 1/2] tmpfs: Add accurate compare function to percpu_counter library
Date: Mon, 21 Jun 2010 13:14:38 -0700	[thread overview]
Message-ID: <20100621131438.a17b818d.akpm@linux-foundation.org> (raw)
In-Reply-To: <1276818992.9661.81.camel@schen9-DESK>

On Thu, 17 Jun 2010 16:56:32 -0700
Tim Chen <tim.c.chen@linux.intel.com> wrote:

> Add percpu_counter_compare that allows for a quick but accurate
> comparison of percpu_counter with a given value.  
> 
> A rough count is provided by the count field in percpu_counter structure, 
> without accounting for the other values stored in individual cpu counters.
> The actual count is a sum of count and the cpu counters.  However, count field is
> never different from the actual value by a factor of batch*num_online_cpu.
> We do not need to get actual count for comparison if count
> is different from the given value by this factor and allows for
> quick comparison without summing up all the per cpu counters.
> 
> Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com>
>  include/linux/percpu_counter.h |   11 +++++++++++
>  lib/percpu_counter.c           |   27 +++++++++++++++++++++++++++
>  2 files changed, 38 insertions(+), 0 deletions(-)
> 
> diff --git a/include/linux/percpu_counter.h b/include/linux/percpu_counter.h
> index c88d67b..8a7d510 100644
> --- a/include/linux/percpu_counter.h
> +++ b/include/linux/percpu_counter.h
> @@ -40,6 +40,7 @@ void percpu_counter_destroy(struct percpu_counter *fbc);
>  void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
>  void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch);
>  s64 __percpu_counter_sum(struct percpu_counter *fbc);
> +int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs);
>  
>  static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
>  {
> @@ -98,6 +99,16 @@ static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
>  	fbc->count = amount;
>  }
>  
> +static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
> +{
> +	if (fbc->count > rhs)
> +		return 1;
> +	else if (fbc->count < rhs)
> +		return -1;
> +	else
> +		return 0;
> +}

It'd be nice if this interface were defined as returning a number
less-than, greater-than or equal to zero.  Like the qsort() callback. 
It's a pretty common idiom.  That way, the above code becomes just

	return fbc->count - rhs;

However that does require that percpu_counter_compare() return an s64,
which might make the code generated at callers a little less efficient.

I guess it doesn't matter much.

>  static inline void
>  percpu_counter_add(struct percpu_counter *fbc, s64 amount)
>  {
> diff --git a/lib/percpu_counter.c b/lib/percpu_counter.c
> index aeaa6d7..ec9048e 100644
> --- a/lib/percpu_counter.c
> +++ b/lib/percpu_counter.c
> @@ -137,6 +137,33 @@ static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
>  	return NOTIFY_OK;
>  }
>  
> +/*
> + * Compare counter against given value.
> + * Return 1 if greater, 0 if equal and -1 if less
> + */
> +int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
> +{
> +	s64	count;
> +
> +	count = percpu_counter_read(fbc);
> +	/* Check to see if rough count will be sufficient for comparison */
> +	if (abs(count - rhs) > (percpu_counter_batch*num_online_cpus())) {
> +		if (count > rhs)
> +			return 1;
> +		else
> +			return -1;
> +	}
> +	/* Need to use precise count */
> +	count = percpu_counter_sum(fbc);
> +	if (count > rhs)
> +		return 1;
> +	else if (count < rhs)
> +		return -1;
> +	else
> +		return 0;
> +}
> +EXPORT_SYMBOL(percpu_counter_compare);

Looks OK.  For API uniformity we should have a

__percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, int batch)

but that can be added later if needed I guess.


      reply	other threads:[~2010-06-21 20:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-17 23:56 Tim Chen
2010-06-21 20:14 ` Andrew Morton [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100621131438.a17b818d.akpm@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=ak@linux.intel.com \
    --cc=hughd@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tim.c.chen@linux.intel.com \
    --cc=yanmin.zhang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome