Skip to content

Commit

Permalink
percpu: Add percpu_counter_add_local and percpu_counter_sub_local
Browse files Browse the repository at this point in the history
The batch size in percpu_counter_add_batch should be very large
in heavy writing and rare reading case. Add the "_local" version,
and mostly it will do local adding, reduce the global updating
and mitigate lock contention in writing.

Signed-off-by: Jiebin Sun <jiebin.sun@intel.com>
  • Loading branch information
jiebinn authored and intel-lab-lkp committed Sep 9, 2022
1 parent 509590d commit 44e7288
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions include/linux/percpu_counter.h
Expand Up @@ -15,6 +15,9 @@
#include <linux/types.h>
#include <linux/gfp.h>

/* percpu_counter batch for local add or sub */
#define PERCPU_COUNTER_LOCAL_BATCH INT_MAX

#ifdef CONFIG_SMP

struct percpu_counter {
Expand Down Expand Up @@ -56,6 +59,27 @@ static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
percpu_counter_add_batch(fbc, amount, percpu_counter_batch);
}

/*
* Use this function in heavy writing but rare reading case. The large
* batch size will reduce the global updating.
*/
static inline void
percpu_counter_add_local(struct percpu_counter *fbc, s64 amount)
{
percpu_counter_add_batch(fbc, amount, PERCPU_COUNTER_LOCAL_BATCH);
}

/*
* Similar with percpu_counter_add_local, use it in heavy writing but
* rare reading case. The large batch size will reduce the global
* updating.
*/
static inline void
percpu_counter_sub_local(struct percpu_counter *fbc, s64 amount)
{
percpu_counter_add_batch(fbc, -amount, PERCPU_COUNTER_LOCAL_BATCH);
}

static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
{
s64 ret = __percpu_counter_sum(fbc);
Expand Down Expand Up @@ -138,6 +162,20 @@ percpu_counter_add(struct percpu_counter *fbc, s64 amount)
preempt_enable();
}

/* no smp percpu_counter_add_local is the same with percpu_counter_add */
static inline void
percpu_counter_add_local(struct percpu_counter *fbc, s64 amount)
{
percpu_counter_add(fbc, amount);
}

/* no smp percpu_counter_sub_local is the same with percpu_counter_sub */
static inline void
percpu_counter_sub_local(struct percpu_counter *fbc, s64 amount)
{
percpu_counter_sub(fbc, amount);
}

static inline void
percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch)
{
Expand Down

0 comments on commit 44e7288

Please sign in to comment.