Skip to content

Commit

Permalink
Add a switch for perfschema (pingcap#1390)
Browse files Browse the repository at this point in the history
Add a switch for perfschema and disable it by default.
You can enable perfschema by set tidb-server flag.
  • Loading branch information
shenli authored Jul 5, 2016
1 parent 5520e26 commit 4e177d3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
12 changes: 11 additions & 1 deletion perfschema/perfschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,17 @@ type perfSchema struct {
stmtInfos map[reflect.Type]*statementInfo
}

var _ PerfSchema = (*perfSchema)(nil)
var (
_ PerfSchema = (*perfSchema)(nil)

// perfschema is disabled by default to avoid performance consuming.
enablePerfSchema = false
)

// EnablePerfSchema enables perfschema.
func EnablePerfSchema() {
enablePerfSchema = true
}

// NewPerfHandle creates a new perfSchema on store.
func NewPerfHandle() (PerfSchema, error) {
Expand Down
5 changes: 5 additions & 0 deletions perfschema/perfschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/perfschema"
"github.com/pingcap/tidb/util/testleak"
)

Expand All @@ -36,6 +37,10 @@ var _ = Suite(&testPerfSchemaSuit{
vars: make(map[string]interface{}),
})

func (s *testPerfSchemaSuit) SetUpSuite(c *C) {
perfschema.EnablePerfSchema()
}

func mustBegin(c *C, currDB *sql.DB) *sql.Tx {
tx, err := currDB.Begin()
c.Assert(err, IsNil)
Expand Down
6 changes: 6 additions & 0 deletions perfschema/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ func (ps *perfSchema) RegisterStatement(category, name string, elem interface{})
}

func (ps *perfSchema) StartStatement(sql string, connID uint64, callerName EnumCallerName, elem interface{}) *StatementState {
if !enablePerfSchema {
return nil
}
stmtType := reflect.TypeOf(elem)
info, ok := ps.stmtInfos[stmtType]
if !ok {
Expand Down Expand Up @@ -176,6 +179,9 @@ func (ps *perfSchema) StartStatement(sql string, connID uint64, callerName EnumC
}

func (ps *perfSchema) EndStatement(state *StatementState) {
if !enablePerfSchema {
return
}
if state == nil {
return
}
Expand Down
12 changes: 12 additions & 0 deletions perfschema/statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package perfschema

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/util/testleak"
"github.com/pingcap/tidb/util/types"
)
Expand All @@ -36,3 +37,14 @@ func (p *testStatementSuit) TestUninitPS(c *C) {
err = ps.appendEventsStmtsHistory([]types.Datum{})
c.Assert(err, IsNil)
}

func (p *testStatementSuit) TestDisablePS(c *C) {
defer testleak.AfterTest(c)()
ps, err := NewPerfHandle()
c.Assert(err, IsNil)
s := ps.StartStatement("select * from t", uint64(1), CallerNameSessionExecute, &ast.SelectStmt{})
c.Assert(s, IsNil)
EnablePerfSchema()
s = ps.StartStatement("select * from t", uint64(1), CallerNameSessionExecute, &ast.SelectStmt{})
c.Assert(s, NotNil)
}
7 changes: 7 additions & 0 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/ngaut/log"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/metric"
"github.com/pingcap/tidb/perfschema"
"github.com/pingcap/tidb/server"
"github.com/pingcap/tidb/store/localstore/boltdb"
"github.com/pingcap/tidb/store/tikv"
Expand All @@ -40,6 +41,7 @@ var (
statusPort = flag.String("status", "10080", "tidb server status port")
lease = flag.Int("lease", 1, "schema lease seconds, very dangerous to change only if you know what you do")
socket = flag.String("socket", "", "The socket file to use for connection.")
enablePS = flag.Int("perfschema", 0, "If enable performance schema.")
)

func main() {
Expand Down Expand Up @@ -70,6 +72,11 @@ func main() {
if err != nil {
log.Fatal(errors.ErrorStack(err))
}

if *enablePS == 1 {
perfschema.EnablePerfSchema()
}

// Create a session to load information schema.
se, err := tidb.CreateSession(store)
if err != nil {
Expand Down

0 comments on commit 4e177d3

Please sign in to comment.