78 lines
2.4 KiB
Go
78 lines
2.4 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 reachable Windows commit limit and available
|
|
// commit charge, adding expandable paging-file capacity on top of current
|
|
// values. Windows expands system-managed or min/max-sized paging files on
|
|
// demand when a commit exceeds the current limit, capped by each file's
|
|
// maximum size and its volume's free space minus pagingFileDiskReserveGB;
|
|
// fixed-size or disabled paging files add zero growth.
|
|
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
|
|
}
|