Skip to content

Commit d9c543d

Browse files
committed
feat: logger: decrease binary size
When new rate-limiter log macros were added, the binary size increased by ~150KiB. (mainly the .text section got bigger by ~105KiB) In order to partially mitigate this move duplicated checking logic into a `LogRateLimiter` type and mark that function with `inline(never)` This saves ~70KiB of .text section space. Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
1 parent 53b382a commit d9c543d

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

src/vmm/src/logger/mod.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,8 @@ macro_rules! __log_rate_limited_impl {
108108
$crate::logger::rate_limited::DEFAULT_BURST,
109109
$crate::logger::rate_limited::DEFAULT_REFILL_TIME_MS,
110110
);
111-
static SUPPRESSED: std::sync::atomic::AtomicU64 =
112-
std::sync::atomic::AtomicU64::new(0);
113-
114-
if LIMITER.check() {
115-
let suppressed =
116-
SUPPRESSED.swap(0, std::sync::atomic::Ordering::Relaxed);
117-
if suppressed > 0 {
118-
$crate::warn_unrestricted!(
119-
"{suppressed} messages were suppressed due to rate limiting"
120-
);
121-
}
111+
if LIMITER.check_maybe_suppressed() {
122112
$level_macro!($($arg)+);
123-
} else {
124-
SUPPRESSED.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
125-
$crate::logger::IncMetric::inc(
126-
&$crate::logger::METRICS.logger.rate_limited_log_count,
127-
);
128113
}
129114
}
130115
}};

src/vmm/src/logger/rate_limited.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
//! `LogRateLimiter` instance via a `static`, so flooding one callsite does
77
//! not suppress unrelated log messages.
88
9+
use std::sync::atomic::{AtomicU64, Ordering};
910
use std::sync::{Mutex, OnceLock};
1011

12+
use crate::logger::{IncMetric, METRICS};
1113
use crate::rate_limiter::TokenBucket;
1214

1315
/// Maximum number of messages allowed per refill period.
@@ -25,6 +27,7 @@ pub struct LogRateLimiter {
2527
inner: OnceLock<Mutex<TokenBucket>>,
2628
burst: u64,
2729
refill_time_ms: u64,
30+
suppressed: AtomicU64,
2831
}
2932

3033
impl Default for LogRateLimiter {
@@ -44,6 +47,7 @@ impl LogRateLimiter {
4447
inner: OnceLock::new(),
4548
burst,
4649
refill_time_ms,
50+
suppressed: AtomicU64::new(0),
4751
}
4852
}
4953

@@ -64,6 +68,26 @@ impl LogRateLimiter {
6468
crate::rate_limiter::BucketReduction::Success
6569
)
6670
}
71+
72+
/// Check if log is should be emitted and print a warning if it was
73+
/// suppressed before. Marked to be never inlined since it is called in a
74+
/// lot of macros and would blow up the binary size otherwise.
75+
#[inline(never)]
76+
pub fn check_maybe_suppressed(&self) -> bool {
77+
if self.check() {
78+
let suppressed = self.suppressed.swap(0, Ordering::Relaxed);
79+
if 0 < suppressed {
80+
crate::logger::warn_unrestricted!(
81+
"{suppressed} messages were suppressed due to rate limiting"
82+
);
83+
}
84+
true
85+
} else {
86+
self.suppressed.fetch_add(1, Ordering::Relaxed);
87+
METRICS.logger.rate_limited_log_count.inc();
88+
false
89+
}
90+
}
6791
}
6892

6993
#[cfg(test)]

0 commit comments

Comments
 (0)