Skip to content

Commit

Permalink
*: reduce mem allocs of stmt summary (#54128)
Browse files Browse the repository at this point in the history
ref #54047
  • Loading branch information
zyguan committed Jun 24, 2024
1 parent eac8012 commit d5b89f8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pkg/executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
TimeOptimize: sessVars.DurationOptimization,
TimeWaitTS: sessVars.DurationWaitTS,
IndexNames: indexNames,
CopTasks: copTaskInfo,
CopTasks: &copTaskInfo,
ExecDetail: execDetail,
MemMax: memMax,
DiskMax: diskMax,
Expand Down Expand Up @@ -1989,7 +1989,7 @@ func (a *ExecStmt) SummaryStmt(succ bool) {
ParseLatency: sessVars.DurationParse,
CompileLatency: sessVars.DurationCompile,
StmtCtx: stmtCtx,
CopTasks: copTaskInfo,
CopTasks: &copTaskInfo,
ExecDetail: &execDetail,
MemMax: memMax,
DiskMax: diskMax,
Expand Down
20 changes: 10 additions & 10 deletions pkg/util/execdetails/execdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,19 +464,11 @@ func (s *SyncExecDetails) GetExecDetails() ExecDetails {
}

// CopTasksDetails returns some useful information of cop-tasks during execution.
func (s *SyncExecDetails) CopTasksDetails() *CopTasksDetails {
func (s *SyncExecDetails) CopTasksDetails() CopTasksDetails {
s.mu.Lock()
defer s.mu.Unlock()
n := s.detailsSummary.NumCopTasks
d := &CopTasksDetails{
NumCopTasks: n,
MaxBackoffTime: make(map[string]time.Duration),
AvgBackoffTime: make(map[string]time.Duration),
P90BackoffTime: make(map[string]time.Duration),
TotBackoffTime: make(map[string]time.Duration),
TotBackoffTimes: make(map[string]int),
MaxBackoffAddress: make(map[string]string),
}
d := CopTasksDetails{NumCopTasks: n}
if n == 0 {
return d
}
Expand All @@ -491,6 +483,14 @@ func (s *SyncExecDetails) CopTasksDetails() *CopTasksDetails {
d.MaxWaitTime = s.detailsSummary.WaitTimePercentile.GetMax().D
d.MaxWaitAddress = s.detailsSummary.WaitTimePercentile.GetMax().Addr

if len(s.detailsSummary.BackoffInfo) > 0 {
d.MaxBackoffTime = make(map[string]time.Duration)
d.AvgBackoffTime = make(map[string]time.Duration)
d.P90BackoffTime = make(map[string]time.Duration)
d.TotBackoffTime = make(map[string]time.Duration)
d.TotBackoffTimes = make(map[string]int)
d.MaxBackoffAddress = make(map[string]string)
}
for backoff, items := range s.detailsSummary.BackoffInfo {
if items == nil {
continue
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/stmtsummary/statement_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ type stmtSummaryByDigestKey struct {
prevDigest string
// The digest of the plan of this SQL.
planDigest string
// `hash` is the hash value of this object.
hash []byte
// `resourceGroupName` is the resource group's name of this statement is bind to.
resourceGroupName string
// `hash` is the hash value of this object.
hash []byte
}

// Hash implements SimpleLRUCache.Key.
// Only when current SQL is `commit` do we record `prevSQL`. Otherwise, `prevSQL` is empty.
// `prevSQL` is included in the key To distinguish different transactions.
func (key *stmtSummaryByDigestKey) Hash() []byte {
if len(key.hash) == 0 {
key.hash = make([]byte, 0, len(key.schemaName)+len(key.digest)+len(key.prevDigest)+len(key.planDigest))
key.hash = make([]byte, 0, len(key.schemaName)+len(key.digest)+len(key.prevDigest)+len(key.planDigest)+len(key.resourceGroupName))
key.hash = append(key.hash, hack.Slice(key.digest)...)
key.hash = append(key.hash, hack.Slice(key.schemaName)...)
key.hash = append(key.hash, hack.Slice(key.prevDigest)...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/stmtsummary/v2/stmtsummary.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ type stmtKey struct {
// `prevSQL` is included in the key To distinguish different transactions.
func (k *stmtKey) Hash() []byte {
if len(k.hash) == 0 {
k.hash = make([]byte, 0, len(k.schemaName)+len(k.digest)+len(k.prevDigest)+len(k.planDigest))
k.hash = make([]byte, 0, len(k.schemaName)+len(k.digest)+len(k.prevDigest)+len(k.planDigest)+len(k.resourceGroupName))
k.hash = append(k.hash, hack.Slice(k.digest)...)
k.hash = append(k.hash, hack.Slice(k.schemaName)...)
k.hash = append(k.hash, hack.Slice(k.prevDigest)...)
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ func GenLogFields(costTime time.Duration, info *ProcessInfo, needTruncateSQL boo
logFields = append(logFields, zap.String("cost_time", strconv.FormatFloat(costTime.Seconds(), 'f', -1, 64)+"s"))
execDetail := info.StmtCtx.GetExecDetails()
logFields = append(logFields, execDetail.ToZapFields()...)
if copTaskInfo := info.StmtCtx.CopTasksDetails(); copTaskInfo != nil {
logFields = append(logFields, copTaskInfo.ToZapFields()...)
}
copTaskInfo := info.StmtCtx.CopTasksDetails()
logFields = append(logFields, copTaskInfo.ToZapFields()...)
if statsInfo := info.StatsInfo(info.Plan); len(statsInfo) > 0 {
var buf strings.Builder
firstComma := false
Expand Down

0 comments on commit d5b89f8

Please sign in to comment.