Skip to content

Commit a1771db

Browse files
kolyshkindims
authored andcommitted
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 <kolyshkin@gmail.com>
1 parent db40fde commit a1771db

1 file changed

Lines changed: 2 additions & 53 deletions

File tree

cgroup2/manager.go

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24-
"io"
2524
"math"
2625
"os"
2726
"path/filepath"
@@ -519,11 +518,6 @@ func (c *Manager) MoveTo(destination *Manager) error {
519518
return nil
520519
}
521520

522-
var singleValueFiles = []string{
523-
"pids.current",
524-
"pids.max",
525-
}
526-
527521
func (c *Manager) Stat() (*stats.Metrics, error) {
528522
controllers, err := c.Controllers()
529523
if err != nil {
@@ -541,14 +535,6 @@ func (c *Manager) Stat() (*stats.Metrics, error) {
541535
}
542536
}
543537
}
544-
for _, name := range singleValueFiles {
545-
if err := readSingleFile(c.path, name, out); err != nil {
546-
if os.IsNotExist(err) {
547-
continue
548-
}
549-
return nil, err
550-
}
551-
}
552538
memoryEvents := make(map[string]interface{})
553539
if err := readKVStatsFile(c.path, "memory.events", memoryEvents); err != nil {
554540
if !os.IsNotExist(err) {
@@ -558,8 +544,8 @@ func (c *Manager) Stat() (*stats.Metrics, error) {
558544
var metrics stats.Metrics
559545

560546
metrics.Pids = &stats.PidsStat{
561-
Current: getPidValue("pids.current", out),
562-
Limit: getPidValue("pids.max", out),
547+
Current: getStatFileContentUint64(filepath.Join(c.path, "pids.current")),
548+
Limit: getStatFileContentUint64(filepath.Join(c.path, "pids.max")),
563549
}
564550
metrics.CPU = &stats.CPUStat{
565551
UsageUsec: getUint64Value("usage_usec", out),
@@ -637,43 +623,6 @@ func getUint64Value(key string, out map[string]interface{}) uint64 {
637623
return 0
638624
}
639625

640-
func getPidValue(key string, out map[string]interface{}) uint64 {
641-
v, ok := out[key]
642-
if !ok {
643-
return 0
644-
}
645-
switch t := v.(type) {
646-
case uint64:
647-
return t
648-
case string:
649-
if t == "max" {
650-
return math.MaxUint64
651-
}
652-
}
653-
return 0
654-
}
655-
656-
func readSingleFile(path string, file string, out map[string]interface{}) error {
657-
f, err := os.Open(filepath.Join(path, file))
658-
if err != nil {
659-
return err
660-
}
661-
defer f.Close()
662-
data, err := io.ReadAll(f)
663-
if err != nil {
664-
return err
665-
}
666-
s := strings.TrimSpace(string(data))
667-
v, err := parseUint(s, 10, 64)
668-
if err != nil {
669-
// if we cannot parse as a uint, parse as a string
670-
out[file] = s
671-
return nil
672-
}
673-
out[file] = v
674-
return nil
675-
}
676-
677626
func readKVStatsFile(path string, file string, out map[string]interface{}) error {
678627
f, err := os.Open(filepath.Join(path, file))
679628
if err != nil {

0 commit comments

Comments
 (0)