Path: blob/main/components/ws-daemon/pkg/cpulimit/cfs_test.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cpulimit56import (7"math"8"os"9"path/filepath"10"strconv"11"testing"12"time"1314"github.com/opencontainers/runc/libcontainer/cgroups"15)1617func init() {18cgroups.TestMode = true19}2021func createTempDir(t *testing.T, subsystem string) string {22path := filepath.Join(t.TempDir(), subsystem)23if err := os.Mkdir(path, 0o755); err != nil {24t.Fatal(err)25}26return path27}2829func TestCfsSetLimit(t *testing.T) {30type test struct {31beforeCfsPeriodUs int32beforeCfsQuotaUs int33bandWidth Bandwidth34cfsQuotaUs int35changed bool36}37tests := []test{38{39beforeCfsPeriodUs: 10000,40beforeCfsQuotaUs: -1,41bandWidth: Bandwidth(6000),42cfsQuotaUs: 60000,43changed: true,44},45{46beforeCfsPeriodUs: 5000,47beforeCfsQuotaUs: -1,48bandWidth: Bandwidth(6000),49cfsQuotaUs: 30000,50changed: true,51},52{53beforeCfsPeriodUs: 10000,54beforeCfsQuotaUs: 60000,55bandWidth: Bandwidth(6000),56cfsQuotaUs: 60000,57changed: false,58},59}60for _, tc := range tests {61tempdir := createTempDir(t, "cpu")62err := cgroups.WriteFile(tempdir, "cpu.cfs_period_us", strconv.Itoa(tc.beforeCfsPeriodUs))63if err != nil {64t.Fatal(err)65}66err = cgroups.WriteFile(tempdir, "cpu.cfs_quota_us", strconv.Itoa(tc.beforeCfsQuotaUs))67if err != nil {68t.Fatal(err)69}7071cfs := CgroupV1CFSController(tempdir)72changed, err := cfs.SetLimit(tc.bandWidth)73if err != nil {74t.Fatal(err)75}76if changed != tc.changed {77t.Fatalf("unexpected error: changed is '%v' but expected '%v'", changed, tc.changed)78}79cfsQuotaUs, err := cgroups.ReadFile(tempdir, "cpu.cfs_quota_us")80if err != nil {81t.Fatal(err)82}83if cfsQuotaUs != strconv.Itoa(tc.cfsQuotaUs) {84t.Fatalf("unexpected error: cfsQuotaUs is '%v' but expected '%v'", cfsQuotaUs, tc.cfsQuotaUs)85}86}87}8889func TestReadCfsQuota(t *testing.T) {90type test struct {91value int92expect int93}94tests := []test{95{96value: 100000,97expect: 100000,98},99{100value: -1,101expect: int(time.Duration(math.MaxInt64).Microseconds()),102},103}104105for _, tc := range tests {106tempdir := createTempDir(t, "cpu")107err := cgroups.WriteFile(tempdir, "cpu.cfs_quota_us", strconv.Itoa(tc.value))108if err != nil {109t.Fatal(err)110}111112cfs := CgroupV1CFSController(tempdir)113v, err := cfs.readCfsQuota()114if err != nil {115t.Fatal(err)116}117if v.Microseconds() != int64(tc.expect) {118t.Fatalf("unexpected error: cfs quota is '%v' but expected '%v'", v, tc.expect)119}120}121}122123func TestReadCfsPeriod(t *testing.T) {124tests := []int{12510000,126}127for _, tc := range tests {128tempdir := createTempDir(t, "cpu")129err := cgroups.WriteFile(tempdir, "cpu.cfs_period_us", strconv.Itoa(tc))130if err != nil {131t.Fatal(err)132}133134cfs := CgroupV1CFSController(tempdir)135v, err := cfs.readCfsPeriod()136if err != nil {137t.Fatal(err)138}139if v.Microseconds() != int64(tc) {140t.Fatalf("unexpected error: cfs period is '%v' but expected '%v'", v, tc)141}142}143}144145func TestReadCpuUsage(t *testing.T) {146tests := []int{1470,148100000,149}150for _, tc := range tests {151tempdir := createTempDir(t, "cpu")152err := cgroups.WriteFile(tempdir, "cpuacct.usage", strconv.Itoa(tc))153if err != nil {154t.Fatal(err)155}156157cfs := CgroupV1CFSController(tempdir)158v, err := cfs.readCpuUsage()159if err != nil {160t.Fatal(err)161}162if v.Nanoseconds() != int64(tc) {163t.Fatalf("unexpected error: cpu usage is '%v' but expected '%v'", v, tc)164}165}166}167168169