Initial commit (code only without large binaries)

This commit is contained in:
robin
2026-02-15 18:58:44 +08:00
commit 35df75498f
9442 changed files with 1495866 additions and 0 deletions

View 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
}