From 712c756a6941890b0c2030056066c07014e3191a Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Feb 2023 16:24:30 +0800 Subject: [PATCH 01/10] update Signed-off-by: Weizhen Wang --- util/cgroup/cgroup_cpu_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/util/cgroup/cgroup_cpu_test.go b/util/cgroup/cgroup_cpu_test.go index 481ed3e32ccf8..cd9505607a66a 100644 --- a/util/cgroup/cgroup_cpu_test.go +++ b/util/cgroup/cgroup_cpu_test.go @@ -41,6 +41,7 @@ func TestGetCgroupCPU(t *testing.T) { } }() } + require.True(t, InContainer()) cpu, err := GetCgroupCPU() require.NoError(t, err) require.NotZero(t, cpu.Period) From def5098e8d370ed969a81c0a8876f4fda4e56bbf Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Feb 2023 16:28:23 +0800 Subject: [PATCH 02/10] fix --- util/cgroup/cgroup_cpu.go | 4 +++- util/cgroup/cgroup_cpu_test.go | 11 +++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/util/cgroup/cgroup_cpu.go b/util/cgroup/cgroup_cpu.go index bafa319f5ad62..ae33f56cae83c 100644 --- a/util/cgroup/cgroup_cpu.go +++ b/util/cgroup/cgroup_cpu.go @@ -21,6 +21,8 @@ import ( "github.com/pingcap/errors" ) +const noCPUControllerDetected = 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{}, noCPUControllerDetected } 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 cd9505607a66a..6af4fa645aae8 100644 --- a/util/cgroup/cgroup_cpu_test.go +++ b/util/cgroup/cgroup_cpu_test.go @@ -41,11 +41,14 @@ func TestGetCgroupCPU(t *testing.T) { } }() } - require.True(t, InContainer()) cpu, err := GetCgroupCPU() - require.NoError(t, err) - require.NotZero(t, cpu.Period) - require.Less(t, int64(1), cpu.Period) + if err == noCPUControllerDetected { + require.false(t, InContainer()) + } else { + require.NoError(t, err) + require.NotZero(t, cpu.Period) + require.Less(t, int64(1), cpu.Period) + } close(exit) wg.Wait() } From 1eb603765178ea38786c00608b54ae3a4f850794 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Feb 2023 16:37:33 +0800 Subject: [PATCH 03/10] fix --- util/cgroup/cgroup_cpu.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/cgroup/cgroup_cpu.go b/util/cgroup/cgroup_cpu.go index ae33f56cae83c..aff7c3164360f 100644 --- a/util/cgroup/cgroup_cpu.go +++ b/util/cgroup/cgroup_cpu.go @@ -21,7 +21,7 @@ import ( "github.com/pingcap/errors" ) -const noCPUControllerDetected = errors.New("no cpu controller detected") +var noCPUControllerDetected = errors.New("no cpu controller detected") // Helper function for getCgroupCPU. Root is always "/", except in tests. func getCgroupCPU(root string) (CPUUsage, error) { From 43ce2d6775aad712366c1bbac0b4e45e39572ae9 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Feb 2023 16:38:18 +0800 Subject: [PATCH 04/10] fix --- util/cgroup/cgroup_cpu_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/cgroup/cgroup_cpu_test.go b/util/cgroup/cgroup_cpu_test.go index 6af4fa645aae8..2a34c3c0de3be 100644 --- a/util/cgroup/cgroup_cpu_test.go +++ b/util/cgroup/cgroup_cpu_test.go @@ -43,7 +43,7 @@ func TestGetCgroupCPU(t *testing.T) { } cpu, err := GetCgroupCPU() if err == noCPUControllerDetected { - require.false(t, InContainer()) + require.False(t, InContainer()) } else { require.NoError(t, err) require.NotZero(t, cpu.Period) From 32c740191291b9a5a115ac7b82a38a15f835d7f8 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Feb 2023 16:47:32 +0800 Subject: [PATCH 05/10] fix --- util/cgroup/cgroup_cpu.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/util/cgroup/cgroup_cpu.go b/util/cgroup/cgroup_cpu.go index aff7c3164360f..39fb03dae5849 100644 --- a/util/cgroup/cgroup_cpu.go +++ b/util/cgroup/cgroup_cpu.go @@ -25,7 +25,16 @@ var noCPUControllerDetected = 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") + cu, err := getCgroupCPUInternal(root, "cpu,cpuacct") + if err == nil { + return cu, nil + } + return getCgroupCPUInternal(root, "cpuacct,cpu") +} + +// getCgroupCPUInternal is to deal with different control keyword +func getCgroupCPUInternal(root, controlKeyword string) (CPUUsage, error) { + path, err := detectControlPath(filepath.Join(root, procPathCGroup), controlKeyword) if err != nil { return CPUUsage{}, err } @@ -35,7 +44,7 @@ func getCgroupCPU(root string) (CPUUsage, error) { return CPUUsage{}, noCPUControllerDetected } - mount, ver, err := getCgroupDetails(filepath.Join(root, procPathMountInfo), path, "cpu,cpuacct") + mount, ver, err := getCgroupDetails(filepath.Join(root, procPathMountInfo), path, controlKeyword) if err != nil { return CPUUsage{}, err } From 0044ae5905e86b3b9aaa5118937e4cb278f390cb Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Feb 2023 18:08:51 +0800 Subject: [PATCH 06/10] fix --- util/cgroup/cgroup_cpu.go | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/util/cgroup/cgroup_cpu.go b/util/cgroup/cgroup_cpu.go index 39fb03dae5849..bafa319f5ad62 100644 --- a/util/cgroup/cgroup_cpu.go +++ b/util/cgroup/cgroup_cpu.go @@ -21,30 +21,19 @@ import ( "github.com/pingcap/errors" ) -var noCPUControllerDetected = errors.New("no cpu controller detected") - // Helper function for getCgroupCPU. Root is always "/", except in tests. func getCgroupCPU(root string) (CPUUsage, error) { - cu, err := getCgroupCPUInternal(root, "cpu,cpuacct") - if err == nil { - return cu, nil - } - return getCgroupCPUInternal(root, "cpuacct,cpu") -} - -// getCgroupCPUInternal is to deal with different control keyword -func getCgroupCPUInternal(root, controlKeyword string) (CPUUsage, error) { - path, err := detectControlPath(filepath.Join(root, procPathCGroup), controlKeyword) + path, err := detectControlPath(filepath.Join(root, procPathCGroup), "cpu,cpuacct") if err != nil { return CPUUsage{}, err } // No CPU controller detected if path == "" { - return CPUUsage{}, noCPUControllerDetected + return CPUUsage{}, errors.New("no cpu controller detected") } - mount, ver, err := getCgroupDetails(filepath.Join(root, procPathMountInfo), path, controlKeyword) + mount, ver, err := getCgroupDetails(filepath.Join(root, procPathMountInfo), path, "cpu,cpuacct") if err != nil { return CPUUsage{}, err } From cf3e65bd7fc67e0dfbe785e84f60dda5ae51b369 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Feb 2023 18:09:55 +0800 Subject: [PATCH 07/10] fix --- util/cgroup/cgroup_cpu.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/util/cgroup/cgroup_cpu.go b/util/cgroup/cgroup_cpu.go index bafa319f5ad62..aff7c3164360f 100644 --- a/util/cgroup/cgroup_cpu.go +++ b/util/cgroup/cgroup_cpu.go @@ -21,6 +21,8 @@ import ( "github.com/pingcap/errors" ) +var noCPUControllerDetected = 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{}, noCPUControllerDetected } mount, ver, err := getCgroupDetails(filepath.Join(root, procPathMountInfo), path, "cpu,cpuacct") From 912fdcb806b16e48942f99af05ad41924909c1c5 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Feb 2023 18:12:07 +0800 Subject: [PATCH 08/10] fix --- util/cgroup/cgroup_cpu_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/cgroup/cgroup_cpu_test.go b/util/cgroup/cgroup_cpu_test.go index 2a34c3c0de3be..2a8f73db7a8ce 100644 --- a/util/cgroup/cgroup_cpu_test.go +++ b/util/cgroup/cgroup_cpu_test.go @@ -43,7 +43,7 @@ func TestGetCgroupCPU(t *testing.T) { } cpu, err := GetCgroupCPU() if err == noCPUControllerDetected { - require.False(t, InContainer()) + require.False(t, InContainer(), "Please check linux version. This is related to cgroup compatibility.") } else { require.NoError(t, err) require.NotZero(t, cpu.Period) From a95aedccd21758e081fb5427c5388b5b1ea26535 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Feb 2023 18:18:49 +0800 Subject: [PATCH 09/10] fix --- util/cgroup/cgroup_cpu.go | 4 ++-- util/cgroup/cgroup_cpu_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/util/cgroup/cgroup_cpu.go b/util/cgroup/cgroup_cpu.go index aff7c3164360f..fdfdf2c9f979d 100644 --- a/util/cgroup/cgroup_cpu.go +++ b/util/cgroup/cgroup_cpu.go @@ -21,7 +21,7 @@ import ( "github.com/pingcap/errors" ) -var noCPUControllerDetected = errors.New("no cpu controller detected") +var errNoCPUControllerDetected = errors.New("no cpu controller detected") // Helper function for getCgroupCPU. Root is always "/", except in tests. func getCgroupCPU(root string) (CPUUsage, error) { @@ -32,7 +32,7 @@ func getCgroupCPU(root string) (CPUUsage, error) { // No CPU controller detected if path == "" { - return CPUUsage{}, noCPUControllerDetected + 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 2a8f73db7a8ce..c2e2f61e431b5 100644 --- a/util/cgroup/cgroup_cpu_test.go +++ b/util/cgroup/cgroup_cpu_test.go @@ -42,7 +42,7 @@ func TestGetCgroupCPU(t *testing.T) { }() } cpu, err := GetCgroupCPU() - if err == noCPUControllerDetected { + if err == errNoCPUControllerDetected { require.False(t, InContainer(), "Please check linux version. This is related to cgroup compatibility.") } else { require.NoError(t, err) From d6d2fc76c968271a445a9da7bf9cfe19030bce9a Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Feb 2023 18:58:36 +0800 Subject: [PATCH 10/10] update Signed-off-by: Weizhen Wang --- util/cgroup/cgroup_cpu_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/cgroup/cgroup_cpu_test.go b/util/cgroup/cgroup_cpu_test.go index c2e2f61e431b5..7c74994a7dbb0 100644 --- a/util/cgroup/cgroup_cpu_test.go +++ b/util/cgroup/cgroup_cpu_test.go @@ -43,7 +43,7 @@ func TestGetCgroupCPU(t *testing.T) { } cpu, err := GetCgroupCPU() if err == errNoCPUControllerDetected { - require.False(t, InContainer(), "Please check linux version. This is related to cgroup compatibility.") + 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)