14e76af6aSGleb Smirnoff /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3c4e20cadSPedro F. Giffuni * 44e76af6aSGleb Smirnoff * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org> 54e76af6aSGleb Smirnoff * All rights reserved. 64e76af6aSGleb Smirnoff * 74e76af6aSGleb Smirnoff * Redistribution and use in source and binary forms, with or without 84e76af6aSGleb Smirnoff * modification, are permitted provided that the following conditions 94e76af6aSGleb Smirnoff * are met: 104e76af6aSGleb Smirnoff * 1. Redistributions of source code must retain the above copyright 114e76af6aSGleb Smirnoff * notice, this list of conditions and the following disclaimer. 124e76af6aSGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright 134e76af6aSGleb Smirnoff * notice, this list of conditions and the following disclaimer in the 144e76af6aSGleb Smirnoff * documentation and/or other materials provided with the distribution. 154e76af6aSGleb Smirnoff * 164e76af6aSGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 174e76af6aSGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 184e76af6aSGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 194e76af6aSGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 204e76af6aSGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 214e76af6aSGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 224e76af6aSGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 234e76af6aSGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 244e76af6aSGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 254e76af6aSGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 264e76af6aSGleb Smirnoff * SUCH DAMAGE. 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 { \ 44cb1ee1bfSAndrey V. Elsukov for (int _i = 0; _i < (n); _i++) \ 45cb1ee1bfSAndrey 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 { \ 49cb1ee1bfSAndrey V. Elsukov for (int _i = 0; _i < (n); _i++) \ 50cb1ee1bfSAndrey 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 { \ 54cb1ee1bfSAndrey V. Elsukov for (int _i = 0; _i < (n); _i++) \ 55cb1ee1bfSAndrey 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 { \ 59cb1ee1bfSAndrey V. Elsukov for (int _i = 0; _i < (n); _i++) \ 60cb1ee1bfSAndrey 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 75fffcb56fSMark Johnston #define COUNTER_U64_SYSINIT(c) \ 76fffcb56fSMark Johnston SYSINIT(c##_counter_sysinit, SI_SUB_COUNTER, \ 77fffcb56fSMark Johnston SI_ORDER_ANY, counter_u64_sysinit, &c); \ 78fffcb56fSMark Johnston SYSUNINIT(c##_counter_sysuninit, SI_SUB_COUNTER, \ 79fffcb56fSMark Johnston SI_ORDER_ANY, counter_u64_sysuninit, &c) 80fffcb56fSMark Johnston 81fffcb56fSMark Johnston #define COUNTER_U64_DEFINE_EARLY(c) \ 82fffcb56fSMark Johnston counter_u64_t __read_mostly c = EARLY_COUNTER; \ 83fffcb56fSMark Johnston COUNTER_U64_SYSINIT(c) 84fffcb56fSMark Johnston 85fffcb56fSMark Johnston void counter_u64_sysinit(void *); 86fffcb56fSMark Johnston void counter_u64_sysuninit(void *); 87fffcb56fSMark Johnston 88511b5fa5SGleb Smirnoff #endif /* _KERNEL */ 894e76af6aSGleb Smirnoff #endif /* ! __SYS_COUNTER_H__ */ 90