SYSCTL_COUNTER_U64_ARRAY (9)
Leading comments
- Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org> All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in...
NAME
counter - SMP-friendly kernel counter implementationSYNOPSIS
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 Fn SYSCTL_COUNTER_U64_ARRAY parent nbr name access ptr len descr Fn SYSCTL_ADD_COUNTER_U64_ARRAY ctx parent nbr name access ptr len descrDESCRIPTION
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 paths.- Fn counter_u64_alloc wait
- 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.
- Fn SYSCTL_COUNTER_U64_ARRAY parent nbr name access ptr len descr
- Declare a static sysctl oid that would represent an array of . The Fa ptr argument should be a pointer to allocated array of Vt counter_u64_t's . The Fa len argument should specify number of elements in the array. A read of the oid returns len-sized array of Vt uint64_t values obtained through Fn counter_u64_fetch . Any write to the oid zeroes all array elements.
- Fn SYSCTL_ADD_COUNTER_U64_ARRAY ctx parent nbr name access ptr len descr
- Create a sysctl oid that would represent an array of . The Fa ptr argument should be a pointer to allocated array of Vt counter_u64_t's . The Fa len argument should specify number of elements in the array. A read of the oid returns len-sized array of Vt uint64_t values obtained through Fn counter_u64_fetch . Any write to the oid zeroes all array elements.
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.
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");