Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual available memory calculation #626

Merged
merged 6 commits into from
Mar 1, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion mem/mem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package mem

import (
"context"
"math"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -103,8 +105,9 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
ret.Cached += ret.SReclaimable

if !memavail {
ret.Available = ret.Free + ret.Buffers + ret.Cached
ret.Available = calcuateAvailVmem(ret)
}

ret.Used = ret.Total - ret.Free - ret.Buffers - ret.Cached
ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0

Expand Down Expand Up @@ -156,3 +159,39 @@ func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
}
return ret, nil
}

func calcuateAvailVmem(ret *VirtualMemoryStat) uint64 {
mznet marked this conversation as resolved.
Show resolved Hide resolved
var watermarkLow uint64
fn := common.HostProc("zoneinfo")
lines, _ := common.ReadLines(fn)
mznet marked this conversation as resolved.
Show resolved Hide resolved
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 := ret.Active + ret.Inactive
pageCache -= uint64(math.Min(float64(pageCache/2), float64(watermarkLow)))
availMemory += pageCache
availMemory += ret.SReclaimable - uint64(math.Min(float64(ret.SReclaimable/2.0), float64(watermarkLow)))

if availMemory < 0 {
availMemory = 0
}

return availMemory
}