Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ws-daemon/pkg/cpulimit/cfs_test.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cpulimit
6
7
import (
8
"math"
9
"os"
10
"path/filepath"
11
"strconv"
12
"testing"
13
"time"
14
15
"github.com/opencontainers/runc/libcontainer/cgroups"
16
)
17
18
func init() {
19
cgroups.TestMode = true
20
}
21
22
func createTempDir(t *testing.T, subsystem string) string {
23
path := filepath.Join(t.TempDir(), subsystem)
24
if err := os.Mkdir(path, 0o755); err != nil {
25
t.Fatal(err)
26
}
27
return path
28
}
29
30
func TestCfsSetLimit(t *testing.T) {
31
type test struct {
32
beforeCfsPeriodUs int
33
beforeCfsQuotaUs int
34
bandWidth Bandwidth
35
cfsQuotaUs int
36
changed bool
37
}
38
tests := []test{
39
{
40
beforeCfsPeriodUs: 10000,
41
beforeCfsQuotaUs: -1,
42
bandWidth: Bandwidth(6000),
43
cfsQuotaUs: 60000,
44
changed: true,
45
},
46
{
47
beforeCfsPeriodUs: 5000,
48
beforeCfsQuotaUs: -1,
49
bandWidth: Bandwidth(6000),
50
cfsQuotaUs: 30000,
51
changed: true,
52
},
53
{
54
beforeCfsPeriodUs: 10000,
55
beforeCfsQuotaUs: 60000,
56
bandWidth: Bandwidth(6000),
57
cfsQuotaUs: 60000,
58
changed: false,
59
},
60
}
61
for _, tc := range tests {
62
tempdir := createTempDir(t, "cpu")
63
err := cgroups.WriteFile(tempdir, "cpu.cfs_period_us", strconv.Itoa(tc.beforeCfsPeriodUs))
64
if err != nil {
65
t.Fatal(err)
66
}
67
err = cgroups.WriteFile(tempdir, "cpu.cfs_quota_us", strconv.Itoa(tc.beforeCfsQuotaUs))
68
if err != nil {
69
t.Fatal(err)
70
}
71
72
cfs := CgroupV1CFSController(tempdir)
73
changed, err := cfs.SetLimit(tc.bandWidth)
74
if err != nil {
75
t.Fatal(err)
76
}
77
if changed != tc.changed {
78
t.Fatalf("unexpected error: changed is '%v' but expected '%v'", changed, tc.changed)
79
}
80
cfsQuotaUs, err := cgroups.ReadFile(tempdir, "cpu.cfs_quota_us")
81
if err != nil {
82
t.Fatal(err)
83
}
84
if cfsQuotaUs != strconv.Itoa(tc.cfsQuotaUs) {
85
t.Fatalf("unexpected error: cfsQuotaUs is '%v' but expected '%v'", cfsQuotaUs, tc.cfsQuotaUs)
86
}
87
}
88
}
89
90
func TestReadCfsQuota(t *testing.T) {
91
type test struct {
92
value int
93
expect int
94
}
95
tests := []test{
96
{
97
value: 100000,
98
expect: 100000,
99
},
100
{
101
value: -1,
102
expect: int(time.Duration(math.MaxInt64).Microseconds()),
103
},
104
}
105
106
for _, tc := range tests {
107
tempdir := createTempDir(t, "cpu")
108
err := cgroups.WriteFile(tempdir, "cpu.cfs_quota_us", strconv.Itoa(tc.value))
109
if err != nil {
110
t.Fatal(err)
111
}
112
113
cfs := CgroupV1CFSController(tempdir)
114
v, err := cfs.readCfsQuota()
115
if err != nil {
116
t.Fatal(err)
117
}
118
if v.Microseconds() != int64(tc.expect) {
119
t.Fatalf("unexpected error: cfs quota is '%v' but expected '%v'", v, tc.expect)
120
}
121
}
122
}
123
124
func TestReadCfsPeriod(t *testing.T) {
125
tests := []int{
126
10000,
127
}
128
for _, tc := range tests {
129
tempdir := createTempDir(t, "cpu")
130
err := cgroups.WriteFile(tempdir, "cpu.cfs_period_us", strconv.Itoa(tc))
131
if err != nil {
132
t.Fatal(err)
133
}
134
135
cfs := CgroupV1CFSController(tempdir)
136
v, err := cfs.readCfsPeriod()
137
if err != nil {
138
t.Fatal(err)
139
}
140
if v.Microseconds() != int64(tc) {
141
t.Fatalf("unexpected error: cfs period is '%v' but expected '%v'", v, tc)
142
}
143
}
144
}
145
146
func TestReadCpuUsage(t *testing.T) {
147
tests := []int{
148
0,
149
100000,
150
}
151
for _, tc := range tests {
152
tempdir := createTempDir(t, "cpu")
153
err := cgroups.WriteFile(tempdir, "cpuacct.usage", strconv.Itoa(tc))
154
if err != nil {
155
t.Fatal(err)
156
}
157
158
cfs := CgroupV1CFSController(tempdir)
159
v, err := cfs.readCpuUsage()
160
if err != nil {
161
t.Fatal(err)
162
}
163
if v.Nanoseconds() != int64(tc) {
164
t.Fatalf("unexpected error: cpu usage is '%v' but expected '%v'", v, tc)
165
}
166
}
167
}
168
169