Files
Reims-Go/internal/host/pagefile_windows.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

129 lines
3.5 KiB
Go

//go:build windows
package host
import (
"encoding/binary"
"errors"
"os"
"path/filepath"
"strings"
"unicode/utf16"
"unsafe"
"golang.org/x/sys/windows"
)
// systemPageFileInformationEx is SYSTEM_INFORMATION_CLASS 144 (Windows 8+):
// SYSTEM_PAGEFILE_INFORMATION followed by the file's minimum and maximum size,
// which the plain class 18 leaves out.
const systemPageFileInformationEx = 144
// Offsets inside one SYSTEM_PAGEFILE_INFORMATION_EX entry on 64-bit Windows.
const (
pfNextEntryOffset = 0 // ULONG
pfTotalSize = 4 // ULONG, pages
pfNameLength = 16 // UNICODE_STRING.Length, bytes
pfNameBuffer = 24 // UNICODE_STRING.Buffer
pfMaximumSize = 36 // ULONG, pages
pfEntryMinSize = 40
)
// rawPagingFile is one entry as NtQuerySystemInformation returns it.
type rawPagingFile struct {
Name string // NT path such as `\??\C:\pagefile.sys`
SizePg uint32
MaxSizePg uint32
}
// parsePagingFilesEx walks the SYSTEM_PAGEFILE_INFORMATION_EX list in buf.
// base is the address of buf[0]: the kernel writes each name into buf and
// stores its address, so a name is read back by offset and only when it lies
// inside buf.
func parsePagingFilesEx(buf []byte, base uintptr) ([]rawPagingFile, error) {
var out []rawPagingFile
off := 0
for {
if off < 0 || off+pfEntryMinSize > len(buf) {
return nil, errors.New("paging file list is truncated")
}
e := buf[off:]
next := binary.LittleEndian.Uint32(e[pfNextEntryOffset:])
nameLen := int(binary.LittleEndian.Uint16(e[pfNameLength:]))
namePtr := uintptr(binary.LittleEndian.Uint64(e[pfNameBuffer:]))
name := ""
if nameLen > 0 && namePtr >= base {
start := int(namePtr - base)
if start >= 0 && start+nameLen <= len(buf) {
u := make([]uint16, nameLen/2)
for i := range u {
u[i] = binary.LittleEndian.Uint16(buf[start+2*i:])
}
name = string(utf16.Decode(u))
}
}
out = append(out, rawPagingFile{
Name: name,
SizePg: binary.LittleEndian.Uint32(e[pfTotalSize:]),
MaxSizePg: binary.LittleEndian.Uint32(e[pfMaximumSize:]),
})
if next == 0 {
return out, nil
}
off += int(next)
}
}
// pagingFileVolume turns `\??\C:\pagefile.sys` into `C:\`.
func pagingFileVolume(ntPath string) string {
p := strings.TrimPrefix(ntPath, `\??\`)
if vol := filepath.VolumeName(p); vol != "" {
return vol + `\`
}
return ""
}
// probePagingFiles lists the paging files in use with their maximum size and
// their volume's free space. An empty list (paging file disabled) is valid.
func probePagingFiles() ([]PagingFile, error) {
size := 16 * 1024
for {
buf := make([]byte, size)
var ret uint32
err := windows.NtQuerySystemInformation(systemPageFileInformationEx, unsafe.Pointer(&buf[0]), uint32(len(buf)), &ret)
if errors.Is(err, windows.STATUS_INFO_LENGTH_MISMATCH) && size < 1<<20 {
size *= 4
continue
}
if err != nil {
return nil, err
}
if ret == 0 {
return nil, nil
}
raw, err := parsePagingFilesEx(buf[:ret], uintptr(unsafe.Pointer(&buf[0])))
if err != nil {
return nil, err
}
pageGB := float64(os.Getpagesize()) / (1024 * 1024 * 1024)
files := make([]PagingFile, 0, len(raw))
for _, r := range raw {
vol := pagingFileVolume(r.Name)
if vol == "" {
continue
}
free, ok := probeFreeDiskGB(vol)
if !ok {
continue
}
files = append(files, PagingFile{
Volume: vol,
CurrentGB: float64(r.SizePg) * pageGB,
MaxGB: float64(r.MaxSizePg) * pageGB,
VolumeFreeGB: free,
})
}
return files, nil
}
}