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