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 274e76af6aSGleb Smirnoff #include <sys/cdefs.h> 284e76af6aSGleb Smirnoff __FBSDID("$FreeBSD$"); 294e76af6aSGleb Smirnoff 304e76af6aSGleb Smirnoff #include <sys/param.h> 314e76af6aSGleb Smirnoff #include <sys/systm.h> 324e76af6aSGleb Smirnoff #include <sys/kernel.h> 33*70a7dd5dSKonstantin Belousov #include <sys/lock.h> 34*70a7dd5dSKonstantin Belousov #include <sys/mutex.h> 35*70a7dd5dSKonstantin Belousov #include <sys/proc.h> 36*70a7dd5dSKonstantin Belousov #include <sys/sched.h> 374e76af6aSGleb Smirnoff #include <sys/smp.h> 384e76af6aSGleb Smirnoff #include <sys/sysctl.h> 394e76af6aSGleb Smirnoff #include <vm/uma.h> 404e76af6aSGleb Smirnoff 41*70a7dd5dSKonstantin Belousov #define IN_SUBR_COUNTER_C 42*70a7dd5dSKonstantin Belousov #include <sys/counter.h> 43*70a7dd5dSKonstantin Belousov 444e76af6aSGleb Smirnoff static uma_zone_t uint64_pcpu_zone; 454e76af6aSGleb Smirnoff 464e76af6aSGleb Smirnoff void 474e76af6aSGleb Smirnoff counter_u64_zero(counter_u64_t c) 484e76af6aSGleb Smirnoff { 494e76af6aSGleb Smirnoff 50*70a7dd5dSKonstantin Belousov counter_u64_zero_inline(c); 514e76af6aSGleb Smirnoff } 524e76af6aSGleb Smirnoff 534e76af6aSGleb Smirnoff uint64_t 544e76af6aSGleb Smirnoff counter_u64_fetch(counter_u64_t c) 554e76af6aSGleb Smirnoff { 564e76af6aSGleb Smirnoff 57*70a7dd5dSKonstantin Belousov return (counter_u64_fetch_inline(c)); 584e76af6aSGleb Smirnoff } 594e76af6aSGleb Smirnoff 604e76af6aSGleb Smirnoff counter_u64_t 614e76af6aSGleb Smirnoff counter_u64_alloc(int flags) 624e76af6aSGleb Smirnoff { 634e76af6aSGleb Smirnoff counter_u64_t r; 644e76af6aSGleb Smirnoff 654e76af6aSGleb Smirnoff r = uma_zalloc(uint64_pcpu_zone, flags); 664e76af6aSGleb Smirnoff if (r != NULL) 674e76af6aSGleb Smirnoff counter_u64_zero(r); 684e76af6aSGleb Smirnoff 694e76af6aSGleb Smirnoff return (r); 704e76af6aSGleb Smirnoff } 714e76af6aSGleb Smirnoff 724e76af6aSGleb Smirnoff void 734e76af6aSGleb Smirnoff counter_u64_free(counter_u64_t c) 744e76af6aSGleb Smirnoff { 754e76af6aSGleb Smirnoff 764e76af6aSGleb Smirnoff uma_zfree(uint64_pcpu_zone, c); 774e76af6aSGleb Smirnoff } 784e76af6aSGleb Smirnoff 794e76af6aSGleb Smirnoff int 804e76af6aSGleb Smirnoff sysctl_handle_counter_u64(SYSCTL_HANDLER_ARGS) 814e76af6aSGleb Smirnoff { 824e76af6aSGleb Smirnoff uint64_t out; 834e76af6aSGleb Smirnoff int error; 844e76af6aSGleb Smirnoff 854e76af6aSGleb Smirnoff out = counter_u64_fetch(*(counter_u64_t *)arg1); 864e76af6aSGleb Smirnoff 874e76af6aSGleb Smirnoff error = SYSCTL_OUT(req, &out, sizeof(uint64_t)); 884e76af6aSGleb Smirnoff 894e76af6aSGleb Smirnoff if (error || !req->newptr) 904e76af6aSGleb Smirnoff return (error); 914e76af6aSGleb Smirnoff 924e76af6aSGleb Smirnoff /* 934e76af6aSGleb Smirnoff * Any write attempt to a counter zeroes it. 944e76af6aSGleb Smirnoff */ 954e76af6aSGleb Smirnoff counter_u64_zero(*(counter_u64_t *)arg1); 964e76af6aSGleb Smirnoff 974e76af6aSGleb Smirnoff return (0); 984e76af6aSGleb Smirnoff } 994e76af6aSGleb Smirnoff 1004e76af6aSGleb Smirnoff static void 1014e76af6aSGleb Smirnoff counter_startup(void) 1024e76af6aSGleb Smirnoff { 1034e76af6aSGleb Smirnoff 1044e76af6aSGleb Smirnoff uint64_pcpu_zone = uma_zcreate("uint64 pcpu", sizeof(uint64_t), 1054e76af6aSGleb Smirnoff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU); 1064e76af6aSGleb Smirnoff } 1078f779cc5SGleb Smirnoff SYSINIT(counter, SI_SUB_CPU, SI_ORDER_FOURTH, counter_startup, NULL); 108