execdetails.go 17.3 KB
Newer Older
L
liipx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package execdetails

import (
	"fmt"
martianzhang's avatar
martianzhang 已提交
18
	"sort"
martianzhang's avatar
martianzhang 已提交
19
	"strconv"
L
liipx 已提交
20 21 22 23
	"strings"
	"sync"
	"sync/atomic"
	"time"
martianzhang's avatar
martianzhang 已提交
24 25

	"github.com/pingcap/tipb/go-tipb"
martianzhang's avatar
martianzhang 已提交
26
	"go.uber.org/zap"
L
liipx 已提交
27 28
)

martianzhang's avatar
martianzhang 已提交
29 30
type commitDetailCtxKeyType struct{}

martianzhang's avatar
martianzhang 已提交
31
// CommitDetailCtxKey presents CommitDetail info key in context.
martianzhang's avatar
martianzhang 已提交
32
var CommitDetailCtxKey = commitDetailCtxKeyType{}
martianzhang's avatar
martianzhang 已提交
33

L
liipx 已提交
34 35
// ExecDetails contains execution detail information.
type ExecDetails struct {
martianzhang's avatar
martianzhang 已提交
36 37 38 39 40 41 42 43 44 45 46
	CalleeAddress    string
	ProcessTime      time.Duration
	WaitTime         time.Duration
	BackoffTime      time.Duration
	LockKeysDuration time.Duration
	BackoffSleep     map[string]time.Duration
	BackoffTimes     map[string]int
	RequestCount     int
	TotalKeys        int64
	ProcessedKeys    int64
	CommitDetail     *CommitDetails
martianzhang's avatar
martianzhang 已提交
47 48 49 50
}

// CommitDetails contains commit detail information.
type CommitDetails struct {
martianzhang's avatar
martianzhang 已提交
51 52 53 54 55 56 57
	GetCommitTsTime        time.Duration
	PrewriteTime           time.Duration
	WaitPrewriteBinlogTime time.Duration
	CommitTime             time.Duration
	LocalLatchTime         time.Duration
	CommitBackoffTime      int64
	Mu                     struct {
martianzhang's avatar
martianzhang 已提交
58 59 60
		sync.Mutex
		BackoffTypes []fmt.Stringer
	}
martianzhang's avatar
martianzhang 已提交
61 62 63 64 65
	ResolveLockTime   int64
	WriteKeys         int
	WriteSize         int
	PrewriteRegionNum int32
	TxnRetry          int
L
liipx 已提交
66 67
}

martianzhang's avatar
martianzhang 已提交
68 69 70 71 72 73 74
const (
	// ProcessTimeStr represents the sum of process time of all the coprocessor tasks.
	ProcessTimeStr = "Process_time"
	// WaitTimeStr means the time of all coprocessor wait.
	WaitTimeStr = "Wait_time"
	// BackoffTimeStr means the time of all back-off.
	BackoffTimeStr = "Backoff_time"
martianzhang's avatar
martianzhang 已提交
75 76
	// LockKeysTimeStr means the time interval between pessimistic lock wait start and lock got obtain
	LockKeysTimeStr = "LockKeys_time"
martianzhang's avatar
martianzhang 已提交
77 78 79 80 81 82
	// RequestCountStr means the request count.
	RequestCountStr = "Request_count"
	// TotalKeysStr means the total scan keys.
	TotalKeysStr = "Total_keys"
	// ProcessKeysStr means the total processed keys.
	ProcessKeysStr = "Process_keys"
martianzhang's avatar
martianzhang 已提交
83 84
	// PreWriteTimeStr means the time of pre-write.
	PreWriteTimeStr = "Prewrite_time"
martianzhang's avatar
martianzhang 已提交
85 86
	// WaitPrewriteBinlogTimeStr means the time of waiting prewrite binlog finished when transaction committing.
	WaitPrewriteBinlogTimeStr = "Wait_prewrite_binlog_time"
martianzhang's avatar
martianzhang 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	// CommitTimeStr means the time of commit.
	CommitTimeStr = "Commit_time"
	// GetCommitTSTimeStr means the time of getting commit ts.
	GetCommitTSTimeStr = "Get_commit_ts_time"
	// CommitBackoffTimeStr means the time of commit backoff.
	CommitBackoffTimeStr = "Commit_backoff_time"
	// BackoffTypesStr means the backoff type.
	BackoffTypesStr = "Backoff_types"
	// ResolveLockTimeStr means the time of resolving lock.
	ResolveLockTimeStr = "Resolve_lock_time"
	// LocalLatchWaitTimeStr means the time of waiting in local latch.
	LocalLatchWaitTimeStr = "Local_latch_wait_time"
	// WriteKeysStr means the count of keys in the transaction.
	WriteKeysStr = "Write_keys"
	// WriteSizeStr means the key/value size in the transaction.
	WriteSizeStr = "Write_size"
	// PrewriteRegionStr means the count of region when pre-write.
	PrewriteRegionStr = "Prewrite_region"
	// TxnRetryStr means the count of transaction retry.
	TxnRetryStr = "Txn_retry"
martianzhang's avatar
martianzhang 已提交
107 108
)

L
liipx 已提交
109 110 111 112
// String implements the fmt.Stringer interface.
func (d ExecDetails) String() string {
	parts := make([]string, 0, 6)
	if d.ProcessTime > 0 {
martianzhang's avatar
martianzhang 已提交
113
		parts = append(parts, ProcessTimeStr+": "+strconv.FormatFloat(d.ProcessTime.Seconds(), 'f', -1, 64))
L
liipx 已提交
114 115
	}
	if d.WaitTime > 0 {
martianzhang's avatar
martianzhang 已提交
116
		parts = append(parts, WaitTimeStr+": "+strconv.FormatFloat(d.WaitTime.Seconds(), 'f', -1, 64))
L
liipx 已提交
117 118
	}
	if d.BackoffTime > 0 {
martianzhang's avatar
martianzhang 已提交
119
		parts = append(parts, BackoffTimeStr+": "+strconv.FormatFloat(d.BackoffTime.Seconds(), 'f', -1, 64))
L
liipx 已提交
120
	}
martianzhang's avatar
martianzhang 已提交
121 122 123
	if d.LockKeysDuration > 0 {
		parts = append(parts, LockKeysTimeStr+": "+strconv.FormatFloat(d.LockKeysDuration.Seconds(), 'f', -1, 64))
	}
L
liipx 已提交
124
	if d.RequestCount > 0 {
martianzhang's avatar
martianzhang 已提交
125
		parts = append(parts, RequestCountStr+": "+strconv.FormatInt(int64(d.RequestCount), 10))
L
liipx 已提交
126 127
	}
	if d.TotalKeys > 0 {
martianzhang's avatar
martianzhang 已提交
128
		parts = append(parts, TotalKeysStr+": "+strconv.FormatInt(d.TotalKeys, 10))
L
liipx 已提交
129 130
	}
	if d.ProcessedKeys > 0 {
martianzhang's avatar
martianzhang 已提交
131
		parts = append(parts, ProcessKeysStr+": "+strconv.FormatInt(d.ProcessedKeys, 10))
L
liipx 已提交
132
	}
martianzhang's avatar
martianzhang 已提交
133 134 135
	commitDetails := d.CommitDetail
	if commitDetails != nil {
		if commitDetails.PrewriteTime > 0 {
martianzhang's avatar
martianzhang 已提交
136 137
			parts = append(parts, PreWriteTimeStr+": "+strconv.FormatFloat(commitDetails.PrewriteTime.Seconds(), 'f', -1, 64))
		}
martianzhang's avatar
martianzhang 已提交
138 139
		if commitDetails.WaitPrewriteBinlogTime > 0 {
			parts = append(parts, WaitPrewriteBinlogTimeStr+": "+strconv.FormatFloat(commitDetails.WaitPrewriteBinlogTime.Seconds(), 'f', -1, 64))
martianzhang's avatar
martianzhang 已提交
140 141
		}
		if commitDetails.CommitTime > 0 {
martianzhang's avatar
martianzhang 已提交
142
			parts = append(parts, CommitTimeStr+": "+strconv.FormatFloat(commitDetails.CommitTime.Seconds(), 'f', -1, 64))
martianzhang's avatar
martianzhang 已提交
143 144
		}
		if commitDetails.GetCommitTsTime > 0 {
martianzhang's avatar
martianzhang 已提交
145
			parts = append(parts, GetCommitTSTimeStr+": "+strconv.FormatFloat(commitDetails.GetCommitTsTime.Seconds(), 'f', -1, 64))
martianzhang's avatar
martianzhang 已提交
146
		}
martianzhang's avatar
martianzhang 已提交
147 148
		commitBackoffTime := atomic.LoadInt64(&commitDetails.CommitBackoffTime)
		if commitBackoffTime > 0 {
martianzhang's avatar
martianzhang 已提交
149
			parts = append(parts, CommitBackoffTimeStr+": "+strconv.FormatFloat(time.Duration(commitBackoffTime).Seconds(), 'f', -1, 64))
martianzhang's avatar
martianzhang 已提交
150 151 152
		}
		commitDetails.Mu.Lock()
		if len(commitDetails.Mu.BackoffTypes) > 0 {
martianzhang's avatar
martianzhang 已提交
153
			parts = append(parts, BackoffTypesStr+": "+fmt.Sprintf("%v", commitDetails.Mu.BackoffTypes))
martianzhang's avatar
martianzhang 已提交
154
		}
martianzhang's avatar
martianzhang 已提交
155
		commitDetails.Mu.Unlock()
martianzhang's avatar
martianzhang 已提交
156 157
		resolveLockTime := atomic.LoadInt64(&commitDetails.ResolveLockTime)
		if resolveLockTime > 0 {
martianzhang's avatar
martianzhang 已提交
158
			parts = append(parts, ResolveLockTimeStr+": "+strconv.FormatFloat(time.Duration(resolveLockTime).Seconds(), 'f', -1, 64))
martianzhang's avatar
martianzhang 已提交
159 160
		}
		if commitDetails.LocalLatchTime > 0 {
martianzhang's avatar
martianzhang 已提交
161
			parts = append(parts, LocalLatchWaitTimeStr+": "+strconv.FormatFloat(commitDetails.LocalLatchTime.Seconds(), 'f', -1, 64))
martianzhang's avatar
martianzhang 已提交
162 163
		}
		if commitDetails.WriteKeys > 0 {
martianzhang's avatar
martianzhang 已提交
164
			parts = append(parts, WriteKeysStr+": "+strconv.FormatInt(int64(commitDetails.WriteKeys), 10))
martianzhang's avatar
martianzhang 已提交
165 166
		}
		if commitDetails.WriteSize > 0 {
martianzhang's avatar
martianzhang 已提交
167
			parts = append(parts, WriteSizeStr+": "+strconv.FormatInt(int64(commitDetails.WriteSize), 10))
martianzhang's avatar
martianzhang 已提交
168 169 170
		}
		prewriteRegionNum := atomic.LoadInt32(&commitDetails.PrewriteRegionNum)
		if prewriteRegionNum > 0 {
martianzhang's avatar
martianzhang 已提交
171
			parts = append(parts, PrewriteRegionStr+": "+strconv.FormatInt(int64(prewriteRegionNum), 10))
martianzhang's avatar
martianzhang 已提交
172 173
		}
		if commitDetails.TxnRetry > 0 {
martianzhang's avatar
martianzhang 已提交
174
			parts = append(parts, TxnRetryStr+": "+strconv.FormatInt(int64(commitDetails.TxnRetry), 10))
martianzhang's avatar
martianzhang 已提交
175 176
		}
	}
L
liipx 已提交
177 178 179
	return strings.Join(parts, " ")
}

martianzhang's avatar
martianzhang 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
// ToZapFields wraps the ExecDetails as zap.Fields.
func (d ExecDetails) ToZapFields() (fields []zap.Field) {
	fields = make([]zap.Field, 0, 16)
	if d.ProcessTime > 0 {
		fields = append(fields, zap.String(strings.ToLower(ProcessTimeStr), strconv.FormatFloat(d.ProcessTime.Seconds(), 'f', -1, 64)+"s"))
	}
	if d.WaitTime > 0 {
		fields = append(fields, zap.String(strings.ToLower(WaitTimeStr), strconv.FormatFloat(d.ProcessTime.Seconds(), 'f', -1, 64)+"s"))
	}
	if d.BackoffTime > 0 {
		fields = append(fields, zap.String(strings.ToLower(BackoffTimeStr), strconv.FormatFloat(d.BackoffTime.Seconds(), 'f', -1, 64)+"s"))
	}
	if d.RequestCount > 0 {
		fields = append(fields, zap.String(strings.ToLower(RequestCountStr), strconv.FormatInt(int64(d.RequestCount), 10)))
	}
	if d.TotalKeys > 0 {
		fields = append(fields, zap.String(strings.ToLower(TotalKeysStr), strconv.FormatInt(d.TotalKeys, 10)))
	}
	if d.ProcessedKeys > 0 {
		fields = append(fields, zap.String(strings.ToLower(ProcessKeysStr), strconv.FormatInt(d.ProcessedKeys, 10)))
	}
	commitDetails := d.CommitDetail
	if commitDetails != nil {
		if commitDetails.PrewriteTime > 0 {
			fields = append(fields, zap.String("prewrite_time", fmt.Sprintf("%v", strconv.FormatFloat(commitDetails.PrewriteTime.Seconds(), 'f', -1, 64)+"s")))
		}
		if commitDetails.CommitTime > 0 {
			fields = append(fields, zap.String("commit_time", fmt.Sprintf("%v", strconv.FormatFloat(commitDetails.CommitTime.Seconds(), 'f', -1, 64)+"s")))
		}
		if commitDetails.GetCommitTsTime > 0 {
			fields = append(fields, zap.String("get_commit_ts_time", fmt.Sprintf("%v", strconv.FormatFloat(commitDetails.GetCommitTsTime.Seconds(), 'f', -1, 64)+"s")))
		}
martianzhang's avatar
martianzhang 已提交
212 213 214 215 216 217 218
		commitBackoffTime := atomic.LoadInt64(&commitDetails.CommitBackoffTime)
		if commitBackoffTime > 0 {
			fields = append(fields, zap.String("commit_backoff_time", fmt.Sprintf("%v", strconv.FormatFloat(time.Duration(commitBackoffTime).Seconds(), 'f', -1, 64)+"s")))
		}
		commitDetails.Mu.Lock()
		if len(commitDetails.Mu.BackoffTypes) > 0 {
			fields = append(fields, zap.String("backoff_types", fmt.Sprintf("%v", commitDetails.Mu.BackoffTypes)))
martianzhang's avatar
martianzhang 已提交
219
		}
martianzhang's avatar
martianzhang 已提交
220
		commitDetails.Mu.Unlock()
martianzhang's avatar
martianzhang 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
		resolveLockTime := atomic.LoadInt64(&commitDetails.ResolveLockTime)
		if resolveLockTime > 0 {
			fields = append(fields, zap.String("resolve_lock_time", fmt.Sprintf("%v", strconv.FormatFloat(time.Duration(resolveLockTime).Seconds(), 'f', -1, 64)+"s")))
		}
		if commitDetails.LocalLatchTime > 0 {
			fields = append(fields, zap.String("local_latch_wait_time", fmt.Sprintf("%v", strconv.FormatFloat(commitDetails.LocalLatchTime.Seconds(), 'f', -1, 64)+"s")))
		}
		if commitDetails.WriteKeys > 0 {
			fields = append(fields, zap.Int("write_keys", commitDetails.WriteKeys))
		}
		if commitDetails.WriteSize > 0 {
			fields = append(fields, zap.Int("write_size", commitDetails.WriteSize))
		}
		prewriteRegionNum := atomic.LoadInt32(&commitDetails.PrewriteRegionNum)
		if prewriteRegionNum > 0 {
			fields = append(fields, zap.Int32("prewrite_region", prewriteRegionNum))
		}
		if commitDetails.TxnRetry > 0 {
			fields = append(fields, zap.Int("txn_retry", commitDetails.TxnRetry))
		}
	}
	return fields
}

martianzhang's avatar
martianzhang 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
// CopRuntimeStats collects cop tasks' execution info.
type CopRuntimeStats struct {
	sync.Mutex

	// stats stores the runtime statistics of coprocessor tasks.
	// The key of the map is the tikv-server address. Because a tikv-server can
	// have many region leaders, several coprocessor tasks can be sent to the
	// same tikv-server instance. We have to use a list to maintain all tasks
	// executed on each instance.
	stats map[string][]*RuntimeStats
}

// RecordOneCopTask records a specific cop tasks's execution detail.
func (crs *CopRuntimeStats) RecordOneCopTask(address string, summary *tipb.ExecutorExecutionSummary) {
	crs.Lock()
	defer crs.Unlock()
	crs.stats[address] = append(crs.stats[address],
martianzhang's avatar
martianzhang 已提交
262 263 264
		&RuntimeStats{loop: int32(*summary.NumIterations),
			consume: int64(*summary.TimeProcessedNs),
			rows:    int64(*summary.NumProducedRows)})
martianzhang's avatar
martianzhang 已提交
265 266
}

martianzhang's avatar
martianzhang 已提交
267 268 269 270 271 272 273 274 275 276
// GetActRows return total rows of CopRuntimeStats.
func (crs *CopRuntimeStats) GetActRows() (totalRows int64) {
	for _, instanceStats := range crs.stats {
		for _, stat := range instanceStats {
			totalRows += stat.rows
		}
	}
	return totalRows
}

martianzhang's avatar
martianzhang 已提交
277 278 279 280 281
func (crs *CopRuntimeStats) String() string {
	if len(crs.stats) == 0 {
		return ""
	}

martianzhang's avatar
martianzhang 已提交
282
	var totalTasks int64
martianzhang's avatar
martianzhang 已提交
283 284 285 286 287 288 289 290 291 292 293
	var totalIters int32
	procTimes := make([]time.Duration, 0, 32)
	for _, instanceStats := range crs.stats {
		for _, stat := range instanceStats {
			procTimes = append(procTimes, time.Duration(stat.consume)*time.Nanosecond)
			totalIters += stat.loop
			totalTasks++
		}
	}

	if totalTasks == 1 {
martianzhang's avatar
martianzhang 已提交
294
		return fmt.Sprintf("time:%v, loops:%d", procTimes[0], totalIters)
martianzhang's avatar
martianzhang 已提交
295 296 297 298
	}

	n := len(procTimes)
	sort.Slice(procTimes, func(i, j int) bool { return procTimes[i] < procTimes[j] })
martianzhang's avatar
martianzhang 已提交
299 300
	return fmt.Sprintf("proc max:%v, min:%v, p80:%v, p95:%v, iters:%v, tasks:%v",
		procTimes[n-1], procTimes[0], procTimes[n*4/5], procTimes[n*19/20], totalIters, totalTasks)
martianzhang's avatar
martianzhang 已提交
301 302
}

martianzhang's avatar
martianzhang 已提交
303 304 305 306 307
// ReaderRuntimeStats collects stats for TableReader, IndexReader and IndexLookupReader
type ReaderRuntimeStats struct {
	sync.Mutex

	copRespTime []time.Duration
martianzhang's avatar
martianzhang 已提交
308
	procKeys    []int64
martianzhang's avatar
martianzhang 已提交
309 310 311
}

// recordOneCopTask record once cop response time to update maxcopRespTime
martianzhang's avatar
martianzhang 已提交
312
func (rrs *ReaderRuntimeStats) recordOneCopTask(t time.Duration, detail *ExecDetails) {
martianzhang's avatar
martianzhang 已提交
313 314 315
	rrs.Lock()
	defer rrs.Unlock()
	rrs.copRespTime = append(rrs.copRespTime, t)
martianzhang's avatar
martianzhang 已提交
316
	rrs.procKeys = append(rrs.procKeys, detail.ProcessedKeys)
martianzhang's avatar
martianzhang 已提交
317 318 319 320 321 322 323 324
}

func (rrs *ReaderRuntimeStats) String() string {
	size := len(rrs.copRespTime)
	if size == 0 {
		return ""
	}
	if size == 1 {
martianzhang's avatar
martianzhang 已提交
325
		return fmt.Sprintf("rpc num: 1, rpc time:%v, proc keys:%v", rrs.copRespTime[0], rrs.procKeys[0])
martianzhang's avatar
martianzhang 已提交
326 327 328 329 330 331 332 333 334 335 336
	}
	sort.Slice(rrs.copRespTime, func(i, j int) bool {
		return rrs.copRespTime[i] < rrs.copRespTime[j]
	})
	vMax, vMin := rrs.copRespTime[size-1], rrs.copRespTime[0]
	vP80, vP95 := rrs.copRespTime[size*4/5], rrs.copRespTime[size*19/20]
	sum := 0.0
	for _, t := range rrs.copRespTime {
		sum += float64(t)
	}
	vAvg := time.Duration(sum / float64(size))
martianzhang's avatar
martianzhang 已提交
337 338 339 340 341 342 343

	sort.Slice(rrs.procKeys, func(i, j int) bool {
		return rrs.procKeys[i] < rrs.procKeys[j]
	})
	keyMax := rrs.procKeys[size-1]
	keyP95 := rrs.procKeys[size*19/20]
	return fmt.Sprintf("rpc num: %v, rpc max:%v, min:%v, avg:%v, p80:%v, p95:%v, proc keys max:%v, p95:%v", size, vMax, vMin, vAvg, vP80, vP95, keyMax, keyP95)
martianzhang's avatar
martianzhang 已提交
344 345
}

L
liipx 已提交
346 347
// RuntimeStatsColl collects executors's execution info.
type RuntimeStatsColl struct {
martianzhang's avatar
martianzhang 已提交
348 349 350 351
	mu          sync.Mutex
	rootStats   map[string]*RuntimeStats
	copStats    map[string]*CopRuntimeStats
	readerStats map[string]*ReaderRuntimeStats
L
liipx 已提交
352 353
}

martianzhang's avatar
martianzhang 已提交
354 355 356 357 358 359
// concurrencyInfo is used to save the concurrency information of the executor operator
type concurrencyInfo struct {
	concurrencyName string
	concurrencyNum  int
}

L
liipx 已提交
360 361 362 363 364 365 366 367
// RuntimeStats collects one executor's execution info.
type RuntimeStats struct {
	// executor's Next() called times.
	loop int32
	// executor consume time.
	consume int64
	// executor return row count.
	rows int64
martianzhang's avatar
martianzhang 已提交
368

martianzhang's avatar
martianzhang 已提交
369
	// protect concurrency
martianzhang's avatar
martianzhang 已提交
370 371 372
	mu sync.Mutex
	// executor concurrency information
	concurrency []concurrencyInfo
martianzhang's avatar
martianzhang 已提交
373 374 375

	// additional information for executors
	additionalInfo string
L
liipx 已提交
376 377 378 379
}

// NewRuntimeStatsColl creates new executor collector.
func NewRuntimeStatsColl() *RuntimeStatsColl {
martianzhang's avatar
martianzhang 已提交
380
	return &RuntimeStatsColl{rootStats: make(map[string]*RuntimeStats),
martianzhang's avatar
martianzhang 已提交
381
		copStats: make(map[string]*CopRuntimeStats), readerStats: make(map[string]*ReaderRuntimeStats)}
L
liipx 已提交
382 383
}

martianzhang's avatar
martianzhang 已提交
384 385
// GetRootStats gets execStat for a executor.
func (e *RuntimeStatsColl) GetRootStats(planID string) *RuntimeStats {
L
liipx 已提交
386 387
	e.mu.Lock()
	defer e.mu.Unlock()
martianzhang's avatar
martianzhang 已提交
388
	runtimeStats, exists := e.rootStats[planID]
L
liipx 已提交
389 390
	if !exists {
		runtimeStats = &RuntimeStats{}
martianzhang's avatar
martianzhang 已提交
391
		e.rootStats[planID] = runtimeStats
L
liipx 已提交
392 393 394 395
	}
	return runtimeStats
}

martianzhang's avatar
martianzhang 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
// GetCopStats gets the CopRuntimeStats specified by planID.
func (e *RuntimeStatsColl) GetCopStats(planID string) *CopRuntimeStats {
	e.mu.Lock()
	defer e.mu.Unlock()
	copStats, ok := e.copStats[planID]
	if !ok {
		copStats = &CopRuntimeStats{stats: make(map[string][]*RuntimeStats)}
		e.copStats[planID] = copStats
	}
	return copStats
}

// RecordOneCopTask records a specific cop tasks's execution detail.
func (e *RuntimeStatsColl) RecordOneCopTask(planID, address string, summary *tipb.ExecutorExecutionSummary) {
	copStats := e.GetCopStats(planID)
	copStats.RecordOneCopTask(address, summary)
}

martianzhang's avatar
martianzhang 已提交
414
// RecordOneReaderStats records a specific stats for TableReader, IndexReader and IndexLookupReader.
martianzhang's avatar
martianzhang 已提交
415
func (e *RuntimeStatsColl) RecordOneReaderStats(planID string, copRespTime time.Duration, detail *ExecDetails) {
martianzhang's avatar
martianzhang 已提交
416
	readerStats := e.GetReaderStats(planID)
martianzhang's avatar
martianzhang 已提交
417
	readerStats.recordOneCopTask(copRespTime, detail)
martianzhang's avatar
martianzhang 已提交
418 419
}

martianzhang's avatar
martianzhang 已提交
420 421 422 423 424 425 426 427 428 429
// ExistsRootStats checks if the planID exists in the rootStats collection.
func (e *RuntimeStatsColl) ExistsRootStats(planID string) bool {
	e.mu.Lock()
	defer e.mu.Unlock()
	_, exists := e.rootStats[planID]
	return exists
}

// ExistsCopStats checks if the planID exists in the copStats collection.
func (e *RuntimeStatsColl) ExistsCopStats(planID string) bool {
L
liipx 已提交
430 431
	e.mu.Lock()
	defer e.mu.Unlock()
martianzhang's avatar
martianzhang 已提交
432
	_, exists := e.copStats[planID]
L
liipx 已提交
433 434 435
	return exists
}

martianzhang's avatar
martianzhang 已提交
436 437 438 439 440 441 442 443 444 445 446 447
// GetReaderStats gets the ReaderRuntimeStats specified by planID.
func (e *RuntimeStatsColl) GetReaderStats(planID string) *ReaderRuntimeStats {
	e.mu.Lock()
	defer e.mu.Unlock()
	stats, exists := e.readerStats[planID]
	if !exists {
		stats = &ReaderRuntimeStats{copRespTime: make([]time.Duration, 0, 20)}
		e.readerStats[planID] = stats
	}
	return stats
}

L
liipx 已提交
448 449 450 451 452 453 454
// Record records executor's execution.
func (e *RuntimeStats) Record(d time.Duration, rowNum int) {
	atomic.AddInt32(&e.loop, 1)
	atomic.AddInt64(&e.consume, int64(d))
	atomic.AddInt64(&e.rows, int64(rowNum))
}

L
liipx 已提交
455 456 457 458 459
// SetRowNum sets the row num.
func (e *RuntimeStats) SetRowNum(rowNum int64) {
	atomic.StoreInt64(&e.rows, rowNum)
}

martianzhang's avatar
martianzhang 已提交
460 461 462 463 464 465 466 467
// SetConcurrencyInfo sets the concurrency information.
// When the num <= 0, it means the exector operator is not executed parallel.
func (e *RuntimeStats) SetConcurrencyInfo(name string, num int) {
	e.mu.Lock()
	defer e.mu.Unlock()
	e.concurrency = append(e.concurrency, concurrencyInfo{concurrencyName: name, concurrencyNum: num})
}

martianzhang's avatar
martianzhang 已提交
468 469 470 471 472 473 474 475 476 477 478 479
// SetAdditionalInfo sets the additional information.
func (e *RuntimeStats) SetAdditionalInfo(info string) {
	e.mu.Lock()
	e.additionalInfo = info
	e.mu.Unlock()
}

// GetActRows return rows of CopRuntimeStats.
func (e *RuntimeStats) GetActRows() int64 {
	return e.rows
}

L
liipx 已提交
480
func (e *RuntimeStats) String() string {
martianzhang's avatar
martianzhang 已提交
481
	result := fmt.Sprintf("time:%v, loops:%d", time.Duration(e.consume), e.loop)
martianzhang's avatar
martianzhang 已提交
482 483 484 485 486 487 488 489 490
	if len(e.concurrency) > 0 {
		for _, concurrency := range e.concurrency {
			if concurrency.concurrencyNum > 0 {
				result += fmt.Sprintf(", %s:%d", concurrency.concurrencyName, concurrency.concurrencyNum)
			} else {
				result += fmt.Sprintf(", %s:OFF", concurrency.concurrencyName)
			}
		}
	}
martianzhang's avatar
martianzhang 已提交
491 492 493
	if len(e.additionalInfo) > 0 {
		result += ", " + e.additionalInfo
	}
martianzhang's avatar
martianzhang 已提交
494
	return result
L
liipx 已提交
495
}