Skip to content

Commit

Permalink
update the metrics calculation
Browse files Browse the repository at this point in the history
Signed-off-by: jason yang <[email protected]>
  • Loading branch information
JasonYangShadow committed Oct 18, 2023
1 parent 8656846 commit d3b4db4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/cgroup/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *CGroup) CreateStats() ([]parser.StatFunc, error) {
}

statManager := &parser.StatManager{Stats: stat}
statManager.WithCPU().WithMemory().WithMemorySwap().WithMemoryKernel().WithPid()
statManager.WithCPU().WithMemory().WithMemorySwap().WithPid()
return statManager.All(), nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cgroup/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestCgroup(t *testing.T) {
funcs, err := cgroup.CreateStats()
require.NoError(t, err)
require.NotEmpty(t, funcs)
require.Len(t, funcs, 5)
require.Len(t, funcs, 4)

var buffer bytes.Buffer
_, err = cgroup.Marshal(&buffer)
Expand Down
63 changes: 55 additions & 8 deletions internal/cgroup/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package parser

import (
"bytes"
"math"
"syscall"
"time"

"github.com/opencontainers/runc/libcontainer/cgroups"
)
Expand All @@ -16,6 +19,9 @@ type Marshal interface {
type StatManager struct {
funcs []StatFunc
*cgroups.Stats

// for cpu metric
prevTime, prevCPU uint64
}

func (s *StatManager) add(fc StatFunc) *StatManager {
Expand All @@ -25,25 +31,66 @@ func (s *StatManager) add(fc StatFunc) *StatManager {

func (s *StatManager) WithCPU() *StatManager {
return s.add(func() (string, float64) {
return "cpu_usage", float64(s.CpuStats.CpuUsage.TotalUsage)
nowTime := time.Now()

curTime := uint64(nowTime.UnixNano())
curCPU := s.CpuStats.CpuUsage.TotalUsage

deltaCPU := float64(curCPU - s.prevCPU)
if s.prevTime == 0 {
// 500ms earlier by default
s.prevTime = uint64(nowTime.Truncate(500 * time.Millisecond).UnixNano())
}

deltaTime := float64(curTime - s.prevTime)
cpuPercent := (deltaCPU / deltaTime) * 100

// update the saved metrics
s.prevTime = curTime
s.prevCPU = curCPU
return "cpu_usage", float64(cpuPercent)
})
}

func (s *StatManager) WithMemory() *StatManager {
return s.add(func() (string, float64) {
return "memory_usage", float64(s.MemoryStats.Usage.Usage)
memUsage := s.MemoryStats.Usage.Usage
memLimit := s.MemoryStats.Usage.Limit
memPercent := 0.0

// If there is no limit, show system RAM instead of max uint64...
if memLimit == math.MaxUint64 {
in := &syscall.Sysinfo_t{}
err := syscall.Sysinfo(in)
if err == nil {
memLimit = uint64(in.Totalram) * uint64(in.Unit)
}
}
if memLimit != 0 {
memPercent = float64(memUsage) / float64(memLimit) * 100.0
}
return "memory_usage", float64(memPercent)

Check failure on line 72 in internal/cgroup/parser/parser.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
})
}

func (s *StatManager) WithMemorySwap() *StatManager {
return s.add(func() (string, float64) {
return "memory_swap_usage", float64(s.MemoryStats.SwapUsage.Usage)
})
}
swapUsage := s.MemoryStats.SwapUsage.Usage
swapLimit := s.MemoryStats.SwapUsage.Limit
swapPercent := 0.0

func (s *StatManager) WithMemoryKernel() *StatManager {
return s.add(func() (string, float64) {
return "memory_kernel_usage", float64(s.MemoryStats.KernelUsage.Usage)
// If there is no limit, show system RAM instead of max uint64...
if swapLimit == math.MaxUint64 {
in := &syscall.Sysinfo_t{}
err := syscall.Sysinfo(in)
if err == nil {
swapLimit = uint64(in.Totalswap) * uint64(in.Unit)

Check failure on line 87 in internal/cgroup/parser/parser.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
}
}
if swapLimit != 0 {
swapPercent = float64(swapUsage) / float64(swapLimit) * 100.0
}
return "memory_swap_usage", float64(swapPercent)

Check failure on line 93 in internal/cgroup/parser/parser.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cgroup/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ func TestParser(t *testing.T) {
}

require.NotNil(t, mgr)
require.Len(t, mgr.WithCPU().WithMemory().WithMemorySwap().WithMemoryKernel().WithPid().All(), 5)
require.Len(t, mgr.WithCPU().WithMemory().WithMemorySwap().WithPid().All(), 4)
}

0 comments on commit d3b4db4

Please sign in to comment.