xref: /freebsd/sys/kern/subr_counter.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
14e76af6aSGleb Smirnoff /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro 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 #include <sys/param.h>
304e76af6aSGleb Smirnoff #include <sys/systm.h>
314e76af6aSGleb Smirnoff #include <sys/kernel.h>
3270a7dd5dSKonstantin Belousov #include <sys/lock.h>
3370a7dd5dSKonstantin Belousov #include <sys/mutex.h>
3470a7dd5dSKonstantin Belousov #include <sys/proc.h>
3570a7dd5dSKonstantin Belousov #include <sys/sched.h>
364e76af6aSGleb Smirnoff #include <sys/smp.h>
374e76af6aSGleb Smirnoff #include <sys/sysctl.h>
384e76af6aSGleb Smirnoff #include <vm/uma.h>
394e76af6aSGleb Smirnoff 
4070a7dd5dSKonstantin Belousov #define IN_SUBR_COUNTER_C
4170a7dd5dSKonstantin Belousov #include <sys/counter.h>
4270a7dd5dSKonstantin Belousov 
434e76af6aSGleb Smirnoff void
counter_u64_zero(counter_u64_t c)444e76af6aSGleb Smirnoff counter_u64_zero(counter_u64_t c)
454e76af6aSGleb Smirnoff {
464e76af6aSGleb Smirnoff 
4770a7dd5dSKonstantin Belousov 	counter_u64_zero_inline(c);
484e76af6aSGleb Smirnoff }
494e76af6aSGleb Smirnoff 
504e76af6aSGleb Smirnoff uint64_t
counter_u64_fetch(counter_u64_t c)514e76af6aSGleb Smirnoff counter_u64_fetch(counter_u64_t c)
524e76af6aSGleb Smirnoff {
534e76af6aSGleb Smirnoff 
5470a7dd5dSKonstantin Belousov 	return (counter_u64_fetch_inline(c));
554e76af6aSGleb Smirnoff }
564e76af6aSGleb Smirnoff 
574e76af6aSGleb Smirnoff counter_u64_t
counter_u64_alloc(int flags)584e76af6aSGleb Smirnoff counter_u64_alloc(int flags)
594e76af6aSGleb Smirnoff {
604e76af6aSGleb Smirnoff 
612dee296aSMateusz Guzik 	return (uma_zalloc_pcpu(pcpu_zone_8, flags | M_ZERO));
624e76af6aSGleb Smirnoff }
634e76af6aSGleb Smirnoff 
644e76af6aSGleb Smirnoff void
counter_u64_free(counter_u64_t c)654e76af6aSGleb Smirnoff counter_u64_free(counter_u64_t c)
664e76af6aSGleb Smirnoff {
674e76af6aSGleb Smirnoff 
682dee296aSMateusz Guzik 	uma_zfree_pcpu(pcpu_zone_8, c);
694e76af6aSGleb Smirnoff }
704e76af6aSGleb Smirnoff 
714e76af6aSGleb Smirnoff int
sysctl_handle_counter_u64(SYSCTL_HANDLER_ARGS)724e76af6aSGleb Smirnoff sysctl_handle_counter_u64(SYSCTL_HANDLER_ARGS)
734e76af6aSGleb Smirnoff {
744e76af6aSGleb Smirnoff 	uint64_t out;
754e76af6aSGleb Smirnoff 	int error;
764e76af6aSGleb Smirnoff 
774e76af6aSGleb Smirnoff 	out = counter_u64_fetch(*(counter_u64_t *)arg1);
784e76af6aSGleb Smirnoff 
794e76af6aSGleb Smirnoff 	error = SYSCTL_OUT(req, &out, sizeof(uint64_t));
804e76af6aSGleb Smirnoff 
814e76af6aSGleb Smirnoff 	if (error || !req->newptr)
824e76af6aSGleb Smirnoff 		return (error);
834e76af6aSGleb Smirnoff 
844e76af6aSGleb Smirnoff 	/*
854e76af6aSGleb Smirnoff 	 * Any write attempt to a counter zeroes it.
864e76af6aSGleb Smirnoff 	 */
874e76af6aSGleb Smirnoff 	counter_u64_zero(*(counter_u64_t *)arg1);
884e76af6aSGleb Smirnoff 
894e76af6aSGleb Smirnoff 	return (0);
904e76af6aSGleb Smirnoff }
91b5b7b142SGleb Smirnoff 
92b5b7b142SGleb Smirnoff int
sysctl_handle_counter_u64_array(SYSCTL_HANDLER_ARGS)93b5b7b142SGleb Smirnoff sysctl_handle_counter_u64_array(SYSCTL_HANDLER_ARGS)
94b5b7b142SGleb Smirnoff {
95b5b7b142SGleb Smirnoff 	uint64_t *out;
96b5b7b142SGleb Smirnoff 	int error;
97b5b7b142SGleb Smirnoff 
98b5b7b142SGleb Smirnoff 	out = malloc(arg2 * sizeof(uint64_t), M_TEMP, M_WAITOK);
99b5b7b142SGleb Smirnoff 	for (int i = 0; i < arg2; i++)
100b5b7b142SGleb Smirnoff 		out[i] = counter_u64_fetch(((counter_u64_t *)arg1)[i]);
101b5b7b142SGleb Smirnoff 
102b5b7b142SGleb Smirnoff 	error = SYSCTL_OUT(req, out, arg2 * sizeof(uint64_t));
1031d522501SGleb Smirnoff 	free(out, M_TEMP);
104b5b7b142SGleb Smirnoff 
105b5b7b142SGleb Smirnoff 	if (error || !req->newptr)
106b5b7b142SGleb Smirnoff 		return (error);
107b5b7b142SGleb Smirnoff 
108b5b7b142SGleb Smirnoff 	/*
109b5b7b142SGleb Smirnoff 	 * Any write attempt to a counter zeroes it.
110b5b7b142SGleb Smirnoff 	 */
111b5b7b142SGleb Smirnoff 	for (int i = 0; i < arg2; i++)
112b5b7b142SGleb Smirnoff 		counter_u64_zero(((counter_u64_t *)arg1)[i]);
113b5b7b142SGleb Smirnoff 
114b5b7b142SGleb Smirnoff 	return (0);
115b5b7b142SGleb Smirnoff }
11616917020SGleb Smirnoff 
11716917020SGleb Smirnoff /*
11816917020SGleb Smirnoff  * MP-friendly version of ppsratecheck().
11916917020SGleb Smirnoff  *
12016917020SGleb Smirnoff  * Returns non-negative if we are in the rate, negative otherwise.
12116917020SGleb Smirnoff  *  0 - rate limit not reached.
12216917020SGleb Smirnoff  * -1 - rate limit reached.
12316917020SGleb Smirnoff  * >0 - rate limit was reached before, and was just reset. The return value
12416917020SGleb Smirnoff  *      is number of events since last reset.
12516917020SGleb Smirnoff  */
12616917020SGleb Smirnoff int64_t
counter_ratecheck(struct counter_rate * cr,int64_t limit)12716917020SGleb Smirnoff counter_ratecheck(struct counter_rate *cr, int64_t limit)
12816917020SGleb Smirnoff {
12916917020SGleb Smirnoff 	int64_t val;
13016917020SGleb Smirnoff 	int now;
13116917020SGleb Smirnoff 
13216917020SGleb Smirnoff 	val = cr->cr_over;
13316917020SGleb Smirnoff 	now = ticks;
13416917020SGleb Smirnoff 
13595dce07dSGleb Smirnoff 	if ((u_int)(now - cr->cr_ticks) >= hz) {
13616917020SGleb Smirnoff 		/*
13716917020SGleb Smirnoff 		 * Time to clear the structure, we are in the next second.
13816917020SGleb Smirnoff 		 * First try unlocked read, and then proceed with atomic.
13916917020SGleb Smirnoff 		 */
14016917020SGleb Smirnoff 		if ((cr->cr_lock == 0) &&
1415040da77SGleb Smirnoff 		    atomic_cmpset_acq_int(&cr->cr_lock, 0, 1)) {
14216917020SGleb Smirnoff 			/*
14316917020SGleb Smirnoff 			 * Check if other thread has just went through the
14416917020SGleb Smirnoff 			 * reset sequence before us.
14516917020SGleb Smirnoff 			 */
14695dce07dSGleb Smirnoff 			if ((u_int)(now - cr->cr_ticks) >= hz) {
14716917020SGleb Smirnoff 				val = counter_u64_fetch(cr->cr_rate);
14816917020SGleb Smirnoff 				counter_u64_zero(cr->cr_rate);
14916917020SGleb Smirnoff 				cr->cr_over = 0;
15016917020SGleb Smirnoff 				cr->cr_ticks = now;
1511276a836SGleb Smirnoff 				if (val <= limit)
1521276a836SGleb Smirnoff 					val = 0;
15316917020SGleb Smirnoff 			}
15416917020SGleb Smirnoff 			atomic_store_rel_int(&cr->cr_lock, 0);
15516917020SGleb Smirnoff 		} else
15616917020SGleb Smirnoff 			/*
15716917020SGleb Smirnoff 			 * We failed to lock, in this case other thread may
15816917020SGleb Smirnoff 			 * be running counter_u64_zero(), so it is not safe
15916917020SGleb Smirnoff 			 * to do an update, we skip it.
16016917020SGleb Smirnoff 			 */
16116917020SGleb Smirnoff 			return (val);
16216917020SGleb Smirnoff 	}
16316917020SGleb Smirnoff 
16416917020SGleb Smirnoff 	counter_u64_add(cr->cr_rate, 1);
16516917020SGleb Smirnoff 	if (cr->cr_over != 0)
16616917020SGleb Smirnoff 		return (-1);
16716917020SGleb Smirnoff 	if (counter_u64_fetch(cr->cr_rate) > limit)
16816917020SGleb Smirnoff 		val = cr->cr_over = -1;
16916917020SGleb Smirnoff 
17016917020SGleb Smirnoff 	return (val);
17116917020SGleb Smirnoff }
172fffcb56fSMark Johnston 
173fffcb56fSMark Johnston void
counter_u64_sysinit(void * arg)174fffcb56fSMark Johnston counter_u64_sysinit(void *arg)
175fffcb56fSMark Johnston {
176fffcb56fSMark Johnston 	counter_u64_t *cp;
177fffcb56fSMark Johnston 
178fffcb56fSMark Johnston 	cp = arg;
179fffcb56fSMark Johnston 	*cp = counter_u64_alloc(M_WAITOK);
180fffcb56fSMark Johnston }
181fffcb56fSMark Johnston 
182fffcb56fSMark Johnston void
counter_u64_sysuninit(void * arg)183fffcb56fSMark Johnston counter_u64_sysuninit(void *arg)
184fffcb56fSMark Johnston {
185fffcb56fSMark Johnston 	counter_u64_t *cp;
186fffcb56fSMark Johnston 
187fffcb56fSMark Johnston 	cp = arg;
188fffcb56fSMark Johnston 	counter_u64_free(*cp);
189fffcb56fSMark Johnston }
190