Skip to content

Commit

Permalink
cgroup2: rm some code
Browse files Browse the repository at this point in the history
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 <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Apr 7, 2023
1 parent db40fde commit 9a2f1d3
Showing 1 changed file with 2 additions and 53 deletions.
55 changes: 2 additions & 53 deletions cgroup2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"errors"
"fmt"
"io"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9a2f1d3

Please sign in to comment.