Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2016, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software without specific prior written permission.
17 : : *
18 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 : : */
30 : :
31 : : /*
32 : : * Simple log rate limiting. Allow for bursts, then drop messages
33 : : * until some interval expired.
34 : : */
35 : :
36 : : #ifdef HAVE_CONFIG_H
37 : : #include "config.h"
38 : : #endif
39 : :
40 : : #include "tapdisk-loglimit.h"
41 : : #include "compiler.h"
42 : : #include "list.h"
43 : :
44 : : void
45 : 0 : tapdisk_loglimit_init(td_loglimit_t *rl, int burst, int interval)
46 : : {
47 : 0 : rl->burst = burst;
48 : 0 : rl->interval = interval;
49 : :
50 : 0 : rl->count = 0;
51 : 0 : rl->dropped = 0;
52 : :
53 : 0 : gettimeofday(&rl->ts, NULL);
54 : 0 : }
55 : :
56 : : static void
57 : : timeradd_ms(struct timeval *tv, long ms)
58 : : {
59 : 0 : tv->tv_usec += ms * 1000;
60 [ # # ]: 0 : if (tv->tv_usec > 1000000) {
61 : 0 : tv->tv_sec += tv->tv_usec / 1000000;
62 : 0 : tv->tv_usec %= 1000000;
63 : : }
64 : : }
65 : :
66 : : static void
67 : 0 : tapdisk_loglimit_update(td_loglimit_t *rl, struct timeval *now)
68 : : {
69 : 0 : struct timeval next = rl->ts;
70 : :
71 : 0 : timeradd_ms(&next, rl->interval);
72 : :
73 [ # # ][ # # ]: 0 : if (timercmp(&next, now, <)) {
74 : 0 : rl->count = 0;
75 : 0 : rl->ts = *now;
76 : : }
77 : 0 : }
78 : :
79 : : static void
80 : 0 : tapdisk_loglimit_update_now(td_loglimit_t *rl)
81 : : {
82 : : struct timeval now;
83 : :
84 : 0 : gettimeofday(&now, NULL);
85 : :
86 : 0 : tapdisk_loglimit_update(rl, &now);
87 : 0 : }
88 : :
89 : : int
90 : 0 : tapdisk_loglimit_pass(td_loglimit_t *rl)
91 : : {
92 [ # # ]: 0 : if (!rl->interval)
93 : : return 1; /* unlimited */
94 : :
95 [ # # ]: 0 : if (unlikely(rl->count >= rl->burst)) {
96 : :
97 : 0 : tapdisk_loglimit_update_now(rl);
98 : :
99 [ # # ]: 0 : if (rl->count >= rl->burst) {
100 : 0 : rl->dropped++;
101 : 0 : return 0;
102 : : }
103 : : }
104 : :
105 : 0 : rl->count++;
106 : 0 : return 1;
107 : : }
|