Files
Reims-Go/internal/host/memory.go
T
NwflowerandClaude Opus 5.5 43c387affc fix(host): count page file growth in commit memory checks
The start check, start wait, memory recommendation and create/settings
validation compared guest RAM against GlobalMemoryStatusEx's current
commit limit. A system-managed or min/max page file grows on demand, so
that limit refused VMs that would start: with 30.7/37.4 GB committed an
8G guest was rejected while about 38 GB could still be committed.

Read each paging file's current and maximum size with
NtQuerySystemInformation(SystemPageFileInformationEx) and add the room
it can still grow, capped by its volume's free space minus a 2 GB
reserve. Fixed-size or disabled page files add nothing; if the query
fails the old values stand. Measured on Windows 10: one commit of free
commit + 10 GB (VirtualAlloc and CreateFileMapping) succeeded and grew
the limit from 37.4 to 47.4 GB.

The start error now names the total requirement and the guest share and
says page file growth is already counted; the host report gains
paging_file_growth_gb.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 08:04:14 -07:00

81 lines
2.6 KiB
Go

package host
import "strings"
// StartMarginGB is the commit memory a VM needs beyond its guest RAM: QEMU's own
// allocations and the graphics path. Conservative; refine with measurements.
const StartMarginGB = 1.0
// pagingFileDiskReserveGB is the free space left on a paging file's volume
// when estimating how far the file can still grow.
const pagingFileDiskReserveGB = 2.0
// memoryChoices are the sizes the UI offers (GB).
var memoryChoices = []float64{4, 6, 8, 12, 16, 24, 32, 48, 64}
// PagingFile is one paging file as Windows reports it, plus the free space of
// the volume it lives on.
type PagingFile struct {
Volume string // e.g. `C:\`; files on one volume share its free space
CurrentGB float64 // current size
MaxGB float64 // largest size Windows may grow it to
VolumeFreeGB float64
}
// CommitHeadroom returns the commit limit Windows can reach and the commit it
// can still grant, counting what paging files can still grow on top of the
// current limit and free commit. A commit larger than the free commit makes
// Windows grow a system-managed or min/max-sized paging file on the spot
// (measured 2026-12-01: one 24.3 GB CreateFileMapping with 14.3 GB free grew
// the limit from 37.4 to 47.4 GB), so the current limit alone refuses VMs
// that would start. Growth is capped by each file's maximum and its volume's
// free space less pagingFileDiskReserveGB; a fixed-size or absent paging file
// adds nothing.
func CommitHeadroom(limitGB, availGB float64, files []PagingFile) (reachableLimitGB, reachableAvailGB float64) {
growth := PagingFileGrowthGB(files)
return limitGB + growth, availGB + growth
}
// PagingFileGrowthGB is how much the paging files can still grow in total.
func PagingFileGrowthGB(files []PagingFile) float64 {
volumeLeft := map[string]float64{}
total := 0.0
for _, f := range files {
vol := strings.ToUpper(f.Volume)
left, seen := volumeLeft[vol]
if !seen {
left = f.VolumeFreeGB - pagingFileDiskReserveGB
}
grow := f.MaxGB - f.CurrentGB
if grow > left {
grow = left
}
if grow <= 0 {
volumeLeft[vol] = left
continue
}
volumeLeft[vol] = left - grow
total += grow
}
return total
}
// RecommendedMemoryGB is the largest offered size that fits both in 60% of
// physical RAM and in the commit memory Windows can still grant (minus
// StartMarginGB and 2 GB headroom for the host). At least 4.
func RecommendedMemoryGB(rep Report) float64 {
limit := rep.TotalRAMGB * 0.6
if rep.CommitAvailGB > 0 {
if c := rep.CommitAvailGB - StartMarginGB - 2; c < limit {
limit = c
}
}
best := memoryChoices[0]
for _, m := range memoryChoices {
if m <= limit {
best = m
}
}
return best
}