From 9a2f1d398d2abb4da04ef20e629f4b77f93beb93 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Fri, 7 Apr 2023 15:34:38 -0700 Subject: [PATCH] cgroup2: rm some code Functions readSingleFile and getPidValue are only used for two files: pids.current and pids.max. It is easier and more efficient to use getStatFileContentUint64 for the same purpose. Switch to getStatFileContentUint64, remove the unused code. Signed-off-by: Kir Kolyshkin --- cgroup2/manager.go | 55 ++-------------------------------------------- 1 file changed, 2 insertions(+), 53 deletions(-) diff --git a/cgroup2/manager.go b/cgroup2/manager.go index aa95ec77..5101b3d2 100644 --- a/cgroup2/manager.go +++ b/cgroup2/manager.go @@ -21,7 +21,6 @@ import ( "context" "errors" "fmt" - "io" "math" "os" "path/filepath" @@ -519,11 +518,6 @@ func (c *Manager) MoveTo(destination *Manager) error { return nil } -var singleValueFiles = []string{ - "pids.current", - "pids.max", -} - func (c *Manager) Stat() (*stats.Metrics, error) { controllers, err := c.Controllers() if err != nil { @@ -541,14 +535,6 @@ func (c *Manager) Stat() (*stats.Metrics, error) { } } } - for _, name := range singleValueFiles { - if err := readSingleFile(c.path, name, out); err != nil { - if os.IsNotExist(err) { - continue - } - return nil, err - } - } memoryEvents := make(map[string]interface{}) if err := readKVStatsFile(c.path, "memory.events", memoryEvents); err != nil { if !os.IsNotExist(err) { @@ -558,8 +544,8 @@ func (c *Manager) Stat() (*stats.Metrics, error) { var metrics stats.Metrics metrics.Pids = &stats.PidsStat{ - Current: getPidValue("pids.current", out), - Limit: getPidValue("pids.max", out), + Current: getStatFileContentUint64(filepath.Join(c.path, "pids.current")), + Limit: getStatFileContentUint64(filepath.Join(c.path, "pids.max")), } metrics.CPU = &stats.CPUStat{ UsageUsec: getUint64Value("usage_usec", out), @@ -637,43 +623,6 @@ func getUint64Value(key string, out map[string]interface{}) uint64 { return 0 } -func getPidValue(key string, out map[string]interface{}) uint64 { - v, ok := out[key] - if !ok { - return 0 - } - switch t := v.(type) { - case uint64: - return t - case string: - if t == "max" { - return math.MaxUint64 - } - } - return 0 -} - -func readSingleFile(path string, file string, out map[string]interface{}) error { - f, err := os.Open(filepath.Join(path, file)) - if err != nil { - return err - } - defer f.Close() - data, err := io.ReadAll(f) - if err != nil { - return err - } - s := strings.TrimSpace(string(data)) - v, err := parseUint(s, 10, 64) - if err != nil { - // if we cannot parse as a uint, parse as a string - out[file] = s - return nil - } - out[file] = v - return nil -} - func readKVStatsFile(path string, file string, out map[string]interface{}) error { f, err := os.Open(filepath.Join(path, file)) if err != nil {