Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/prometheus/client_golang v1.23.2
github.com/stretchr/testify v1.11.1
golang.org/x/net v0.56.0
golang.org/x/sys v0.46.0
)

require (
Expand Down Expand Up @@ -61,7 +62,6 @@ require (
go.opentelemetry.io/otel/trace v1.44.0 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.53.0 // indirect
golang.org/x/sys v0.46.0 // indirect
golang.org/x/text v0.38.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
13 changes: 13 additions & 0 deletions internal/memory/memory_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package memory

import "golang.org/x/sys/unix"

// TotalSysMemory returns the total physical memory in bytes via sysctl hw.memsize
func TotalSysMemory() uint64 {
memsize, err := unix.SysctlUint64("hw.memsize")
if err != nil {
return 0
}

return memsize
}
2 changes: 1 addition & 1 deletion internal/memory/memory_others.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !linux
//go:build !linux && !darwin && !windows

package memory

Expand Down
36 changes: 36 additions & 0 deletions internal/memory/memory_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package memory

import (
"unsafe"

"golang.org/x/sys/windows"
)

// memoryStatusEx mirrors the Win32 MEMORYSTATUSEX structure.
// https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex
type memoryStatusEx struct {
length uint32
memoryLoad uint32
totalPhys uint64
availPhys uint64
totalPageFile uint64
availPageFile uint64
totalVirtual uint64
availVirtual uint64
availExtendedVirtual uint64
}

var procGlobalMemoryStatusEx = windows.NewLazySystemDLL("kernel32.dll").NewProc("GlobalMemoryStatusEx")

// TotalSysMemory returns the total physical memory in bytes via GlobalMemoryStatusEx
func TotalSysMemory() uint64 {
memStatus := memoryStatusEx{}
memStatus.length = uint32(unsafe.Sizeof(memStatus))

ret, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memStatus)))
if ret == 0 {
return 0
}

return memStatus.totalPhys
}
Loading