-
Notifications
You must be signed in to change notification settings - Fork 90
/
cgroup_linux.go
148 lines (125 loc) · 3.69 KB
/
cgroup_linux.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//go:build linux
// +build linux
/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"bufio"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/opencontainers/runc/libcontainer/cgroups"
)
// CheckCgroup2UnifiedMode return whether it is in cgroupv2 env
func CheckCgroup2UnifiedMode() bool {
return cgroups.IsCgroup2UnifiedMode()
}
func ReadTasksFile(file string) ([]string, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
var (
s = bufio.NewScanner(f)
result = []string{}
)
for s.Scan() {
if t := s.Text(); t != "" {
result = append(result, t)
}
}
return result, s.Err()
}
func GetCgroupParamInt(cgroupPath, cgroupFile string) (int64, error) {
fileName := filepath.Join(cgroupPath, cgroupFile)
contents, err := ioutil.ReadFile(fileName)
if err != nil {
return 0, err
}
trimmed := strings.TrimSpace(string(contents))
if trimmed == "max" {
return math.MaxInt64, nil
}
res, err := strconv.ParseInt(trimmed, 10, 64)
if err != nil {
return res, fmt.Errorf("unable to parse %q as a uint from Cgroup file %q", string(contents), fileName)
}
return res, nil
}
// ParseCgroupNumaValue parse cgroup numa stat files like memory.numa_stat, the format is like "anon N0=1686843392 N1=1069957120 N2=316747776 N3=163962880"
func ParseCgroupNumaValue(cgroupPath, cgroupFile string) (map[string]map[int]uint64, error) {
fileName := filepath.Join(cgroupPath, cgroupFile)
content, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
result := make(map[string]map[int]uint64)
lines := strings.Split(string(content), "\n")
for _, line := range lines {
cols := strings.Fields(line)
if len(cols) <= 1 {
continue
}
key := cols[0]
numaInfo := make(map[int]uint64)
for _, pair := range cols[1:] {
parts := strings.Split(pair, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("failed to parse line [%v]", line)
}
if !strings.HasPrefix(parts[0], "N") {
return nil, fmt.Errorf("failed to parse line [%v]", line)
}
numaID, err := strconv.Atoi(strings.TrimPrefix(parts[0], "N"))
if err != nil {
return nil, fmt.Errorf("failed to parse line [%v]", line)
}
val, err := strconv.ParseUint(parts[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse line [%v]", line)
}
numaInfo[numaID] = val
}
if len(numaInfo) > 0 {
result[key] = numaInfo
}
}
return result, nil
}
// WriteFileIfChange writes data to the cgroup joined by dir and
// file if new data is not equal to the old data and return the old data.
func WriteFileIfChange(dir, file, data string) (error, bool, string) {
oldData, err := cgroups.ReadFile(dir, file)
if err != nil {
return err, false, ""
}
if strings.TrimSpace(data) != strings.TrimSpace(oldData) {
if err := cgroups.WriteFile(dir, file, data); err != nil {
return err, false, oldData
} else {
return nil, true, oldData
}
}
return nil, false, oldData
}
// IsCPUIdleSupported checks if cpu idle supported by
// checking if the cpu.idle interface file exists
func IsCPUIdleSupported() bool {
_, err := GetKubernetesAnyExistAbsCgroupPath(CgroupSubsysCPU, "cpu.idle")
return err == nil
}