Simplify struct usage and comments

Signed-off-by: Ben Ridley <benridley29@gmail.com>
This commit is contained in:
Ben Ridley 2021-03-18 16:08:52 -07:00
parent df2a7a9ec0
commit ee3848141c
2 changed files with 14 additions and 65 deletions

View File

@ -6,38 +6,20 @@ import (
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
) )
// LPPerformanceInformation is a wrapper of the PERFORMANCE_INFORMATION struct. // PerformanceInformation is a wrapper of the PERFORMANCE_INFORMATION struct.
// https://docs.microsoft.com/en-us/windows/win32/api/psapi/ns-psapi-performance_information // https://docs.microsoft.com/en-us/windows/win32/api/psapi/ns-psapi-performance_information
type LPPerformanceInformation struct {
cb uint32
CommitTotal *uint32
CommitLimit *uint32
CommitPeak *uint32
PhysicalTotal *uint32
PhysicalAvailable *uint32
SystemCache *uint32
KernelTotal *uint32
KernelPaged *uint32
KernelNonpaged *uint32
PageSize *uint32
HandleCount uint32
ProcessCount uint32
ThreadCount uint32
}
// PerformanceInformation is a dereferenced version of LPPerformanceInformation
type PerformanceInformation struct { type PerformanceInformation struct {
cb uint32 cb uint32
CommitTotal uint32 CommitTotal uint
CommitLimit uint32 CommitLimit uint
CommitPeak uint32 CommitPeak uint
PhysicalTotal uint32 PhysicalTotal uint
PhysicalAvailable uint32 PhysicalAvailable uint
SystemCache uint32 SystemCache uint
KernelTotal uint32 KernelTotal uint
KernelPaged uint32 KernelPaged uint
KernelNonpaged uint32 KernelNonpaged uint
PageSize uint32 PageSize uint
HandleCount uint32 HandleCount uint32
ProcessCount uint32 ProcessCount uint32
ThreadCount uint32 ThreadCount uint32
@ -48,24 +30,9 @@ var (
procGetPerformanceInfo = psapi.NewProc("GetPerformanceInfo") procGetPerformanceInfo = psapi.NewProc("GetPerformanceInfo")
) )
// GetLPPerformanceInfo retrieves the performance values contained in the LPPerformanceInformation structure.
// https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getperformanceinfo
func GetLPPerformanceInfo() (LPPerformanceInformation, error) {
var pi LPPerformanceInformation
size := (uint32)(unsafe.Sizeof(pi))
pi.cb = size
r1, _, err := procGetPerformanceInfo.Call(uintptr(unsafe.Pointer(&pi)), uintptr(size))
if ret := *(*bool)(unsafe.Pointer(&r1)); !ret {
return LPPerformanceInformation{}, err
}
return pi, nil
}
// GetPerformanceInfo returns the dereferenced version of GetLPPerformanceInfo. // GetPerformanceInfo returns the dereferenced version of GetLPPerformanceInfo.
func GetPerformanceInfo() (PerformanceInformation, error) { func GetPerformanceInfo() (PerformanceInformation, error) {
var lppi LPPerformanceInformation var lppi PerformanceInformation
size := (uint32)(unsafe.Sizeof(lppi)) size := (uint32)(unsafe.Sizeof(lppi))
lppi.cb = size lppi.cb = size
r1, _, err := procGetPerformanceInfo.Call(uintptr(unsafe.Pointer(&lppi)), uintptr(size)) r1, _, err := procGetPerformanceInfo.Call(uintptr(unsafe.Pointer(&lppi)), uintptr(size))
@ -74,21 +41,5 @@ func GetPerformanceInfo() (PerformanceInformation, error) {
return PerformanceInformation{}, err return PerformanceInformation{}, err
} }
var pi PerformanceInformation return lppi, nil
pi.cb = lppi.cb
pi.CommitTotal = *(lppi.CommitTotal)
pi.CommitLimit = *(lppi.CommitLimit)
pi.CommitPeak = *(lppi.CommitPeak)
pi.PhysicalTotal = *(lppi.PhysicalTotal)
pi.PhysicalAvailable = *(lppi.PhysicalAvailable)
pi.SystemCache = *(lppi.SystemCache)
pi.KernelTotal = *(lppi.KernelTotal)
pi.KernelPaged = *(lppi.KernelPaged)
pi.KernelNonpaged = *(lppi.KernelNonpaged)
pi.PageSize = *(lppi.PageSize)
pi.HandleCount = lppi.HandleCount
pi.ProcessCount = lppi.ProcessCount
pi.ThreadCount = lppi.ThreadCount
return pi, nil
} }

View File

@ -1,7 +1,6 @@
package sysinfoapi package sysinfoapi
import ( import (
"fmt"
"unicode/utf16" "unicode/utf16"
"unsafe" "unsafe"
@ -129,12 +128,11 @@ func GlobalMemoryStatusEx() (MemoryStatus, error) {
}, nil }, nil
} }
// GetSystemInfo wraps the GetSystemInfo function from sysinfoapi // GetSystemInfo is an idiomatic wrapper for the GetSystemInfo function from sysinfoapi
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo // https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo
func GetSystemInfo() SystemInfo { func GetSystemInfo() SystemInfo {
var info lpSystemInfo var info lpSystemInfo
procGetSystemInfo.Call(uintptr(unsafe.Pointer(&info))) procGetSystemInfo.Call(uintptr(unsafe.Pointer(&info)))
fmt.Printf("%+v", info)
return SystemInfo{ return SystemInfo{
Arch: ProcessorArchitecture(info.Arch.WProcessorArchitecture), Arch: ProcessorArchitecture(info.Arch.WProcessorArchitecture),
PageSize: info.DwPageSize, PageSize: info.DwPageSize,