Skip to content

Commit

Permalink
Add new method to get memory ignoring cgroups
Browse files Browse the repository at this point in the history
In PR #50, the behavior of GetMem() was changed to return the amount of
memory used / available within the current cgroup. There are some use
cases that you don't want cgroup-constrained memory information. A new
GetMemIgnoringCGroups method has been introduced to provide the system
wide memory information.

[#179517901] Update Bosh Agent to correctly report memory used in `bosh vms --vitals` and related code paths, and publish new stemcells

Signed-off-by: Brian Upton <bupton@vmware.com>
  • Loading branch information
klakin-pivotal authored and Ops Manager committed Sep 9, 2021
1 parent 299d387 commit cb8b796
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions concrete_sigar.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func (c *ConcreteSigar) GetMem() (Mem, error) {
return m, err
}

func (c *ConcreteSigar) GetMemIgnoringCGroups() (Mem, error) {
m := Mem{}
err := m.GetIgnoringCGroups()
return m, err
}

func (c *ConcreteSigar) GetSwap() (Swap, error) {
s := Swap{}
err := s.Get()
Expand Down
3 changes: 3 additions & 0 deletions concrete_sigar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ var _ = Describe("ConcreteSigar", func() {

Expect(mem.Total).To(BeNumerically(">", 0))
Expect(mem.Used + mem.Free).To(BeNumerically("<=", mem.Total))

mem, err = concreteSigar.GetMemIgnoringCGroups()
Expect(err).ToNot(HaveOccurred())
})

It("GetSwap", func() {
Expand Down
4 changes: 4 additions & 0 deletions sigar_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (self *Mem) Get() error {
return nil
}

func (self *Mem) GetIgnoringCGroups() error {
return self.Get()
}

type xsw_usage struct {
Total, Avail, Used uint64
}
Expand Down
12 changes: 12 additions & 0 deletions sigar_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ func (self *Uptime) Get() error {
}

func (self *Mem) Get() error {
return self.get(false)
}

func (self *Mem) GetIgnoringCGroups() error {
return self.get(true)
}

func (self *Mem) get(ignoreCGroups bool) error {
var available uint64 = MaxUint64
var buffers, cached uint64
table := map[string]*uint64{
Expand All @@ -123,6 +131,10 @@ func (self *Mem) Get() error {
self.Used = self.Total - self.Free
self.ActualUsed = self.Total - self.ActualFree

if ignoreCGroups {
return nil
}

// Instead of detecting if this code is run within a container
// or not (*), we simply attempt to retrieve the cgroup
// information about memory limits and usage and if present
Expand Down
21 changes: 21 additions & 0 deletions sigar_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,27 @@ DirectMap2M: 34983936 kB`)
})
})

Describe("When cgroups are enabled but the GetIgnoringCGroups method is called", func() {
// cgroup = '/user'
// The other tests use cgroup = '/'. This reduces the amount of changes needed
BeforeEach(func() {
cgroupSetup(`4:memory:/user`)
memInfoWithMemAvailable()
memLimitSetup2(`/user`, `21390950400`)
memUsageWithSwap(`/user`)
})

It("returns the system memory info", func() {
mem := Mem{}
err := mem.GetIgnoringCGroups()
Expect(err).ToNot(HaveOccurred())

Expect(mem.Total).To(BeNumerically("==", 35008180*1024))
Expect(mem.Free).To(BeNumerically("==", 487816*1024))
Expect(mem.ActualFree).To(BeNumerically("==", 20913400*1024))
Expect(mem.ActualUsed).To(BeNumerically("==", 14433054720))
})
})
})

Describe("Swap", func() {
Expand Down
4 changes: 4 additions & 0 deletions sigar_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (m *Mem) Get() error {
return nil
}

func (m *Mem) GetIgnoringCGroups() error {
return m.Get()
}

func (s *Swap) Get() error {
const MB = 1024 * 1024
out, err := exec.Command("wmic", "pagefile", "list", "full").Output()
Expand Down

0 comments on commit cb8b796

Please sign in to comment.