2021-10-04 09:12:17 +08:00
|
|
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
|
|
2024-03-28 17:17:34 +08:00
|
|
|
package memutils
|
2021-10-04 09:12:17 +08:00
|
|
|
|
|
|
|
|
import (
|
2023-03-10 15:14:14 +08:00
|
|
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
2022-03-12 19:11:29 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/mem"
|
2021-10-04 09:12:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var systemTotalMemory = -1
|
2023-10-16 14:28:07 +08:00
|
|
|
var systemMemoryBytes uint64
|
2021-10-04 09:12:17 +08:00
|
|
|
|
2021-12-29 10:53:59 +08:00
|
|
|
func init() {
|
2023-03-10 15:14:14 +08:00
|
|
|
if !teaconst.IsMain {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 10:53:59 +08:00
|
|
|
_ = SystemMemoryGB()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 17:24:05 +08:00
|
|
|
// SystemMemoryGB 系统内存GB数量
|
|
|
|
|
// 必须保证不小于1
|
2021-12-29 10:53:59 +08:00
|
|
|
func SystemMemoryGB() int {
|
2021-10-04 09:12:17 +08:00
|
|
|
if systemTotalMemory > 0 {
|
|
|
|
|
return systemTotalMemory
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stat, err := mem.VirtualMemory()
|
|
|
|
|
if err != nil {
|
2023-04-25 17:24:05 +08:00
|
|
|
return 1
|
2021-10-04 09:12:17 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-16 14:28:07 +08:00
|
|
|
systemMemoryBytes = stat.Total
|
|
|
|
|
|
|
|
|
|
systemTotalMemory = int(stat.Total / (1 << 30))
|
2023-03-05 12:34:46 +08:00
|
|
|
if systemTotalMemory <= 0 {
|
|
|
|
|
systemTotalMemory = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setMaxMemory(systemTotalMemory)
|
|
|
|
|
|
2021-10-04 09:12:17 +08:00
|
|
|
return systemTotalMemory
|
|
|
|
|
}
|
2023-10-16 14:28:07 +08:00
|
|
|
|
|
|
|
|
// SystemMemoryBytes 系统内存总字节数
|
|
|
|
|
func SystemMemoryBytes() uint64 {
|
|
|
|
|
return systemMemoryBytes
|
|
|
|
|
}
|