Index: head/share/man/man9/counter.9 =================================================================== --- head/share/man/man9/counter.9 (revision 309744) +++ head/share/man/man9/counter.9 (revision 309745) @@ -1,246 +1,260 @@ .\"- .\" Copyright (c) 2013 Gleb Smirnoff .\" 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 the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" -.Dd March 14, 2016 +.Dd December 8, 2016 .Dt COUNTER 9 .Os .Sh NAME .Nm counter .Nd "SMP-friendly kernel counter implementation" .Sh 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" +.Ft int64_t +.Fn counter_ratecheck "struct counter_rate *cr" "int64_t limit" .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 descr .Sh DESCRIPTION .Nm is a generic facility to create counters that can be utilized for any purpose (such as collecting statistical data). A .Nm is guaranteed to be lossless when several kernel threads do simultaneous updates. However, .Nm does not block the calling thread, also no .Xr atomic 9 operations are used for the update, therefore the counters can be used in any non-interrupt context. Moreover, .Nm has special optimisations for SMP environments, making .Nm update faster than simple arithmetic on the global variable. Thus .Nm is considered suitable for accounting in the performance-critical code paths. .Bl -tag -width indent .It Fn counter_u64_alloc wait Allocate a new 64-bit unsigned counter. The .Fa wait argument is the .Xr malloc 9 wait flag, should be either .Va M_NOWAIT or .Va M_WAITOK . If .Va M_NOWAIT is specified the operation may fail. .It Fn counter_u64_free c Free the previously allocated counter .Fa c . .It Fn counter_u64_add c v Add .Fa v to .Fa c . The KPI does not guarantee any protection from wraparound. .It 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 .Xr critical 9 section, while on other is a nop. See .Sx IMPLEMENTATION DETAILS . .It Fn counter_exit Exit mode for updating several counters. .It Fn counter_u64_add_protected c v Same as .Fn counter_u64_add , but should be preceded by .Fn counter_enter . .It 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. .It Fn counter_u64_zero c Clear the counter .Fa c and set it to zero. +.It Fn counter_ratecheck cr limit +The function is a multiprocessor-friendly version of +.Fn ppsratecheck , +which uses +.Nm +internally. +Returns non-negative value if the rate isn't 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, +then +.Fn counter_ratecheck +returns number of events since previous reset. .It Fn SYSCTL_COUNTER_U64 parent nbr name access ptr descr Declare a static .Xr sysctl oid that would represent a .Nm . 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. .It Fn SYSCTL_ADD_COUNTER_U64 ctx parent nbr name access ptr descr Create a .Xr sysctl oid that would represent a .Nm . 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. .It Fn SYSCTL_COUNTER_U64_ARRAY parent nbr name access ptr len descr Declare a static .Xr sysctl oid that would represent an array of .Nm . 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. .It Fn SYSCTL_ADD_COUNTER_U64_ARRAY ctx parent nbr name access ptr len descr Create a .Xr sysctl oid that would represent an array of .Nm . 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. .El .Sh IMPLEMENTATION DETAILS On all architectures .Nm 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 .Va UMA_ZONE_PCPU .Xr 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. .Pp On amd64 a .Nm 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. .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. .Pp On some architectures updating a counter require a .Xr critical 9 section. .Sh EXAMPLES The following example creates a static counter array exported to userspace through a sysctl: .Bd -literal -offset indent #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"); .Ed .Sh SEE ALSO .Xr atomic 9 , .Xr critical 9 , .Xr locking 9 , .Xr malloc 9 , .Xr sysctl 9 , .Xr uma 9 .Sh HISTORY The .Nm facility first appeared in .Fx 10.0 . .Sh AUTHORS .An -nosplit The .Nm facility was written by .An Gleb Smirnoff and .An Konstantin Belousov . Index: head/sys/kern/subr_counter.c =================================================================== --- head/sys/kern/subr_counter.c (revision 309744) +++ head/sys/kern/subr_counter.c (revision 309745) @@ -1,121 +1,175 @@ /*- * Copyright (c) 2012 Gleb Smirnoff * 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 the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #define IN_SUBR_COUNTER_C #include void counter_u64_zero(counter_u64_t c) { counter_u64_zero_inline(c); } uint64_t counter_u64_fetch(counter_u64_t c) { return (counter_u64_fetch_inline(c)); } counter_u64_t counter_u64_alloc(int flags) { counter_u64_t r; r = uma_zalloc(pcpu_zone_64, flags); if (r != NULL) counter_u64_zero(r); return (r); } void counter_u64_free(counter_u64_t c) { uma_zfree(pcpu_zone_64, c); } int sysctl_handle_counter_u64(SYSCTL_HANDLER_ARGS) { uint64_t out; int error; out = counter_u64_fetch(*(counter_u64_t *)arg1); error = SYSCTL_OUT(req, &out, sizeof(uint64_t)); if (error || !req->newptr) return (error); /* * Any write attempt to a counter zeroes it. */ counter_u64_zero(*(counter_u64_t *)arg1); return (0); } int sysctl_handle_counter_u64_array(SYSCTL_HANDLER_ARGS) { uint64_t *out; int error; out = malloc(arg2 * sizeof(uint64_t), M_TEMP, M_WAITOK); for (int i = 0; i < arg2; i++) out[i] = counter_u64_fetch(((counter_u64_t *)arg1)[i]); error = SYSCTL_OUT(req, out, arg2 * sizeof(uint64_t)); free(out, M_TEMP); if (error || !req->newptr) return (error); /* * Any write attempt to a counter zeroes it. */ for (int i = 0; i < arg2; i++) counter_u64_zero(((counter_u64_t *)arg1)[i]); return (0); } + +/* + * MP-friendly version of ppsratecheck(). + * + * Returns non-negative if we are in the rate, negative otherwise. + * 0 - rate limit not reached. + * -1 - rate limit reached. + * >0 - rate limit was reached before, and was just reset. The return value + * is number of events since last reset. + */ +int64_t +counter_ratecheck(struct counter_rate *cr, int64_t limit) +{ + int64_t val; + int now; + + val = cr->cr_over; + now = ticks; + + if (now - cr->cr_ticks >= hz) { + /* + * Time to clear the structure, we are in the next second. + * First try unlocked read, and then proceed with atomic. + */ + if ((cr->cr_lock == 0) && + atomic_cmpset_int(&cr->cr_lock, 0, 1)) { + /* + * Check if other thread has just went through the + * reset sequence before us. + */ + if (now - cr->cr_ticks >= hz) { + val = counter_u64_fetch(cr->cr_rate); + counter_u64_zero(cr->cr_rate); + cr->cr_over = 0; + cr->cr_ticks = now; + } + atomic_store_rel_int(&cr->cr_lock, 0); + } else + /* + * We failed to lock, in this case other thread may + * be running counter_u64_zero(), so it is not safe + * to do an update, we skip it. + */ + return (val); + } + + counter_u64_add(cr->cr_rate, 1); + if (cr->cr_over != 0) + return (-1); + if (counter_u64_fetch(cr->cr_rate) > limit) + val = cr->cr_over = -1; + + return (val); +} Index: head/sys/sys/counter.h =================================================================== --- head/sys/sys/counter.h (revision 309744) +++ head/sys/sys/counter.h (revision 309745) @@ -1,63 +1,76 @@ /*- * Copyright (c) 2012 Gleb Smirnoff * 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 the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef __SYS_COUNTER_H__ #define __SYS_COUNTER_H__ typedef uint64_t *counter_u64_t; #ifdef _KERNEL #include counter_u64_t counter_u64_alloc(int); void counter_u64_free(counter_u64_t); void counter_u64_zero(counter_u64_t); uint64_t counter_u64_fetch(counter_u64_t); #define COUNTER_ARRAY_ALLOC(a, n, wait) do { \ for (int i = 0; i < (n); i++) \ (a)[i] = counter_u64_alloc(wait); \ } while (0) #define COUNTER_ARRAY_FREE(a, n) do { \ for (int i = 0; i < (n); i++) \ counter_u64_free((a)[i]); \ } while (0) #define COUNTER_ARRAY_COPY(a, dstp, n) do { \ for (int i = 0; i < (n); i++) \ ((uint64_t *)(dstp))[i] = counter_u64_fetch((a)[i]);\ } while (0) #define COUNTER_ARRAY_ZERO(a, n) do { \ for (int i = 0; i < (n); i++) \ counter_u64_zero((a)[i]); \ } while (0) + +/* + * counter(9) based rate checking. + */ +struct counter_rate { + counter_u64_t cr_rate; /* Events since last second */ + volatile int cr_lock; /* Lock to clean the struct */ + int cr_ticks; /* Ticks on last clean */ + int cr_over; /* Over limit since cr_ticks? */ +}; + +int64_t counter_ratecheck(struct counter_rate *, int64_t); + #endif /* _KERNEL */ #endif /* ! __SYS_COUNTER_H__ */