2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/mutex.h>
36 #include <sys/sched.h>
38 #include <sys/sysctl.h>
41 #define IN_SUBR_COUNTER_C
42 #include <sys/counter.h>
45 counter_u64_zero(counter_u64_t c)
48 counter_u64_zero_inline(c);
52 counter_u64_fetch(counter_u64_t c)
55 return (counter_u64_fetch_inline(c));
59 counter_u64_alloc(int flags)
62 return (uma_zalloc_pcpu(pcpu_zone_8, flags | M_ZERO));
66 counter_u64_free(counter_u64_t c)
69 uma_zfree_pcpu(pcpu_zone_8, c);
73 sysctl_handle_counter_u64(SYSCTL_HANDLER_ARGS)
78 out = counter_u64_fetch(*(counter_u64_t *)arg1);
80 error = SYSCTL_OUT(req, &out, sizeof(uint64_t));
82 if (error || !req->newptr)
86 * Any write attempt to a counter zeroes it.
88 counter_u64_zero(*(counter_u64_t *)arg1);
94 sysctl_handle_counter_u64_array(SYSCTL_HANDLER_ARGS)
99 out = malloc(arg2 * sizeof(uint64_t), M_TEMP, M_WAITOK);
100 for (int i = 0; i < arg2; i++)
101 out[i] = counter_u64_fetch(((counter_u64_t *)arg1)[i]);
103 error = SYSCTL_OUT(req, out, arg2 * sizeof(uint64_t));
106 if (error || !req->newptr)
110 * Any write attempt to a counter zeroes it.
112 for (int i = 0; i < arg2; i++)
113 counter_u64_zero(((counter_u64_t *)arg1)[i]);
119 * MP-friendly version of ppsratecheck().
121 * Returns non-negative if we are in the rate, negative otherwise.
122 * 0 - rate limit not reached.
123 * -1 - rate limit reached.
124 * >0 - rate limit was reached before, and was just reset. The return value
125 * is number of events since last reset.
128 counter_ratecheck(struct counter_rate *cr, int64_t limit)
136 if ((u_int)(now - cr->cr_ticks) >= hz) {
138 * Time to clear the structure, we are in the next second.
139 * First try unlocked read, and then proceed with atomic.
141 if ((cr->cr_lock == 0) &&
142 atomic_cmpset_acq_int(&cr->cr_lock, 0, 1)) {
144 * Check if other thread has just went through the
145 * reset sequence before us.
147 if ((u_int)(now - cr->cr_ticks) >= hz) {
148 val = counter_u64_fetch(cr->cr_rate);
149 counter_u64_zero(cr->cr_rate);
155 atomic_store_rel_int(&cr->cr_lock, 0);
158 * We failed to lock, in this case other thread may
159 * be running counter_u64_zero(), so it is not safe
160 * to do an update, we skip it.
165 counter_u64_add(cr->cr_rate, 1);
166 if (cr->cr_over != 0)
168 if (counter_u64_fetch(cr->cr_rate) > limit)
169 val = cr->cr_over = -1;
175 counter_u64_sysinit(void *arg)
180 *cp = counter_u64_alloc(M_WAITOK);
184 counter_u64_sysuninit(void *arg)
189 counter_u64_free(*cp);