Skip to content

Commit

Permalink
add ut for psi
Browse files Browse the repository at this point in the history
Signed-off-by: zouyee <zouyee1989@gmail.com>
  • Loading branch information
zouyee committed Dec 2, 2023
1 parent 2686038 commit f873e57
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
28 changes: 14 additions & 14 deletions cgroup2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,43 +482,43 @@ func getHugePageSizeFromFilenames(fileNames []string) ([]string, error) {
func getStatPSIFromFile(path string) *stats.PSIStats {
f, err := os.Open(path)
if err != nil {
return &stats.PSIStats{}
return nil
}
defer f.Close()

var psistats stats.PSIStats
psistats := &stats.PSIStats{}
sc := bufio.NewScanner(f)
for sc.Scan() {
parts := strings.Fields(sc.Text())
var pv *stats.PSIData
switch parts[0] {
case "some":
psistats.Some = pv
psistats.Some = &stats.PSIData{}
pv = psistats.Some
case "full":
psistats.Full = pv
psistats.Full = &stats.PSIData{}
pv = psistats.Full
}
if pv != nil {
pv, err = parsePSIData(parts[1:])
err = parsePSIData(parts[1:], pv)
if err != nil {
logrus.Errorf("unable to parse psi data: %v", err)
return &stats.PSIStats{}
return nil
}
}
}

if err := sc.Err(); err != nil {
return &stats.PSIStats{}
return nil
}
return &psistats
return psistats
}

func parsePSIData(psi []string) (*stats.PSIData, error) {
data := &stats.PSIData{}
func parsePSIData(psi []string, data *stats.PSIData) error {
for _, f := range psi {
kv := strings.SplitN(f, "=", 2)
if len(kv) != 2 {
return data, fmt.Errorf("invalid psi data: %q", f)
return fmt.Errorf("invalid psi data: %q", f)
}
var pv *float64
switch kv[0] {
Expand All @@ -531,19 +531,19 @@ func parsePSIData(psi []string) (*stats.PSIData, error) {
case "total":
v, err := strconv.ParseUint(kv[1], 10, 64)
if err != nil {
return data, fmt.Errorf("invalid %s PSI value: %w", kv[0], err)
return fmt.Errorf("invalid %s PSI value: %w", kv[0], err)
}
data.Total = v
}
if pv != nil {
v, err := strconv.ParseFloat(kv[1], 64)
if err != nil {
return data, fmt.Errorf("invalid %s PSI value: %w", kv[0], err)
return fmt.Errorf("invalid %s PSI value: %w", kv[0], err)
}
*pv = v
}
}
return data, nil
return nil
}

func getSubreaper() (int, error) {
Expand Down
38 changes: 37 additions & 1 deletion cgroup2/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
package cgroup2

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/containerd/cgroups/v3/cgroup2/stats"

"github.com/google/go-cmp/cmp"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/testing/protocmp"
)

func TestParseCgroupFromReader(t *testing.T) {
Expand All @@ -41,6 +47,36 @@ func TestParseCgroupFromReader(t *testing.T) {
}
}

func TestParseStatCPUPSI(t *testing.T) {
const examplePSIData = `some avg10=1.71 avg60=2.36 avg300=2.57 total=230548833
full avg10=1.00 avg60=1.01 avg300=1.00 total=157622356`

fakeCgroupDir := t.TempDir()
statPath := filepath.Join(fakeCgroupDir, "cpu.pressure")

if err := os.WriteFile(statPath, []byte(examplePSIData), 0o644); err != nil {
t.Fatal(err)
}

st := getStatPSIFromFile(filepath.Join(fakeCgroupDir, "cpu.pressure"))
if diff := cmp.Diff(st, stats.PSIStats{
Some: &stats.PSIData{
Avg10: 1.71,
Avg60: 2.36,
Avg300: 2.57,
Total: 230548833,
},
Full: &stats.PSIData{
Avg10: 1.00,
Avg60: 1.01,
Avg300: 1.00,
Total: 157622356,
},
}, protocmp.Transform()); diff != "" {
t.Errorf("unexpected PSI result: %+v", diff)
}
}

func TestToResources(t *testing.T) {
var (
quota int64 = 8000
Expand All @@ -63,6 +99,6 @@ func BenchmarkGetStatFileContentUint64(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
_ = getStatFileContentUint64("/proc/self/loginuid")
_ = getStatFileContentUint64("/proc/self/2loginuid")
}
}

0 comments on commit f873e57

Please sign in to comment.