From fd2782312531cca75a7527fb8bd7b36024356f6d Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Wed, 1 Nov 2023 00:12:36 +0800 Subject: [PATCH 1/2] This is an automated cherry-pick of #48096 Signed-off-by: ti-chi-bot --- cmd/tidb-server/main.go | 1 + pkg/util/memory/meminfo.go | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/cmd/tidb-server/main.go b/cmd/tidb-server/main.go index cd7da32550a4d..81908fd04a825 100644 --- a/cmd/tidb-server/main.go +++ b/cmd/tidb-server/main.go @@ -266,6 +266,7 @@ func main() { checkTempStorageQuota() } setupLog() + memory.InitMemoryHook() setupExtensions() setupStmtSummary() diff --git a/pkg/util/memory/meminfo.go b/pkg/util/memory/meminfo.go index 8d0e25bcb759e..1bfd5f024b141 100644 --- a/pkg/util/memory/meminfo.go +++ b/pkg/util/memory/meminfo.go @@ -22,8 +22,13 @@ import ( "github.com/pingcap/sysutil" "github.com/pingcap/tidb/pkg/parser/terror" "github.com/pingcap/tidb/pkg/util/cgroup" +<<<<<<< HEAD "github.com/pingcap/tidb/pkg/util/mathutil" +======= + "github.com/pingcap/tidb/pkg/util/logutil" +>>>>>>> 4667ed9e168 (*: support cgroup with systemd (#48096)) "github.com/shirou/gopsutil/v3/mem" + "go.uber.org/zap" ) // MemTotal returns the total amount of RAM on this system @@ -51,6 +56,10 @@ func MemTotalNormal() (uint64, error) { if time.Since(t) < 60*time.Second { return total, nil } + return memTotalNormal() +} + +func memTotalNormal() (uint64, error) { v, err := mem.VirtualMemory() if err != nil { return 0, err @@ -140,6 +149,7 @@ func MemUsedCGroup() (uint64, error) { return memo, nil } +// it is for test and init. func init() { if cgroup.InContainer() { MemTotal = MemTotalCGroup @@ -164,6 +174,37 @@ func init() { terror.MustNil(err) } +// InitMemoryHook initializes the memory hook. +// It is to solve the problem that tidb cannot read cgroup in the systemd. +// so if we are not in the container, we compare the cgroup memory limit and the physical memory, +// the cgroup memory limit is smaller, we use the cgroup memory hook. +func InitMemoryHook() { + if cgroup.InContainer() { + logutil.BgLogger().Info("use cgroup memory hook because TiDB is in the container") + return + } + cgroupValue, err := cgroup.GetMemoryLimit() + if err != nil { + return + } + physicalValue, err := memTotalNormal() + if err != nil { + return + } + if physicalValue > cgroupValue && cgroupValue != 0 { + MemTotal = MemTotalCGroup + MemUsed = MemUsedCGroup + sysutil.RegisterGetMemoryCapacity(MemTotalCGroup) + logutil.BgLogger().Info("use cgroup memory hook", zap.Int64("cgroupMemorySize", int64(cgroupValue)), zap.Int64("physicalMemorySize", int64(physicalValue))) + } else { + logutil.BgLogger().Info("use physical memory hook", zap.Int64("cgroupMemorySize", int64(cgroupValue)), zap.Int64("physicalMemorySize", int64(physicalValue))) + } + _, err = MemTotal() + terror.MustNil(err) + _, err = MemUsed() + terror.MustNil(err) +} + // InstanceMemUsed returns the memory usage of this TiDB server func InstanceMemUsed() (uint64, error) { used, t := serverMemUsage.get() From 88df1fbbd7f008e64d0d1e5b4c4d935942ea3671 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Wed, 1 Nov 2023 10:12:12 +0800 Subject: [PATCH 2/2] update Signed-off-by: Weizhen Wang --- pkg/util/memory/BUILD.bazel | 1 - pkg/util/memory/meminfo.go | 8 ++------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/pkg/util/memory/BUILD.bazel b/pkg/util/memory/BUILD.bazel index 0a1c6321a7a75..49921982c9d75 100644 --- a/pkg/util/memory/BUILD.bazel +++ b/pkg/util/memory/BUILD.bazel @@ -17,7 +17,6 @@ go_library( "//pkg/util/cgroup", "//pkg/util/dbterror", "//pkg/util/logutil", - "//pkg/util/mathutil", "@com_github_pingcap_failpoint//:failpoint", "@com_github_pingcap_sysutil//:sysutil", "@com_github_shirou_gopsutil_v3//mem", diff --git a/pkg/util/memory/meminfo.go b/pkg/util/memory/meminfo.go index 1bfd5f024b141..0d7bd68a2bcbe 100644 --- a/pkg/util/memory/meminfo.go +++ b/pkg/util/memory/meminfo.go @@ -22,11 +22,7 @@ import ( "github.com/pingcap/sysutil" "github.com/pingcap/tidb/pkg/parser/terror" "github.com/pingcap/tidb/pkg/util/cgroup" -<<<<<<< HEAD - "github.com/pingcap/tidb/pkg/util/mathutil" -======= "github.com/pingcap/tidb/pkg/util/logutil" ->>>>>>> 4667ed9e168 (*: support cgroup with systemd (#48096)) "github.com/shirou/gopsutil/v3/mem" "go.uber.org/zap" ) @@ -125,7 +121,7 @@ func MemTotalCGroup() (uint64, error) { if err != nil { return 0, err } - memo = mathutil.Min(v.Total, memo) + memo = min(v.Total, memo) memLimit.set(memo, time.Now()) return memo, nil } @@ -144,7 +140,7 @@ func MemUsedCGroup() (uint64, error) { if err != nil { return 0, err } - memo = mathutil.Min(v.Used, memo) + memo = min(v.Used, memo) memUsage.set(memo, time.Now()) return memo, nil }