From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202602 header.b=FDtlrFM1; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 5ECE65A026D for ; Fri, 27 Mar 2026 01:17:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1774570659; bh=NBSPMhg0jua/+lQbuw3SK0bkG8Gizs5aAk8us3to/nY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=FDtlrFM1dwn7i7b7apaSecIpk96erkgGF0MFWeUk1u5nv/58cwy7zbbBRdKSsI5jv ECMtXSZemKYzBIGHOFyDbWMDx33RP65RunELwb4QqBeWQvXEHK8TyoFT45oMA/QUOJ eqzPCEJfTd58lDzgKFkMuSl0OOdo5UiCy2AZAUCQZmtHqywjFeeD9T0PTQ+Zp9HiP8 a+odxX+uQ7vmoQPHSH2jeBbo7me+Z3EpRHVufRCLMmjnEGhfqxtcUgMtA9/NaU42GV iPYu3fHTGgI7qucFL1WZp9P9vs+U25w3TOlbUWykkp8rSJCkEoVe1FWK6t5SalR7Gw eRZBm5hN53Z0Q== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fhh6q51rQz4wSw; Fri, 27 Mar 2026 11:17:39 +1100 (AEDT) Date: Fri, 27 Mar 2026 11:17:34 +1100 From: David Gibson To: Anshu Kumari Subject: Re: [PATCH v4] log: Add rate-limiting macros for log messages Message-ID: References: <20260326095019.387307-2-anskuma@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="6E3AkTkEy6u7VKCJ" Content-Disposition: inline In-Reply-To: <20260326095019.387307-2-anskuma@redhat.com> Message-ID-Hash: TXOJN6BDWT23YW222YOYZHPSJ75J3CKB X-Message-ID-Hash: TXOJN6BDWT23YW222YOYZHPSJ75J3CKB X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, sbrivio@redhat.com, lvivier@redhat.com X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --6E3AkTkEy6u7VKCJ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Mar 26, 2026 at 03:20:20PM +0530, Anshu Kumari wrote: > Currently, some log messages that would be useful at info or warn level > are kept at debug level because there is no way to throttle them, and a > guest could otherwise flood the host logs. >=20 > Add a logmsg_ratelimit() macro that uses per-call-site static variables > to independently track each call site's rate. It allows up to > LOG_RATELIMIT_BURST (5) messages per LOG_RATELIMIT_INTERVAL (1 second) > window, then prints a suppression notice. When a new window opens and > messages were suppressed, the count is reported after the next allowed > message. >=20 > Link: https://bugs.passt.top/show_bug.cgi?id=3D134 > Signed-off-by: Anshu Kumari Reviewed-by: David Gibson > --- > v4: > - Print suppression notice immediately after the last allowed message, > not on the next call, to avoid confusion with unrelated messages > in between. > - Add Link: tag before Signed-off-by > --- > log.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 44 insertions(+) >=20 > diff --git a/log.h b/log.h > index 6ceb686..dbab006 100644 > --- a/log.h > +++ b/log.h > @@ -48,6 +48,50 @@ void logmsg_perror(int pri, const char *format, ...) > passt_exit(EXIT_FAILURE); \ > } while (0) > =20 > +#define LOG_RATELIMIT_INTERVAL 1 /* Default rate limit window in seconds= */ > +#define LOG_RATELIMIT_BURST 5 /* Max messages per window per call site */ > + > +/** > + * logmsg_ratelimit() - Log a message with rate limiting > + * @fn: Logging function name (e.g. warn, info, debug) > + * @now: Current timestamp > + */ > +#define logmsg_ratelimit(fn, now, ...) \ > + do { \ > + static unsigned int rl_suppressed_; \ > + static unsigned int rl_printed_; \ > + static time_t rl_last_; \ > + \ > + if ((now)->tv_sec - rl_last_ > LOG_RATELIMIT_INTERVAL) {\ > + rl_last_ =3D (now)->tv_sec; \ > + rl_printed_ =3D 0; \ > + } \ > + \ > + if (rl_printed_ < LOG_RATELIMIT_BURST) { \ > + fn(__VA_ARGS__); \ > + if (rl_suppressed_) { \ > + fn("(suppressed %u similar messages)", \ > + rl_suppressed_); \ > + rl_suppressed_ =3D 0; \ > + } \ > + rl_printed_++; \ > + if (rl_printed_ =3D=3D LOG_RATELIMIT_BURST) \ > + fn("(suppressing further similar" \ > + " messages)"); \ > + } else { \ > + rl_suppressed_++; \ > + } \ > + } while (0) > + > +#define err_ratelimit(now, ...) \ > + logmsg_ratelimit(err, now, __VA_ARGS__) > +#define warn_ratelimit(now, ...) \ > + logmsg_ratelimit(warn, now, __VA_ARGS__) > +#define info_ratelimit(now, ...) \ > + logmsg_ratelimit(info, now, __VA_ARGS__) > +#define debug_ratelimit(now, ...) \ > + logmsg_ratelimit(debug, now, __VA_ARGS__) > + > extern int log_file; > extern int log_trace; > extern bool log_conf_parsed; > --=20 > 2.53.0 >=20 --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --6E3AkTkEy6u7VKCJ Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmnFzJIACgkQzQJF27ox 2GdIYg//T1Q9DGWRonWhhUoGyhx0WSRBUaLxBkvU7a16nvZxpwFwvBVMPT/Z0LnU jhOW57ccN4DGoaQ8xgGPCX6OffkG24E6l3ThB7isOmZCD6WtuoG7wKi9tvFvLc6j +AUKgzi98LbbDQY6i7BdeXMIldd1catR3GgOIBsc6OA2IAwo67pjm0JX7gEPuF8l KwCRcxCa8t4aPHtfg5cG1gHENYY2gyJUUhy57rEGmckuBPyFXfrwbG4LseKk+rnU bXUEChgu7m9heaCaPbQJIw0C7vcB5teZwQ3P+3h6fWv+dn1QGoGVskVx2X0kuG3U FiOvpfFk3LtVsS8/jtp4Cee5Z9ybKgQb923VNzG8nVx2N4vxokt4Upx0jY3XwVPn sFGyHfK2CQ5T+jlC51f1DfspFYebvxiRUcWJOjzkDVk3IeEfDaRmc/RK3QMh5ggF 8JhsbnlT7HViCSBOvSYlw/k6OZt2r3JYuHUcsjA40hTkxiIE9K/twEudz8VEw0tE +BzF6lVMUxvwLH5lvzeZjljevHK9OOSMiKXK/vduS9awnoY0vC4PhWBABTXhVkBj 0NTMxCsF4gVp+GOMtIWWcNp3T0MAHuuII9Kd0+xtTTvHumwWfUIKLRUAe8qH4Mvh 9tGiHFhDGlGhggWOSzAkJgZHKcolj3CnbnC2MvkVsnPCFO6HxLw= =D//l -----END PGP SIGNATURE----- --6E3AkTkEy6u7VKCJ--