/
cpu.go
266 lines (240 loc) · 7.59 KB
/
cpu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2021 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package collector
import (
"context"
"runtime/pprof"
"sync"
"time"
"github.com/google/pprof/profile"
"github.com/wuhuizuo/tidb6/util"
"github.com/wuhuizuo/tidb6/util/cpuprofile"
"github.com/wuhuizuo/tidb6/util/hack"
"github.com/wuhuizuo/tidb6/util/logutil"
topsqlstate "github.com/wuhuizuo/tidb6/util/topsql/state"
"go.uber.org/zap"
)
const (
labelSQLDigest = "sql_digest"
labelPlanDigest = "plan_digest"
)
// Collector uses to collect SQL execution cpu time.
type Collector interface {
// Collect uses to collect the SQL execution cpu time.
Collect(stats []SQLCPUTimeRecord)
}
// SQLCPUTimeRecord represents a single record of how much cpu time a sql plan consumes in one second.
//
// PlanDigest can be empty, because:
// 1. some sql statements has no plan, like `COMMIT`
// 2. when a sql statement is being compiled, there's no plan yet
type SQLCPUTimeRecord struct {
SQLDigest []byte
PlanDigest []byte
CPUTimeMs uint32
}
// SQLCPUCollector uses to consume cpu profile from globalCPUProfiler, then parse the SQL CPU usage from the cpu profile data.
// It is not thread-safe, should only be used in one goroutine.
type SQLCPUCollector struct {
ctx context.Context
collector Collector
cancel context.CancelFunc
wg sync.WaitGroup
started bool
registered bool
}
// NewSQLCPUCollector create a SQLCPUCollector.
func NewSQLCPUCollector(c Collector) *SQLCPUCollector {
return &SQLCPUCollector{
collector: c,
}
}
// Start uses to start to run SQLCPUCollector.
// This will register a consumer into globalCPUProfiler, then SQLCPUCollector will receive cpu profile data per seconds.
// WARN: this function is not thread-safe.
func (sp *SQLCPUCollector) Start() {
if sp.started {
return
}
sp.started = true
sp.ctx, sp.cancel = context.WithCancel(context.Background())
sp.wg.Add(1)
go sp.collectSQLCPULoop()
logutil.BgLogger().Info("sql cpu collector started")
}
// Stop uses to stop the SQLCPUCollector.
// WARN: this function is not thread-safe.
func (sp *SQLCPUCollector) Stop() {
if !sp.started {
return
}
sp.started = false
if sp.cancel != nil {
sp.cancel()
}
sp.wg.Wait()
logutil.BgLogger().Info("sql cpu collector stopped")
}
var defCollectTickerInterval = time.Second
func (sp *SQLCPUCollector) collectSQLCPULoop() {
profileConsumer := make(cpuprofile.ProfileConsumer, 1)
ticker := time.NewTicker(defCollectTickerInterval)
defer func() {
util.Recover("top-sql", "startAnalyzeProfileWorker", nil, false)
sp.wg.Done()
sp.doUnregister(profileConsumer)
ticker.Stop()
}()
for {
if topsqlstate.TopSQLEnabled() {
sp.doRegister(profileConsumer)
} else {
sp.doUnregister(profileConsumer)
}
select {
case <-sp.ctx.Done():
return
case <-ticker.C:
case data := <-profileConsumer:
sp.handleProfileData(data)
}
}
}
func (sp *SQLCPUCollector) handleProfileData(data *cpuprofile.ProfileData) {
if data.Error != nil {
return
}
p, err := profile.ParseData(data.Data.Bytes())
if err != nil {
logutil.BgLogger().Error("parse profile error", zap.Error(err))
return
}
stats := sp.parseCPUProfileBySQLLabels(p)
sp.collector.Collect(stats)
}
func (sp *SQLCPUCollector) doRegister(profileConsumer cpuprofile.ProfileConsumer) {
if sp.registered {
return
}
sp.registered = true
cpuprofile.Register(profileConsumer)
}
func (sp *SQLCPUCollector) doUnregister(profileConsumer cpuprofile.ProfileConsumer) {
if !sp.registered {
return
}
sp.registered = false
cpuprofile.Unregister(profileConsumer)
}
// parseCPUProfileBySQLLabels uses to aggregate the cpu-profile sample data by sql_digest and plan_digest labels,
// output the TopSQLCPUTimeRecord slice. Want to know more information about profile labels, see https://rakyll.org/profiler-labels/
// The sql_digest label is been set by `SetSQLLabels` function after parse the SQL.
// The plan_digest label is been set by `SetSQLAndPlanLabels` function after build the SQL plan.
// Since `SQLCPUCollector` only care about the cpu time that consume by (sql_digest,plan_digest), the other sample data
// without those label will be ignore.
func (sp *SQLCPUCollector) parseCPUProfileBySQLLabels(p *profile.Profile) []SQLCPUTimeRecord {
sqlMap := make(map[string]*sqlStats)
idx := len(p.SampleType) - 1
for _, s := range p.Sample {
digests, ok := s.Label[labelSQLDigest]
if !ok || len(digests) == 0 {
continue
}
for _, digest := range digests {
stmt, ok := sqlMap[digest]
if !ok {
stmt = &sqlStats{
plans: make(map[string]int64),
total: 0,
}
sqlMap[digest] = stmt
}
stmt.total += s.Value[idx]
plans := s.Label[labelPlanDigest]
for _, plan := range plans {
stmt.plans[plan] += s.Value[idx]
}
}
}
return sp.createSQLStats(sqlMap)
}
func (*SQLCPUCollector) createSQLStats(sqlMap map[string]*sqlStats) []SQLCPUTimeRecord {
stats := make([]SQLCPUTimeRecord, 0, len(sqlMap))
for sqlDigest, stmt := range sqlMap {
stmt.tune()
for planDigest, val := range stmt.plans {
stats = append(stats, SQLCPUTimeRecord{
SQLDigest: []byte(sqlDigest),
PlanDigest: []byte(planDigest),
CPUTimeMs: uint32(time.Duration(val).Milliseconds()),
})
}
}
return stats
}
type sqlStats struct {
plans map[string]int64
total int64
}
// tune use to adjust sql stats. Consider following situation:
// The `sqlStats` maybe:
//
// plans: {
// "table_scan": 200ms, // The cpu time of the sql that plan with `table_scan` is 200ms.
// "index_scan": 300ms, // The cpu time of the sql that plan with `index_scan` is 300ms.
// },
// total: 600ms, // The total cpu time of the sql is 600ms.
//
// total_time - table_scan_time - index_scan_time = 100ms, and this 100ms means those sample data only contain the
// sql_digest label, doesn't contain the plan_digest label. This is cause by the `pprof profile` is base on sample,
// and the plan digest can only be set after optimizer generated execution plan. So the remain 100ms means the plan
// optimizer takes time to generated plan.
// After this tune function, the `sqlStats` become to:
//
// plans: {
// "" : 100ms, // 600 - 200 - 300 = 100ms, indicate the optimizer generated plan time cost.
// "table_scan": 200ms,
// "index_scan": 300ms,
// },
// total: 600ms,
func (s *sqlStats) tune() {
if len(s.plans) == 0 {
s.plans[""] = s.total
return
}
if len(s.plans) == 1 {
for k := range s.plans {
s.plans[k] = s.total
return
}
}
planTotal := int64(0)
for _, v := range s.plans {
planTotal += v
}
optimize := s.total - planTotal
if optimize <= 0 {
return
}
s.plans[""] += optimize
}
// CtxWithSQLDigest wrap the ctx with sql digest.
func CtxWithSQLDigest(ctx context.Context, sqlDigest []byte) context.Context {
return pprof.WithLabels(ctx, pprof.Labels(labelSQLDigest, string(hack.String(sqlDigest))))
}
// CtxWithSQLAndPlanDigest wrap the ctx with sql digest and plan digest.
func CtxWithSQLAndPlanDigest(ctx context.Context, sqlDigest, planDigest []byte) context.Context {
return pprof.WithLabels(ctx, pprof.Labels(labelSQLDigest, string(hack.String(sqlDigest)),
labelPlanDigest, string(hack.String(planDigest))))
}