From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=JXuhj9VR; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTPS id 3FEF85A026E for ; Thu, 26 Mar 2026 00:42:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774482147; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uP7mw1JyHDR2hXFeSClzkBWqKjex6Sjq8h81pHu9v7A=; b=JXuhj9VRLK/b9EG9jLxlMRtEwDeDP2Xv0cv+BPdUTC6PNNhkaSAlAoVZrd2i7r9tkeLtIw z90M6S8KJMPf0on4Ohzb2qn/oRm9X7kSxDZ1a3QWqvFayT0iFIgTOO/m4M80+7SDq1wCXZ 43oyStTakMk7HFd6T3k59Uv1RMgSj94= Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-322-NhYHLyKBOoKNdLWBrB5kRA-1; Wed, 25 Mar 2026 19:42:25 -0400 X-MC-Unique: NhYHLyKBOoKNdLWBrB5kRA-1 X-Mimecast-MFC-AGG-ID: NhYHLyKBOoKNdLWBrB5kRA_1774482145 Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2b0b339b8dbso5664985ad.0 for ; Wed, 25 Mar 2026 16:42:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774482145; x=1775086945; h=content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uP7mw1JyHDR2hXFeSClzkBWqKjex6Sjq8h81pHu9v7A=; b=Lwd6HcyrhHdj2MP+xzFGVFDYGnKMLv1OP/oG2pQ+Ro0eQV11jF7c9jD/i9CdgTwgfR MEMHYKgHcO3+JVsEg3FHJhLsZ5UWgg7rA+yIqJk0OnHCjJlqBgyZGzKcKhz24JOlkzy6 shiqIAoLqxGC5tqGcIz98RdE7713beonYvShI6m2f9Ox1CwYUYd8NfPgHr5VG4HYyeQa xCY1aai1k7GyRx1lV0FNm9RLteWCmyEMgCHR3s4uTvgsPF+eFGUbfGwBzIEVnxR6nXL0 Hhz1z21F6AjY4pvrAOU0JUE+D6pTLjpCS+mdcSufE5Yaoxe+R2ZdEeJBp1iUrqUQZA/s fRLA== X-Gm-Message-State: AOJu0YyLG0xHF9L0crBVlF5QMuMbnpWZdCn8CMkHaVsO+LWtHsOrhzlL ITnN3Z8/qbzZVaCM7Q1o3KQXm+I4h6oDjtDSFafXLRJeJry5aWDzl6YxwaVPN//u7UWAvrd+ZqC rBhkONHCE6WE10a6OyCHiSMu3GUSxenEt39IYsbFPevuqSfEGaWm0GQ== X-Gm-Gg: ATEYQzwr7Ut8NuICCcHRzWJcL2duti3ZgzcYx7xDliqwEOxoyF0hKFJ91QURVNUPWba YjrmAjN8TvVSggx94MJ6Z65wYVgbrL0L/8pe+a4cDZRcNH/SgZpJXZICA9jinCRSNl/HcTULsI5 Kmt/KnM9dcoY3UJvIQ/L5Ei4sdZBc1DIyWshYvRYxmyLfOTrHUSLQFqRxf0yc4beq9rDsihqFFP +2pd6DZhWnk64T2FJoIN3xKv56kbMVMvZHcNJiTTpT1LkYju5It4x21Addwsdx+A49H2DKmoKvk DQjcu+KyV1eLWOMNczJxSZXNgeGtr6utvFzD7Av7OslVkCP3j6Jv9YXReOBOMqgwzqteGFdwHQZ EDvVwPD3N4VMhWqUSig== X-Received: by 2002:a17:902:d586:b0:2ae:4555:479e with SMTP id d9443c01a7336-2b0b09ead1dmr54699665ad.18.1774482144732; Wed, 25 Mar 2026 16:42:24 -0700 (PDT) X-Received: by 2002:a17:902:d586:b0:2ae:4555:479e with SMTP id d9443c01a7336-2b0b09ead1dmr54699515ad.18.1774482144133; Wed, 25 Mar 2026 16:42:24 -0700 (PDT) Received: from zatzit ([2001:4479:e502:9600:40ff:8507:d0b5:9116]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b0bc911530sm11975305ad.78.2026.03.25.16.42.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Mar 2026 16:42:22 -0700 (PDT) Date: Thu, 26 Mar 2026 10:42:17 +1100 From: David Gibson To: Anshu Kumari Subject: Re: [PATCH v3] log: Add rate-limiting macros for log messages Message-ID: <20260326104217.75531132@zatzit> In-Reply-To: <20260325131120.299924-2-anskuma@redhat.com> References: <20260325131120.299924-2-anskuma@redhat.com> Organization: Red Hat X-Mailer: Claws Mail 4.3.1 (GTK 3.24.51; x86_64-redhat-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: gs1R_6vhZ-ZzaOYiAEIzDTuI0JsMutiyrBmSkqgaU3A_1774482145 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-MailFrom: dgibson@redhat.com X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation Message-ID-Hash: WTGPOP6XXAU243IYIO5D2HCFDDOEYAFR X-Message-ID-Hash: WTGPOP6XXAU243IYIO5D2HCFDDOEYAFR X-Mailman-Approved-At: Thu, 26 Mar 2026 05:29:16 +0100 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: On Wed, 25 Mar 2026 18:41:21 +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. > > 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. > > Signed-off-by: Anshu Kumari Reviewed-by: David Gibson For future reference, I prefer to handle upstream reviews at my upstream email address rather than my Red Hat address. > --- > v3: > - Print suppressed count after the message, not before > - Add suppression notice when burst limit is hit > - Reverse Christmas tree variable ordering > - Fix tab/space alignment in macro > - swapped LOG_RATELIMIT_INTERVAL and LOG_RATELIMIT_BURST location > > v2: > - Use _ suffix for macro variables instead of prefix > - Remove intv parameter from convenience wrappers > --- > log.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 45 insertions(+) > > diff --git a/log.h b/log.h > index 6ceb686..8e6d65e 100644 > --- a/log.h > +++ b/log.h > @@ -48,6 +48,51 @@ void logmsg_perror(int pri, const char *format, ...) > passt_exit(EXIT_FAILURE); \ > } while (0) > > +#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_ = (now)->tv_sec; \ > + rl_printed_ = 0; \ > + } \ > + \ > + if (rl_printed_ < LOG_RATELIMIT_BURST) { \ > + fn(__VA_ARGS__); \ > + if (rl_suppressed_) { \ > + fn("(suppressed %u similar messages)", \ > + rl_suppressed_); \ > + rl_suppressed_ = 0; \ > + } \ > + rl_printed_++; \ > + } else if (rl_printed_ == LOG_RATELIMIT_BURST) { \ > + fn("(suppressing further similar messages)"); \ > + rl_printed_++; \ > + rl_suppressed_++; \ > + } 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; > -- > 2.53.0 > -- David Gibson (He or They) Principal Software Engineer, Virtualization, Red Hat