Skip to content

Commit b11578f

Browse files
SunRunAwayeurekaka
authored andcommitted
*: add a column describing memory usage for table information_schema.processlist (pingcap#10837) (pingcap#12801)
1 parent c6bbf0f commit b11578f

File tree

7 files changed

+108
-15
lines changed

7 files changed

+108
-15
lines changed

executor/show.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (e *ShowExec) fetchShowProcessList() error {
274274
if !hasProcessPriv && pi.User != loginUser.Username {
275275
continue
276276
}
277-
row := pi.ToRow(e.Full)
277+
row := pi.ToRowForShow(e.Full)
278278
e.appendRow(row)
279279
}
280280
return nil

infoschema/tables.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ var tableProcesslistCols = []columnInfo{
543543
{"TIME", mysql.TypeLong, 7, mysql.NotNullFlag, 0, nil},
544544
{"STATE", mysql.TypeVarchar, 7, 0, nil, nil},
545545
{"INFO", mysql.TypeString, 512, 0, nil, nil},
546+
{"MEM", mysql.TypeLonglong, 21, 0, nil, nil},
546547
}
547548

548549
var tableTiDBIndexesCols = []columnInfo{
@@ -862,7 +863,7 @@ func dataForProcesslist(ctx sessionctx.Context) [][]types.Datum {
862863
continue
863864
}
864865

865-
rows := pi.ToRow(true)
866+
rows := pi.ToRow()
866867
record := types.MakeDatums(rows...)
867868
records = append(records, record)
868869
}

infoschema/tables_test.go

+26-9
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func (s *testTableSuite) TestInfoschemaFieldValue(c *C) {
131131
User: "root",
132132
Host: "127.0.0.1",
133133
Command: mysql.ComQuery,
134+
StmtCtx: tk.Se.GetSessionVars().StmtCtx,
134135
}
135136
tk.Se.SetSessionManager(sm)
136137
tk.MustQuery("SELECT user,host,command FROM information_schema.processlist;").Check(testkit.Rows("root 127.0.0.1 Query"))
@@ -342,19 +343,35 @@ func (s *testTableSuite) TestSomeTables(c *C) {
342343
DB: "information_schema",
343344
Command: byte(1),
344345
State: 1,
345-
Info: "do something"}
346+
Info: "do something",
347+
StmtCtx: tk.Se.GetSessionVars().StmtCtx,
348+
}
346349
sm.processInfoMap[2] = &util.ProcessInfo{
347350
ID: 2,
348351
User: "user-2",
349352
Host: "localhost",
350353
DB: "test",
351354
Command: byte(2),
352355
State: 2,
353-
Info: "do something"}
356+
Info: strings.Repeat("x", 101),
357+
StmtCtx: tk.Se.GetSessionVars().StmtCtx,
358+
}
354359
tk.Se.SetSessionManager(sm)
355-
tk.MustQuery("select * from information_schema.PROCESSLIST order by ID;").Check(
356-
testkit.Rows("1 user-1 localhost information_schema Quit 9223372036 1 do something",
357-
"2 user-2 localhost test Init DB 9223372036 2 do something"))
360+
tk.MustQuery("select * from information_schema.PROCESSLIST order by ID;").Sort().Check(
361+
testkit.Rows(
362+
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0", "do something"),
363+
fmt.Sprintf("2 user-2 localhost test Init DB 9223372036 2 %s 0", strings.Repeat("x", 101)),
364+
))
365+
tk.MustQuery("SHOW PROCESSLIST;").Sort().Check(
366+
testkit.Rows(
367+
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s", "do something"),
368+
fmt.Sprintf("2 user-2 localhost test Init DB 9223372036 2 %s", strings.Repeat("x", 100)),
369+
))
370+
tk.MustQuery("SHOW FULL PROCESSLIST;").Sort().Check(
371+
testkit.Rows(
372+
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s", "do something"),
373+
fmt.Sprintf("2 user-2 localhost test Init DB 9223372036 2 %s", strings.Repeat("x", 101)),
374+
))
358375

359376
sm = &mockSessionManager{make(map[uint64]*util.ProcessInfo, 2)}
360377
sm.processInfoMap[1] = &util.ProcessInfo{
@@ -380,8 +397,8 @@ func (s *testTableSuite) TestSomeTables(c *C) {
380397
tk.Se.SetSessionManager(sm)
381398
tk.MustQuery("select * from information_schema.PROCESSLIST order by ID;").Check(
382399
testkit.Rows(
383-
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s", "<nil>"),
384-
fmt.Sprintf("2 user-2 localhost <nil> Init DB 9223372036 2 %s", strings.Repeat("x", 101)),
400+
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0", "<nil>"),
401+
fmt.Sprintf("2 user-2 localhost <nil> Init DB 9223372036 2 %s 0", strings.Repeat("x", 101)),
385402
))
386403
tk.MustQuery("SHOW PROCESSLIST;").Sort().Check(
387404
testkit.Rows(
@@ -395,11 +412,11 @@ func (s *testTableSuite) TestSomeTables(c *C) {
395412
))
396413
tk.MustQuery("select * from information_schema.PROCESSLIST where db is null;").Check(
397414
testkit.Rows(
398-
fmt.Sprintf("2 user-2 localhost <nil> Init DB 9223372036 2 %s", strings.Repeat("x", 101)),
415+
fmt.Sprintf("2 user-2 localhost <nil> Init DB 9223372036 2 %s 0", strings.Repeat("x", 101)),
399416
))
400417
tk.MustQuery("select * from information_schema.PROCESSLIST where Info is null;").Check(
401418
testkit.Rows(
402-
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s", "<nil>"),
419+
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0", "<nil>"),
403420
))
404421
}
405422

planner/core/logical_plans.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ type LogicalUnionScan struct {
322322
conditions []expression.Expression
323323
}
324324

325-
// DataSource represents a tablescan without condition push down.
325+
// DataSource represents a tableScan without condition push down.
326326
type DataSource struct {
327327
logicalSchemaProducer
328328

session/session.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ var (
9898
sessionExecuteParseDurationGeneral = metrics.SessionExecuteParseDuration.WithLabelValues(metrics.LblGeneral)
9999
)
100100

101-
// Session context
101+
// Session context, it is consistent with the lifecycle of a client connection.
102102
type Session interface {
103103
sessionctx.Context
104104
Status() uint16 // Flag of current status, such as autocommit.

util/misc_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@
1414
package util
1515

1616
import (
17+
"bytes"
1718
"time"
1819

1920
. "github.com/pingcap/check"
2021
"github.com/pingcap/errors"
22+
"github.com/pingcap/parser"
23+
"github.com/pingcap/parser/mysql"
24+
"github.com/pingcap/parser/terror"
25+
"github.com/pingcap/tidb/sessionctx/stmtctx"
26+
"github.com/pingcap/tidb/util/memory"
27+
"github.com/pingcap/tidb/util/stringutil"
2128
"github.com/pingcap/tidb/util/testleak"
2229
)
2330

@@ -113,3 +120,65 @@ func (s *testMiscSuite) TestCompatibleParseGCTime(c *C) {
113120
c.Assert(err, NotNil)
114121
}
115122
}
123+
124+
func (s *testMiscSuite) TestBasicFunc(c *C) {
125+
// Test for GetStack.
126+
b := GetStack()
127+
c.Assert(len(b) < 4096, IsTrue)
128+
129+
// Test for WithRecovery.
130+
var recover interface{}
131+
WithRecovery(func() {
132+
panic("test")
133+
}, func(r interface{}) {
134+
recover = r
135+
})
136+
c.Assert(recover, Equals, "test")
137+
138+
// Test for SyntaxError.
139+
c.Assert(SyntaxError(nil), IsNil)
140+
c.Assert(terror.ErrorEqual(SyntaxError(errors.New("test")), parser.ErrParse), IsTrue)
141+
c.Assert(terror.ErrorEqual(SyntaxError(parser.ErrSyntax.GenWithStackByArgs()), parser.ErrSyntax), IsTrue)
142+
143+
// Test for SyntaxWarn.
144+
c.Assert(SyntaxWarn(nil), IsNil)
145+
c.Assert(terror.ErrorEqual(SyntaxWarn(errors.New("test")), parser.ErrParse), IsTrue)
146+
147+
// Test for ProcessInfo.
148+
pi := ProcessInfo{
149+
ID: 1,
150+
User: "test",
151+
Host: "www",
152+
DB: "db",
153+
Command: mysql.ComSleep,
154+
Plan: nil,
155+
Time: time.Now(),
156+
State: 1,
157+
Info: "test",
158+
StmtCtx: &stmtctx.StatementContext{
159+
MemTracker: memory.NewTracker(stringutil.StringerStr(""), -1),
160+
},
161+
}
162+
row := pi.ToRowForShow(false)
163+
row2 := pi.ToRowForShow(true)
164+
c.Assert(row, DeepEquals, row2)
165+
c.Assert(len(row), Equals, 8)
166+
c.Assert(row[0], Equals, pi.ID)
167+
c.Assert(row[1], Equals, pi.User)
168+
c.Assert(row[2], Equals, pi.Host)
169+
c.Assert(row[3], Equals, pi.DB)
170+
c.Assert(row[4], Equals, "Sleep")
171+
c.Assert(row[5], Equals, uint64(0))
172+
c.Assert(row[6], Equals, "1")
173+
c.Assert(row[7], Equals, "test")
174+
175+
row3 := pi.ToRow()
176+
c.Assert(row3[:8], DeepEquals, row)
177+
c.Assert(row3[8], Equals, int64(0))
178+
179+
// Test for RandomBuf.
180+
buf := RandomBuf(5)
181+
c.Assert(len(buf), Equals, 5)
182+
c.Assert(bytes.Contains(buf, []byte("$")), IsFalse)
183+
c.Assert(bytes.Contains(buf, []byte{0}), IsFalse)
184+
}

util/processinfo.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ type ProcessInfo struct {
4141
MaxExecutionTime uint64
4242
}
4343

44-
// ToRow returns []interface{} for the row data of "show processlist" and "select * from infoschema.processlist".
45-
func (pi *ProcessInfo) ToRow(full bool) []interface{} {
44+
// ToRowForShow returns []interface{} for the row data of "SHOW [FULL] PROCESSLIST".
45+
func (pi *ProcessInfo) ToRowForShow(full bool) []interface{} {
4646
var info interface{}
4747
if pi.Info != nil {
4848
if full {
@@ -64,6 +64,12 @@ func (pi *ProcessInfo) ToRow(full bool) []interface{} {
6464
}
6565
}
6666

67+
// ToRow returns []interface{} for the row data of
68+
// "SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST".
69+
func (pi *ProcessInfo) ToRow() []interface{} {
70+
return append(pi.ToRowForShow(true), pi.StmtCtx.MemTracker.BytesConsumed())
71+
}
72+
6773
// SessionManager is an interface for session manage. Show processlist and
6874
// kill statement rely on this interface.
6975
type SessionManager interface {

0 commit comments

Comments
 (0)