xref: /freebsd/sys/contrib/openzfs/module/zfs/zfs_ratelimit.c (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy /*
2*eda14cbcSMatt Macy  * CDDL HEADER START
3*eda14cbcSMatt Macy  *
4*eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5*eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6*eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7*eda14cbcSMatt Macy  *
8*eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10*eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11*eda14cbcSMatt Macy  * and limitations under the License.
12*eda14cbcSMatt Macy  *
13*eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14*eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16*eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17*eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18*eda14cbcSMatt Macy  *
19*eda14cbcSMatt Macy  * CDDL HEADER END
20*eda14cbcSMatt Macy  */
21*eda14cbcSMatt Macy 
22*eda14cbcSMatt Macy /*
23*eda14cbcSMatt Macy  * Copyright (c) 2017, Lawrence Livermore National Security, LLC.
24*eda14cbcSMatt Macy  */
25*eda14cbcSMatt Macy 
26*eda14cbcSMatt Macy #include <sys/zfs_ratelimit.h>
27*eda14cbcSMatt Macy 
28*eda14cbcSMatt Macy /*
29*eda14cbcSMatt Macy  * Initialize rate limit struct
30*eda14cbcSMatt Macy  *
31*eda14cbcSMatt Macy  * rl:		zfs_ratelimit_t struct
32*eda14cbcSMatt Macy  * burst:	Number to allow in an interval before rate limiting
33*eda14cbcSMatt Macy  * interval:	Interval time in seconds
34*eda14cbcSMatt Macy  */
35*eda14cbcSMatt Macy void
36*eda14cbcSMatt Macy zfs_ratelimit_init(zfs_ratelimit_t *rl, unsigned int *burst,
37*eda14cbcSMatt Macy     unsigned int interval)
38*eda14cbcSMatt Macy {
39*eda14cbcSMatt Macy 	rl->count = 0;
40*eda14cbcSMatt Macy 	rl->start = 0;
41*eda14cbcSMatt Macy 	rl->interval = interval;
42*eda14cbcSMatt Macy 	rl->burst = burst;
43*eda14cbcSMatt Macy 	mutex_init(&rl->lock, NULL, MUTEX_DEFAULT, NULL);
44*eda14cbcSMatt Macy }
45*eda14cbcSMatt Macy 
46*eda14cbcSMatt Macy /*
47*eda14cbcSMatt Macy  * Finalize rate limit struct
48*eda14cbcSMatt Macy  *
49*eda14cbcSMatt Macy  * rl:		zfs_ratelimit_t struct
50*eda14cbcSMatt Macy  */
51*eda14cbcSMatt Macy void
52*eda14cbcSMatt Macy zfs_ratelimit_fini(zfs_ratelimit_t *rl)
53*eda14cbcSMatt Macy {
54*eda14cbcSMatt Macy 	mutex_destroy(&rl->lock);
55*eda14cbcSMatt Macy }
56*eda14cbcSMatt Macy 
57*eda14cbcSMatt Macy /*
58*eda14cbcSMatt Macy  * Re-implementation of the kernel's __ratelimit() function
59*eda14cbcSMatt Macy  *
60*eda14cbcSMatt Macy  * We had to write our own rate limiter because the kernel's __ratelimit()
61*eda14cbcSMatt Macy  * function annoyingly prints out how many times it rate limited to the kernel
62*eda14cbcSMatt Macy  * logs (and there's no way to turn it off):
63*eda14cbcSMatt Macy  *
64*eda14cbcSMatt Macy  *	__ratelimit: 59 callbacks suppressed
65*eda14cbcSMatt Macy  *
66*eda14cbcSMatt Macy  * If the kernel ever allows us to disable these prints, we should go back to
67*eda14cbcSMatt Macy  * using __ratelimit() instead.
68*eda14cbcSMatt Macy  *
69*eda14cbcSMatt Macy  * Return values are the same as __ratelimit():
70*eda14cbcSMatt Macy  *
71*eda14cbcSMatt Macy  * 0: If we're rate limiting
72*eda14cbcSMatt Macy  * 1: If we're not rate limiting.
73*eda14cbcSMatt Macy  */
74*eda14cbcSMatt Macy int
75*eda14cbcSMatt Macy zfs_ratelimit(zfs_ratelimit_t *rl)
76*eda14cbcSMatt Macy {
77*eda14cbcSMatt Macy 	hrtime_t now;
78*eda14cbcSMatt Macy 
79*eda14cbcSMatt Macy 	hrtime_t elapsed;
80*eda14cbcSMatt Macy 	int error = 1;
81*eda14cbcSMatt Macy 
82*eda14cbcSMatt Macy 	mutex_enter(&rl->lock);
83*eda14cbcSMatt Macy 
84*eda14cbcSMatt Macy 	now = gethrtime();
85*eda14cbcSMatt Macy 	elapsed = now - rl->start;
86*eda14cbcSMatt Macy 
87*eda14cbcSMatt Macy 	rl->count++;
88*eda14cbcSMatt Macy 	if (NSEC2SEC(elapsed) >= rl->interval) {
89*eda14cbcSMatt Macy 		rl->start = now;
90*eda14cbcSMatt Macy 		rl->count = 0;
91*eda14cbcSMatt Macy 	} else {
92*eda14cbcSMatt Macy 		if (rl->count >= *rl->burst) {
93*eda14cbcSMatt Macy 			error = 0; /* We're ratelimiting */
94*eda14cbcSMatt Macy 		}
95*eda14cbcSMatt Macy 	}
96*eda14cbcSMatt Macy 	mutex_exit(&rl->lock);
97*eda14cbcSMatt Macy 
98*eda14cbcSMatt Macy 	return (error);
99*eda14cbcSMatt Macy }
100