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

*: track the memory usage of runtime info #44048

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 2 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ func (a *ExecStmt) handleNoDelay(ctx context.Context, e Executor, isPessimistic
// `rs.Close` in `handleStmt`
if handled && sc != nil && rs == nil {
sc.DetachMemDiskTracker()
sc.ClearRuntimeInfo()
}
}()

Expand Down Expand Up @@ -1415,6 +1416,7 @@ func (a *ExecStmt) CloseRecordSet(txnStartTS uint64, lastErr error) {
a.FinishExecuteStmt(txnStartTS, lastErr, false)
a.logAudit()
a.Ctx.GetSessionVars().StmtCtx.DetachMemDiskTracker()
a.Ctx.GetSessionVars().StmtCtx.ClearRuntimeInfo()
}

// LogSlowQuery is used to print the slow query in the log files.
Expand Down
1 change: 1 addition & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4498,6 +4498,7 @@ func (builder *dataReaderBuilder) buildTableReaderBase(ctx context.Context, e *T
SetClosestReplicaReadAdjuster(newClosestReadAdjuster(e.ctx, &reqBuilderWithRange.Request, e.netDataSize)).
SetPaging(e.paging).
SetConnID(e.ctx.GetSessionVars().ConnectionID).
SetMemTracker(e.memTracker).
Build()
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions executor/distsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@ func (e *IndexLookUpExecutor) buildTableReader(ctx context.Context, task *lookup
plans: e.tblPlans,
netDataSize: e.avgRowSize * float64(len(task.handles)),
byItems: e.byItems,
memTracker: e.memTracker,
}
tableReaderExec.buildVirtualColumnInfo()
tableReader, err := e.dataReaderBuilder.buildTableReaderFromHandles(ctx, tableReaderExec, task.handles, true)
Expand Down
1 change: 1 addition & 0 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,7 @@ func (cc *clientConn) handleStmt(ctx context.Context, stmt ast.StmtNode, warns [
// will not be detached. We need to detach them manually.
if sv := cc.ctx.GetSessionVars(); sv != nil && sv.StmtCtx != nil {
sv.StmtCtx.DetachMemDiskTracker()
sv.StmtCtx.ClearRuntimeInfo()
}
return true, err
}
Expand Down
13 changes: 13 additions & 0 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,19 @@ func (sc *StatementContext) DetachMemDiskTracker() {
}
}

// ClearRuntimeInfo clears the runtime info.
func (sc *StatementContext) ClearRuntimeInfo() {
if sc == nil {
return
}
sc.mu.Lock()
defer sc.mu.Unlock()
sc.mu.allExecDetails = nil
if sc.RuntimeStatsColl != nil {
sc.RuntimeStatsColl = execdetails.NewRuntimeStatsColl(sc.RuntimeStatsColl)
}
}

// CopTasksDetails collects some useful information of cop-tasks during execution.
type CopTasksDetails struct {
NumCopTasks int
Expand Down
1 change: 1 addition & 0 deletions store/copr/coprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,7 @@ func (worker *copIteratorWorker) handleCollectExecutionInfo(bo *Backoffer, rpcCt
if rpcCtx != nil {
resp.detail.CalleeAddress = rpcCtx.Addr
}
worker.memTracker.Consume(execdetails.AvgMemorySizeForDetailsNeedP90)
sd := &util.ScanDetail{}
td := util.TimeDetail{}
if pbDetails := resp.pbResp.ExecDetailsV2; pbDetails != nil {
Expand Down
8 changes: 8 additions & 0 deletions util/execdetails/execdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/pingcap/tipb/go-tipb"
"github.com/tikv/client-go/v2/util"
Expand Down Expand Up @@ -50,6 +51,13 @@ type DetailsNeedP90 struct {
TimeDetail util.TimeDetail
}

// AvgMemorySizeForDetailsNeedP90 is the estimated size of DetailsNeedP90 struct.
// "48" represents the base memory overhead of a Go map after initialization.
// Here, the memory overhead of the `BackoffSleep` and `BackoffTimes` member attributes is recorded.
// "8" represents the memory overhead of some related slices. For details, see the following function:
// StatementContext.MergeExecDetails(), basicCopRuntimeStats.Merge(), selectResultRuntimeStats.Merge().
const AvgMemorySizeForDetailsNeedP90 = int64(unsafe.Sizeof(DetailsNeedP90{})) + 48*2 + 8*4

type stmtExecDetailKeyType struct{}

// StmtExecDetailKey used to carry StmtExecDetail info in context.Context.
Expand Down