COUNTER(9)
π COUNTER(9) β the kernel developer's manual page, but make it cute
okay bestie, so this is COUNTER(9) from the Kernel Developer's Manual, and it's literally the blueprint for SMP-friendly kernel counter implementation. sit down, hydrate, let's get into it π
πΈ NAME πΈ
counter β SMP-friendly kernel counter implementation
β¨ SYNOPSIS β¨
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/counter.h>
counter_u64_t
counter_u64_alloc(int wait);
void
counter_u64_free(counter_u64_t c);
void
counter_u64_add(counter_u64_t c, int64_t v);
void
counter_enter();
void
counter_exit();
void
counter_u64_add_protected(counter_u64_t c, int64_t v);
uint64_t
counter_u64_fetch(counter_u64_t c);
void
counter_u64_zero(counter_u64_t c);
int64_t
counter_ratecheck(struct counter_rate *cr, int64_t limit);
COUNTER_U64_SYSINIT(counter_u64_t c);
COUNTER_U64_DEFINE_EARLY(counter_u64_t c);
#include <sys/sysctl.h>
SYSCTL_COUNTER_U64(parent, nbr, name, access, ptr, descr);
SYSCTL_ADD_COUNTER_U64(ctx, parent, nbr, name, access, ptr, descr);
SYSCTL_COUNTER_U64_ARRAY(parent, nbr, name, access, ptr, len, descr);
SYSCTL_ADD_COUNTER_U64_ARRAY(ctx, parent, nbr, name, access, ptr, len,
descr);
π DESCRIPTION π
so counter is a generic facility to create counters that can be utilized for literally any purpose (collecting statistical data? iconic. yes). here's the tea: a counter is guaranteed to be lossless when several kernel threads do simultaneous updates. but also β and this is the part that ate β counter does not block the calling thread, and no atomic(9) operations are used for the update, which means the counters can be used in any non-interrupt context. periodt.
moreover, counter has special optimisations for SMP environments, making counter update faster than simple arithmetic on the global variable. so it's considered suitable for accounting in the performance-critical code paths. no cap. π₯
the functions, one by one, bestie π«Ά
counter_u64_alloc(wait)β Allocate a new 64-bit unsigned counter. The wait argument is themalloc(9)wait flag, and it should be eitherM_NOWAITorM_WAITOK. if you specifyM_NOWAITthe operation may fail. heads up βcounter_u64_free(c)β Free the previously allocated counter c.counter_u64_add(c, v)β Add v to c. the KPI does not guarantee any protection from wraparound. that's just how it is, bestie πcounter_enter()β Enter mode that would allow the safe update of several counters viacounter_u64_add_protected(). on some machines this expands to acritical(9)section, while on other is a nop. see βIMPLEMENTATION DETAILSβ.counter_exit()β Exit mode for updating several counters.counter_u64_add_protected(c, v)β Same ascounter_u64_add(), but should be preceded bycounter_enter().counter_u64_fetch(c)β Take a snapshot of counter c. The data obtained is not guaranteed to reflect the real cumulative value for any moment. it's giving "vibes only" πcounter_u64_zero(c)β Clear the counter c and set it to zero.counter_ratecheck(cr, limit)β This function is a multiprocessor-friendly version ofppsratecheck()which uses counter internally. Returns a non-negative value if the rate is not yet reached during the current second, and a negative value otherwise. If the limit was reached on previous second, but was just reset back to zero, thencounter_ratecheck()returns number of events since previous reset. math queen behavior β¨COUNTER_U64_SYSINIT(c)β Define aSYSINIT(9)initializer for the global counter c.COUNTER_U64_DEFINE_EARLY(c)β Define and initialize a global counter c. It is always safe to increment c, though updates prior to theSI_SUB_COUNTERSYSINIT(9)event are lost. π«SYSCTL_COUNTER_U64(parent, nbr, name, access, ptr, descr)β Declare a staticsysctl(9)oid that would represent a counter. The ptr argument should be a pointer to allocatedcounter_u64_t. A read of the oid returns value obtained throughcounter_u64_fetch(). Any write to the oid zeroes it.SYSCTL_ADD_COUNTER_U64(ctx, parent, nbr, name, access, ptr, descr)β Create asysctl(9)oid that would represent a counter. The ptr argument should be a pointer to allocatedcounter_u64_t. A read of the oid returns value obtained throughcounter_u64_fetch(). Any write to the oid zeroes it.SYSCTL_COUNTER_U64_ARRAY(parent, nbr, name, access, ptr, len, descr)β Declare a staticsysctl(9)oid that would represent an array of counter. The ptr argument should be a pointer to allocated array ofcounter_u64_t's. The len argument should specify number of elements in the array. A read of the oid returns len-sized array ofuint64_tvalues obtained throughcounter_u64_fetch(). Any write to the oid zeroes all array elements.SYSCTL_ADD_COUNTER_U64_ARRAY(ctx, parent, nbr, name, access, ptr, len, descr)β Create asysctl(9)oid that would represent an array of counter. The ptr argument should be a pointer to allocated array ofcounter_u64_t's. The len argument should specify number of elements in the array. A read of the oid returns len-sized array ofuint64_tvalues obtained throughcounter_u64_fetch(). Any write to the oid zeroes all array elements.
π§ IMPLEMENTATION DETAILS π§
okay this is the behind-the-scenes lore, and it's lowkey fascinating, stay with me π¦
On all architectures counter 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. slay π
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.
π EXAMPLES π
The following example creates a static counter array exported to userspace through a sysctl:
#define MY_SIZE 8
static counter_u64_t array[MY_SIZE];
SYSCTL_COUNTER_U64_ARRAY(_debug, OID_AUTO, counter_array, CTLFLAG_RW,
&array[0], MY_SIZE, "Test counter array");
π SEE ALSO π
π HISTORY π
The counter facility first appeared in FreeBSD 10.0.
π AUTHORS π
The counter facility was written by Gleb Smirnoff and Konstantin Belousov. legends, honestly π«Ά
and that's the whole page, bestie β this one came from the Debian build of the manpage, dated March 6, 2020. go forth and count things safely, ily πβ¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms