Skip to content

Support RSS trimming for idle bthread stacks - #3668

Merged
chenBright merged 2 commits into
apache:masterfrom
chenBright:bthread_stack_trim_simple
Oct 29, 2026
Merged

chenBright merged 2 commits into
apache:masterfrom
chenBright:bthread_stack_trim_simple

Conversation

@chenBright

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve

Problem Summary:

Traffic spikes can leave tens of thousands of bthread stacks cached in ObjectPool.
After concurrency drops, their resident pages remain allocated, keeping process
RSS high even when only a few hundred stacks are needed.

What is changed and the side effects?

Changed:

  • Add a generic process_idle_objects API to ObjectPool. It processes idle
    global chunks outside the pool lock and retains processed chunks for reuse
    after regular free chunks.
  • Reclaim idle SMALL, NORMAL, and LARGE stack pages with MADV_DONTNEED,
    preserving and guard pages. Rebuild the context on reuse, including after partial
    reclamation failures.
  • Start one background thread named bthread_trim during bthread initialization and
    expose the bthread_stack_trimmed_count metric.

Single-run measurements with 50,000 stacks and 8 workers showed the following
process RSS changes after synchronous trimming:

Stack type Before trimming After trimming Reduction
SMALL 1025.66 MiB 51.27 MiB 95.00%
NORMAL 3564.87 MiB 52.02 MiB 98.54%
LARGE 13136.81 MiB 53.37 MiB 99.59%

Side effects:

  • Performance effects:

  • Breaking backward compatibility:


Check List:

@chenBright
chenBright force-pushed the bthread_stack_trim_simple branch 2 times, most recently from c8d4e9a to 42afe63 Compare October 26, 2026 15:38
@chenBright
chenBright force-pushed the bthread_stack_trim_simple branch from 42afe63 to df3d2d7 Compare October 27, 2026 02:25
@chenBright
chenBright requested review from wasphin and wwbmmm October 27, 2026 03:06
Comment thread src/bthread/stack.cpp Resolved
@chenBright
chenBright requested a review from wasphin October 29, 2026 04:30

@wasphin wasphin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chenBright
chenBright merged commit 2f7775b into apache:master Oct 29, 2026
29 checks passed
@chenBright
chenBright deleted the bthread_stack_trim_simple branch October 29, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants