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> 3370a7dd5dSKonstantin Belousov #include <sys/lock.h> 3470a7dd5dSKonstantin Belousov #include <sys/mutex.h> 3570a7dd5dSKonstantin Belousov #include <sys/proc.h> 3670a7dd5dSKonstantin 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 4170a7dd5dSKonstantin Belousov #define IN_SUBR_COUNTER_C 4270a7dd5dSKonstantin Belousov #include <sys/counter.h> 4370a7dd5dSKonstantin Belousov 444e76af6aSGleb Smirnoff void 454e76af6aSGleb Smirnoff counter_u64_zero(counter_u64_t c) 464e76af6aSGleb Smirnoff { 474e76af6aSGleb Smirnoff 4870a7dd5dSKonstantin Belousov counter_u64_zero_inline(c); 494e76af6aSGleb Smirnoff } 504e76af6aSGleb Smirnoff 514e76af6aSGleb Smirnoff uint64_t 524e76af6aSGleb Smirnoff counter_u64_fetch(counter_u64_t c) 534e76af6aSGleb Smirnoff { 544e76af6aSGleb Smirnoff 5570a7dd5dSKonstantin Belousov return (counter_u64_fetch_inline(c)); 564e76af6aSGleb Smirnoff } 574e76af6aSGleb Smirnoff 584e76af6aSGleb Smirnoff counter_u64_t 594e76af6aSGleb Smirnoff counter_u64_alloc(int flags) 604e76af6aSGleb Smirnoff { 614e76af6aSGleb Smirnoff counter_u64_t r; 624e76af6aSGleb Smirnoff 6349fef6a2SGleb Smirnoff r = uma_zalloc(pcpu_zone_64, flags); 644e76af6aSGleb Smirnoff if (r != NULL) 654e76af6aSGleb Smirnoff counter_u64_zero(r); 664e76af6aSGleb Smirnoff 674e76af6aSGleb Smirnoff return (r); 684e76af6aSGleb Smirnoff } 694e76af6aSGleb Smirnoff 704e76af6aSGleb Smirnoff void 714e76af6aSGleb Smirnoff counter_u64_free(counter_u64_t c) 724e76af6aSGleb Smirnoff { 734e76af6aSGleb Smirnoff 7449fef6a2SGleb Smirnoff uma_zfree(pcpu_zone_64, c); 754e76af6aSGleb Smirnoff } 764e76af6aSGleb Smirnoff 774e76af6aSGleb Smirnoff int 784e76af6aSGleb Smirnoff sysctl_handle_counter_u64(SYSCTL_HANDLER_ARGS) 794e76af6aSGleb Smirnoff { 804e76af6aSGleb Smirnoff uint64_t out; 814e76af6aSGleb Smirnoff int error; 824e76af6aSGleb Smirnoff 834e76af6aSGleb Smirnoff out = counter_u64_fetch(*(counter_u64_t *)arg1); 844e76af6aSGleb Smirnoff 854e76af6aSGleb Smirnoff error = SYSCTL_OUT(req, &out, sizeof(uint64_t)); 864e76af6aSGleb Smirnoff 874e76af6aSGleb Smirnoff if (error || !req->newptr) 884e76af6aSGleb Smirnoff return (error); 894e76af6aSGleb Smirnoff 904e76af6aSGleb Smirnoff /* 914e76af6aSGleb Smirnoff * Any write attempt to a counter zeroes it. 924e76af6aSGleb Smirnoff */ 934e76af6aSGleb Smirnoff counter_u64_zero(*(counter_u64_t *)arg1); 944e76af6aSGleb Smirnoff 954e76af6aSGleb Smirnoff return (0); 964e76af6aSGleb Smirnoff } 97b5b7b142SGleb Smirnoff 98b5b7b142SGleb Smirnoff int 99b5b7b142SGleb Smirnoff sysctl_handle_counter_u64_array(SYSCTL_HANDLER_ARGS) 100b5b7b142SGleb Smirnoff { 101b5b7b142SGleb Smirnoff uint64_t *out; 102b5b7b142SGleb Smirnoff int error; 103b5b7b142SGleb Smirnoff 104b5b7b142SGleb Smirnoff out = malloc(arg2 * sizeof(uint64_t), M_TEMP, M_WAITOK); 105b5b7b142SGleb Smirnoff for (int i = 0; i < arg2; i++) 106b5b7b142SGleb Smirnoff out[i] = counter_u64_fetch(((counter_u64_t *)arg1)[i]); 107b5b7b142SGleb Smirnoff 108b5b7b142SGleb Smirnoff error = SYSCTL_OUT(req, out, arg2 * sizeof(uint64_t)); 1091d522501SGleb Smirnoff free(out, M_TEMP); 110b5b7b142SGleb Smirnoff 111b5b7b142SGleb Smirnoff if (error || !req->newptr) 112b5b7b142SGleb Smirnoff return (error); 113b5b7b142SGleb Smirnoff 114b5b7b142SGleb Smirnoff /* 115b5b7b142SGleb Smirnoff * Any write attempt to a counter zeroes it. 116b5b7b142SGleb Smirnoff */ 117b5b7b142SGleb Smirnoff for (int i = 0; i < arg2; i++) 118b5b7b142SGleb Smirnoff counter_u64_zero(((counter_u64_t *)arg1)[i]); 119b5b7b142SGleb Smirnoff 120b5b7b142SGleb Smirnoff return (0); 121b5b7b142SGleb Smirnoff } 12216917020SGleb Smirnoff 12316917020SGleb Smirnoff /* 12416917020SGleb Smirnoff * MP-friendly version of ppsratecheck(). 12516917020SGleb Smirnoff * 12616917020SGleb Smirnoff * Returns non-negative if we are in the rate, negative otherwise. 12716917020SGleb Smirnoff * 0 - rate limit not reached. 12816917020SGleb Smirnoff * -1 - rate limit reached. 12916917020SGleb Smirnoff * >0 - rate limit was reached before, and was just reset. The return value 13016917020SGleb Smirnoff * is number of events since last reset. 13116917020SGleb Smirnoff */ 13216917020SGleb Smirnoff int64_t 13316917020SGleb Smirnoff counter_ratecheck(struct counter_rate *cr, int64_t limit) 13416917020SGleb Smirnoff { 13516917020SGleb Smirnoff int64_t val; 13616917020SGleb Smirnoff int now; 13716917020SGleb Smirnoff 13816917020SGleb Smirnoff val = cr->cr_over; 13916917020SGleb Smirnoff now = ticks; 14016917020SGleb Smirnoff 14116917020SGleb Smirnoff if (now - cr->cr_ticks >= hz) { 14216917020SGleb Smirnoff /* 14316917020SGleb Smirnoff * Time to clear the structure, we are in the next second. 14416917020SGleb Smirnoff * First try unlocked read, and then proceed with atomic. 14516917020SGleb Smirnoff */ 14616917020SGleb Smirnoff if ((cr->cr_lock == 0) && 147*5040da77SGleb Smirnoff atomic_cmpset_acq_int(&cr->cr_lock, 0, 1)) { 14816917020SGleb Smirnoff /* 14916917020SGleb Smirnoff * Check if other thread has just went through the 15016917020SGleb Smirnoff * reset sequence before us. 15116917020SGleb Smirnoff */ 15216917020SGleb Smirnoff if (now - cr->cr_ticks >= hz) { 15316917020SGleb Smirnoff val = counter_u64_fetch(cr->cr_rate); 15416917020SGleb Smirnoff counter_u64_zero(cr->cr_rate); 15516917020SGleb Smirnoff cr->cr_over = 0; 15616917020SGleb Smirnoff cr->cr_ticks = now; 15716917020SGleb Smirnoff } 15816917020SGleb Smirnoff atomic_store_rel_int(&cr->cr_lock, 0); 15916917020SGleb Smirnoff } else 16016917020SGleb Smirnoff /* 16116917020SGleb Smirnoff * We failed to lock, in this case other thread may 16216917020SGleb Smirnoff * be running counter_u64_zero(), so it is not safe 16316917020SGleb Smirnoff * to do an update, we skip it. 16416917020SGleb Smirnoff */ 16516917020SGleb Smirnoff return (val); 16616917020SGleb Smirnoff } 16716917020SGleb Smirnoff 16816917020SGleb Smirnoff counter_u64_add(cr->cr_rate, 1); 16916917020SGleb Smirnoff if (cr->cr_over != 0) 17016917020SGleb Smirnoff return (-1); 17116917020SGleb Smirnoff if (counter_u64_fetch(cr->cr_rate) > limit) 17216917020SGleb Smirnoff val = cr->cr_over = -1; 17316917020SGleb Smirnoff 17416917020SGleb Smirnoff return (val); 17516917020SGleb Smirnoff } 176