Skip to content

Commit 98fd586

Browse files
authored
Merge pull request #106 from zuzzas/cgroup-path-fix
Scan whole /sys/fs/cgroup directory in the cgroup decoder and use a faster filepath.WalkDir function
2 parents c3416ad + 3e5ff5a commit 98fd586

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

decoder/cgroup.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package decoder
22

33
import (
44
"fmt"
5+
"io/fs"
56
"log"
6-
"os"
77
"path/filepath"
88
"strconv"
99

@@ -18,7 +18,7 @@ type CGroup struct {
1818
}
1919

2020
// Decode transforms cgroup id to path in cgroupfs
21-
func (c *CGroup) Decode(in []byte, conf config.Decoder) ([]byte, error) {
21+
func (c *CGroup) Decode(in []byte, _ config.Decoder) ([]byte, error) {
2222
if c.cache == nil {
2323
c.cache = map[uint64][]byte{}
2424
}
@@ -46,12 +46,9 @@ func (c *CGroup) Decode(in []byte, conf config.Decoder) ([]byte, error) {
4646
func (c *CGroup) refreshCache() error {
4747
byteOrder := bcc.GetHostByteOrder()
4848

49-
cgroupPath := "/sys/fs/cgroup/unified"
50-
if _, err := os.Stat(cgroupPath); os.IsNotExist(err) {
51-
cgroupPath = "/sys/fs/cgroup"
52-
}
49+
cgroupPath := "/sys/fs/cgroup"
5350

54-
return filepath.Walk(cgroupPath, func(path string, info os.FileInfo, err error) error {
51+
return filepath.WalkDir(cgroupPath, func(path string, info fs.DirEntry, err error) error {
5552
if err != nil {
5653
return err
5754
}

decoder/cgroup_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ func TestCgroupDecoder(t *testing.T) {
3838
}
3939
}
4040
}
41+
42+
func BenchmarkCgroupRefresh(b *testing.B) {
43+
d := &CGroup{cache: map[uint64][]byte{}}
44+
b.ResetTimer()
45+
46+
for i := 0; i < b.N; i++ {
47+
err := d.refreshCache()
48+
if err != nil {
49+
b.Errorf("Failed to refresh cgroup cache: %s", err)
50+
}
51+
}
52+
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/cloudflare/ebpf_exporter
22

3-
go 1.14
3+
go 1.16
44

55
require (
66
github.com/alecthomas/units v0.0.0-20210912230133-d1bdfacee922 // indirect

0 commit comments

Comments
 (0)