Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#48096
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
hawkingrei authored and ti-chi-bot committed Oct 31, 2023
1 parent 699227f commit e157aeb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func main() {
checkTempStorageQuota()
}
setupLog()
memory.InitMemoryHook()
setupExtensions()
setupStmtSummary()

Expand Down
43 changes: 43 additions & 0 deletions util/memory/meminfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ import (

"github.com/pingcap/failpoint"
"github.com/pingcap/sysutil"
<<<<<<< HEAD:util/memory/meminfo.go
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/util/cgroup"
"github.com/pingcap/tidb/util/mathutil"
=======
"github.com/pingcap/tidb/pkg/parser/terror"
"github.com/pingcap/tidb/pkg/util/cgroup"
"github.com/pingcap/tidb/pkg/util/logutil"
>>>>>>> 4667ed9e168 (*: support cgroup with systemd (#48096)):pkg/util/memory/meminfo.go
"github.com/shirou/gopsutil/v3/mem"
"go.uber.org/zap"
)

// MemTotal returns the total amount of RAM on this system
Expand Down Expand Up @@ -51,6 +58,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 v.Total, err
Expand Down Expand Up @@ -140,6 +151,7 @@ func MemUsedCGroup() (uint64, error) {
return memo, nil
}

// it is for test and init.
func init() {
if cgroup.InContainer() {
MemTotal = MemTotalCGroup
Expand All @@ -164,6 +176,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()
Expand Down

0 comments on commit e157aeb

Please sign in to comment.