Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: support tagging SQL statements in pprof results #14312

Merged
merged 4 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ type Performance struct {
TCPKeepAlive bool `toml:"tcp-keep-alive" json:"tcp-keep-alive"`
CrossJoin bool `toml:"cross-join" json:"cross-join"`
RunAutoAnalyze bool `toml:"run-auto-analyze" json:"run-auto-analyze"`
PProfSQLCPU bool `toml:"pprof-sql-cpu" json:"pprof-sql-cpu"`
}

// PlanCache is the PlanCache section of the config.
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 @@ -804,6 +807,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)
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -51,6 +51,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/domainutil"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/memory"
Expand Down Expand Up @@ -394,7 +395,7 @@ func loadConfig() string {
var hotReloadConfigItems = []string{"Performance.MaxProcs", "Performance.MaxMemory", "Performance.CrossJoin",
"Performance.FeedbackProbability", "Performance.QueryFeedbackLimit", "Performance.PseudoEstimateRatio",
"OOMUseTmpStorage", "OOMAction", "MemQuotaQuery", "StmtSummary.MaxStmtCount", "StmtSummary.MaxSQLLength", "Log.QueryLogMaxLen",
"TiKVClient.EnableChunkRPC", "TiKVClient.StoreLimit"}
"TiKVClient.EnableChunkRPC", "TiKVClient.StoreLimit", "Performance.PProfSQLCPU"}

func reloadConfig(nc, c *config.Config) {
// Just a part of config items need to be reload explicitly.
Expand All @@ -417,6 +418,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 @@ -536,6 +540,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 @@ -28,6 +28,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 @@ -40,6 +41,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