[windows][mem]: change to use Performance Counter on SwapMemory.

pull/1677/head
shirou 8 months ago
parent 262afcf2e6
commit 1221983189

@ -15,6 +15,7 @@ import (
"errors"
"fmt"
"io"
"math"
"net/url"
"os"
"os/exec"
@ -463,3 +464,11 @@ func getSysctrlEnv(env []string) []string {
}
return env
}
// Round places rounds the number 'val' to 'n' decimal places
func Round(val float64, n int) float64 {
// Calculate the power of 10 to the n
pow10 := math.Pow(10, float64(n))
// Multiply the value by pow10, round it, then divide it by pow10
return math.Round(val*pow10) / pow10
}

@ -77,26 +77,40 @@ func SwapMemory() (*SwapMemoryStat, error) {
}
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
// Use the performance counter to get the swap usage percentage
counter, err := common.NewWin32PerformanceCounter("swap_percentage", `\Paging File(_Total)\% Usage`)
if err != nil {
return nil, err
}
usedPercent, err := counter.GetValue()
if err != nil {
return nil, err
}
// Get total memory from performance information
var perfInfo performanceInformation
perfInfo.cb = uint32(unsafe.Sizeof(perfInfo))
mem, _, _ := procGetPerformanceInfo.Call(uintptr(unsafe.Pointer(&perfInfo)), uintptr(perfInfo.cb))
if mem == 0 {
return nil, windows.GetLastError()
}
tot := perfInfo.commitLimit * perfInfo.pageSize
used := perfInfo.commitTotal * perfInfo.pageSize
free := tot - used
var usedPercent float64
if tot == 0 {
usedPercent = 0
totalPhys := perfInfo.physicalTotal * perfInfo.pageSize
totalSys := perfInfo.commitLimit * perfInfo.pageSize
total := totalSys - totalPhys
var used uint64
if total > 0 {
used = uint64(0.01 * usedPercent * float64(total))
} else {
usedPercent = float64(used) / float64(tot) * 100
usedPercent = 0.0
used = 0
}
ret := &SwapMemoryStat{
Total: tot,
Total: total,
Used: used,
Free: free,
UsedPercent: usedPercent,
Free: total - used,
UsedPercent: common.Round(usedPercent, 1),
}
return ret, nil

Loading…
Cancel
Save