2017-02-01 18:26:00 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package mem
|
|
|
|
|
|
|
|
import (
|
2018-10-19 18:33:23 +00:00
|
|
|
"context"
|
2020-07-01 12:47:56 +00:00
|
|
|
"encoding/json"
|
|
|
|
"math"
|
|
|
|
"os"
|
2017-02-01 18:26:00 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-11-07 01:48:38 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
2018-10-19 18:33:23 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 18:26:00 +00:00
|
|
|
)
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
type VirtualMemoryExStat struct {
|
|
|
|
ActiveFile uint64 `json:"activefile"`
|
|
|
|
InactiveFile uint64 `json:"inactivefile"`
|
|
|
|
ActiveAnon uint64 `json:"activeanon"`
|
|
|
|
InactiveAnon uint64 `json:"inactiveanon"`
|
|
|
|
Unevictable uint64 `json:"unevictable"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v VirtualMemoryExStat) String() string {
|
|
|
|
s, _ := json.Marshal(v)
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:26:00 +00:00
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return VirtualMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
2020-07-01 12:47:56 +00:00
|
|
|
vm, _, err := fillFromMeminfoWithContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return vm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualMemoryEx() (*VirtualMemoryExStat, error) {
|
|
|
|
return VirtualMemoryExWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualMemoryExWithContext(ctx context.Context) (*VirtualMemoryExStat, error) {
|
|
|
|
_, vmEx, err := fillFromMeminfoWithContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return vmEx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fillFromMeminfoWithContext(ctx context.Context) (*VirtualMemoryStat, *VirtualMemoryExStat, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
filename := common.HostProc("meminfo")
|
|
|
|
lines, _ := common.ReadLines(filename)
|
2020-07-01 12:47:56 +00:00
|
|
|
|
2017-02-01 18:26:00 +00:00
|
|
|
// flag if MemAvailable is in /proc/meminfo (kernel 3.14+)
|
|
|
|
memavail := false
|
2020-07-01 12:47:56 +00:00
|
|
|
activeFile := false // "Active(file)" not available: 2.6.28 / Dec 2008
|
|
|
|
inactiveFile := false // "Inactive(file)" not available: 2.6.28 / Dec 2008
|
2020-11-07 01:48:38 +00:00
|
|
|
sReclaimable := false // "Sreclaimable:" not available: 2.6.19 / Nov 2006
|
2017-02-01 18:26:00 +00:00
|
|
|
|
|
|
|
ret := &VirtualMemoryStat{}
|
2020-07-01 12:47:56 +00:00
|
|
|
retEx := &VirtualMemoryExStat{}
|
|
|
|
|
2017-02-01 18:26:00 +00:00
|
|
|
for _, line := range lines {
|
|
|
|
fields := strings.Split(line, ":")
|
|
|
|
if len(fields) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key := strings.TrimSpace(fields[0])
|
|
|
|
value := strings.TrimSpace(fields[1])
|
|
|
|
value = strings.Replace(value, " kB", "", -1)
|
|
|
|
|
|
|
|
t, err := strconv.ParseUint(value, 10, 64)
|
|
|
|
if err != nil {
|
2020-07-01 12:47:56 +00:00
|
|
|
return ret, retEx,err
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "MemTotal":
|
|
|
|
ret.Total = t * 1024
|
|
|
|
case "MemFree":
|
|
|
|
ret.Free = t * 1024
|
|
|
|
case "MemAvailable":
|
|
|
|
memavail = true
|
|
|
|
ret.Available = t * 1024
|
|
|
|
case "Buffers":
|
|
|
|
ret.Buffers = t * 1024
|
|
|
|
case "Cached":
|
|
|
|
ret.Cached = t * 1024
|
|
|
|
case "Active":
|
|
|
|
ret.Active = t * 1024
|
|
|
|
case "Inactive":
|
|
|
|
ret.Inactive = t * 1024
|
2020-07-01 12:47:56 +00:00
|
|
|
case "Active(anon)":
|
|
|
|
retEx.ActiveAnon = t * 1024
|
|
|
|
case "Inactive(anon)":
|
|
|
|
retEx.InactiveAnon = t * 1024
|
|
|
|
case "Active(file)":
|
|
|
|
activeFile = true
|
|
|
|
retEx.ActiveFile = t * 1024
|
|
|
|
case "Inactive(file)":
|
|
|
|
inactiveFile = true
|
|
|
|
retEx.InactiveFile = t * 1024
|
|
|
|
case "Unevictable":
|
|
|
|
retEx.Unevictable = t * 1024
|
2020-11-07 01:48:38 +00:00
|
|
|
case "WriteBack":
|
|
|
|
ret.WriteBack = t * 1024
|
|
|
|
case "WriteBackTmp":
|
|
|
|
ret.WriteBackTmp = t * 1024
|
2017-02-01 18:26:00 +00:00
|
|
|
case "Dirty":
|
|
|
|
ret.Dirty = t * 1024
|
|
|
|
case "Shmem":
|
|
|
|
ret.Shared = t * 1024
|
|
|
|
case "Slab":
|
|
|
|
ret.Slab = t * 1024
|
2020-11-07 01:48:38 +00:00
|
|
|
case "Sreclaimable":
|
2020-07-01 12:47:56 +00:00
|
|
|
sReclaimable = true
|
2020-11-07 01:48:38 +00:00
|
|
|
ret.Sreclaimable = t * 1024
|
|
|
|
case "Sunreclaim":
|
|
|
|
ret.Sunreclaim = t * 1024
|
2017-02-01 18:26:00 +00:00
|
|
|
case "PageTables":
|
|
|
|
ret.PageTables = t * 1024
|
|
|
|
case "SwapCached":
|
|
|
|
ret.SwapCached = t * 1024
|
2018-10-19 18:33:23 +00:00
|
|
|
case "CommitLimit":
|
|
|
|
ret.CommitLimit = t * 1024
|
|
|
|
case "Committed_AS":
|
|
|
|
ret.CommittedAS = t * 1024
|
|
|
|
case "HighTotal":
|
|
|
|
ret.HighTotal = t * 1024
|
|
|
|
case "HighFree":
|
|
|
|
ret.HighFree = t * 1024
|
|
|
|
case "LowTotal":
|
|
|
|
ret.LowTotal = t * 1024
|
|
|
|
case "LowFree":
|
|
|
|
ret.LowFree = t * 1024
|
|
|
|
case "SwapTotal":
|
|
|
|
ret.SwapTotal = t * 1024
|
|
|
|
case "SwapFree":
|
|
|
|
ret.SwapFree = t * 1024
|
|
|
|
case "Mapped":
|
|
|
|
ret.Mapped = t * 1024
|
|
|
|
case "VmallocTotal":
|
2020-11-07 01:48:38 +00:00
|
|
|
ret.VmallocTotal = t * 1024
|
2018-10-19 18:33:23 +00:00
|
|
|
case "VmallocUsed":
|
2020-11-07 01:48:38 +00:00
|
|
|
ret.VmallocUsed = t * 1024
|
2018-10-19 18:33:23 +00:00
|
|
|
case "VmallocChunk":
|
2020-11-07 01:48:38 +00:00
|
|
|
ret.VmallocChunk = t * 1024
|
2018-10-19 18:33:23 +00:00
|
|
|
case "HugePages_Total":
|
|
|
|
ret.HugePagesTotal = t
|
|
|
|
case "HugePages_Free":
|
|
|
|
ret.HugePagesFree = t
|
|
|
|
case "Hugepagesize":
|
|
|
|
ret.HugePageSize = t * 1024
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
|
2020-11-07 01:48:38 +00:00
|
|
|
ret.Cached += ret.Sreclaimable
|
2020-07-01 12:47:56 +00:00
|
|
|
|
2017-02-01 18:26:00 +00:00
|
|
|
if !memavail {
|
2020-07-01 12:47:56 +00:00
|
|
|
if activeFile && inactiveFile && sReclaimable {
|
|
|
|
ret.Available = calcuateAvailVmem(ret, retEx)
|
|
|
|
} else {
|
|
|
|
ret.Available = ret.Cached + ret.Free
|
|
|
|
}
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
ret.Used = ret.Total - ret.Free - ret.Buffers - ret.Cached
|
|
|
|
ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0
|
2017-02-01 18:26:00 +00:00
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
return ret, retEx, nil
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return SwapMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
|
|
|
sysinfo := &unix.Sysinfo_t{}
|
2017-02-01 18:26:00 +00:00
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
if err := unix.Sysinfo(sysinfo); err != nil {
|
2017-02-01 18:26:00 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := &SwapMemoryStat{
|
|
|
|
Total: uint64(sysinfo.Totalswap) * uint64(sysinfo.Unit),
|
|
|
|
Free: uint64(sysinfo.Freeswap) * uint64(sysinfo.Unit),
|
|
|
|
}
|
|
|
|
ret.Used = ret.Total - ret.Free
|
|
|
|
//check Infinity
|
|
|
|
if ret.Total != 0 {
|
|
|
|
ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0
|
|
|
|
} else {
|
|
|
|
ret.UsedPercent = 0
|
|
|
|
}
|
|
|
|
filename := common.HostProc("vmstat")
|
|
|
|
lines, _ := common.ReadLines(filename)
|
|
|
|
for _, l := range lines {
|
|
|
|
fields := strings.Fields(l)
|
|
|
|
if len(fields) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch fields[0] {
|
|
|
|
case "pswpin":
|
|
|
|
value, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.Sin = value * 4 * 1024
|
|
|
|
case "pswpout":
|
|
|
|
value, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.Sout = value * 4 * 1024
|
2020-11-07 01:48:38 +00:00
|
|
|
case "pgpgIn":
|
2020-07-01 12:47:56 +00:00
|
|
|
value, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.PgIn = value * 4 * 1024
|
2020-11-07 01:48:38 +00:00
|
|
|
case "pgpgOut":
|
2020-07-01 12:47:56 +00:00
|
|
|
value, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.PgOut = value * 4 * 1024
|
2020-11-07 01:48:38 +00:00
|
|
|
case "pgFault":
|
2020-07-01 12:47:56 +00:00
|
|
|
value, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.PgFault = value * 4 * 1024
|
2020-11-07 01:48:38 +00:00
|
|
|
case "pgMajFault":
|
2020-10-07 16:57:18 +00:00
|
|
|
value, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.PgMajFault = value * 4 * 1024
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
|
|
|
|
// calcuateAvailVmem is a fallback under kernel 3.14 where /proc/meminfo does not provide
|
|
|
|
// "MemAvailable:" column. It reimplements an algorithm from the link below
|
|
|
|
// https://github.com/giampaolo/psutil/pull/890
|
|
|
|
func calcuateAvailVmem(ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint64 {
|
|
|
|
var watermarkLow uint64
|
|
|
|
|
|
|
|
fn := common.HostProc("zoneinfo")
|
|
|
|
lines, err := common.ReadLines(fn)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ret.Free + ret.Cached // fallback under kernel 2.6.13
|
|
|
|
}
|
|
|
|
|
|
|
|
pagesize := uint64(os.Getpagesize())
|
|
|
|
watermarkLow = 0
|
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
|
|
|
|
if strings.HasPrefix(fields[0], "low") {
|
|
|
|
lowValue, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
lowValue = 0
|
|
|
|
}
|
|
|
|
watermarkLow += lowValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
watermarkLow *= pagesize
|
|
|
|
|
|
|
|
availMemory := ret.Free - watermarkLow
|
|
|
|
pageCache := retEx.ActiveFile + retEx.InactiveFile
|
|
|
|
pageCache -= uint64(math.Min(float64(pageCache/2), float64(watermarkLow)))
|
|
|
|
availMemory += pageCache
|
2020-11-07 01:48:38 +00:00
|
|
|
availMemory += ret.Sreclaimable - uint64(math.Min(float64(ret.Sreclaimable/2.0), float64(watermarkLow)))
|
2020-07-01 12:47:56 +00:00
|
|
|
|
|
|
|
if availMemory < 0 {
|
|
|
|
availMemory = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return availMemory
|
|
|
|
}
|