/* SPDX-License-Identifier: GPL-2.0 */ #ifndef _LINUX_PERCPU_COUNTER_H #define _LINUX_PERCPU_COUNTER_H /* * A simple "approximate counter" for use in ext2 and ext3 superblocks. * * WARNING: these things are HUGE. 4 kbytes per counter on 32-way P4.
*/
/* percpu_counter batch for local add or sub */ #define PERCPU_COUNTER_LOCAL_BATCH INT_MAX
#ifdef CONFIG_SMP
struct percpu_counter {
raw_spinlock_t lock;
s64 count; #ifdef CONFIG_HOTPLUG_CPU struct list_head list; /* All percpu_counters are on a list */ #endif
s32 __percpu *counters;
};
/* * With percpu_counter_add_local() and percpu_counter_sub_local(), counts * are accumulated in local per cpu counter and not in fbc->count until * local count overflows PERCPU_COUNTER_LOCAL_BATCH. This makes counter * write efficient. * But percpu_counter_sum(), instead of percpu_counter_read(), needs to be * used to add up the counts from each CPU to account for all the local * counts. So percpu_counter_add_local() and percpu_counter_sub_local() * should be used when a counter is updated frequently and read rarely.
*/ staticinlinevoid
percpu_counter_add_local(struct percpu_counter *fbc, s64 amount)
{
percpu_counter_add_batch(fbc, amount, PERCPU_COUNTER_LOCAL_BATCH);
}
staticinline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
{
s64 ret = __percpu_counter_sum(fbc); return ret < 0 ? 0 : ret;
}
/* * It is possible for the percpu_counter_read() to return a small negative * number for some counter which should never be negative. *
*/ staticinline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
{ /* Prevent reloads of fbc->count */
s64 ret = READ_ONCE(fbc->count);
/* non-SMP percpu_counter_add_local is the same with percpu_counter_add */ staticinlinevoid
percpu_counter_add_local(struct percpu_counter *fbc, s64 amount)
{
percpu_counter_add(fbc, amount);
}
/* * percpu_counter is intended to track positive numbers. In the UP case the * number should never be negative.
*/ staticinline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
{ return fbc->count;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.