xref: /freebsd/sys/sys/counter.h (revision 16917020)
14e76af6aSGleb Smirnoff /*-
24e76af6aSGleb Smirnoff  * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
34e76af6aSGleb Smirnoff  * All rights reserved.
44e76af6aSGleb Smirnoff  *
54e76af6aSGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
64e76af6aSGleb Smirnoff  * modification, are permitted provided that the following conditions
74e76af6aSGleb Smirnoff  * are met:
84e76af6aSGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
94e76af6aSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
104e76af6aSGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
114e76af6aSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
124e76af6aSGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
134e76af6aSGleb Smirnoff  *
144e76af6aSGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
154e76af6aSGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164e76af6aSGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
174e76af6aSGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
184e76af6aSGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
194e76af6aSGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
204e76af6aSGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
214e76af6aSGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
224e76af6aSGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
234e76af6aSGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
244e76af6aSGleb Smirnoff  * SUCH DAMAGE.
254e76af6aSGleb Smirnoff  *
264e76af6aSGleb Smirnoff  * $FreeBSD$
274e76af6aSGleb Smirnoff  */
284e76af6aSGleb Smirnoff 
294e76af6aSGleb Smirnoff #ifndef __SYS_COUNTER_H__
304e76af6aSGleb Smirnoff #define __SYS_COUNTER_H__
314e76af6aSGleb Smirnoff 
324e76af6aSGleb Smirnoff typedef uint64_t *counter_u64_t;
334e76af6aSGleb Smirnoff 
34511b5fa5SGleb Smirnoff #ifdef _KERNEL
354e76af6aSGleb Smirnoff #include <machine/counter.h>
364e76af6aSGleb Smirnoff 
374e76af6aSGleb Smirnoff counter_u64_t	counter_u64_alloc(int);
384e76af6aSGleb Smirnoff void		counter_u64_free(counter_u64_t);
394e76af6aSGleb Smirnoff 
404e76af6aSGleb Smirnoff void		counter_u64_zero(counter_u64_t);
414e76af6aSGleb Smirnoff uint64_t	counter_u64_fetch(counter_u64_t);
424e76af6aSGleb Smirnoff 
437daad711SAndrey V. Elsukov #define	COUNTER_ARRAY_ALLOC(a, n, wait)	do {			\
447daad711SAndrey V. Elsukov 	for (int i = 0; i < (n); i++)				\
457daad711SAndrey V. Elsukov 		(a)[i] = counter_u64_alloc(wait);		\
467daad711SAndrey V. Elsukov } while (0)
477daad711SAndrey V. Elsukov 
487daad711SAndrey V. Elsukov #define	COUNTER_ARRAY_FREE(a, n)	do {			\
497daad711SAndrey V. Elsukov 	for (int i = 0; i < (n); i++)				\
507daad711SAndrey V. Elsukov 		counter_u64_free((a)[i]);			\
517daad711SAndrey V. Elsukov } while (0)
527daad711SAndrey V. Elsukov 
537daad711SAndrey V. Elsukov #define	COUNTER_ARRAY_COPY(a, dstp, n)	do {			\
547daad711SAndrey V. Elsukov 	for (int i = 0; i < (n); i++)				\
557daad711SAndrey V. Elsukov 		((uint64_t *)(dstp))[i] = counter_u64_fetch((a)[i]);\
567daad711SAndrey V. Elsukov } while (0)
577daad711SAndrey V. Elsukov 
587daad711SAndrey V. Elsukov #define	COUNTER_ARRAY_ZERO(a, n)	do {			\
597daad711SAndrey V. Elsukov 	for (int i = 0; i < (n); i++)				\
607daad711SAndrey V. Elsukov 		counter_u64_zero((a)[i]);			\
617daad711SAndrey V. Elsukov } while (0)
6216917020SGleb Smirnoff 
6316917020SGleb Smirnoff /*
6416917020SGleb Smirnoff  * counter(9) based rate checking.
6516917020SGleb Smirnoff  */
6616917020SGleb Smirnoff struct counter_rate {
6716917020SGleb Smirnoff 	counter_u64_t	cr_rate;	/* Events since last second */
6816917020SGleb Smirnoff 	volatile int	cr_lock;	/* Lock to clean the struct */
6916917020SGleb Smirnoff 	int		cr_ticks;	/* Ticks on last clean */
7016917020SGleb Smirnoff 	int		cr_over;	/* Over limit since cr_ticks? */
7116917020SGleb Smirnoff };
7216917020SGleb Smirnoff 
7316917020SGleb Smirnoff int64_t	counter_ratecheck(struct counter_rate *, int64_t);
7416917020SGleb Smirnoff 
75511b5fa5SGleb Smirnoff #endif	/* _KERNEL */
764e76af6aSGleb Smirnoff #endif	/* ! __SYS_COUNTER_H__ */
77