33package cgroups
44
55import (
6- "bufio"
7- "errors"
8- "fmt"
9- "os"
10- "path/filepath"
116 "strconv"
127 "strings"
138
149 "github.com/opencontainers/cgroups"
15- "github.com/opencontainers/cgroups/fs"
16- "github.com/opencontainers/cgroups/fs2"
1710)
1811
19- type linuxBlkioHandler struct {
20- Blkio fs.BlkioGroup
21- }
22-
23- func getBlkioHandler () * linuxBlkioHandler {
24- return & linuxBlkioHandler {}
25- }
26-
27- // Apply set the specified constraints.
28- func (c * linuxBlkioHandler ) Apply (ctr * CgroupControl , res * cgroups.Resources ) error {
29- if ctr .cgroup2 {
30- man , err := fs2 .NewManager (ctr .config , filepath .Join (cgroupRoot , ctr .config .Path ))
31- if err != nil {
32- return err
33- }
34- return man .Set (res )
35- }
36- path := filepath .Join (cgroupRoot , Blkio , ctr .config .Path )
37- return c .Blkio .Set (path , res )
38- }
39-
40- // Create the cgroup.
41- func (c * linuxBlkioHandler ) Create (ctr * CgroupControl ) (bool , error ) {
42- if ctr .cgroup2 {
43- return false , nil
44- }
45- return ctr .createCgroupDirectory (Blkio )
46- }
47-
48- // Destroy the cgroup.
49- func (c * linuxBlkioHandler ) Destroy (ctr * CgroupControl ) error {
50- return rmDirRecursively (ctr .getCgroupv1Path (Blkio ))
51- }
52-
53- // Stat fills a metrics structure with usage stats for the controller.
54- func (c * linuxBlkioHandler ) Stat (ctr * CgroupControl , m * cgroups.Stats ) error {
12+ // blkioStat fills a metrics structure with usage stats for the blkio controller.
13+ func blkioStat (ctr * CgroupControl , m * cgroups.Stats ) error {
5514 var ioServiceBytesRecursive []cgroups.BlkioStatEntry
5615
57- if ctr .cgroup2 {
58- // more details on the io.stat file format:X https://facebookmicrosites.github.io/cgroup2/docs/io-controller.html
59- values , err := readCgroup2MapFile (ctr , "io.stat" )
16+ // more details on the io.stat file format:X https://facebookmicrosites.github.io/cgroup2/docs/io-controller.html
17+ values , err := readCgroup2MapFile (ctr , "io.stat" )
18+ if err != nil {
19+ return err
20+ }
21+ for k , v := range values {
22+ d := strings .Split (k , ":" )
23+ if len (d ) != 2 {
24+ continue
25+ }
26+ minor , err := strconv .ParseUint (d [0 ], 10 , 0 )
6027 if err != nil {
6128 return err
6229 }
63- for k , v := range values {
64- d := strings .Split (k , ":" )
65- if len (d ) != 2 {
66- continue
67- }
68- minor , err := strconv .ParseUint (d [0 ], 10 , 0 )
69- if err != nil {
70- return err
71- }
72- major , err := strconv .ParseUint (d [1 ], 10 , 0 )
73- if err != nil {
74- return err
75- }
76-
77- for _ , item := range v {
78- d := strings .Split (item , "=" )
79- if len (d ) != 2 {
80- continue
81- }
82- op := d [0 ]
83-
84- // Accommodate the cgroup v1 naming
85- switch op {
86- case "rbytes" :
87- op = "read"
88- case "wbytes" :
89- op = "write"
90- }
91-
92- value , err := strconv .ParseUint (d [1 ], 10 , 0 )
93- if err != nil {
94- return err
95- }
96-
97- entry := cgroups.BlkioStatEntry {
98- Op : op ,
99- Major : major ,
100- Minor : minor ,
101- Value : value ,
102- }
103- ioServiceBytesRecursive = append (ioServiceBytesRecursive , entry )
104- }
105- }
106- } else {
107- BlkioRoot := ctr .getCgroupv1Path (Blkio )
108-
109- p := filepath .Join (BlkioRoot , "blkio.throttle.io_service_bytes_recursive" )
110- f , err := os .Open (p )
30+ major , err := strconv .ParseUint (d [1 ], 10 , 0 )
11131 if err != nil {
112- if errors .Is (err , os .ErrNotExist ) {
113- return nil
114- }
115- return fmt .Errorf ("open %s: %w" , p , err )
32+ return err
11633 }
117- defer f .Close ()
11834
119- scanner := bufio .NewScanner (f )
120- for scanner .Scan () {
121- line := scanner .Text ()
122- parts := strings .Fields (line )
123- if len (parts ) < 3 {
124- continue
125- }
126- d := strings .Split (parts [0 ], ":" )
35+ for _ , item := range v {
36+ d := strings .Split (item , "=" )
12737 if len (d ) != 2 {
12838 continue
12939 }
130- minor , err := strconv .ParseUint (d [0 ], 10 , 0 )
131- if err != nil {
132- return err
40+ op := d [0 ]
41+
42+ // Accommodate the cgroup v1 naming
43+ switch op {
44+ case "rbytes" :
45+ op = "read"
46+ case "wbytes" :
47+ op = "write"
13348 }
134- major , err := strconv .ParseUint (d [1 ], 10 , 0 )
135- if err != nil {
136- return err
137- }
138-
139- op := parts [1 ]
14049
141- value , err := strconv .ParseUint (parts [ 2 ], 10 , 0 )
50+ value , err := strconv .ParseUint (d [ 1 ], 10 , 0 )
14251 if err != nil {
14352 return err
14453 }
54+
14555 entry := cgroups.BlkioStatEntry {
14656 Op : op ,
14757 Major : major ,
@@ -150,9 +60,6 @@ func (c *linuxBlkioHandler) Stat(ctr *CgroupControl, m *cgroups.Stats) error {
15060 }
15161 ioServiceBytesRecursive = append (ioServiceBytesRecursive , entry )
15262 }
153- if err := scanner .Err (); err != nil {
154- return fmt .Errorf ("parse %s: %w" , p , err )
155- }
15663 }
15764 m .BlkioStats .IoServiceBytesRecursive = ioServiceBytesRecursive
15865 return nil
0 commit comments