Skip to content
Open
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
8 changes: 4 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v5
uses: actions/setup-go@v6
with:
go-version: ^1.25
- name: Cache go module
uses: actions/cache@v4
uses: actions/cache@v5
with:
path: |
~/go/pkg/mod
key: go-${{ hashFiles('**/go.sum') }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v8
uses: golangci/golangci-lint-action@v9
with:
version: v2.4.0
args: --timeout=30m
Expand Down
24 changes: 12 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v5
uses: actions/setup-go@v6
with:
go-version: ^1.23
- name: Build
Expand All @@ -35,11 +35,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v5
uses: actions/setup-go@v6
with:
go-version: ~1.20
continue-on-error: true
Expand All @@ -51,11 +51,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v5
uses: actions/setup-go@v6
with:
go-version: ~1.21
continue-on-error: true
Expand All @@ -67,11 +67,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v5
uses: actions/setup-go@v6
with:
go-version: ~1.22
continue-on-error: true
Expand All @@ -83,11 +83,11 @@ jobs:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v5
uses: actions/setup-go@v6
with:
go-version: ^1.23
continue-on-error: true
Expand All @@ -99,11 +99,11 @@ jobs:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v5
uses: actions/setup-go@v6
with:
go-version: ^1.23
continue-on-error: true
Expand Down
21 changes: 11 additions & 10 deletions common/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package memory
import "runtime"

func Total() uint64 {
if nativeAvailable {
return usageNative()
}
return Inuse()
return totalNative()
}

func Inuse() uint64 {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
return memStats.StackInuse + memStats.HeapInuse + memStats.HeapIdle - memStats.HeapReleased
func TotalAvailable() bool {
return totalAvailable()
}

func Available() uint64 {
return availableNative()
}

func AvailableSupported() bool {
return availableNativeSupported()
func AvailableAvailable() bool {
return availableAvailable()
}

func Inuse() uint64 {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
return memStats.StackInuse + memStats.HeapInuse + memStats.HeapIdle - memStats.HeapReleased
}
36 changes: 0 additions & 36 deletions common/memory/memory_available_darwin.go

This file was deleted.

11 changes: 0 additions & 11 deletions common/memory/memory_available_stub.go

This file was deleted.

57 changes: 49 additions & 8 deletions common/memory/memory_darwin.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,59 @@
package memory

// #include <mach/mach.h>
// #include <stddef.h>
// #include <dlfcn.h>
//
// typedef size_t (*proc_available_memory_func)(void);
// static int resolved = 0;
// static proc_available_memory_func fn = NULL;
//
// static void resolve_available_memory() {
// if (!resolved) {
// fn = (proc_available_memory_func)dlsym(RTLD_DEFAULT, "os_proc_available_memory");
// resolved = 1;
// }
// }
//
// static size_t get_available_memory(int *supported) {
// resolve_available_memory();
// if (fn) {
// *supported = 1;
// return fn();
// }
// *supported = 0;
// return 0;
// }
//
// static int is_available_memory_supported() {
// resolve_available_memory();
// return fn != NULL;
// }
import "C"
import "unsafe"

const nativeAvailable = true

func usageNative() uint64 {
var memoryUsageInByte uint64
func totalNative() uint64 {
var vmInfo C.task_vm_info_data_t
var count C.mach_msg_type_number_t = C.TASK_VM_INFO_COUNT
var kernelReturn C.kern_return_t = C.task_info(C.vm_map_t(C.mach_task_self_), C.TASK_VM_INFO, (*C.integer_t)(unsafe.Pointer(&vmInfo)), &count)
if kernelReturn == C.KERN_SUCCESS {
memoryUsageInByte = uint64(vmInfo.phys_footprint)
if C.task_info(C.vm_map_t(C.mach_task_self_), C.TASK_VM_INFO, (*C.integer_t)(unsafe.Pointer(&vmInfo)), &count) == C.KERN_SUCCESS {
return uint64(vmInfo.phys_footprint)
}
return 0
}

func totalAvailable() bool {
return true
}

func availableNative() uint64 {
var supported C.int
result := C.get_available_memory(&supported)
if supported == 0 {
return 0
}
return memoryUsageInByte
return uint64(result)
}

func availableAvailable() bool {
return C.is_available_memory_supported() != 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,41 @@ import (
"strings"
)

func availableNativeSupported() bool {
return true
var pageSize = uint64(os.Getpagesize())

func totalNative() uint64 {
fd, err := os.Open("/proc/self/statm")
if err != nil {
return 0
}
defer fd.Close()
var buf [128]byte
n, _ := fd.Read(buf[:])
if n == 0 {
return 0
}
i := 0
for i < n && buf[i] != ' ' {
i++
}
i++
var rss uint64
for i < n && buf[i] >= '0' && buf[i] <= '9' {
rss = rss*10 + uint64(buf[i]-'0')
i++
}
return rss * pageSize
}

func totalAvailable() bool {
fd, err := os.Open("/proc/self/statm")
if err != nil {
return false
}
defer fd.Close()
var buf [1]byte
n, _ := fd.Read(buf[:])
return n > 0
}

func availableNative() uint64 {
Expand All @@ -20,6 +53,19 @@ func availableNative() uint64 {
return procMemAvailable()
}

func availableAvailable() bool {
_, ok := cgroupAvailable()
if ok {
return true
}
fd, err := os.Open("/proc/meminfo")
if err != nil {
return false
}
fd.Close()
return true
}

func cgroupAvailable() (uint64, bool) {
max, err := readCgroupUint("/sys/fs/cgroup/memory.max")
if err == nil && max != math.MaxUint64 {
Expand Down
16 changes: 13 additions & 3 deletions common/memory/memory_stub.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
//go:build (darwin && !cgo) || !darwin
//go:build (darwin && !cgo) || (!darwin && !linux && !windows)

package memory

const nativeAvailable = false
func totalNative() uint64 {
return 0
}

func totalAvailable() bool {
return false
}

func usageNative() uint64 {
func availableNative() uint64 {
return 0
}

func availableAvailable() bool {
return false
}
35 changes: 35 additions & 0 deletions common/memory/memory_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package memory

import (
"unsafe"

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

func totalNative() uint64 {
var mem processMemoryCounters
mem.cb = uint32(unsafe.Sizeof(mem))
err := getProcessMemoryInfo(windows.CurrentProcess(), &mem, mem.cb)
if err != nil {
return 0
}
return uint64(mem.workingSetSize)
}

func totalAvailable() bool {
return true
}

func availableNative() uint64 {
var mem memoryStatusEx
mem.dwLength = uint32(unsafe.Sizeof(mem))
err := globalMemoryStatusEx(&mem)
if err != nil {
return 0
}
return mem.ullAvailPhys
}

func availableAvailable() bool {
return true
}
Loading
Loading