Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: supply Task memory usage and correct some accumulation #38638

Merged
merged 36 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6d65432
plancache monitor
fzzf678 Oct 17, 2022
022ad83
Update plan_cache_lru.go
fzzf678 Oct 17, 2022
ea66c50
undo this
fzzf678 Oct 18, 2022
0a87a94
Update tidb.json
fzzf678 Oct 18, 2022
0462a6b
grafana json
fzzf678 Oct 19, 2022
7eb23a6
use defer
fzzf678 Oct 19, 2022
db78523
undo
fzzf678 Oct 19, 2022
130cbab
undo!
fzzf678 Oct 20, 2022
4eb8acc
Merge remote-tracking branch 'upstream/master' into monitor_for_planC…
fzzf678 Oct 21, 2022
b2ffddb
Merge remote-tracking branch 'upstream/master' into monitor_for_planC…
fzzf678 Oct 21, 2022
055c0c6
Update plan_cache_lru_test.go
fzzf678 Oct 21, 2022
19e4fa2
Update plan_cache_lru.go
fzzf678 Oct 21, 2022
42fc907
Update plan_cache_lru.go
fzzf678 Oct 21, 2022
29531d1
undo
fzzf678 Oct 21, 2022
f1daf34
Update physical_plans.go
fzzf678 Oct 24, 2022
b1ea388
Update physical_plans.go
fzzf678 Oct 24, 2022
fb9349c
Update plan_cache_lru.go
fzzf678 Oct 24, 2022
2070e10
undo
fzzf678 Oct 24, 2022
edf9f9d
Update plan_cache_lru.go
fzzf678 Oct 24, 2022
0492e5b
Update tidb.json
fzzf678 Oct 24, 2022
1f4317f
rename
fzzf678 Oct 24, 2022
3a668dc
close plan cache in closeConn
fzzf678 Oct 24, 2022
1aa6c79
undo
fzzf678 Oct 24, 2022
08f1253
basePhysicalJoin
fzzf678 Oct 25, 2022
a6cd7ad
supply memoryusage
fzzf678 Oct 26, 2022
55d27d8
undo
fzzf678 Oct 26, 2022
a6773ab
fix
fzzf678 Oct 26, 2022
13ac663
Update physical_plans.go
fzzf678 Oct 26, 2022
b1ff8e3
supply
fzzf678 Oct 26, 2022
b8fbb3a
Merge branch 'master' into monitor_for_planCacheMemUsage_test
fzzf678 Oct 26, 2022
8f388f9
Update model.go
fzzf678 Oct 27, 2022
f526037
Update model.go
fzzf678 Oct 27, 2022
7290a83
Merge branch 'master' into monitor_for_planCacheMemUsage_test
ti-chi-bot Nov 2, 2022
d2f1ca1
Merge branch 'master' into monitor_for_planCacheMemUsage_test
ti-chi-bot Nov 2, 2022
d03151f
Merge branch 'master' into monitor_for_planCacheMemUsage_test
fzzf678 Nov 2, 2022
08dfe48
Merge branch 'master' into monitor_for_planCacheMemUsage_test
ti-chi-bot Nov 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions expression/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,8 +988,13 @@ func (b *baseBuiltinFunc) MemoryUsage() (sum int64) {
return
}

sum = emptyBaseBuiltinFunc + b.bufAllocator.MemoryUsage() +
b.tp.MemoryUsage() + int64(len(b.charset)+len(b.collation))
sum = emptyBaseBuiltinFunc + int64(len(b.charset)+len(b.collation))
if b.bufAllocator != nil {
sum += b.bufAllocator.MemoryUsage()
}
if b.tp != nil {
sum += b.tp.MemoryUsage()
}
if b.childrenVectorizedOnce != nil {
sum += onceSize
}
Expand Down
3 changes: 3 additions & 0 deletions parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ func (c *ColumnInfo) GetTypeDesc() string {
return desc
}

// EmptyColumnInfoSize is the memory usage of ColumnInfoSize
const EmptyColumnInfoSize = int64(unsafe.Sizeof(ColumnInfo{}))

// FindColumnInfo finds ColumnInfo in cols by name.
func FindColumnInfo(cols []*ColumnInfo, name string) *ColumnInfo {
name = strings.ToLower(name)
Expand Down
25 changes: 13 additions & 12 deletions planner/core/physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,7 @@ func (p *PhysicalTableReader) MemoryUsage() (sum int64) {
if p.tablePlan != nil {
sum += p.tablePlan.MemoryUsage()
}

for _, plan := range p.TablePlans {
sum += plan.MemoryUsage()
}
// since TablePlans is the flats of tablePlan, so we don't count it
for _, pInfos := range p.PartitionInfos {
sum += pInfos.tableScan.MemoryUsage() + pInfos.partitionInfo.MemoryUsage()
}
Expand Down Expand Up @@ -527,12 +524,7 @@ func (p *PhysicalIndexLookUpReader) MemoryUsage() (sum int64) {
sum += p.PushedLimit.MemoryUsage()
}

for _, plan := range p.IndexPlans {
sum += plan.MemoryUsage()
}
for _, plan := range p.TablePlans {
sum += plan.MemoryUsage()
}
// since IndexPlans and TablePlans are the flats of indexPlan and tablePlan, so we don't count it
for _, col := range p.CommonHandleCols {
sum += col.MemoryUsage()
}
Expand Down Expand Up @@ -719,7 +711,7 @@ func (p *PhysicalIndexScan) MemoryUsage() (sum int64) {
}

sum = emptyPhysicalIndexScanSize + p.physicalSchemaProducer.MemoryUsage() + int64(cap(p.IdxColLens))*size.SizeOfInt +
p.DBName.MemoryUsage() + int64(len(p.rangeInfo))
p.DBName.MemoryUsage() + int64(len(p.rangeInfo)) + int64(len(p.Columns))*model.EmptyColumnInfoSize
if p.TableAsName != nil {
sum += p.TableAsName.MemoryUsage()
}
Expand All @@ -729,6 +721,9 @@ func (p *PhysicalIndexScan) MemoryUsage() (sum int64) {
if p.prop != nil {
sum += p.prop.MemoryUsage()
}
if p.dataSourceSchema != nil {
sum += p.dataSourceSchema.MemoryUsage()
}
// slice memory usage
for _, cond := range p.AccessCondition {
sum += cond.MemoryUsage()
Expand Down Expand Up @@ -1186,7 +1181,10 @@ func (p *basePhysicalJoin) MemoryUsage() (sum int64) {
return
}

sum = emptyBasePhysicalJoinSize + p.physicalSchemaProducer.MemoryUsage() + int64(cap(p.IsNullEQ))*size.SizeOfBool
sum = emptyBasePhysicalJoinSize + p.physicalSchemaProducer.MemoryUsage() + int64(cap(p.IsNullEQ))*size.SizeOfBool +
int64(cap(p.LeftConditions)+cap(p.RightConditions)+cap(p.OtherConditions))*size.SizeOfInterface +
int64(cap(p.OuterJoinKeys)+cap(p.InnerJoinKeys)+cap(p.LeftJoinKeys)+cap(p.RightNAJoinKeys)+cap(p.LeftNAJoinKeys)+
cap(p.RightNAJoinKeys))*size.SizeOfPointer + int64(cap(p.DefaultValues))*types.EmptyDatumSize

for _, cond := range p.LeftConditions {
sum += cond.MemoryUsage()
Expand Down Expand Up @@ -1357,6 +1355,9 @@ func (p *PhysicalIndexJoin) MemoryUsage() (sum int64) {

sum = p.basePhysicalJoin.MemoryUsage() + size.SizeOfInterface*2 + size.SizeOfSlice*4 +
int64(cap(p.KeyOff2IdxOff)+cap(p.IdxColLens))*size.SizeOfInt + size.SizeOfPointer
if p.innerTask != nil {
sum += p.innerTask.MemoryUsage()
}
if p.CompareFilters != nil {
sum += p.CompareFilters.MemoryUsage()
}
Expand Down
64 changes: 64 additions & 0 deletions planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/pingcap/tidb/util/mathutil"
"github.com/pingcap/tidb/util/paging"
"github.com/pingcap/tidb/util/plancodec"
"github.com/pingcap/tidb/util/size"
"github.com/pingcap/tipb/go-tipb"
"go.uber.org/zap"
)
Expand All @@ -54,6 +55,7 @@ type task interface {
plan() PhysicalPlan
invalid() bool
convertToRootTask(ctx sessionctx.Context) *rootTask
MemoryUsage() int64
}

// copTask is a task that runs in a distributed kv store.
Expand Down Expand Up @@ -173,6 +175,42 @@ func (t *copTask) getStoreType() kv.StoreType {
return kv.TiKV
}

// MemoryUsage return the memory usage of copTask
func (t *copTask) MemoryUsage() (sum int64) {
if t == nil {
return
}

sum = size.SizeOfInterface*(2+int64(cap(t.idxMergePartPlans)+cap(t.rootTaskConds))) + size.SizeOfBool*3 + size.SizeOfUint64 +
size.SizeOfPointer*(3+int64(cap(t.commonHandleCols)+cap(t.tblCols))) + size.SizeOfSlice*4 + t.partitionInfo.MemoryUsage()
if t.indexPlan != nil {
sum += t.indexPlan.MemoryUsage()
}
if t.tablePlan != nil {
sum += t.tablePlan.MemoryUsage()
}
if t.originSchema != nil {
sum += t.originSchema.MemoryUsage()
}
if t.extraHandleCol != nil {
sum += t.extraHandleCol.MemoryUsage()
}

for _, col := range t.commonHandleCols {
sum += col.MemoryUsage()
}
for _, col := range t.tblCols {
sum += col.MemoryUsage()
}
for _, p := range t.idxMergePartPlans {
sum += p.MemoryUsage()
}
for _, expr := range t.rootTaskConds {
sum += expr.MemoryUsage()
}
return
}

func (p *basePhysicalPlan) attach2Task(tasks ...task) task {
t := tasks[0].convertToRootTask(p.ctx)
return attachPlan2Task(p.self, t)
Expand Down Expand Up @@ -757,6 +795,19 @@ func (t *rootTask) plan() PhysicalPlan {
return t.p
}

// MemoryUsage return the memory usage of rootTask
func (t *rootTask) MemoryUsage() (sum int64) {
if t == nil {
return
}

sum = size.SizeOfInterface + size.SizeOfBool
if t.p != nil {
sum += t.p.MemoryUsage()
}
return sum
}

func (p *PhysicalLimit) attach2Task(tasks ...task) task {
t := tasks[0].copy()
sunk := false
Expand Down Expand Up @@ -1887,6 +1938,19 @@ func (t *mppTask) convertToRootTask(ctx sessionctx.Context) *rootTask {
return t.copy().(*mppTask).convertToRootTaskImpl(ctx)
}

// MemoryUsage return the memory usage of mppTask
func (t *mppTask) MemoryUsage() (sum int64) {
if t == nil {
return
}

sum = size.SizeOfInterface + size.SizeOfInt + size.SizeOfSlice + int64(cap(t.hashCols))*size.SizeOfPointer
if t.p != nil {
sum += t.p.MemoryUsage()
}
return
}

func collectPartitionInfosFromMPPPlan(p *PhysicalTableReader, mppPlan PhysicalPlan) {
switch x := mppPlan.(type) {
case *PhysicalTableScan:
Expand Down