Skip to content

Commit

Permalink
server: support tag sql in pprof result (#14312) (#14329)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored and zz-jason committed Jan 7, 2020
1 parent 3c90149 commit ca3e7ec
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ type Performance struct {
BindInfoLease string `toml:"bind-info-lease" json:"bind-info-lease"`
TxnEntryCountLimit uint64 `toml:"txn-entry-count-limit" json:"txn-entry-count-limit"`
TxnTotalSizeLimit uint64 `toml:"txn-total-size-limit" json:"txn-total-size-limit"`
PProfSQLCPU bool `toml:"pprof-sql-cpu" json:"pprof-sql-cpu"`
}

// PlanCache is the PlanCache section of the config.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require (
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72
github.com/struCoder/pidusage v0.1.2
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2
github.com/uber-go/atomic v1.3.2 // indirect
github.com/uber-go/atomic v1.3.2
github.com/uber/jaeger-client-go v2.15.0+incompatible
github.com/uber/jaeger-lib v1.5.0 // indirect
github.com/unrolled/render v0.0.0-20180914162206-b9786414de4d // indirect
Expand Down
9 changes: 9 additions & 0 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"io"
"net"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"sync/atomic"
Expand All @@ -51,6 +52,7 @@ import (
"github.com/opentracing/opentracing-go"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser"
"github.com/pingcap/parser/auth"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
Expand All @@ -60,6 +62,7 @@ import (
"github.com/pingcap/tidb/plugin"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/arena"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/hack"
Expand Down Expand Up @@ -867,6 +870,12 @@ func (cc *clientConn) dispatch(ctx context.Context, data []byte) error {
cc.lastPacket = data
cmd := data[0]
data = data[1:]
if util.EnablePProfSQLCPU.Load() {
defer pprof.SetGoroutineLabels(ctx)
lastSQL := getLastStmtInConn{cc}.String()
ctx = pprof.WithLabels(ctx, pprof.Labels("sql", parser.Normalize(lastSQL)))
pprof.SetGoroutineLabels(ctx)
}
token := cc.server.getToken()
defer func() {
// if handleChangeUser failed, cc.ctx may be nil
Expand Down
7 changes: 6 additions & 1 deletion tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/gcworker"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/memory"
"github.com/pingcap/tidb/util/printer"
Expand Down Expand Up @@ -357,7 +358,7 @@ func loadConfig() string {
// hotReloadConfigItems lists all config items which support hot-reload.
var hotReloadConfigItems = []string{"Performance.MaxProcs", "Performance.MaxMemory", "Performance.CrossJoin",
"Performance.FeedbackProbability", "Performance.QueryFeedbackLimit", "Performance.PseudoEstimateRatio",
"OOMAction", "MemQuotaQuery", "StmtSummary.MaxStmtCount", "StmtSummary.MaxSQLLength", "TiKVClient.StoreLimit"}
"OOMAction", "MemQuotaQuery", "StmtSummary.MaxStmtCount", "StmtSummary.MaxSQLLength", "TiKVClient.StoreLimit", "Performance.PProfSQLCPU"}

func reloadConfig(nc, c *config.Config) {
// Just a part of config items need to be reload explicitly.
Expand All @@ -383,6 +384,9 @@ func reloadConfig(nc, c *config.Config) {
if nc.Performance.PseudoEstimateRatio != c.Performance.PseudoEstimateRatio {
statistics.RatioOfPseudoEstimate.Store(nc.Performance.PseudoEstimateRatio)
}
if nc.Performance.PProfSQLCPU != c.Performance.PProfSQLCPU {
util.EnablePProfSQLCPU.Store(nc.Performance.PProfSQLCPU)
}
if nc.TiKVClient.StoreLimit != c.TiKVClient.StoreLimit {
storeutil.StoreLimit.Store(nc.TiKVClient.StoreLimit)
}
Expand Down Expand Up @@ -491,6 +495,7 @@ func setGlobalVars() {
statistics.FeedbackProbability.Store(cfg.Performance.FeedbackProbability)
handle.MaxQueryFeedbackCount.Store(int64(cfg.Performance.QueryFeedbackLimit))
statistics.RatioOfPseudoEstimate.Store(cfg.Performance.PseudoEstimateRatio)
util.EnablePProfSQLCPU.Store(cfg.Performance.PProfSQLCPU)
ddl.RunWorker = cfg.RunDDL
if cfg.SplitTable {
atomic.StoreUint32(&ddl.EnableSplitTableRegion, 1)
Expand Down
4 changes: 4 additions & 0 deletions util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/util/logutil"
"github.com/uber-go/atomic"
"go.uber.org/zap"
)

Expand All @@ -41,6 +42,9 @@ const (
GCTimeFormat = "20060102-15:04:05 -0700"
)

// EnablePProfSQLCPU control whether collect pprof cpu in SQL level.
var EnablePProfSQLCPU = atomic.NewBool(false)

// RunWithRetry will run the f with backoff and retry.
// retryCnt: Max retry count
// backoff: When run f failed, it will sleep backoff * triedCount time.Millisecond.
Expand Down

0 comments on commit ca3e7ec

Please sign in to comment.