Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions src/vmm/src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,8 @@ macro_rules! __log_rate_limited_impl {
$crate::logger::rate_limited::DEFAULT_BURST,
$crate::logger::rate_limited::DEFAULT_REFILL_TIME_MS,
);
static SUPPRESSED: std::sync::atomic::AtomicU64 =
std::sync::atomic::AtomicU64::new(0);

if LIMITER.check() {
let suppressed =
SUPPRESSED.swap(0, std::sync::atomic::Ordering::Relaxed);
if suppressed > 0 {
$crate::warn_unrestricted!(
"{suppressed} messages were suppressed due to rate limiting"
);
}
if LIMITER.check_maybe_suppressed() {
$level_macro!($($arg)+);
} else {
SUPPRESSED.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
$crate::logger::IncMetric::inc(
&$crate::logger::METRICS.logger.rate_limited_log_count,
);
}
}
}};
Expand Down
24 changes: 24 additions & 0 deletions src/vmm/src/logger/rate_limited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
//! `LogRateLimiter` instance via a `static`, so flooding one callsite does
//! not suppress unrelated log messages.

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Mutex, OnceLock};

use crate::logger::{IncMetric, METRICS};
use crate::rate_limiter::TokenBucket;

/// Maximum number of messages allowed per refill period.
Expand All @@ -25,6 +27,7 @@ pub struct LogRateLimiter {
inner: OnceLock<Mutex<TokenBucket>>,
burst: u64,
refill_time_ms: u64,
suppressed: AtomicU64,
}

impl Default for LogRateLimiter {
Expand All @@ -44,6 +47,7 @@ impl LogRateLimiter {
inner: OnceLock::new(),
burst,
refill_time_ms,
suppressed: AtomicU64::new(0),
}
}

Expand All @@ -64,6 +68,26 @@ impl LogRateLimiter {
crate::rate_limiter::BucketReduction::Success
)
}

/// Check if log is should be emitted and print a warning if it was
/// suppressed before. Marked to be never inlined since it is called in a
/// lot of macros and would blow up the binary size otherwise.
#[inline(never)]
pub fn check_maybe_suppressed(&self) -> bool {
if self.check() {
let suppressed = self.suppressed.swap(0, Ordering::Relaxed);
if 0 < suppressed {
crate::logger::warn_unrestricted!(
"{suppressed} messages were suppressed due to rate limiting"
);
}
true
} else {
self.suppressed.fetch_add(1, Ordering::Relaxed);
METRICS.logger.rate_limited_log_count.inc();
false
}
}
}

#[cfg(test)]
Expand Down
Loading