| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <syslog.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "hashmap.h" |
| 7 |
#include "journald-rate-limit.h" |
| 8 |
#include "logarithm.h" |
| 9 |
#include "time-util.h" |
| 10 |
|
| 11 |
#define POOLS_MAX 5 |
| 12 |
#define GROUPS_MAX 2047 |
| 13 |
|
| 14 |
static const int priority_map[] = { |
| 15 |
[LOG_EMERG] = 0, |
| 16 |
[LOG_ALERT] = 0, |
| 17 |
[LOG_CRIT] = 0, |
| 18 |
[LOG_ERR] = 1, |
| 19 |
[LOG_WARNING] = 2, |
| 20 |
[LOG_NOTICE] = 3, |
| 21 |
[LOG_INFO] = 3, |
| 22 |
[LOG_DEBUG] = 4, |
| 23 |
}; |
| 24 |
|
| 25 |
typedef struct JournalRateLimitPool { |
| 26 |
usec_t begin; |
| 27 |
unsigned num; |
| 28 |
unsigned suppressed; |
| 29 |
} JournalRateLimitPool; |
| 30 |
|
| 31 |
typedef struct JournalRateLimitGroup { |
| 32 |
OrderedHashmap *groups_by_id; |
| 33 |
|
| 34 |
char *id; |
| 35 |
|
| 36 |
/* Interval is stored to keep track of when the group expires */ |
| 37 |
usec_t interval; |
| 38 |
|
| 39 |
JournalRateLimitPool pools[POOLS_MAX]; |
| 40 |
} JournalRateLimitGroup; |
| 41 |
|
| 42 |
static JournalRateLimitGroup* journal_ratelimit_group_free(JournalRateLimitGroup *g) { |
| 43 |
if (!g) |
| 44 |
return NULL; |
| 45 |
|
| 46 |
if (g->groups_by_id && g->id) |
| 47 |
/* The group is already removed from the hashmap when this is called from the |
| 48 |
* destructor of the hashmap. Hence, do not check the return value here. */ |
| 49 |
ordered_hashmap_remove_value(g->groups_by_id, g->id, g); |
| 50 |
|
| 51 |
free(g->id); |
| 52 |
return mfree(g); |
| 53 |
} |
| 54 |
|
| 55 |
DEFINE_TRIVIAL_CLEANUP_FUNC(JournalRateLimitGroup*, journal_ratelimit_group_free); |
| 56 |
|
| 57 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 58 |
journal_ratelimit_group_hash_ops, |
| 59 |
char, |
| 60 |
string_hash_func, |
| 61 |
string_compare_func, |
| 62 |
JournalRateLimitGroup, |
| 63 |
journal_ratelimit_group_free); |
| 64 |
|
| 65 |
static bool journal_ratelimit_group_expired(JournalRateLimitGroup *g, usec_t ts) { |
| 66 |
assert(g); |
| 67 |
|
| 68 |
FOREACH_ELEMENT(p, g->pools) |
| 69 |
if (usec_add(p->begin, g->interval) >= ts) |
| 70 |
return false; |
| 71 |
|
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
static void journal_ratelimit_vacuum(OrderedHashmap *groups_by_id, usec_t ts) { |
| 76 |
|
| 77 |
/* Makes room for at least one new item, but drop all expired items too. */ |
| 78 |
|
| 79 |
while (ordered_hashmap_size(groups_by_id) >= GROUPS_MAX) |
| 80 |
journal_ratelimit_group_free(ordered_hashmap_first(groups_by_id)); |
| 81 |
|
| 82 |
JournalRateLimitGroup *g; |
| 83 |
while ((g = ordered_hashmap_first(groups_by_id)) && journal_ratelimit_group_expired(g, ts)) |
| 84 |
journal_ratelimit_group_free(g); |
| 85 |
} |
| 86 |
|
| 87 |
static int journal_ratelimit_group_new( |
| 88 |
OrderedHashmap **groups_by_id, |
| 89 |
const char *id, |
| 90 |
usec_t interval, |
| 91 |
usec_t ts, |
| 92 |
JournalRateLimitGroup **ret) { |
| 93 |
|
| 94 |
_cleanup_(journal_ratelimit_group_freep) JournalRateLimitGroup *g = NULL; |
| 95 |
int r; |
| 96 |
|
| 97 |
assert(groups_by_id); |
| 98 |
assert(id); |
| 99 |
assert(ret); |
| 100 |
|
| 101 |
g = new(JournalRateLimitGroup, 1); |
| 102 |
if (!g) |
| 103 |
return -ENOMEM; |
| 104 |
|
| 105 |
*g = (JournalRateLimitGroup) { |
| 106 |
.id = strdup(id), |
| 107 |
.interval = interval, |
| 108 |
}; |
| 109 |
if (!g->id) |
| 110 |
return -ENOMEM; |
| 111 |
|
| 112 |
journal_ratelimit_vacuum(*groups_by_id, ts); |
| 113 |
|
| 114 |
r = ordered_hashmap_ensure_put(groups_by_id, &journal_ratelimit_group_hash_ops, g->id, g); |
| 115 |
if (r < 0) |
| 116 |
return r; |
| 117 |
assert(r > 0); |
| 118 |
|
| 119 |
g->groups_by_id = *groups_by_id; |
| 120 |
|
| 121 |
*ret = TAKE_PTR(g); |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
static int journal_ratelimit_group_acquire( |
| 126 |
OrderedHashmap **groups_by_id, |
| 127 |
const char *id, |
| 128 |
usec_t interval, |
| 129 |
usec_t ts, |
| 130 |
JournalRateLimitGroup **ret) { |
| 131 |
|
| 132 |
JournalRateLimitGroup *g; |
| 133 |
|
| 134 |
assert(groups_by_id); |
| 135 |
assert(id); |
| 136 |
assert(ret); |
| 137 |
|
| 138 |
g = ordered_hashmap_get(*groups_by_id, id); |
| 139 |
if (!g) |
| 140 |
return journal_ratelimit_group_new(groups_by_id, id, interval, ts, ret); |
| 141 |
|
| 142 |
g->interval = interval; |
| 143 |
|
| 144 |
*ret = g; |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
static unsigned burst_modulate(unsigned burst, uint64_t available) { |
| 149 |
unsigned k; |
| 150 |
|
| 151 |
/* Modulates the burst rate a bit with the amount of available |
| 152 |
* disk space */ |
| 153 |
|
| 154 |
k = log2u64(available); |
| 155 |
|
| 156 |
/* 1MB */ |
| 157 |
if (k <= 20) |
| 158 |
return burst; |
| 159 |
|
| 160 |
burst = (burst * (k-16)) / 4; |
| 161 |
|
| 162 |
/* |
| 163 |
* Example: |
| 164 |
* |
| 165 |
* <= 1MB = rate * 1 |
| 166 |
* 16MB = rate * 2 |
| 167 |
* 256MB = rate * 3 |
| 168 |
* 4GB = rate * 4 |
| 169 |
* 64GB = rate * 5 |
| 170 |
* 1TB = rate * 6 |
| 171 |
*/ |
| 172 |
|
| 173 |
return burst; |
| 174 |
} |
| 175 |
|
| 176 |
int journal_ratelimit_test( |
| 177 |
OrderedHashmap **groups_by_id, |
| 178 |
const char *id, |
| 179 |
usec_t rl_interval, |
| 180 |
unsigned rl_burst, |
| 181 |
int priority, |
| 182 |
uint64_t available) { |
| 183 |
|
| 184 |
JournalRateLimitGroup *g; |
| 185 |
JournalRateLimitPool *p; |
| 186 |
unsigned burst; |
| 187 |
usec_t ts; |
| 188 |
int r; |
| 189 |
|
| 190 |
assert(groups_by_id); |
| 191 |
assert(id); |
| 192 |
|
| 193 |
/* Returns: |
| 194 |
* |
| 195 |
* 0 → the log message shall be suppressed, |
| 196 |
* 1 + n → the log message shall be permitted, and n messages were dropped from the peer before |
| 197 |
* < 0 → error |
| 198 |
*/ |
| 199 |
|
| 200 |
ts = now(CLOCK_MONOTONIC); |
| 201 |
|
| 202 |
r = journal_ratelimit_group_acquire(groups_by_id, id, rl_interval, ts, &g); |
| 203 |
if (r < 0) |
| 204 |
return r; |
| 205 |
|
| 206 |
if (rl_interval == 0 || rl_burst == 0) |
| 207 |
return 1; |
| 208 |
|
| 209 |
burst = burst_modulate(rl_burst, available); |
| 210 |
|
| 211 |
p = &g->pools[priority_map[priority]]; |
| 212 |
|
| 213 |
if (p->begin <= 0) { |
| 214 |
p->suppressed = 0; |
| 215 |
p->num = 1; |
| 216 |
p->begin = ts; |
| 217 |
return 1; |
| 218 |
} |
| 219 |
|
| 220 |
if (usec_add(p->begin, rl_interval) < ts) { |
| 221 |
unsigned s; |
| 222 |
|
| 223 |
s = p->suppressed; |
| 224 |
p->suppressed = 0; |
| 225 |
p->num = 1; |
| 226 |
p->begin = ts; |
| 227 |
|
| 228 |
return 1 + s; |
| 229 |
} |
| 230 |
|
| 231 |
if (p->num < burst) { |
| 232 |
p->num++; |
| 233 |
return 1; |
| 234 |
} |
| 235 |
|
| 236 |
p->suppressed++; |
| 237 |
return 0; |
| 238 |
} |