1.4.5.2
This commit is contained in:
67
EdgeNode/internal/utils/mem/system.go
Normal file
67
EdgeNode/internal/utils/mem/system.go
Normal file
@@ -0,0 +1,67 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package memutils
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
|
||||
"github.com/shirou/gopsutil/v3/mem"
|
||||
"time"
|
||||
)
|
||||
|
||||
var systemTotalMemory = -1
|
||||
var systemMemoryBytes uint64
|
||||
var availableMemoryGB int
|
||||
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
_ = SystemMemoryGB()
|
||||
|
||||
goman.New(func() {
|
||||
var ticker = time.NewTicker(10 * time.Second)
|
||||
for range ticker.C {
|
||||
stat, err := mem.VirtualMemory()
|
||||
if err == nil {
|
||||
availableMemoryGB = int(stat.Available >> 30)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// SystemMemoryGB 系统内存GB数量
|
||||
// 必须保证不小于1
|
||||
func SystemMemoryGB() int {
|
||||
if systemTotalMemory > 0 {
|
||||
return systemTotalMemory
|
||||
}
|
||||
|
||||
stat, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
systemMemoryBytes = stat.Total
|
||||
|
||||
availableMemoryGB = int(stat.Available >> 30)
|
||||
systemTotalMemory = int(stat.Total >> 30)
|
||||
if systemTotalMemory <= 0 {
|
||||
systemTotalMemory = 1
|
||||
}
|
||||
|
||||
setMaxMemory(systemTotalMemory)
|
||||
|
||||
return systemTotalMemory
|
||||
}
|
||||
|
||||
// SystemMemoryBytes 系统内存总字节数
|
||||
func SystemMemoryBytes() uint64 {
|
||||
return systemMemoryBytes
|
||||
}
|
||||
|
||||
// AvailableMemoryGB 获取当下可用内存GB数
|
||||
func AvailableMemoryGB() int {
|
||||
return availableMemoryGB
|
||||
}
|
||||
18
EdgeNode/internal/utils/mem/system_1.19.go
Normal file
18
EdgeNode/internal/utils/mem/system_1.19.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build go1.19
|
||||
|
||||
package memutils
|
||||
|
||||
import (
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
// 设置软内存最大值
|
||||
func setMaxMemory(memoryGB int) {
|
||||
if memoryGB <= 0 {
|
||||
memoryGB = 1
|
||||
}
|
||||
|
||||
var maxMemoryBytes = (int64(memoryGB) << 30) * 80 / 100 // 默认 80%
|
||||
debug.SetMemoryLimit(maxMemoryBytes)
|
||||
}
|
||||
9
EdgeNode/internal/utils/mem/system_before_1.19.go
Normal file
9
EdgeNode/internal/utils/mem/system_before_1.19.go
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !go1.19
|
||||
|
||||
package memutils
|
||||
|
||||
// 设置软内存最大值
|
||||
func setMaxMemory(memoryGB int) {
|
||||
|
||||
}
|
||||
18
EdgeNode/internal/utils/mem/system_test.go
Normal file
18
EdgeNode/internal/utils/mem/system_test.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package memutils_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/mem"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSystemMemoryGB(t *testing.T) {
|
||||
t.Log(memutils.SystemMemoryGB())
|
||||
t.Log(memutils.SystemMemoryGB())
|
||||
t.Log(memutils.SystemMemoryGB())
|
||||
t.Log(memutils.SystemMemoryBytes())
|
||||
t.Log(memutils.SystemMemoryBytes())
|
||||
t.Log(memutils.SystemMemoryBytes()>>30, "GB")
|
||||
t.Log("available:", memutils.AvailableMemoryGB())
|
||||
}
|
||||
Reference in New Issue
Block a user