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

*: optimize MemTracker object allocation in point get #26064

Merged
merged 3 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1658,12 +1658,13 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
vars := ctx.GetSessionVars()
sc := &stmtctx.StatementContext{
TimeZone: vars.Location(),
MemTracker: memory.NewTracker(memory.LabelForSQLText, vars.MemQuotaQuery),
DiskTracker: disk.NewTracker(memory.LabelForSQLText, -1),
TaskID: stmtctx.AllocateTaskID(),
CTEStorageMap: map[int]*CTEStorages{},
IsStaleness: false,
}

sc.InitMemTracker(memory.LabelForSQLText, vars.MemQuotaQuery)
sc.InitDiskTracker(memory.LabelForSQLText, -1)
sc.MemTracker.AttachToGlobalTracker(GlobalMemoryUsageTracker)
globalConfig := config.GetGlobalConfig()
if globalConfig.OOMUseTmpStorage && GlobalDiskUsageTracker != nil {
Expand Down
20 changes: 20 additions & 0 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ type StatementContext struct {
// Map to store all CTE storages of current SQL.
// Will clean up at the end of the execution.
CTEStorageMap interface{}

// cache is used to reduce object allocation.
cache struct {
execdetails.RuntimeStatsColl
MemTracker memory.Tracker
DiskTracker disk.Tracker
LogOnExceed [2]memory.LogOnExceed
}
}

// StmtHints are SessionVars related sql hints.
Expand Down Expand Up @@ -292,6 +300,18 @@ func (sc *StatementContext) GetPlanHint() (string, bool) {
return sc.planHint, sc.planHintSet
}

// InitDiskTracker initializes the sc.DiskTracker, use cache to avoid allocation.
func (sc *StatementContext) InitDiskTracker(label int, bytesLimit int64) {
memory.InitTracker(&sc.cache.DiskTracker, label, bytesLimit, &sc.cache.LogOnExceed[0])
sc.DiskTracker = &sc.cache.DiskTracker
}

// InitMemTracker initializes the sc.MemTracker, use cache to avoid allocation.
func (sc *StatementContext) InitMemTracker(label int, bytesLimit int64) {
memory.InitTracker(&sc.cache.MemTracker, label, bytesLimit, &sc.cache.LogOnExceed[1])
sc.MemTracker = &sc.cache.MemTracker
}

// SetPlanHint sets the hint for the plan.
func (sc *StatementContext) SetPlanHint(hint string) {
sc.planHintSet = true
Expand Down
18 changes: 17 additions & 1 deletion util/memory/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,26 @@ type Tracker struct {
isGlobal bool // isGlobal indicates whether this tracker is global tracker
}

// NewTracker creates a memory tracker.
// InitTracker initializes a memory tracker.
// 1. "label" is the label used in the usage string.
// 2. "bytesLimit <= 0" means no limit.
// For the common tracker, isGlobal is default as false
func InitTracker(t *Tracker, label int, bytesLimit int64, action ActionOnExceed) {
t.mu.children = nil
t.actionMu.actionOnExceed = action
t.parMu.parent = nil

t.label = label
t.bytesLimit = bytesLimit
t.maxConsumed = 0
t.isGlobal = false
return
}

// NewTracker creates a memory tracker.
// 1. "label" is the label used in the usage string.
// 2. "bytesLimit <= 0" means no limit.
// For the common tracker, isGlobal is default as false
func NewTracker(label int, bytesLimit int64) *Tracker {
t := &Tracker{
label: label,
Expand Down