Skip to content

Commit

Permalink
fixes max memory available detection on linux (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
petethepig committed Jun 23, 2021
1 parent 5e74957 commit 5bee853
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 34 deletions.
37 changes: 3 additions & 34 deletions pkg/storage/mem.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,13 @@
// +build !linux

package storage

import (
"io/ioutil"
"os"
"strconv"
"strings"

"github.com/pyroscope-io/pyroscope/pkg/util/file"
"github.com/shirou/gopsutil/mem"
"github.com/sirupsen/logrus"
)

const memLimitPath = "/sys/fs/cgroup/memory/memory.limit_in_bytes"

func getCgroupMemLimit() (uint64, error) {
f, err := os.Open(memLimitPath)
if err != nil {
return 0, err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return 0, err
}
r, err := strconv.Atoi(strings.TrimSuffix(string(b), "\n"))
if err != nil {
return 0, err
}

return uint64(r), nil
}

// on linux we also look at cgroup mem limit
func getMemTotal() (uint64, error) {
if file.Exists(memLimitPath) {
v, err := getCgroupMemLimit()
if err == nil {
return v, nil
}
logrus.WithError(err).Warn("Could not read cgroup memory limit")
}

vm, err := mem.VirtualMemory()
if err != nil {
return 0, err
Expand Down
55 changes: 55 additions & 0 deletions pkg/storage/mem_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// +build linux

package storage

import (
"io/ioutil"
"os"
"strconv"
"strings"

"github.com/pyroscope-io/pyroscope/pkg/util/file"
"github.com/shirou/gopsutil/mem"
"github.com/sirupsen/logrus"
)

const memLimitPath = "/sys/fs/cgroup/memory/memory.limit_in_bytes"

func getCgroupMemLimit() (uint64, error) {
f, err := os.Open(memLimitPath)
if err != nil {
return 0, err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return 0, err
}
r, err := strconv.Atoi(strings.TrimSuffix(string(b), "\n"))
if err != nil {
return 0, err
}

return uint64(r), nil
}

func getMemTotal() (uint64, error) {
vm, err := mem.VirtualMemory()
if err != nil {
return 0, err
}

if file.Exists(memLimitPath) {
v, err := getCgroupMemLimit()
if err == nil {
if v < vm.Total {
return v, nil
}
return vm.Total, nil
}

logrus.WithError(err).Warn("Could not read cgroup memory limit")
return vm.Total, nil
}

return vm.Total, nil
}

0 comments on commit 5bee853

Please sign in to comment.