Skip to content

Commit fc4edaf

Browse files
authored
*: support cgroup with systemd (pingcap#48096) (pingcap#48147)
close pingcap#47442
1 parent 852b939 commit fc4edaf

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

tidb-server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ func main() {
206206
checkTempStorageQuota()
207207
}
208208
setupLog()
209+
memory.InitMemoryHook()
209210
setupExtensions()
210211
setupStmtSummary()
211212

util/memory/meminfo.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ import (
2222
"github.com/pingcap/sysutil"
2323
"github.com/pingcap/tidb/parser/terror"
2424
"github.com/pingcap/tidb/util/cgroup"
25+
"github.com/pingcap/tidb/util/logutil"
2526
"github.com/pingcap/tidb/util/mathutil"
2627
"github.com/shirou/gopsutil/v3/mem"
28+
"go.uber.org/zap"
2729
)
2830

2931
// MemTotal returns the total amount of RAM on this system
@@ -51,6 +53,10 @@ func MemTotalNormal() (uint64, error) {
5153
if time.Since(t) < 60*time.Second {
5254
return total, nil
5355
}
56+
return memTotalNormal()
57+
}
58+
59+
func memTotalNormal() (uint64, error) {
5460
v, err := mem.VirtualMemory()
5561
if err != nil {
5662
return v.Total, err
@@ -140,6 +146,7 @@ func MemUsedCGroup() (uint64, error) {
140146
return memo, nil
141147
}
142148

149+
// it is for test and init.
143150
func init() {
144151
if cgroup.InContainer() {
145152
MemTotal = MemTotalCGroup
@@ -164,6 +171,37 @@ func init() {
164171
terror.MustNil(err)
165172
}
166173

174+
// InitMemoryHook initializes the memory hook.
175+
// It is to solve the problem that tidb cannot read cgroup in the systemd.
176+
// so if we are not in the container, we compare the cgroup memory limit and the physical memory,
177+
// the cgroup memory limit is smaller, we use the cgroup memory hook.
178+
func InitMemoryHook() {
179+
if cgroup.InContainer() {
180+
logutil.BgLogger().Info("use cgroup memory hook because TiDB is in the container")
181+
return
182+
}
183+
cgroupValue, err := cgroup.GetMemoryLimit()
184+
if err != nil {
185+
return
186+
}
187+
physicalValue, err := memTotalNormal()
188+
if err != nil {
189+
return
190+
}
191+
if physicalValue > cgroupValue && cgroupValue != 0 {
192+
MemTotal = MemTotalCGroup
193+
MemUsed = MemUsedCGroup
194+
sysutil.RegisterGetMemoryCapacity(MemTotalCGroup)
195+
logutil.BgLogger().Info("use cgroup memory hook", zap.Int64("cgroupMemorySize", int64(cgroupValue)), zap.Int64("physicalMemorySize", int64(physicalValue)))
196+
} else {
197+
logutil.BgLogger().Info("use physical memory hook", zap.Int64("cgroupMemorySize", int64(cgroupValue)), zap.Int64("physicalMemorySize", int64(physicalValue)))
198+
}
199+
_, err = MemTotal()
200+
terror.MustNil(err)
201+
_, err = MemUsed()
202+
terror.MustNil(err)
203+
}
204+
167205
// InstanceMemUsed returns the memory usage of this TiDB server
168206
func InstanceMemUsed() (uint64, error) {
169207
used, t := serverMemUsage.get()

0 commit comments

Comments
 (0)