4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2017, Lawrence Livermore National Security, LLC.
26 #include <sys/zfs_ratelimit.h>
29 * Initialize rate limit struct
31 * rl: zfs_ratelimit_t struct
32 * burst: Number to allow in an interval before rate limiting
33 * interval: Interval time in seconds
36 zfs_ratelimit_init(zfs_ratelimit_t
*rl
, unsigned int *burst
,
37 unsigned int interval
)
41 rl
->interval
= interval
;
43 mutex_init(&rl
->lock
, NULL
, MUTEX_DEFAULT
, NULL
);
47 * Finalize rate limit struct
49 * rl: zfs_ratelimit_t struct
52 zfs_ratelimit_fini(zfs_ratelimit_t
*rl
)
54 mutex_destroy(&rl
->lock
);
58 * Re-implementation of the kernel's __ratelimit() function
60 * We had to write our own rate limiter because the kernel's __ratelimit()
61 * function annoyingly prints out how many times it rate limited to the kernel
62 * logs (and there's no way to turn it off):
64 * __ratelimit: 59 callbacks suppressed
66 * If the kernel ever allows us to disable these prints, we should go back to
67 * using __ratelimit() instead.
69 * Return values are the same as __ratelimit():
71 * 0: If we're rate limiting
72 * 1: If we're not rate limiting.
75 zfs_ratelimit(zfs_ratelimit_t
*rl
)
82 mutex_enter(&rl
->lock
);
85 elapsed
= now
- rl
->start
;
88 if (NSEC2SEC(elapsed
) >= rl
->interval
) {
92 if (rl
->count
>= *rl
->burst
) {
93 error
= 0; /* We're ratelimiting */
96 mutex_exit(&rl
->lock
);