-
Notifications
You must be signed in to change notification settings - Fork 153
/
profiler.go
433 lines (399 loc) · 9.68 KB
/
profiler.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package execute
import (
"context"
"fmt"
"strings"
"time"
"github.com/influxdata/flux"
"github.com/influxdata/flux/memory"
"github.com/influxdata/flux/values"
"github.com/opentracing/opentracing-go"
)
type Profiler interface {
Name() string
GetResult(q flux.Query, alloc *memory.Allocator) (flux.Table, error)
GetSortedResult(q flux.Query, alloc *memory.Allocator, desc bool, sortKeys ...string) (flux.Table, error)
}
type CreateProfilerFunc func() Profiler
var AllProfilers = make(map[string]CreateProfilerFunc)
func RegisterProfilerFactories(cpfs ...CreateProfilerFunc) {
for _, cpf := range cpfs {
p := cpf()
AllProfilers[p.Name()] = cpf
}
}
func init() {
RegisterProfilerFactories(
createQueryProfiler,
createOperatorProfiler,
)
}
type OperatorProfilingResult struct {
Type string
// Those labels are actually their operation name. See flux/internal/spec.buildSpec.
// Some examples are:
// merged_fromRemote_range1_filter2_filter3_filter4, window5, window8, generated_yield, etc.
Label string
Start time.Time
Stop time.Time
}
type OperatorProfilingSpan struct {
opentracing.Span
profiler *OperatorProfiler
Result OperatorProfilingResult
}
func (t *OperatorProfilingSpan) finish(finishTime time.Time) time.Time {
t.Result.Stop = finishTime
if t.profiler != nil && t.profiler.chIn != nil {
t.profiler.chIn <- t.Result
}
return t.Result.Stop
}
func (t *OperatorProfilingSpan) Finish() {
finishTime := t.finish(time.Now())
if t.Span != nil {
t.Span.FinishWithOptions(opentracing.FinishOptions{
FinishTime: finishTime,
})
}
}
func (t *OperatorProfilingSpan) FinishWithOptions(opts opentracing.FinishOptions) {
finishTime := t.finish(opts.FinishTime)
opts.FinishTime = finishTime
if t.Span != nil {
t.Span.FinishWithOptions(opts)
}
}
const OperatorProfilerContextKey = "operator-profiler"
type operatorProfilingResultAggregate struct {
operationType string
label string
resultCount int64
resultMin int64
resultMax int64
resultSum int64
resultMean float64
}
type operatorProfilerLabelGroup = map[string]*operatorProfilingResultAggregate
type operatorProfilerTypeGroup = map[string]operatorProfilerLabelGroup
type OperatorProfiler struct {
// Receive the profiling results from the spans.
chIn chan OperatorProfilingResult
chOut chan operatorProfilingResultAggregate
}
func createOperatorProfiler() Profiler {
p := &OperatorProfiler{
chIn: make(chan OperatorProfilingResult),
chOut: make(chan operatorProfilingResultAggregate),
}
go func(p *OperatorProfiler) {
aggs := make(operatorProfilerTypeGroup)
for result := range p.chIn {
_, ok := aggs[result.Type]
if !ok {
aggs[result.Type] = make(operatorProfilerLabelGroup)
}
_, ok = aggs[result.Type][result.Label]
if !ok {
aggs[result.Type][result.Label] = &operatorProfilingResultAggregate{}
}
a := aggs[result.Type][result.Label]
// Aggregate the results
a.resultCount++
duration := result.Stop.Sub(result.Start).Nanoseconds()
if duration > a.resultMax {
a.resultMax = duration
}
if duration < a.resultMin || a.resultMin == 0 {
a.resultMin = duration
}
a.resultSum += duration
}
// Write the aggregated results to chOut, where they'll be
// converted into rows and appended to the final table
for typ, labels := range aggs {
for label, agg := range labels {
agg.resultMean = float64(agg.resultSum) / float64(agg.resultCount)
agg.operationType = typ
agg.label = label
p.chOut <- *agg
}
}
close(p.chOut)
}(p)
return p
}
func (o *OperatorProfiler) Name() string {
return "operator"
}
func (o *OperatorProfiler) closeIncomingChannel() {
if o.chIn != nil {
close(o.chIn)
o.chIn = nil
}
}
func (o *OperatorProfiler) GetResult(q flux.Query, alloc *memory.Allocator) (flux.Table, error) {
o.closeIncomingChannel()
b, err := o.getTableBuilder(alloc)
if err != nil {
return nil, err
}
tbl, err := b.Table()
if err != nil {
return nil, err
}
return tbl, nil
}
// GetSortedResult is identical to GetResult, except it calls Sort()
// on the ColListTableBuilder to make testing easier.
// sortKeys and desc are passed directly into the Sort() call
func (o *OperatorProfiler) GetSortedResult(q flux.Query, alloc *memory.Allocator, desc bool, sortKeys ...string) (flux.Table, error) {
o.closeIncomingChannel()
b, err := o.getTableBuilder(alloc)
if err != nil {
return nil, err
}
b.Sort(sortKeys, desc)
tbl, err := b.Table()
if err != nil {
return nil, err
}
return tbl, nil
}
func (o *OperatorProfiler) getTableBuilder(alloc *memory.Allocator) (*ColListTableBuilder, error) {
groupKey := NewGroupKey(
[]flux.ColMeta{
{
Label: "_measurement",
Type: flux.TString,
},
},
[]values.Value{
values.NewString("profiler/operator"),
},
)
b := NewColListTableBuilder(groupKey, alloc)
colMeta := []flux.ColMeta{
{
Label: "_measurement",
Type: flux.TString,
},
{
Label: "Type",
Type: flux.TString,
},
{
Label: "Label",
Type: flux.TString,
},
{
Label: "Count",
Type: flux.TInt,
},
{
Label: "MinDuration",
Type: flux.TInt,
},
{
Label: "MaxDuration",
Type: flux.TInt,
},
{
Label: "DurationSum",
Type: flux.TInt,
},
{
Label: "MeanDuration",
Type: flux.TFloat,
},
}
for _, col := range colMeta {
if _, err := b.AddCol(col); err != nil {
return nil, err
}
}
for agg := range o.chOut {
b.AppendString(0, "profiler/operator")
b.AppendString(1, agg.operationType)
b.AppendString(2, agg.label)
b.AppendInt(3, agg.resultCount)
b.AppendInt(4, agg.resultMin)
b.AppendInt(5, agg.resultMax)
b.AppendInt(6, agg.resultSum)
b.AppendFloat(7, agg.resultMean)
}
return b, nil
}
// Create a tracing span.
// Depending on whether the Jaeger tracing and/or the operator profiling are enabled,
// the Span produced by this function can be very different.
// It could be a no-op span, a Jaeger span, a no-op span wrapped by a profiling span, or
// a Jaeger span wrapped by a profiling span.
func StartSpanFromContext(ctx context.Context, operationName string, label string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span) {
var span opentracing.Span
var start time.Time
for _, opt := range opts {
if st, ok := opt.(opentracing.StartTime); ok {
start = time.Time(st)
break
}
}
if start.IsZero() {
start = time.Now()
}
if HaveExecutionDependencies(ctx) {
deps := GetExecutionDependencies(ctx)
if deps.ExecutionOptions.OperatorProfiler != nil {
tfp := deps.ExecutionOptions.OperatorProfiler
span = &OperatorProfilingSpan{
Span: span,
profiler: tfp,
Result: OperatorProfilingResult{
Type: operationName,
Label: label,
Start: start,
},
}
}
}
return ctx, span
}
type QueryProfiler struct{}
func createQueryProfiler() Profiler {
return &QueryProfiler{}
}
func (s *QueryProfiler) Name() string {
return "query"
}
func (s *QueryProfiler) GetResult(q flux.Query, alloc *memory.Allocator) (flux.Table, error) {
b, err := s.getTableBuilder(q, alloc)
if err != nil {
return nil, err
}
tbl, err := b.Table()
if err != nil {
return nil, err
}
return tbl, nil
}
// GetSortedResult is identical to GetResult, except it calls Sort()
// on the ColListTableBuilder to make testing easier.
// sortKeys and desc are passed directly into the Sort() call
func (s *QueryProfiler) GetSortedResult(q flux.Query, alloc *memory.Allocator, desc bool, sortKeys ...string) (flux.Table, error) {
b, err := s.getTableBuilder(q, alloc)
if err != nil {
return nil, err
}
b.Sort(sortKeys, desc)
tbl, err := b.Table()
if err != nil {
return nil, err
}
return tbl, nil
}
func (s *QueryProfiler) getTableBuilder(q flux.Query, alloc *memory.Allocator) (*ColListTableBuilder, error) {
groupKey := NewGroupKey(
[]flux.ColMeta{
{
Label: "_measurement",
Type: flux.TString,
},
},
[]values.Value{
values.NewString("profiler/query"),
},
)
b := NewColListTableBuilder(groupKey, alloc)
stats := q.Statistics()
colMeta := []flux.ColMeta{
{
Label: "_measurement",
Type: flux.TString,
},
{
Label: "TotalDuration",
Type: flux.TInt,
},
{
Label: "CompileDuration",
Type: flux.TInt,
},
{
Label: "QueueDuration",
Type: flux.TInt,
},
{
Label: "PlanDuration",
Type: flux.TInt,
},
{
Label: "RequeueDuration",
Type: flux.TInt,
},
{
Label: "ExecuteDuration",
Type: flux.TInt,
},
{
Label: "Concurrency",
Type: flux.TInt,
},
{
Label: "MaxAllocated",
Type: flux.TInt,
},
{
Label: "TotalAllocated",
Type: flux.TInt,
},
{
Label: "RuntimeErrors",
Type: flux.TString,
},
}
colData := []interface{}{
"profiler/query",
stats.TotalDuration.Nanoseconds(),
stats.CompileDuration.Nanoseconds(),
stats.QueueDuration.Nanoseconds(),
stats.PlanDuration.Nanoseconds(),
stats.RequeueDuration.Nanoseconds(),
stats.ExecuteDuration.Nanoseconds(),
int64(stats.Concurrency),
stats.MaxAllocated,
stats.TotalAllocated,
strings.Join(stats.RuntimeErrors, "\n"),
}
for key, values := range stats.Metadata {
var ty flux.ColType
if intValue, ok := values[0].(int); ok {
ty = flux.TInt
colData = append(colData, int64(intValue))
} else {
ty = flux.TString
var data string
for _, value := range values {
valueStr := fmt.Sprintf("%v", value)
data += valueStr + "\n"
}
colData = append(colData, data)
}
colMeta = append(colMeta, flux.ColMeta{
Label: key,
Type: ty,
})
}
for _, col := range colMeta {
if _, err := b.AddCol(col); err != nil {
return nil, err
}
}
for i := 0; i < len(colData); i++ {
if intValue, ok := colData[i].(int64); ok {
b.AppendInt(i, intValue)
} else {
b.AppendString(i, colData[i].(string))
}
}
return b, nil
}