forked from cloudflare/ebpf_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgroup.go
82 lines (69 loc) · 1.92 KB
/
cgroup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package decoder
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"github.com/cloudflare/ebpf_exporter/config"
"github.com/iovisor/gobpf/bcc"
"golang.org/x/sys/unix"
)
// CGroup is a decoder that transforms cgroup id to path in cgroupfs
type CGroup struct {
cache map[uint64]string
}
// Decode transforms cgroup id to path in cgroupfs
func (c *CGroup) Decode(in []byte, _ config.Decoder) ([]byte, error) {
if c.cache == nil {
c.cache = map[uint64]string{}
}
cgroupID, err := strconv.Atoi(string(in))
if err != nil {
return nil, err
}
if path, ok := c.cache[uint64(cgroupID)]; ok {
return []byte(path), nil
}
if err := c.refreshCache(); err != nil {
log.Printf("Error refreshing cgroup id to path map: %s", err)
if path, ok := c.cache[uint64(cgroupID)]; ok {
// Cache unknown result on successful refresh.
return []byte(path), nil
}
return []byte(fmt.Sprintf("unknown_cgroup_id:%d", cgroupID)), nil
}
if _, ok := c.cache[uint64(cgroupID)]; !ok {
// Cache unknown result on successful refresh.
c.cache[uint64(cgroupID)] = fmt.Sprintf("unknown_cgroup_id:%d", cgroupID)
}
return []byte(c.cache[uint64(cgroupID)]), nil
}
func (c *CGroup) refreshCache() error {
byteOrder := bcc.GetHostByteOrder()
newCache := make(map[uint64]string, len(c.cache)) // Reset cache.
if err := filepath.Walk("/sys/fs/cgroup", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
handle, _, err := unix.NameToHandleAt(unix.AT_FDCWD, path, 0)
if err != nil {
log.Printf("Error resolving handle of %s: %s", path, err)
return nil
}
newCache[byteOrder.Uint64(handle.Bytes())] = path
return nil
}); err != nil {
// Update cache on error cases, to have at least partial results.
for k, v := range newCache {
c.cache[k] = v
}
return err
}
c.cache = newCache
log.Println("refreshed cgroup cache, keys: ", len(c.cache))
return nil
}