counter(9) SMP-friendly kernel counter implementation

SYNOPSIS

In sys/types.h In sys/systm.h In sys/counter.h Ft counter_u64_t Fn counter_u64_alloc int wait Ft void Fn counter_u64_free counter_u64_t c Ft void Fn counter_u64_add counter_u64_t c int64_t v Ft void Fn counter_enter Ft void Fn counter_exit Ft void Fn counter_u64_add_protected counter_u64_t c int64_t v Ft uint64_t Fn counter_u64_fetch counter_u64_t c Ft void Fn counter_u64_zero counter_u64_t c In sys/sysctl.h Fn SYSCTL_COUNTER_U64 parent nbr name access ptr descr Fn SYSCTL_ADD_COUNTER_U64 ctx parent nbr name access ptr descr

DESCRIPTION

is a generic facility to create counters that can be utilized for any purpose (such as collecting statistical data). A is guaranteed to be lossless when several kernel threads do simultaneous updates. However, does not block the calling thread, also no atomic(9) operations are used for the update, therefore the counters can be used in any non-interrupt context. Moreover, has special optimisations for SMP environments, making update faster than simple arithmetic on the global variable. Thus is considered suitable for accounting in the performance-critical code pathes.

Fn counter_u64_alloc how
Allocate a new 64-bit unsigned counter. The Fa wait argument is the malloc(9) wait flag, should be either M_NOWAIT or M_WAITOK If M_NOWAIT is specified the operation may fail.
Fn counter_u64_free c
Free the previously allocated counter Fa c .
Fn counter_u64_add c v
Add Fa v to Fa c . The KPI does not guarantee any protection from wraparound.
Fn counter_enter
Enter mode that would allow to safely update several counters via Fn counter_u64_add_protected . On some machines this expands to critical(9) section, while on other is a nop. See Sx IMPLEMENTATION DETAILS .
Fn counter_exit
Exit mode for updating several counters.
Fn counter_u64_add_protected c v
Same as Fn counter_u64_add , but should be preceded by Fn counter_enter .
Fn counter_u64_fetch c
Take a snapshot of counter Fa c . The data obtained is not guaranteed to reflect the real cumulative value for any moment.
Fn counter_u64_zero c
Clear the counter Fa c and set it to zero.
Fn SYSCTL_COUNTER_U64 parent nbr name access ptr descr
Declare a static sysctl oid that would represent a . The Fa ptr argument should be a pointer to allocated Vt counter_u64_t . A read of the oid returns value obtained through Fn counter_u64_fetch . Any write to the oid zeroes it.
Fn SYSCTL_ADD_COUNTER_U64 ctx parent nbr name access ptr descr
Create a sysctl oid that would represent a . The Fa ptr argument should be a pointer to allocated Vt counter_u64_t . A read of the oid returns value obtained through Fn counter_u64_fetch . Any write to the oid zeroes it.

IMPLEMENTATION DETAILS

On all architectures is implemented using per-CPU data fields that are specially aligned in memory, to avoid inter-CPU bus traffic due to shared use of the variables between CPUs. These are allocated using UMA_ZONE_PCPU uma(9) zone. The update operation only touches the field that is private to current CPU. Fetch operation loops through all per-CPU fields and obtains a snapshot sum of all fields.

On amd64 a counter update is implemented as a single instruction without lock semantics, operating on the private data for the current CPU, which is safe against preemption and interrupts.

On i386 architecture, when machine supports the cmpxchg8 instruction, this instruction is used. The multi-instruction sequence provides the same guarantees as the amd64 single-instruction implementation.

On some architectures updating a counter require a critical(9) section.

HISTORY

The facility first appeared in Fx 10.0 .

AUTHORS

An -nosplit The facility was written by An Gleb Smirnoff and An Konstantin Belousov .