Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgroup: refine the handling of errNoCPUControllerDetected #41475

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion util/cgroup/cgroup_cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,54 @@
package cgroup

import (
"fmt"
"regexp"
"runtime"
"strconv"
"sync"
"syscall"
"testing"

"github.com/stretchr/testify/require"
)

func checkKernelVersionNewerThan(t *testing.T, major, minor int) bool {
u := syscall.Utsname{}
err := syscall.Uname(&u)
require.NoError(t, err)
releaseBs := make([]byte, 0, len(u.Release))
for _, v := range u.Release {
if v == 0 {
break
}
releaseBs = append(releaseBs, byte(v))
}
releaseStr := string(releaseBs)
t.Log("kernel release string:", releaseStr)
versionInfoRE := regexp.MustCompile(`[0-9]+\.[0-9]+\.[0-9]+`)
kernelVerion := versionInfoRE.FindAllString(releaseStr, 1)
require.Equal(t, 1, len(kernelVerion), fmt.Sprintf("release str is %s", releaseStr))
kernelVersionPartRE := regexp.MustCompile(`[0-9]+`)
kernelVersionParts := kernelVersionPartRE.FindAllString(kernelVerion[0], -1)
require.Equal(t, 3, len(kernelVersionParts), fmt.Sprintf("kernel verion str is %s", kernelVerion[0]))
t.Logf("parsed kernel version parts: major %s, minor %s, patch %s",
kernelVersionParts[0], kernelVersionParts[1], kernelVersionParts[2])
mustConvInt := func(s string) int {
i, err := strconv.Atoi(s)
require.NoError(t, err, s)
return i
}
versionNewerThanFlag := false
if mustConvInt(kernelVersionParts[0]) > major {
versionNewerThanFlag = true
} else {
if mustConvInt(kernelVersionParts[0]) == major && mustConvInt(kernelVersionParts[1]) > minor {
versionNewerThanFlag = true
}
}
return versionNewerThanFlag
}

func TestGetCgroupCPU(t *testing.T) {
exit := make(chan struct{})
var wg sync.WaitGroup
Expand All @@ -43,7 +84,12 @@ func TestGetCgroupCPU(t *testing.T) {
}
cpu, err := GetCgroupCPU()
if err == errNoCPUControllerDetected {
require.False(t, InContainer(), "Please check linux version > v4.7.x. This is related to cgroup compatibility.")
// for more information, please refer https://github.com/pingcap/tidb/pull/41347
if checkKernelVersionNewerThan(t, 4, 7) {
require.NoError(t, err, "linux version > v4.7 and err still happens")
} else {
t.Logf("the error '%s' is ignored because the kernel is too old", err)
}
} else {
require.NoError(t, err)
require.NotZero(t, cpu.Period)
Expand Down