Skip to content

feat: native cross-platform total memory detection#2527

Draft
dunglas wants to merge 1 commit into
mainfrom
feat/native-memory-probe
Draft

feat: native cross-platform total memory detection#2527
dunglas wants to merge 1 commit into
mainfrom
feat/native-memory-probe

Conversation

@dunglas

@dunglas dunglas commented Jul 18, 2026

Copy link
Copy Markdown
Member

Alternative to #2306.

#2306 replaces the platform-specific CPU/memory probing with gopsutil/v4. The genuinely useful part of that PR is cross-platform memory detection: internal/memory.TotalSysMemory() was Linux-only and returned 0 on macOS/Windows, which silently disabled automatic max_threads calculation.

This PR gets that win without the dependency cost.

What it does

Adds native implementations of TotalSysMemory():

  • darwinsysctl hw.memsize (via golang.org/x/sys/unix)
  • windowsGlobalMemoryStatusEx (kernel32.dll, via golang.org/x/sys/windows)
  • linux — unchanged (syscall.Sysinfo)
  • others — unchanged fallback returning 0

golang.org/x/sys was already an indirect dependency, so this promotes it to direct and pulls in zero new modules. #2306 adds ~8 transitive modules (purego, go-ole, wmi, plan9stats, perfstat, tklauser/*).

Why not gopsutil

  • The CPU-probe rewrite in perf: replace platform-specific CPU/memory probing with gopsutil #2306 has a data race (one shared *process.Process used from both the up- and down-scaling goroutines; Percent(0) reads and writes lastCPUTimes/lastCPUTime without a lock), and Percent(0) measures near-zero windows under a scaling burst, weakening the guard the original 120 ms clock_gettime probe provided. Not worth removing one C call in an already-CGO binary.
  • perf: replace platform-specific CPU/memory probing with gopsutil #2306 also switches the memory figure from total RAM to mem.VirtualMemory().Available, so max_threads=auto would size off whatever happens to be free at boot. This PR keeps total-physical semantics on every platform.

Verification

internal/memory is pure Go (no CGO), cross-compiles clean for linux/darwin/windows. On an Apple Silicon Mac, TotalSysMemory() returns 51539607552, matching sysctl -n hw.memsize exactly. Windows path is the standard MEMORYSTATUSEX pattern (not runnable in CI here).

Add darwin (sysctl hw.memsize) and windows (GlobalMemoryStatusEx)
implementations of TotalSysMemory(), so automatic max_threads works
outside Linux without pulling in gopsutil.
Copilot AI review requested due to automatic review settings July 18, 2026 14:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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