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