feat: native cross-platform total memory detection#2527
Draft
dunglas wants to merge 1 commit into
Draft
Conversation
Add darwin (sysctl hw.memsize) and windows (GlobalMemoryStatusEx) implementations of TotalSysMemory(), so automatic max_threads works outside Linux without pulling in gopsutil.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 returned0on macOS/Windows, which silently disabled automaticmax_threadscalculation.This PR gets that win without the dependency cost.
What it does
Adds native implementations of
TotalSysMemory():sysctl hw.memsize(viagolang.org/x/sys/unix)GlobalMemoryStatusEx(kernel32.dll, viagolang.org/x/sys/windows)syscall.Sysinfo)0golang.org/x/syswas 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
*process.Processused from both the up- and down-scaling goroutines;Percent(0)reads and writeslastCPUTimes/lastCPUTimewithout a lock), andPercent(0)measures near-zero windows under a scaling burst, weakening the guard the original 120 msclock_gettimeprobe provided. Not worth removing one C call in an already-CGO binary.mem.VirtualMemory().Available, somax_threads=autowould size off whatever happens to be free at boot. This PR keeps total-physical semantics on every platform.Verification
internal/memoryis pure Go (no CGO), cross-compiles clean for linux/darwin/windows. On an Apple Silicon Mac,TotalSysMemory()returns51539607552, matchingsysctl -n hw.memsizeexactly. Windows path is the standardMEMORYSTATUSEXpattern (not runnable in CI here).