]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <string.h> | |
5 | ||
6 | #include "alloc-util.h" | |
7 | #include "hashmap.h" | |
8 | #include "journald-rate-limit.h" | |
9 | #include "list.h" | |
10 | #include "random-util.h" | |
11 | #include "string-util.h" | |
12 | #include "util.h" | |
13 | ||
14 | #define POOLS_MAX 5 | |
15 | #define BUCKETS_MAX 127 | |
16 | #define GROUPS_MAX 2047 | |
17 | ||
18 | static const int priority_map[] = { | |
19 | [LOG_EMERG] = 0, | |
20 | [LOG_ALERT] = 0, | |
21 | [LOG_CRIT] = 0, | |
22 | [LOG_ERR] = 1, | |
23 | [LOG_WARNING] = 2, | |
24 | [LOG_NOTICE] = 3, | |
25 | [LOG_INFO] = 3, | |
26 | [LOG_DEBUG] = 4 | |
27 | }; | |
28 | ||
29 | typedef struct JournalRateLimitPool JournalRateLimitPool; | |
30 | typedef struct JournalRateLimitGroup JournalRateLimitGroup; | |
31 | ||
32 | struct JournalRateLimitPool { | |
33 | usec_t begin; | |
34 | unsigned num; | |
35 | unsigned suppressed; | |
36 | }; | |
37 | ||
38 | struct JournalRateLimitGroup { | |
39 | JournalRateLimit *parent; | |
40 | ||
41 | char *id; | |
42 | JournalRateLimitPool pools[POOLS_MAX]; | |
43 | uint64_t hash; | |
44 | ||
45 | LIST_FIELDS(JournalRateLimitGroup, bucket); | |
46 | LIST_FIELDS(JournalRateLimitGroup, lru); | |
47 | }; | |
48 | ||
49 | struct JournalRateLimit { | |
50 | usec_t interval; | |
51 | unsigned burst; | |
52 | ||
53 | JournalRateLimitGroup* buckets[BUCKETS_MAX]; | |
54 | JournalRateLimitGroup *lru, *lru_tail; | |
55 | ||
56 | unsigned n_groups; | |
57 | ||
58 | uint8_t hash_key[16]; | |
59 | }; | |
60 | ||
61 | JournalRateLimit *journal_rate_limit_new(usec_t interval, unsigned burst) { | |
62 | JournalRateLimit *r; | |
63 | ||
64 | assert(interval > 0 || burst == 0); | |
65 | ||
66 | r = new0(JournalRateLimit, 1); | |
67 | if (!r) | |
68 | return NULL; | |
69 | ||
70 | r->interval = interval; | |
71 | r->burst = burst; | |
72 | ||
73 | random_bytes(r->hash_key, sizeof(r->hash_key)); | |
74 | ||
75 | return r; | |
76 | } | |
77 | ||
78 | static void journal_rate_limit_group_free(JournalRateLimitGroup *g) { | |
79 | assert(g); | |
80 | ||
81 | if (g->parent) { | |
82 | assert(g->parent->n_groups > 0); | |
83 | ||
84 | if (g->parent->lru_tail == g) | |
85 | g->parent->lru_tail = g->lru_prev; | |
86 | ||
87 | LIST_REMOVE(lru, g->parent->lru, g); | |
88 | LIST_REMOVE(bucket, g->parent->buckets[g->hash % BUCKETS_MAX], g); | |
89 | ||
90 | g->parent->n_groups--; | |
91 | } | |
92 | ||
93 | free(g->id); | |
94 | free(g); | |
95 | } | |
96 | ||
97 | void journal_rate_limit_free(JournalRateLimit *r) { | |
98 | assert(r); | |
99 | ||
100 | while (r->lru) | |
101 | journal_rate_limit_group_free(r->lru); | |
102 | ||
103 | free(r); | |
104 | } | |
105 | ||
106 | _pure_ static bool journal_rate_limit_group_expired(JournalRateLimitGroup *g, usec_t ts) { | |
107 | unsigned i; | |
108 | ||
109 | assert(g); | |
110 | ||
111 | for (i = 0; i < POOLS_MAX; i++) | |
112 | if (g->pools[i].begin + g->parent->interval >= ts) | |
113 | return false; | |
114 | ||
115 | return true; | |
116 | } | |
117 | ||
118 | static void journal_rate_limit_vacuum(JournalRateLimit *r, usec_t ts) { | |
119 | assert(r); | |
120 | ||
121 | /* Makes room for at least one new item, but drop all | |
122 | * expored items too. */ | |
123 | ||
124 | while (r->n_groups >= GROUPS_MAX || | |
125 | (r->lru_tail && journal_rate_limit_group_expired(r->lru_tail, ts))) | |
126 | journal_rate_limit_group_free(r->lru_tail); | |
127 | } | |
128 | ||
129 | static JournalRateLimitGroup* journal_rate_limit_group_new(JournalRateLimit *r, const char *id, usec_t ts) { | |
130 | JournalRateLimitGroup *g; | |
131 | struct siphash state; | |
132 | ||
133 | assert(r); | |
134 | assert(id); | |
135 | ||
136 | g = new0(JournalRateLimitGroup, 1); | |
137 | if (!g) | |
138 | return NULL; | |
139 | ||
140 | g->id = strdup(id); | |
141 | if (!g->id) | |
142 | goto fail; | |
143 | ||
144 | siphash24_init(&state, r->hash_key); | |
145 | string_hash_func(g->id, &state); | |
146 | g->hash = siphash24_finalize(&state); | |
147 | ||
148 | journal_rate_limit_vacuum(r, ts); | |
149 | ||
150 | LIST_PREPEND(bucket, r->buckets[g->hash % BUCKETS_MAX], g); | |
151 | LIST_PREPEND(lru, r->lru, g); | |
152 | if (!g->lru_next) | |
153 | r->lru_tail = g; | |
154 | r->n_groups++; | |
155 | ||
156 | g->parent = r; | |
157 | return g; | |
158 | ||
159 | fail: | |
160 | journal_rate_limit_group_free(g); | |
161 | return NULL; | |
162 | } | |
163 | ||
164 | static unsigned burst_modulate(unsigned burst, uint64_t available) { | |
165 | unsigned k; | |
166 | ||
167 | /* Modulates the burst rate a bit with the amount of available | |
168 | * disk space */ | |
169 | ||
170 | k = u64log2(available); | |
171 | ||
172 | /* 1MB */ | |
173 | if (k <= 20) | |
174 | return burst; | |
175 | ||
176 | burst = (burst * (k-16)) / 4; | |
177 | ||
178 | /* | |
179 | * Example: | |
180 | * | |
181 | * <= 1MB = rate * 1 | |
182 | * 16MB = rate * 2 | |
183 | * 256MB = rate * 3 | |
184 | * 4GB = rate * 4 | |
185 | * 64GB = rate * 5 | |
186 | * 1TB = rate * 6 | |
187 | */ | |
188 | ||
189 | return burst; | |
190 | } | |
191 | ||
192 | int journal_rate_limit_test(JournalRateLimit *r, const char *id, int priority, uint64_t available) { | |
193 | uint64_t h; | |
194 | JournalRateLimitGroup *g; | |
195 | JournalRateLimitPool *p; | |
196 | struct siphash state; | |
197 | unsigned burst; | |
198 | usec_t ts; | |
199 | ||
200 | assert(id); | |
201 | ||
202 | /* Returns: | |
203 | * | |
204 | * 0 → the log message shall be suppressed, | |
205 | * 1 + n → the log message shall be permitted, and n messages were dropped from the peer before | |
206 | * < 0 → error | |
207 | */ | |
208 | ||
209 | if (!r) | |
210 | return 1; | |
211 | ||
212 | if (r->interval == 0 || r->burst == 0) | |
213 | return 1; | |
214 | ||
215 | burst = burst_modulate(r->burst, available); | |
216 | ||
217 | ts = now(CLOCK_MONOTONIC); | |
218 | ||
219 | siphash24_init(&state, r->hash_key); | |
220 | string_hash_func(id, &state); | |
221 | h = siphash24_finalize(&state); | |
222 | g = r->buckets[h % BUCKETS_MAX]; | |
223 | ||
224 | LIST_FOREACH(bucket, g, g) | |
225 | if (streq(g->id, id)) | |
226 | break; | |
227 | ||
228 | if (!g) { | |
229 | g = journal_rate_limit_group_new(r, id, ts); | |
230 | if (!g) | |
231 | return -ENOMEM; | |
232 | } | |
233 | ||
234 | p = &g->pools[priority_map[priority]]; | |
235 | ||
236 | if (p->begin <= 0) { | |
237 | p->suppressed = 0; | |
238 | p->num = 1; | |
239 | p->begin = ts; | |
240 | return 1; | |
241 | } | |
242 | ||
243 | if (p->begin + r->interval < ts) { | |
244 | unsigned s; | |
245 | ||
246 | s = p->suppressed; | |
247 | p->suppressed = 0; | |
248 | p->num = 1; | |
249 | p->begin = ts; | |
250 | ||
251 | return 1 + s; | |
252 | } | |
253 | ||
254 | if (p->num < burst) { | |
255 | p->num++; | |
256 | return 1; | |
257 | } | |
258 | ||
259 | p->suppressed++; | |
260 | return 0; | |
261 | } |