From be6d592ded25f8a6ae8fbbe2e4c26f6ec456f9a5 Mon Sep 17 00:00:00 2001 From: Weixie Cui Date: Wed, 18 Mar 2026 12:04:19 +0800 Subject: [PATCH 1/2] Fix off-by-one in stacktrace filter probability when pool is empty. When usage_pct == 0 (pool utilization 0%), the condition 'rand_.Next() % 50 <= usage_pct' incorrectly filtered allocations with probability 1/50 = 2%, because rand % 50 == 0 satisfies <= 0. Change to '<' so that usage_pct == 0 yields 0% filter probability, matching the intended behavior: filter probability scales with pool utilization, and 0% utilization should never filter. --- tcmalloc/guarded_page_allocator.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tcmalloc/guarded_page_allocator.cc b/tcmalloc/guarded_page_allocator.cc index 7febad261..aa6406396 100644 --- a/tcmalloc/guarded_page_allocator.cc +++ b/tcmalloc/guarded_page_allocator.cc @@ -146,7 +146,7 @@ GuardedAllocWithStatus GuardedPageAllocator::TrySample( // proportional to pool utilization, with pool utilization of 50% or more // resulting in always filtering currently covered allocations. const size_t usage_pct = (allocated_pages() * 100) / max_allocated_pages_; - if (rand_.Next() % 50 <= usage_pct) { + if (rand_.Next() % 50 < usage_pct) { // Decay even if the current allocation is filtered, so that we keep // sampling even if we only see the same allocations over and over. stacktrace_filter_.Decay(); From 0bdf87a044f6d209f6cea0ba0c43ab6ec76ba888 Mon Sep 17 00:00:00 2001 From: Weixie Cui Date: Wed, 18 Mar 2026 12:50:03 +0800 Subject: [PATCH 2/2] fix: review --- tcmalloc/guarded_page_allocator.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tcmalloc/guarded_page_allocator.cc b/tcmalloc/guarded_page_allocator.cc index aa6406396..b865cb227 100644 --- a/tcmalloc/guarded_page_allocator.cc +++ b/tcmalloc/guarded_page_allocator.cc @@ -143,7 +143,7 @@ GuardedAllocWithStatus GuardedPageAllocator::TrySample( if (stacktrace_filter_.Contains({stack_trace.stack, stack_trace.depth})) { // The probability that we skip a currently covered allocation scales - // proportional to pool utilization, with pool utilization of 50% or more + // proportional to pool utilization, with pool utilization greater than 50% // resulting in always filtering currently covered allocations. const size_t usage_pct = (allocated_pages() * 100) / max_allocated_pages_; if (rand_.Next() % 50 < usage_pct) {