From 95c5328b19fb6517579cb814614c1a0d57cbb47c Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Feb 2023 19:36:01 +0800 Subject: [PATCH] cgroup: improve test to find cgroup compatibility issues (#41347) close pingcap/tidb#39786 --- util/cgroup/cgroup_cpu.go | 4 +++- util/cgroup/cgroup_cpu_test.go | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/util/cgroup/cgroup_cpu.go b/util/cgroup/cgroup_cpu.go index bafa319f5ad62..fdfdf2c9f979d 100644 --- a/util/cgroup/cgroup_cpu.go +++ b/util/cgroup/cgroup_cpu.go @@ -21,6 +21,8 @@ import ( "github.com/pingcap/errors" ) +var errNoCPUControllerDetected = errors.New("no cpu controller detected") + // Helper function for getCgroupCPU. Root is always "/", except in tests. func getCgroupCPU(root string) (CPUUsage, error) { path, err := detectControlPath(filepath.Join(root, procPathCGroup), "cpu,cpuacct") @@ -30,7 +32,7 @@ func getCgroupCPU(root string) (CPUUsage, error) { // No CPU controller detected if path == "" { - return CPUUsage{}, errors.New("no cpu controller detected") + return CPUUsage{}, errNoCPUControllerDetected } mount, ver, err := getCgroupDetails(filepath.Join(root, procPathMountInfo), path, "cpu,cpuacct") diff --git a/util/cgroup/cgroup_cpu_test.go b/util/cgroup/cgroup_cpu_test.go index 481ed3e32ccf8..7c74994a7dbb0 100644 --- a/util/cgroup/cgroup_cpu_test.go +++ b/util/cgroup/cgroup_cpu_test.go @@ -42,9 +42,13 @@ func TestGetCgroupCPU(t *testing.T) { }() } cpu, err := GetCgroupCPU() - require.NoError(t, err) - require.NotZero(t, cpu.Period) - require.Less(t, int64(1), cpu.Period) + if err == errNoCPUControllerDetected { + require.False(t, InContainer(), "Please check linux version > v4.7.x. This is related to cgroup compatibility.") + } else { + require.NoError(t, err) + require.NotZero(t, cpu.Period) + require.Less(t, int64(1), cpu.Period) + } close(exit) wg.Wait() }