Skip to content

Commit

Permalink
Improve performance by for pid stats (cgroups1) re-using readuint
Browse files Browse the repository at this point in the history
Benchstat results

➜  cgroups git:(main) ✗ benchstat old.txt new.txt
goos: linux
goarch: amd64
pkg: github.com/containerd/cgroups/v3/cgroup1
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
           │   old.txt   │              new.txt               │
           │   sec/op    │   sec/op     vs base               │
TestPids-8   19.32µ ± 4%   17.39µ ± 2%  -9.97% (p=0.000 n=10)

           │   old.txt   │              new.txt               │
           │    B/op     │    B/op     vs base                │
TestPids-8   1344.0 ± 0%   624.0 ± 0%  -53.57% (p=0.000 n=10)

           │  old.txt   │              new.txt               │
           │ allocs/op  │ allocs/op   vs base                │
TestPids-8   13.00 ± 0%   11.00 ± 0%  -15.38% (p=0.000 n=10)
➜  cgroups git:(main) ✗

Signed-off-by: Manu Gupta <manugupt1@gmail.com>
  • Loading branch information
manugupt1 committed Apr 28, 2023
1 parent fb1932a commit bd48e06
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
9 changes: 1 addition & 8 deletions cgroup1/pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"os"
"path/filepath"
"strconv"
"strings"

v1 "github.com/containerd/cgroups/v3/cgroup1/stats"
specs "github.com/opencontainers/runtime-spec/specs-go"
Expand Down Expand Up @@ -67,16 +66,10 @@ func (p *pidsController) Stat(path string, stats *v1.Metrics) error {
if err != nil {
return err
}
var max uint64
maxData, err := os.ReadFile(filepath.Join(p.Path(path), "pids.max"))
max, err := readUint(filepath.Join(p.Path(path), "pids.max"))
if err != nil {
return err
}
if maxS := strings.TrimSpace(string(maxData)); maxS != "max" {
if max, err = parseUint(maxS, 10, 64); err != nil {
return err
}
}
stats.Pids = &v1.PidsStat{
Current: current,
Limit: max,
Expand Down
44 changes: 44 additions & 0 deletions cgroup1/pids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,47 @@ func TestPidsOverflowMax(t *testing.T) {
t.Fatal("expected not nil err")
}
}

func BenchmarkTestPids(b *testing.B) {

mock, err := newMock(b)
if err != nil {
b.Fatal(err)
}
defer func() {
if err := mock.delete(); err != nil {
b.Errorf("failed delete: %v", err)
}
}()

pids := NewPids(mock.root)
if pids == nil {
b.Fatal("pids is nil")
}
resources := specs.LinuxResources{}
resources.Pids = &specs.LinuxPids{}
resources.Pids.Limit = int64(10)
err = pids.Create("test", &resources)
if err != nil {
b.Fatal(err)
}

current := filepath.Join(mock.root, "pids", "test", "pids.current")
if err = os.WriteFile(
current,
[]byte(strconv.Itoa(5)),
defaultFilePerm,
); err != nil {
b.Fatal(err)
}

b.ReportAllocs()

for i := 0; i < b.N; i++ {
metrics := v1.Metrics{}
err = pids.Stat("test", &metrics)
if err != nil {
b.Fatal(err)
}
}
}
2 changes: 1 addition & 1 deletion cgroup1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func readUint(path string) (uint64, error) {
}
defer f.Close()

b := make([]byte, 128) // Chose 128 as some files have leading/trailing whitespaces and alignment
b := make([]byte, 32) // Chose 32 as some files have leading/trailing whitespaces and alignment
n, err := f.Read(b)
if err != nil {
return 0, err
Expand Down

0 comments on commit bd48e06

Please sign in to comment.