使用MMAP提升缓存读取性能

This commit is contained in:
GoEdgeLab
2024-03-29 19:28:16 +08:00
parent 789f497afb
commit 60a4016101
5 changed files with 85 additions and 14 deletions

View File

@@ -4,11 +4,14 @@ package memutils
import (
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/shirou/gopsutil/v3/mem"
"time"
)
var systemTotalMemory = -1
var systemMemoryBytes uint64
var availableMemoryGB int
func init() {
if !teaconst.IsMain {
@@ -16,6 +19,16 @@ func init() {
}
_ = 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数量
@@ -32,7 +45,8 @@ func SystemMemoryGB() int {
systemMemoryBytes = stat.Total
systemTotalMemory = int(stat.Total / (1 << 30))
availableMemoryGB = int(stat.Available >> 30)
systemTotalMemory = int(stat.Total >> 30)
if systemTotalMemory <= 0 {
systemTotalMemory = 1
}
@@ -46,3 +60,8 @@ func SystemMemoryGB() int {
func SystemMemoryBytes() uint64 {
return systemMemoryBytes
}
// AvailableMemoryGB 获取当下可用内存GB数
func AvailableMemoryGB() int {
return availableMemoryGB
}