Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


As long as the counter changes only moderately, the mean value received from read operations in this
scheme is quite close to the proper value of the counter.

The kernel implements per-CPU counters with the help of the following data structure:

<percpu_counter.h>
struct percpu_counter {
spinlock_t lock;
long count;
long *counters;
};

countis the proper value of the counter, andlockis a spinlock to protect the counter when the exact
value is required. The CPU-specific array buffering counter manipulations is given bycounters.

The threshold value that triggers the modificationof the proper counter depends on the number of CPUs
found in the system:

<percpu_counter.h>
#if NR_CPUS >= 16
#define FBC_BATCH (NR_CPUS*2)
#else
#define FBC_BATCH (NR_CPUS*4)
#endif

The following functions are available to modify approximate per-CPU counters:

<percpu_counter.h>
static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
static inline void percpu_counter_dec(struct percpu_counter *fbc)
static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
static inline void percpu_counter_inc(struct percpu_counter *fbc)
static inline void percpu_counter_dev(struct percpu_counter *fbc)

❑ percpu_counter_addandpercpu_counter_decmodify the counter by a given increment or
decrement. The change is propagated to the proper counter if the accumulated changes surpass
the threshold as given byFBC_BATCH.
❑ percpu_counter_readreads the current value of the counter without considering changes made
by the individual CPUs.
❑ percpu_counter_incandpercpu_counter_incare shortcuts to, respectively, increment and
decrement an approximate counter by 1.
❑ percpu_counter_setsets the counter to a specific value.
❑ percpu_counter_sumcomputes the exact value.

5.2.10 Lock Contention and Fine-Grained Locking


After having discussed the numerous locking primitives provided by the kernel, let us briefly address
some of the problems related to locking and kernel scalability. While multiprocessor systems were nearly
completely unknown to the average user only a decade ago, they are present on nearly every desktop
Free download pdf