Skip to content

Commit

Permalink
executor: trace the memory usage of Selection executors (#13927)
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored Dec 23, 2019
1 parent 2ce3713 commit f0282d3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,14 +1029,19 @@ type SelectionExec struct {
inputIter *chunk.Iterator4Chunk
inputRow chunk.Row
childResult *chunk.Chunk

memTracker *memory.Tracker
}

// Open implements the Executor Open interface.
func (e *SelectionExec) Open(ctx context.Context) error {
if err := e.baseExecutor.Open(ctx); err != nil {
return err
}
e.memTracker = memory.NewTracker(e.id, -1)
e.memTracker.AttachTo(e.ctx.GetSessionVars().StmtCtx.MemTracker)
e.childResult = newFirstChunk(e.children[0])
e.memTracker.Consume(e.childResult.MemoryUsage())
e.batched = expression.Vectorizable(e.filters)
if e.batched {
e.selected = make([]bool, 0, chunk.InitialCapacity)
Expand All @@ -1048,6 +1053,7 @@ func (e *SelectionExec) Open(ctx context.Context) error {

// Close implements plannercore.Plan Close interface.
func (e *SelectionExec) Close() error {
e.memTracker.Consume(-e.childResult.MemoryUsage())
e.childResult = nil
e.selected = nil
return e.baseExecutor.Close()
Expand All @@ -1071,7 +1077,9 @@ func (e *SelectionExec) Next(ctx context.Context, req *chunk.Chunk) error {
}
req.AppendRow(e.inputRow)
}
mSize := e.childResult.MemoryUsage()
err := Next(ctx, e.children[0], e.childResult)
e.memTracker.Consume(e.childResult.MemoryUsage() - mSize)
if err != nil {
return err
}
Expand Down Expand Up @@ -1103,7 +1111,9 @@ func (e *SelectionExec) unBatchedNext(ctx context.Context, chk *chunk.Chunk) err
return nil
}
}
mSize := e.childResult.MemoryUsage()
err := Next(ctx, e.children[0], e.childResult)
e.memTracker.Consume(e.childResult.MemoryUsage() - mSize)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion executor/explain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (s *testSuite1) TestExplainAnalyzeMemory(c *C) {

func (s *testSuite1) checkMemoryInfo(c *C, tk *testkit.TestKit, sql string) {
memCol := 5
ops := []string{"Join", "Reader", "Top", "Sort", "LookUp", "Projection"}
ops := []string{"Join", "Reader", "Top", "Sort", "LookUp", "Projection", "Selection"}
rows := tk.MustQuery(sql).Rows()
for _, row := range rows {
strs := make([]string, len(row))
Expand Down Expand Up @@ -164,6 +164,7 @@ func (s *testSuite1) TestMemoryUsageAfterClose(c *C) {
tk.MustExec(fmt.Sprintf("insert into t values (%v, %v)", i, i))
}
SQLs := []string{"select v+abs(k) from t",
"select v from t where abs(v) > 0",
"select v from t order by v"}
for _, sql := range SQLs {
tk.MustQuery(sql)
Expand Down

0 comments on commit f0282d3

Please sign in to comment.