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

LockCtx: add memTracker for InitCheckExistence in LockCtx #52739

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ import (
"github.com/pingcap/tidb/pkg/util/deadlockhistory"
"github.com/pingcap/tidb/pkg/util/disk"
"github.com/pingcap/tidb/pkg/util/execdetails"
"github.com/pingcap/tidb/pkg/util/hack"
"github.com/pingcap/tidb/pkg/util/intest"
"github.com/pingcap/tidb/pkg/util/logutil"
"github.com/pingcap/tidb/pkg/util/logutil/consistency"
"github.com/pingcap/tidb/pkg/util/memory"
"github.com/pingcap/tidb/pkg/util/resourcegrouptag"
"github.com/pingcap/tidb/pkg/util/set"
"github.com/pingcap/tidb/pkg/util/sqlexec"
"github.com/pingcap/tidb/pkg/util/syncutil"
"github.com/pingcap/tidb/pkg/util/topsql"
Expand Down Expand Up @@ -1210,6 +1212,8 @@ func (e *SelectLockExec) Next(ctx context.Context, req *chunk.Chunk) error {

func newLockCtx(sctx sessionctx.Context, lockWaitTime int64, numKeys int) (*tikvstore.LockCtx, error) {
seVars := sctx.GetSessionVars()
memTracker := memory.NewTracker(memory.LabelForLock, -1)
memTracker.AttachTo(seVars.MemTracker)
forUpdateTS, err := sessiontxn.GetTxnManager(sctx).GetStmtForUpdateTS()
if err != nil {
return nil, err
Expand Down Expand Up @@ -1246,8 +1250,16 @@ func newLockCtx(sctx sessionctx.Context, lockWaitTime int64, numKeys int) (*tikv
rec := deadlockhistory.ErrDeadlockToDeadlockRecord(deadlock)
deadlockhistory.GlobalDeadlockHistory.Push(rec)
}
lockCtx.OnMemChange = func(capacity int) uint64 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some test cases for it.

bucketMemoryUsage := hack.EstimateBucketMemoryUsageWithKVSize(lockCtx.GetValuesKSize(), lockCtx.GetValuesVSize())
mapSize := set.EstimateMapSize(capacity, bucketMemoryUsage)
return mapSize
}
if lockCtx.ForUpdateTS > 0 && seVars.AssertionLevel != variable.AssertionLevelOff {
lockCtx.InitCheckExistence(numKeys)
if lockCtx.OnMemChange != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OnMemChange is defined in this function and only used here. So it seems to me that it is redundant to have a OnMemChange field in LockCtx.

So you are only recording the initial usage of the struct. That leads to a question: should we dynamically track the structure, or is the initial usage is enough? Anyway it's a good start and better than tracking nothing.

memTracker.Consume(lockCtx.OnMemChange(numKeys))
}
}
return lockCtx, nil
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/executor/executor_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package executor

import (
"fmt"
"github.com/pingcap/tidb/pkg/util/hack"
"github.com/pingcap/tidb/pkg/util/set"
tikvstore "github.com/tikv/client-go/v2/kv"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -490,3 +493,18 @@ func TestErrLevelsForResetStmtContext(t *testing.T) {
}
}
}

func TestTikvstoreLockCtxMemConsume(t *testing.T) {
lockCtx := tikvstore.NewLockCtx(0, 0, time.Time{})
lockCtx.OnMemChange = func(capacity int) uint64 {
bucketMemoryUsage := hack.EstimateBucketMemoryUsageWithKVSize(lockCtx.GetValuesKSize(), lockCtx.GetValuesVSize())
mapSize := set.EstimateMapSize(capacity, bucketMemoryUsage)
return mapSize
}
numKeys := 100
lockCtx.InitCheckExistence(numKeys)
memUsage := lockCtx.OnMemChange(numKeys)
// the type of (key,value) in lockCtx.Value is (string,ReturnedValue) with size (16,48)
expectedMemUsage := set.EstimateMapSize(numKeys, hack.EstimateBucketMemoryUsageWithKVSize(16, 48))
require.Equal(t, expectedMemUsage, memUsage)
}
5 changes: 5 additions & 0 deletions pkg/util/hack/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ const (
func EstimateBucketMemoryUsage[K comparable, V any]() uint64 {
return (8*(1+uint64(unsafe.Sizeof(*new(K))+unsafe.Sizeof(*new(V)))) + 16) / 2 * 3
}

// EstimateBucketMemoryUsage returns the estimated memory usage of a bucket in a map with key size and value size.
func EstimateBucketMemoryUsageWithKVSize(keySize uint64, valueSize uint64) uint64 {
return (8*(1+keySize+valueSize) + 16) / 2 * 3
}
2 changes: 2 additions & 0 deletions pkg/util/memory/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,8 @@ const (
LabelForChunkDataInDiskByChunks int = -30
// LabelForSortPartition represents the label of the sort partition
LabelForSortPartition = -31
// LabelForLock represents the label of the lock
LabelForLock = -32
)

// MetricsTypes is used to get label for metrics
Expand Down