...
1
16
17 package cgroups
18
19 import (
20 "os"
21 "path/filepath"
22 "strconv"
23 "strings"
24
25 v1 "github.com/containerd/cgroups/stats/v1"
26 specs "github.com/opencontainers/runtime-spec/specs-go"
27 )
28
29 func NewPids(root string) *pidsController {
30 return &pidsController{
31 root: filepath.Join(root, string(Pids)),
32 }
33 }
34
35 type pidsController struct {
36 root string
37 }
38
39 func (p *pidsController) Name() Name {
40 return Pids
41 }
42
43 func (p *pidsController) Path(path string) string {
44 return filepath.Join(p.root, path)
45 }
46
47 func (p *pidsController) Create(path string, resources *specs.LinuxResources) error {
48 if err := os.MkdirAll(p.Path(path), defaultDirPerm); err != nil {
49 return err
50 }
51 if resources.Pids != nil && resources.Pids.Limit > 0 {
52 return retryingWriteFile(
53 filepath.Join(p.Path(path), "pids.max"),
54 []byte(strconv.FormatInt(resources.Pids.Limit, 10)),
55 defaultFilePerm,
56 )
57 }
58 return nil
59 }
60
61 func (p *pidsController) Update(path string, resources *specs.LinuxResources) error {
62 return p.Create(path, resources)
63 }
64
65 func (p *pidsController) Stat(path string, stats *v1.Metrics) error {
66 current, err := readUint(filepath.Join(p.Path(path), "pids.current"))
67 if err != nil {
68 return err
69 }
70 var max uint64
71 maxData, err := os.ReadFile(filepath.Join(p.Path(path), "pids.max"))
72 if err != nil {
73 return err
74 }
75 if maxS := strings.TrimSpace(string(maxData)); maxS != "max" {
76 if max, err = parseUint(maxS, 10, 64); err != nil {
77 return err
78 }
79 }
80 stats.Pids = &v1.PidsStat{
81 Current: current,
82 Limit: max,
83 }
84 return nil
85 }
86
View as plain text