Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Remove memory stats we don't use, move CPU/MemLimit
Browse files Browse the repository at this point in the history
We will end up caching the CPU and MemLimits so having them separate
makes this easier
  • Loading branch information
conorbranagan committed Aug 16, 2017
1 parent 103b243 commit dca5876
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
4 changes: 2 additions & 2 deletions checks/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ func fmtContainers(
Name: ctr.Name,
Id: ctr.ID,
Image: ctr.Image,
CpuLimit: float32(ctr.CPU.Limit),
CpuLimit: float32(ctr.CPULimit),
UserPct: calculateCtrPct(ctr.CPU.User, lastCtr.CPU.User, cpus, lastRun),
SystemPct: calculateCtrPct(ctr.CPU.System, lastCtr.CPU.System, cpus, lastRun),
TotalPct: calculateCtrPct(ctr.CPU.User+ctr.CPU.System, lastCtr.CPU.User+lastCtr.CPU.System, cpus, lastRun),
MemoryLimit: ctr.Memory.MemLimitInBytes,
MemoryLimit: ctr.MemLimit,
MemRss: ctr.Memory.RSS,
MemCache: ctr.Memory.Cache,
Created: ctr.Created,
Expand Down
4 changes: 2 additions & 2 deletions checks/container_rt.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ func fmtContainerStats(
UserPct: calculateCtrPct(ctr.CPU.User, lastCtr.CPU.User, cpus, lastRun),
SystemPct: calculateCtrPct(ctr.CPU.System, lastCtr.CPU.System, cpus, lastRun),
TotalPct: calculateCtrPct(ctr.CPU.User+ctr.CPU.System, lastCtr.CPU.User+lastCtr.CPU.System, cpus, lastRun),
CpuLimit: float32(ctr.CPU.Limit),
CpuLimit: float32(ctr.CPULimit),
MemRss: ctr.Memory.RSS,
MemCache: ctr.Memory.Cache,
MemLimit: ctr.Memory.MemLimitInBytes,
MemLimit: ctr.MemLimit,
Rbps: calculateRate(ctr.IO.ReadBytes, lastCtr.IO.ReadBytes, lastRun),
Wbps: calculateRate(ctr.IO.WriteBytes, lastCtr.IO.WriteBytes, lastRun),
NetRcvdPs: calculateRate(ctr.Network.PacketsRcvd, lastCtr.Network.PacketsRcvd, lastRun),
Expand Down
46 changes: 24 additions & 22 deletions util/docker/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ type CgroupMemStat struct {
TotalActiveFile uint64
TotalUnevictable uint64
MemUsageInBytes uint64
MemMaxUsageInBytes uint64
MemLimitInBytes uint64
MemFailCnt uint64
}

Expand All @@ -64,7 +62,6 @@ type CgroupTimesStat struct {
ContainerID string
System uint64
User uint64
Limit float64
}

// CgroupIOStat store I/O statistics about a cgroup.
Expand Down Expand Up @@ -162,24 +159,35 @@ func (c ContainerCgroup) Mem() (*CgroupMemStat, error) {
ret.TotalUnevictable = v
}
}
return ret, nil
}

r, err := c.readCgroupMemFile("memory.usage_in_bytes")
if err == nil {
ret.MemUsageInBytes = r
// MemLimit returns the memory limit of the cgroup, if it exists. If the file does not
// exist or there is no limit then this will default to 0.
func (c ContainerCgroup) MemLimit() (uint64, error) {
statfile, err := c.cgroupFilePath("memory", "memory.limit_in_bytes")
if err != nil {
return 0, err
}
lines, err := util.ReadLines(statfile)
if os.IsNotExist(err) {
return 0, err
} else if err != nil {
return 0, err
}
r, err = c.readCgroupMemFile("memory.max_usage_in_bytes")
if err == nil {
ret.MemMaxUsageInBytes = r
if len(lines) != 1 {
return 0, fmt.Errorf("wrong format file: %s", statfile)
}
r, err = c.readCgroupMemFile("memory.limit_in_bytes")
if err == nil {
ret.MemLimitInBytes = r
v, err := strconv.ParseUint(lines[0], 10, 64)
if err != nil {
return 0, err
}
r, err = c.readCgroupMemFile("memory.failcnt")
if err == nil {
ret.MemFailCnt = r
// limit_in_bytes is a special case here, it's possible that it shows a ridiculous number,
// in which case it represents unlimited, so return 0 here
if v > uint64(math.Pow(2, 60)) {
v = 0
}
return ret, nil
return v, nil
}

// CPU returns the CPU status for this cgroup instance
Expand Down Expand Up @@ -212,12 +220,6 @@ func (c ContainerCgroup) CPU() (*CgroupTimesStat, error) {
}
}
}
limit, err := c.CPULimit()
if err != nil {
return nil, err
}
ret.Limit = limit

return ret, nil
}

Expand Down
18 changes: 14 additions & 4 deletions util/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,12 @@ type Container struct {
Health string
Pids []int32

CPU *CgroupTimesStat
Memory *CgroupMemStat
IO *CgroupIOStat
Network *NetworkStat
CPULimit float64
MemLimit uint64
CPU *CgroupTimesStat
Memory *CgroupMemStat
IO *CgroupIOStat
Network *NetworkStat
}

type dockerNetwork struct {
Expand Down Expand Up @@ -385,6 +387,14 @@ func (d *dockerUtil) containers(pids []int32) ([]*Container, error) {
if err != nil {
return nil, err
}
container.CPULimit, err = cgroup.CPULimit()
if err != nil {
return nil, err
}
container.MemLimit, err = cgroup.MemLimit()
if err != nil {
return nil, err
}

if d.cfg.CollectNetwork {
d.Lock()
Expand Down

0 comments on commit dca5876

Please sign in to comment.