Merge pull request #13600 from keszybz/ratelimit

Clean up ratelimit functions and disable /dev/kmsg ratelimit
This commit is contained in:
Lennart Poettering
2019-09-23 12:59:09 +02:00
committed by GitHub
29 changed files with 155 additions and 149 deletions
+13
View File
@@ -29,6 +29,7 @@
#include "parse-util.h"
#include "proc-cmdline.h"
#include "process-util.h"
#include "ratelimit.h"
#include "signal-util.h"
#include "socket-util.h"
#include "stdio-util.h"
@@ -459,6 +460,15 @@ static int write_to_kmsg(
const char *func,
const char *buffer) {
/* Set a ratelimit on the amount of messages logged to /dev/kmsg. This is mostly supposed to be a
* safety catch for the case where start indiscriminately logging in a loop. It will not catch cases
* where we log excessively, but not in a tight loop.
*
* Note that this ratelimit is per-emitter, so we might still overwhelm /dev/kmsg with multiple
* loggers.
*/
static thread_local RateLimit ratelimit = { 5 * USEC_PER_SEC, 200 };
char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
struct iovec iovec[5] = {};
@@ -466,6 +476,9 @@ static int write_to_kmsg(
if (kmsg_fd < 0)
return 0;
if (!ratelimit_below(&ratelimit))
return 0;
xsprintf(header_priority, "<%i>", level);
xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
+6 -26
View File
@@ -7,34 +7,14 @@
#include "util.h"
typedef struct RateLimit {
usec_t interval;
usec_t begin;
unsigned burst;
usec_t interval; /* Keep those two fields first so they can be initialized easily: */
unsigned burst; /* RateLimit rl = { INTERVAL, BURST }; */
unsigned num;
usec_t begin;
} RateLimit;
#define RATELIMIT_DEFINE(_name, _interval, _burst) \
RateLimit _name = { \
.interval = (_interval), \
.burst = (_burst), \
.num = 0, \
.begin = 0 \
}
#define RATELIMIT_INIT(v, _interval, _burst) \
do { \
RateLimit *_r = &(v); \
_r->interval = (_interval); \
_r->burst = (_burst); \
_r->num = 0; \
_r->begin = 0; \
} while (false)
#define RATELIMIT_RESET(v) \
do { \
RateLimit *_r = &(v); \
_r->num = 0; \
_r->begin = 0; \
} while (false)
static inline void ratelimit_reset(RateLimit *rl) {
rl->num = rl->begin = 0;
}
bool ratelimit_below(RateLimit *r);