Skip to content

Commit

Permalink
hotfix
Browse files Browse the repository at this point in the history
Signed-off-by: jason yang <jasonyangshadow@gmail.com>
  • Loading branch information
JasonYangShadow committed Oct 19, 2023
1 parent d6f953a commit 90674e0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 23 deletions.
7 changes: 4 additions & 3 deletions 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().WithPid()
statManager.WithCPU().WithMemory().WithMemorySwap().WithPid().WithBlkIO()
return statManager.All(), nil
}

Expand All @@ -52,8 +52,9 @@ func (c *CGroup) Marshal(buffer *bytes.Buffer) (*bytes.Buffer, error) {

// write stats
for _, stat := range stats {
key, val := stat()
fmt.Fprintf(buffer, "%s %f\n", key, val)
for k, v := range stat() {
fmt.Fprintf(buffer, "%s %f\n", k, v)
}
}

return buffer, 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, 4)
require.Len(t, funcs, 5)

var buffer bytes.Buffer
_, err = cgroup.Marshal(&buffer)
Expand Down
44 changes: 35 additions & 9 deletions internal/cgroup/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package parser
import (
"bytes"
"math"
"strings"
"syscall"
"time"

Expand All @@ -30,7 +31,7 @@ func (s *StatManager) add(fc StatFunc) *StatManager {
}

func (s *StatManager) WithCPU() *StatManager {
return s.add(func() (string, float64) {
return s.add(func() map[string]float64 {
nowTime := time.Now()

curTime := uint64(nowTime.UnixNano())
Expand All @@ -48,12 +49,12 @@ func (s *StatManager) WithCPU() *StatManager {
// update the saved metrics
s.prevTime = curTime
s.prevCPU = curCPU
return "cpu_usage", cpuPercent
return map[string]float64{"cpu_usage": cpuPercent}
})
}

func (s *StatManager) WithMemory() *StatManager {
return s.add(func() (string, float64) {
return s.add(func() map[string]float64 {
memUsage := s.MemoryStats.Usage.Usage
memLimit := s.MemoryStats.Usage.Limit
memPercent := 0.0
Expand All @@ -69,12 +70,12 @@ func (s *StatManager) WithMemory() *StatManager {
if memLimit != 0 {
memPercent = float64(memUsage) / float64(memLimit) * 100.0
}
return "memory_usage", memPercent
return map[string]float64{"memory_usage": memPercent}
})
}

func (s *StatManager) WithMemorySwap() *StatManager {
return s.add(func() (string, float64) {
return s.add(func() map[string]float64 {
swapUsage := s.MemoryStats.SwapUsage.Usage
swapLimit := s.MemoryStats.SwapUsage.Limit
swapPercent := 0.0
Expand All @@ -90,21 +91,46 @@ func (s *StatManager) WithMemorySwap() *StatManager {
if swapLimit != 0 {
swapPercent = float64(swapUsage) / float64(swapLimit) * 100.0
}
return "memory_swap_usage", swapPercent
return map[string]float64{"memory_swap_usage": swapPercent}
})
}

func (s *StatManager) WithPid() *StatManager {
return s.add(func() (string, float64) {
return "pid_usage", float64(s.PidsStats.Current)
return s.add(func() map[string]float64 {
pidUsage := s.PidsStats.Current
pidLimit := s.PidsStats.Limit
pidPercent := 0.0

if pidLimit != 0 {
pidPercent = float64(pidUsage) / float64(pidLimit) * 100.0
}
return map[string]float64{"pid_usage": pidPercent}
})
}

func (s *StatManager) WithBlkIO() *StatManager {
return s.add(func() map[string]float64 {
var read, write float64
for _, entry := range s.BlkioStats.IoQueuedRecursive {
switch strings.ToLower(entry.Op) {
case "read":
read += float64(entry.Value)
case "write":
write += float64(entry.Value)
}
}
return map[string]float64{
"blkio_read": read,
"blkio_write": write,
}
})
}

func (s *StatManager) All() []StatFunc {
return s.funcs
}

type StatFunc func() (string, float64)
type StatFunc func() map[string]float64

type Stat interface {
CreateStats() ([]StatFunc, error)
Expand Down
24 changes: 14 additions & 10 deletions internal/cgroup/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ func TestParser(t *testing.T) {
}

require.NotNil(t, mgr)
allFuncs := mgr.WithCPU().WithMemory().WithMemorySwap().WithPid().All()
require.Len(t, allFuncs, 4)
allFuncs := mgr.WithCPU().WithMemory().WithMemorySwap().WithPid().WithBlkIO().All()
require.Len(t, allFuncs, 5)

_, usage := allFuncs[0]()
require.Equal(t, 0.0, usage)
usage := allFuncs[0]()
require.Equal(t, 0.0, usage["cpu_usage"])

_, usage = allFuncs[1]()
require.Equal(t, 0.0, usage)
usage = allFuncs[1]()
require.Equal(t, 0.0, usage["memory_usage"])

_, usage = allFuncs[2]()
require.Equal(t, 0.0, usage)
usage = allFuncs[2]()
require.Equal(t, 0.0, usage["memory_swap_usage"])

_, usage = allFuncs[3]()
require.Equal(t, 0.0, usage)
usage = allFuncs[3]()
require.Equal(t, 0.0, usage["pid_usage"])

usage = allFuncs[4]()
require.Equal(t, 0.0, usage["blkio_read"])
require.Equal(t, 0.0, usage["blkio_write"])
}

0 comments on commit 90674e0

Please sign in to comment.