1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
<table class="head">
<tr>
<td class="head-ltitle">COUNTER(9)</td>
<td class="head-vol">Kernel Developer's Manual</td>
<td class="head-rtitle">COUNTER(9)</td>
</tr>
</table>
<div class="manual-text">
<section class="Sh">
<h1 class="Sh" id="NAME"><a class="permalink" href="#NAME">NAME</a></h1>
<p class="Pp"><code class="Nm">counter</code> —
<span class="Nd">SMP-friendly kernel counter implementation</span></p>
</section>
<section class="Sh">
<h1 class="Sh" id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">SYNOPSIS</a></h1>
<p class="Pp"><code class="In">#include
<<a class="In">sys/types.h</a>></code>
<br/>
<code class="In">#include <<a class="In">sys/systm.h</a>></code>
<br/>
<code class="In">#include <<a class="In">sys/counter.h</a>></code></p>
<p class="Pp"><var class="Ft">counter_u64_t</var>
<br/>
<code class="Fn">counter_u64_alloc</code>(<var class="Fa" style="white-space: nowrap;">int
wait</var>);</p>
<p class="Pp"><var class="Ft">void</var>
<br/>
<code class="Fn">counter_u64_free</code>(<var class="Fa" style="white-space: nowrap;">counter_u64_t
c</var>);</p>
<p class="Pp"><var class="Ft">void</var>
<br/>
<code class="Fn">counter_u64_add</code>(<var class="Fa" style="white-space: nowrap;">counter_u64_t
c</var>, <var class="Fa" style="white-space: nowrap;">int64_t v</var>);</p>
<p class="Pp"><var class="Ft">void</var>
<br/>
<code class="Fn">counter_enter</code>();</p>
<p class="Pp"><var class="Ft">void</var>
<br/>
<code class="Fn">counter_exit</code>();</p>
<p class="Pp"><var class="Ft">void</var>
<br/>
<code class="Fn">counter_u64_add_protected</code>(<var class="Fa" style="white-space: nowrap;">counter_u64_t
c</var>, <var class="Fa" style="white-space: nowrap;">int64_t v</var>);</p>
<p class="Pp"><var class="Ft">uint64_t</var>
<br/>
<code class="Fn">counter_u64_fetch</code>(<var class="Fa" style="white-space: nowrap;">counter_u64_t
c</var>);</p>
<p class="Pp"><var class="Ft">void</var>
<br/>
<code class="Fn">counter_u64_zero</code>(<var class="Fa" style="white-space: nowrap;">counter_u64_t
c</var>);</p>
<p class="Pp"><var class="Ft">struct counter_rate *</var>
<br/>
<code class="Fn">counter_rate_alloc</code>(<var class="Fa" style="white-space: nowrap;">int
flags</var>, <var class="Fa" style="white-space: nowrap;">int
period</var>);</p>
<p class="Pp"><var class="Ft">int64_t</var>
<br/>
<code class="Fn">counter_ratecheck</code>(<var class="Fa" style="white-space: nowrap;">struct
counter_rate *cr</var>, <var class="Fa" style="white-space: nowrap;">int64_t
limit</var>);</p>
<p class="Pp"><var class="Ft">uint64_t</var>
<br/>
<code class="Fn">counter_rate_get</code>(<var class="Fa" style="white-space: nowrap;">struct
counter_rate *cr</var>);</p>
<p class="Pp"><var class="Ft">void</var>
<br/>
<code class="Fn">counter_rate_free</code>(<var class="Fa" style="white-space: nowrap;">struct
counter_rate *cr</var>);</p>
<p class="Pp"><code class="Fn">COUNTER_U64_SYSINIT</code>(<var class="Fa" style="white-space: nowrap;">counter_u64_t
c</var>);</p>
<p class="Pp"><code class="Fn">COUNTER_U64_DEFINE_EARLY</code>(<var class="Fa" style="white-space: nowrap;">counter_u64_t
c</var>);</p>
<p class="Pp"><code class="In">#include
<<a class="In">sys/sysctl.h</a>></code></p>
<p class="Pp"><code class="Fn">SYSCTL_COUNTER_U64</code>(<var class="Fa" style="white-space: nowrap;">parent</var>,
<var class="Fa" style="white-space: nowrap;">nbr</var>,
<var class="Fa" style="white-space: nowrap;">name</var>,
<var class="Fa" style="white-space: nowrap;">access</var>,
<var class="Fa" style="white-space: nowrap;">ptr</var>,
<var class="Fa" style="white-space: nowrap;">descr</var>);</p>
<p class="Pp"><code class="Fn">SYSCTL_ADD_COUNTER_U64</code>(<var class="Fa" style="white-space: nowrap;">ctx</var>,
<var class="Fa" style="white-space: nowrap;">parent</var>,
<var class="Fa" style="white-space: nowrap;">nbr</var>,
<var class="Fa" style="white-space: nowrap;">name</var>,
<var class="Fa" style="white-space: nowrap;">access</var>,
<var class="Fa" style="white-space: nowrap;">ptr</var>,
<var class="Fa" style="white-space: nowrap;">descr</var>);</p>
<p class="Pp"><code class="Fn">SYSCTL_COUNTER_U64_ARRAY</code>(<var class="Fa" style="white-space: nowrap;">parent</var>,
<var class="Fa" style="white-space: nowrap;">nbr</var>,
<var class="Fa" style="white-space: nowrap;">name</var>,
<var class="Fa" style="white-space: nowrap;">access</var>,
<var class="Fa" style="white-space: nowrap;">ptr</var>,
<var class="Fa" style="white-space: nowrap;">len</var>,
<var class="Fa" style="white-space: nowrap;">descr</var>);</p>
<p class="Pp"><code class="Fn">SYSCTL_ADD_COUNTER_U64_ARRAY</code>(<var class="Fa" style="white-space: nowrap;">ctx</var>,
<var class="Fa" style="white-space: nowrap;">parent</var>,
<var class="Fa" style="white-space: nowrap;">nbr</var>,
<var class="Fa" style="white-space: nowrap;">name</var>,
<var class="Fa" style="white-space: nowrap;">access</var>,
<var class="Fa" style="white-space: nowrap;">ptr</var>,
<var class="Fa" style="white-space: nowrap;">len</var>,
<var class="Fa" style="white-space: nowrap;">descr</var>);</p>
</section>
<section class="Sh">
<h1 class="Sh" id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">DESCRIPTION</a></h1>
<p class="Pp"><code class="Nm">counter</code> is a generic facility to create
counters that can be utilized for any purpose (such as collecting
statistical data). A <code class="Nm">counter</code> is guaranteed to be
lossless when several kernel threads do simultaneous updates. However,
<code class="Nm">counter</code> does not block the calling thread, also no
<a class="Xr">atomic(9)</a> operations are used for the update, therefore
the counters can be used in any non-interrupt context. Moreover,
<code class="Nm">counter</code> has special optimisations for SMP
environments, making <code class="Nm">counter</code> update faster than
simple arithmetic on the global variable. Thus
<code class="Nm">counter</code> is considered suitable for accounting in the
performance-critical code paths.</p>
<dl class="Bl-tag">
<dt id="counter_u64_alloc"><a class="permalink" href="#counter_u64_alloc"><code class="Fn">counter_u64_alloc</code></a>(<var class="Fa">wait</var>)</dt>
<dd>Allocate a new 64-bit unsigned counter. The <var class="Fa">wait</var>
argument is the <a class="Xr">malloc(9)</a> wait flag, should be either
<var class="Va">M_NOWAIT</var> or <var class="Va">M_WAITOK</var>. If
<var class="Va">M_NOWAIT</var> is specified the operation may fail and
return <code class="Dv">NULL</code>.</dd>
<dt id="counter_u64_free"><a class="permalink" href="#counter_u64_free"><code class="Fn">counter_u64_free</code></a>(<var class="Fa">c</var>)</dt>
<dd>Free the previously allocated counter <var class="Fa">c</var>. It is safe
to pass <code class="Dv">NULL</code>.</dd>
<dt id="counter_u64_add"><a class="permalink" href="#counter_u64_add"><code class="Fn">counter_u64_add</code></a>(<var class="Fa">c</var>,
<var class="Fa">v</var>)</dt>
<dd>Add <var class="Fa">v</var> to <var class="Fa">c</var>. The KPI does not
guarantee any protection from wraparound.</dd>
<dt id="counter_enter"><a class="permalink" href="#counter_enter"><code class="Fn">counter_enter</code></a>()</dt>
<dd>Enter mode that would allow the safe update of several counters via
<a class="permalink" href="#counter_u64_add_protected"><code class="Fn" id="counter_u64_add_protected">counter_u64_add_protected</code></a>().
On some machines this expands to <a class="Xr">critical(9)</a> section,
while on other is a nop. See
<a class="Sx" href="#IMPLEMENTATION_DETAILS">IMPLEMENTATION
DETAILS</a>.</dd>
<dt id="counter_exit"><a class="permalink" href="#counter_exit"><code class="Fn">counter_exit</code></a>()</dt>
<dd>Exit mode for updating several counters.</dd>
<dt><code class="Fn">counter_u64_add_protected</code>(<var class="Fa">c</var>,
<var class="Fa">v</var>)</dt>
<dd>Same as <code class="Fn">counter_u64_add</code>(), but should be preceded
by <code class="Fn">counter_enter</code>().</dd>
<dt id="counter_u64_fetch"><a class="permalink" href="#counter_u64_fetch"><code class="Fn">counter_u64_fetch</code></a>(<var class="Fa">c</var>)</dt>
<dd>Take a snapshot of counter <var class="Fa">c</var>. The data obtained is
not guaranteed to reflect the real cumulative value for any moment.</dd>
<dt id="counter_u64_zero"><a class="permalink" href="#counter_u64_zero"><code class="Fn">counter_u64_zero</code></a>(<var class="Fa">c</var>)</dt>
<dd>Clear the counter <var class="Fa">c</var> and set it to zero.</dd>
<dt id="counter_rate_alloc"><a class="permalink" href="#counter_rate_alloc"><code class="Fn">counter_rate_alloc</code></a>(<var class="Fa">flags</var>,
<var class="Fa">period</var>)</dt>
<dd>Allocate a new struct counter_rate. <var class="Fa">flags</var> is passed
to <a class="Xr">malloc(9)</a>. <var class="Fa">period</var> is the time
over which the rate is checked.</dd>
<dt id="counter_ratecheck"><a class="permalink" href="#counter_ratecheck"><code class="Fn">counter_ratecheck</code></a>(<var class="Fa">cr</var>,
<var class="Fa">limit</var>)</dt>
<dd>The function is a multiprocessor-friendly version of
<a class="permalink" href="#ppsratecheck"><code class="Fn" id="ppsratecheck">ppsratecheck</code></a>()
which uses <code class="Nm">counter</code> internally. Returns
non-negative value if the rate is not yet reached during the current
period, and a negative value otherwise. If the limit was reached during
the previous period, but was just reset back to zero, then
<code class="Fn">counter_ratecheck</code>() returns number of events since
previous reset.</dd>
<dt id="counter_rate_get"><a class="permalink" href="#counter_rate_get"><code class="Fn">counter_rate_get</code></a>(<var class="Fa">cr</var>)</dt>
<dd>The number of hits to this check within the current period.</dd>
<dt id="counter_rate_free"><a class="permalink" href="#counter_rate_free"><code class="Fn">counter_rate_free</code></a>(<var class="Fa">cr</var>)</dt>
<dd>Free the <var class="Fa">cr</var> counter.</dd>
<dt id="COUNTER_U64_SYSINIT"><a class="permalink" href="#COUNTER_U64_SYSINIT"><code class="Fn">COUNTER_U64_SYSINIT</code></a>(<var class="Fa">c</var>)</dt>
<dd>Define a <a class="Xr">SYSINIT(9)</a> initializer for the global counter
<var class="Fa">c</var>.</dd>
<dt id="COUNTER_U64_DEFINE_EARLY"><a class="permalink" href="#COUNTER_U64_DEFINE_EARLY"><code class="Fn">COUNTER_U64_DEFINE_EARLY</code></a>(<var class="Fa">c</var>)</dt>
<dd>Define and initialize a global counter <var class="Fa">c</var>. It is
always safe to increment <var class="Fa">c</var>, though updates prior to
the <code class="Dv">SI_SUB_COUNTER</code> <a class="Xr">SYSINIT(9)</a>
event are lost.</dd>
<dt id="SYSCTL_COUNTER_U64"><a class="permalink" href="#SYSCTL_COUNTER_U64"><code class="Fn">SYSCTL_COUNTER_U64</code></a>(<var class="Fa">parent</var>,
<var class="Fa">nbr</var>, <var class="Fa">name</var>,
<var class="Fa">access</var>, <var class="Fa">ptr</var>,
<var class="Fa">descr</var>)</dt>
<dd>Declare a static <a class="Xr">sysctl(9)</a> oid that would represent a
<code class="Nm">counter</code>. The <var class="Fa">ptr</var> argument
should be a pointer to allocated <var class="Vt">counter_u64_t</var>. A
read of the oid returns value obtained through
<code class="Fn">counter_u64_fetch</code>(). Any write to the oid zeroes
it.</dd>
<dt id="SYSCTL_ADD_COUNTER_U64"><a class="permalink" href="#SYSCTL_ADD_COUNTER_U64"><code class="Fn">SYSCTL_ADD_COUNTER_U64</code></a>(<var class="Fa">ctx</var>,
<var class="Fa">parent</var>, <var class="Fa">nbr</var>,
<var class="Fa">name</var>, <var class="Fa">access</var>,
<var class="Fa">ptr</var>, <var class="Fa">descr</var>)</dt>
<dd>Create a <a class="Xr">sysctl(9)</a> oid that would represent a
<code class="Nm">counter</code>. The <var class="Fa">ptr</var> argument
should be a pointer to allocated <var class="Vt">counter_u64_t</var>. A
read of the oid returns value obtained through
<code class="Fn">counter_u64_fetch</code>(). Any write to the oid zeroes
it.</dd>
<dt id="SYSCTL_COUNTER_U64_ARRAY"><a class="permalink" href="#SYSCTL_COUNTER_U64_ARRAY"><code class="Fn">SYSCTL_COUNTER_U64_ARRAY</code></a>(<var class="Fa">parent</var>,
<var class="Fa">nbr</var>, <var class="Fa">name</var>,
<var class="Fa">access</var>, <var class="Fa">ptr</var>,
<var class="Fa">len</var>, <var class="Fa">descr</var>)</dt>
<dd>Declare a static <a class="Xr">sysctl(9)</a> oid that would represent an
array of <code class="Nm">counter</code>. The <var class="Fa">ptr</var>
argument should be a pointer to allocated array of
<var class="Vt">counter_u64_t's</var>. The <var class="Fa">len</var>
argument should specify number of elements in the array. A read of the oid
returns len-sized array of <var class="Vt">uint64_t</var> values obtained
through <code class="Fn">counter_u64_fetch</code>(). Any write to the oid
zeroes all array elements.</dd>
<dt id="SYSCTL_ADD_COUNTER_U64_ARRAY"><a class="permalink" href="#SYSCTL_ADD_COUNTER_U64_ARRAY"><code class="Fn">SYSCTL_ADD_COUNTER_U64_ARRAY</code></a>(<var class="Fa">ctx</var>,
<var class="Fa">parent</var>, <var class="Fa">nbr</var>,
<var class="Fa">name</var>, <var class="Fa">access</var>,
<var class="Fa">ptr</var>, <var class="Fa">len</var>,
<var class="Fa">descr</var>)</dt>
<dd>Create a <a class="Xr">sysctl(9)</a> oid that would represent an array of
<code class="Nm">counter</code>. The <var class="Fa">ptr</var> argument
should be a pointer to allocated array of
<var class="Vt">counter_u64_t's</var>. The <var class="Fa">len</var>
argument should specify number of elements in the array. A read of the oid
returns len-sized array of <var class="Vt">uint64_t</var> values obtained
through <code class="Fn">counter_u64_fetch</code>(). Any write to the oid
zeroes all array elements.</dd>
</dl>
</section>
<section class="Sh">
<h1 class="Sh" id="IMPLEMENTATION_DETAILS"><a class="permalink" href="#IMPLEMENTATION_DETAILS">IMPLEMENTATION
DETAILS</a></h1>
<p class="Pp">On all architectures <code class="Nm">counter</code> 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 <var class="Va">UMA_ZONE_PCPU</var>
<a class="Xr">uma(9)</a> 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.</p>
<p class="Pp">On amd64 a <code class="Nm">counter</code> 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.</p>
<p class="Pp">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.</p>
<p class="Pp">On some architectures updating a counter require a
<a class="Xr">critical(9)</a> section.</p>
</section>
<section class="Sh">
<h1 class="Sh" id="EXAMPLES"><a class="permalink" href="#EXAMPLES">EXAMPLES</a></h1>
<p class="Pp">The following example creates a static counter array exported to
userspace through a sysctl:</p>
<div class="Bd Pp Bd-indent Li">
<pre>#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");</pre>
</div>
</section>
<section class="Sh">
<h1 class="Sh" id="SEE_ALSO"><a class="permalink" href="#SEE_ALSO">SEE
ALSO</a></h1>
<p class="Pp"><a class="Xr">atomic(9)</a>, <a class="Xr">critical(9)</a>,
<a class="Xr">locking(9)</a>, <a class="Xr">malloc(9)</a>,
<a class="Xr">ratecheck(9)</a>, <a class="Xr">sysctl(9)</a>,
<a class="Xr">SYSINIT(9)</a>, <a class="Xr">uma(9)</a></p>
</section>
<section class="Sh">
<h1 class="Sh" id="HISTORY"><a class="permalink" href="#HISTORY">HISTORY</a></h1>
<p class="Pp">The <code class="Nm">counter</code> facility first appeared in
<span class="Ux">FreeBSD 10.0</span>.</p>
</section>
<section class="Sh">
<h1 class="Sh" id="AUTHORS"><a class="permalink" href="#AUTHORS">AUTHORS</a></h1>
<p class="Pp">The <code class="Nm">counter</code> facility was written by
<span class="An">Gleb Smirnoff</span> and <span class="An">Konstantin
Belousov</span>.</p>
</section>
</div>
<table class="foot">
<tr>
<td class="foot-date">June 19, 2025</td>
<td class="foot-os">FreeBSD 15.0</td>
</tr>
</table>
|