From 90674e0f112fe1696156d806f0aa817b148d64d3 Mon Sep 17 00:00:00 2001 From: jason yang Date: Thu, 19 Oct 2023 00:44:49 +0000 Subject: [PATCH] hotfix Signed-off-by: jason yang --- internal/cgroup/cgroup.go | 7 +++-- internal/cgroup/cgroup_test.go | 2 +- internal/cgroup/parser/parser.go | 44 +++++++++++++++++++++------ internal/cgroup/parser/parser_test.go | 24 +++++++++------ 4 files changed, 54 insertions(+), 23 deletions(-) diff --git a/internal/cgroup/cgroup.go b/internal/cgroup/cgroup.go index 5f99ed6..a9ed8ee 100644 --- a/internal/cgroup/cgroup.go +++ b/internal/cgroup/cgroup.go @@ -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 } @@ -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 diff --git a/internal/cgroup/cgroup_test.go b/internal/cgroup/cgroup_test.go index 9a9333b..a8f154e 100644 --- a/internal/cgroup/cgroup_test.go +++ b/internal/cgroup/cgroup_test.go @@ -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) diff --git a/internal/cgroup/parser/parser.go b/internal/cgroup/parser/parser.go index 17da31f..5e3c2ad 100644 --- a/internal/cgroup/parser/parser.go +++ b/internal/cgroup/parser/parser.go @@ -6,6 +6,7 @@ package parser import ( "bytes" "math" + "strings" "syscall" "time" @@ -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()) @@ -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 @@ -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 @@ -90,13 +91,38 @@ 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, + } }) } @@ -104,7 +130,7 @@ 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) diff --git a/internal/cgroup/parser/parser_test.go b/internal/cgroup/parser/parser_test.go index 72e65b4..2dee249 100644 --- a/internal/cgroup/parser/parser_test.go +++ b/internal/cgroup/parser/parser_test.go @@ -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"]) }