Skip to content

Commit

Permalink
*: add a column describing memory usage for table information_schema.…
Browse files Browse the repository at this point in the history
…processlist (#10837)

*: add a column describing memory usage for table information_schema.processlist

Closes #10199
  • Loading branch information
SunRunAway committed Jun 20, 2019
1 parent 421de5e commit 75b9470
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (e *ShowExec) fetchShowProcessList() error {
if !hasProcessPriv && pi.User != loginUser.Username {
continue
}
row := pi.ToRow(e.Full)
row := pi.ToRowForShow(e.Full)
e.appendRow(row)
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ var tableProcesslistCols = []columnInfo{
{"TIME", mysql.TypeLong, 7, mysql.NotNullFlag, 0, nil},
{"STATE", mysql.TypeVarchar, 7, 0, nil, nil},
{"INFO", mysql.TypeString, 512, 0, nil, nil},
{"MEM", mysql.TypeLonglong, 21, 0, nil, nil},
}

var tableTiDBIndexesCols = []columnInfo{
Expand Down Expand Up @@ -861,7 +862,7 @@ func dataForProcesslist(ctx sessionctx.Context) [][]types.Datum {
continue
}

rows := pi.ToRow(true)
rows := pi.ToRow()
record := types.MakeDatums(rows...)
records = append(records, record)
}
Expand Down
28 changes: 23 additions & 5 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"os"
"strconv"
"strings"

. "github.com/pingcap/check"
"github.com/pingcap/parser/auth"
Expand Down Expand Up @@ -114,6 +115,7 @@ func (s *testTableSuite) TestInfoschemaFieldValue(c *C) {
User: "root",
Host: "127.0.0.1",
Command: mysql.ComQuery,
StmtCtx: tk.Se.GetSessionVars().StmtCtx,
}
tk.Se.SetSessionManager(sm)
tk.MustQuery("SELECT user,host,command FROM information_schema.processlist;").Check(testkit.Rows("root 127.0.0.1 Query"))
Expand Down Expand Up @@ -275,19 +277,35 @@ func (s *testTableSuite) TestSomeTables(c *C) {
DB: "information_schema",
Command: byte(1),
State: 1,
Info: "do something"}
Info: "do something",
StmtCtx: tk.Se.GetSessionVars().StmtCtx,
}
sm.processInfoMap[2] = &util.ProcessInfo{
ID: 2,
User: "user-2",
Host: "localhost",
DB: "test",
Command: byte(2),
State: 2,
Info: "do something"}
Info: strings.Repeat("x", 101),
StmtCtx: tk.Se.GetSessionVars().StmtCtx,
}
tk.Se.SetSessionManager(sm)
tk.MustQuery("select * from information_schema.PROCESSLIST order by ID;").Check(
testkit.Rows("1 user-1 localhost information_schema Quit 9223372036 1 do something",
"2 user-2 localhost test Init DB 9223372036 2 do something"))
tk.MustQuery("select * from information_schema.PROCESSLIST order by ID;").Sort().Check(
testkit.Rows(
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0", "do something"),
fmt.Sprintf("2 user-2 localhost test Init DB 9223372036 2 %s 0", strings.Repeat("x", 101)),
))
tk.MustQuery("SHOW PROCESSLIST;").Sort().Check(
testkit.Rows(
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s", "do something"),
fmt.Sprintf("2 user-2 localhost test Init DB 9223372036 2 %s", strings.Repeat("x", 100)),
))
tk.MustQuery("SHOW FULL PROCESSLIST;").Sort().Check(
testkit.Rows(
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s", "do something"),
fmt.Sprintf("2 user-2 localhost test Init DB 9223372036 2 %s", strings.Repeat("x", 101)),
))
}

func (s *testTableSuite) TestSchemataCharacterSet(c *C) {
Expand Down
2 changes: 1 addition & 1 deletion planner/core/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ type LogicalUnionScan struct {
conditions []expression.Expression
}

// DataSource represents a tablescan without condition push down.
// DataSource represents a tableScan without condition push down.
type DataSource struct {
logicalSchemaProducer

Expand Down
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var (
sessionExecuteParseDurationGeneral = metrics.SessionExecuteParseDuration.WithLabelValues(metrics.LblGeneral)
)

// Session context
// Session context, it is consistent with the lifecycle of a client connection.
type Session interface {
sessionctx.Context
Status() uint16 // Flag of current status, such as autocommit.
Expand Down
14 changes: 12 additions & 2 deletions util/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
"github.com/pingcap/parser"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/util/memory"
"github.com/pingcap/tidb/util/stringutil"
"github.com/pingcap/tidb/util/testleak"
)

Expand Down Expand Up @@ -152,9 +155,12 @@ func (s *testMiscSuite) TestBasicFunc(c *C) {
Time: time.Now(),
State: 1,
Info: "test",
StmtCtx: &stmtctx.StatementContext{
MemTracker: memory.NewTracker(stringutil.StringerStr(""), -1),
},
}
row := pi.ToRow(false)
row2 := pi.ToRow(true)
row := pi.ToRowForShow(false)
row2 := pi.ToRowForShow(true)
c.Assert(row, DeepEquals, row2)
c.Assert(len(row), Equals, 8)
c.Assert(row[0], Equals, pi.ID)
Expand All @@ -166,6 +172,10 @@ func (s *testMiscSuite) TestBasicFunc(c *C) {
c.Assert(row[6], Equals, "1")
c.Assert(row[7], Equals, "test")

row3 := pi.ToRow()
c.Assert(row3[:8], DeepEquals, row)
c.Assert(row3[8], Equals, int64(0))

// Test for RandomBuf.
buf := RandomBuf(5)
c.Assert(len(buf), Equals, 5)
Expand Down
10 changes: 8 additions & 2 deletions util/processinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type ProcessInfo struct {
ExceedExpensiveTimeThresh bool
}

// ToRow returns []interface{} for the row data of "show processlist" and "select * from infoschema.processlist".
func (pi *ProcessInfo) ToRow(full bool) []interface{} {
// ToRowForShow returns []interface{} for the row data of "SHOW [FULL] PROCESSLIST".
func (pi *ProcessInfo) ToRowForShow(full bool) []interface{} {
var info string
if full {
info = pi.Info
Expand All @@ -59,6 +59,12 @@ func (pi *ProcessInfo) ToRow(full bool) []interface{} {
}
}

// ToRow returns []interface{} for the row data of
// "SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST".
func (pi *ProcessInfo) ToRow() []interface{} {
return append(pi.ToRowForShow(true), pi.StmtCtx.MemTracker.BytesConsumed())
}

// SessionManager is an interface for session manage. Show processlist and
// kill statement rely on this interface.
type SessionManager interface {
Expand Down

0 comments on commit 75b9470

Please sign in to comment.