Skip to content

Share memory reduction logic and enable compaction for WSLC#40376

Open
benhillis wants to merge 3 commits intomasterfrom
user/benhill/wslc_compaction
Open

Share memory reduction logic and enable compaction for WSLC#40376
benhillis wants to merge 3 commits intomasterfrom
user/benhill/wslc_compaction

Conversation

@benhillis
Copy link
Copy Markdown
Member

@benhillis benhillis commented May 1, 2026

Summary

This PR refactors the Linux init memory-reduction logic so it can be shared between the WSL2 init (main.cpp) and WSLC init (WSLCInit.cpp), and enables idle-time compaction + cache dropping for WSLC VMs.

Changes

  • Extracts the memory reduction thread logic into a shared StartMemoryReductionThread(LX_MINI_INIT_MEMORY_RECLAIM_MODE) entry point in util.cpp.
  • Moves GetUserCpuTime and GetMemoryInUse helpers to util.cpp as file-local (static) functions.
  • WSL2 init calls StartMemoryReductionThread directly instead of going through ConfigureMemoryReduction().
  • Starts the memory reduction thread in WSLC init with drop-caches + compaction behavior when idle.
  • Uses LX_MINI_INIT_MEMORY_RECLAIM_MODE wire enum directly rather than introducing a separate enum.

Notes

  • Memory compaction always runs when the VM is idle, regardless of the reclaim mode setting. Compaction and reclaim are independent concerns — compaction coalesces free pages for efficient page reporting, while reclaim controls cache/memory pressure behavior.

Copilot AI review requested due to automatic review settings May 1, 2026 04:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the Linux init memory-reduction logic so it can be shared between the WSL2 init (main.cpp) and WSLC init (WSLCInit.cpp), and enables idle-time compaction + cache dropping for WSLC VMs.

Changes:

  • Introduces a shared UtilStartMemoryReductionThread() entry point in util.cpp and a MemoryReductionMode enum in util.h.
  • Updates WSL2 init’s ConfigureMemoryReduction() to delegate to the shared thread logic.
  • Starts the memory reduction thread in WSLC init with drop-caches behavior when idle.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
src/linux/init/util.h Adds MemoryReductionMode and the shared thread API declaration.
src/linux/init/util.cpp Implements shared idle monitoring + compaction/reclaim thread and moves helpers into util.
src/linux/init/main.cpp Replaces local memory reduction thread logic with a call into UtilStartMemoryReductionThread().
src/linux/init/WSLCInit.cpp Enables memory reduction thread for WSLC init (drop caches + compaction when idle).

Comment thread src/linux/init/util.h Outdated
Comment thread src/linux/init/main.cpp Outdated
Comment thread src/linux/init/util.cpp
Comment thread src/linux/init/util.cpp Outdated
Comment thread src/linux/init/util.cpp Outdated
Comment thread src/linux/init/util.cpp
@benhillis benhillis force-pushed the user/benhill/wslc_compaction branch from c49dab8 to 5ae279f Compare May 1, 2026 05:44
Copilot AI review requested due to automatic review settings May 1, 2026 15:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 18 out of 25 changed files in this pull request and generated 2 comments.

Comment thread src/linux/init/util.cpp Outdated
Comment thread src/linux/init/util.h
@benhillis benhillis force-pushed the user/benhill/wslc_compaction branch from 39ccd6f to 19108ff Compare May 1, 2026 22:29
Copilot AI review requested due to automatic review settings May 1, 2026 23:48
@benhillis benhillis force-pushed the user/benhill/wslc_compaction branch from 19108ff to 539a091 Compare May 1, 2026 23:48
@benhillis benhillis marked this pull request as ready for review May 1, 2026 23:48
@benhillis benhillis requested a review from a team as a code owner May 1, 2026 23:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread src/linux/init/util.cpp
Comment thread src/linux/init/util.cpp
Comment thread src/linux/init/util.cpp
Comment thread src/linux/init/WSLCInit.cpp Outdated
Copilot AI review requested due to automatic review settings May 6, 2026 16:22
Ben Hillis and others added 2 commits May 6, 2026 12:35
Extract the memory compaction and cache reclaim thread into
StartMemoryReductionThread() in util.cpp. WSLC now runs the same
idle-based compaction and drop_caches logic that WSL2 uses, improving
page reporting efficiency and reducing host memory pressure.

- Add StartMemoryReductionThread() and MemoryReductionMode to util.h/cpp
- WSL2 main.cpp delegates to shared implementation
- WSLC WSLCInit.cpp calls StartMemoryReductionThread(DropCache)
- Remove duplicated GetUserCpuTime/GetMemoryInUse from main.cpp

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… directly

Address PR review feedback:
- Remove redundant MemoryReductionMode enum and use the existing wire enum
  LX_MINI_INIT_MEMORY_RECLAIM_MODE directly, eliminating the unsafe static_cast
  that could have swapped Gradual/DropCache behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@benhillis benhillis force-pushed the user/benhill/wslc_compaction branch from 6ebc314 to 96357be Compare May 6, 2026 20:51
ProcessMessages may MS_MOVE /proc into the chroot target before invoking
chroot(); starting the thread before that window risked failing path
lookups under /proc. Defer the thread start until the first successful
chroot, guarded by a once_flag so subsequent mount messages don't re-start.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

3 participants