forked from apache/beam
/
perf.go
170 lines (152 loc) · 5.66 KB
/
perf.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
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 perf is to add performance measuring hooks to a runner, such as cpu, or trace profiles.
package perf
import (
"bytes"
"context"
"fmt"
"io"
"runtime/pprof"
"runtime/trace"
"github.com/apache/beam/sdks/go/pkg/beam/core/util/hooks"
fnpb "github.com/apache/beam/sdks/go/pkg/beam/model/fnexecution_v1"
)
// CaptureHook is used by the harness to have the runner
// persist a trace record with the supplied name and comment.
// The type of trace can be determined by the prefix of the string.
//
// * prof: A profile compatible with traces produced by runtime/pprof
// * trace: A trace compatible with traces produced by runtime/trace
type CaptureHook func(context.Context, string, io.Reader) error
// CaptureHookFactory creates a CaptureHook from the supplied options.
type CaptureHookFactory func([]string) CaptureHook
var profCaptureHookRegistry = make(map[string]CaptureHookFactory)
var enabledProfCaptureHooks []string
func init() {
hf := func(opts []string) hooks.Hook {
enabledProfCaptureHooks = opts
enabled := len(enabledProfCaptureHooks) > 0
var cpuProfBuf bytes.Buffer
return hooks.Hook{
Req: func(ctx context.Context, req *fnpb.InstructionRequest) (context.Context, error) {
if !enabled || req.GetProcessBundle() == nil {
return ctx, nil
}
cpuProfBuf.Reset()
return ctx, pprof.StartCPUProfile(&cpuProfBuf)
},
Resp: func(ctx context.Context, req *fnpb.InstructionRequest, _ *fnpb.InstructionResponse) error {
if !enabled || req.GetProcessBundle() == nil {
return nil
}
pprof.StopCPUProfile()
for _, h := range enabledProfCaptureHooks {
name, opts := hooks.Decode(h)
if err := profCaptureHookRegistry[name](opts)(ctx, fmt.Sprintf("prof%s", req.InstructionId), &cpuProfBuf); err != nil {
return err
}
}
return nil
},
}
}
hooks.RegisterHook("prof", hf)
hf = func(opts []string) hooks.Hook {
var traceProfBuf bytes.Buffer
enabledTraceCaptureHooks = opts
enabled := len(enabledTraceCaptureHooks) > 0
return hooks.Hook{
Req: func(ctx context.Context, _ *fnpb.InstructionRequest) (context.Context, error) {
if !enabled {
return ctx, nil
}
traceProfBuf.Reset()
return ctx, trace.Start(&traceProfBuf)
},
Resp: func(ctx context.Context, req *fnpb.InstructionRequest, _ *fnpb.InstructionResponse) error {
if !enabled {
return nil
}
trace.Stop()
for _, h := range enabledTraceCaptureHooks {
name, opts := hooks.Decode(h)
if err := traceCaptureHookRegistry[name](opts)(ctx, fmt.Sprintf("trace_prof%s", req.InstructionId), &traceProfBuf); err != nil {
return err
}
}
return nil
},
}
}
hooks.RegisterHook("trace", hf)
}
// RegisterProfCaptureHook registers a CaptureHookFactory for the
// supplied identifier. It panics if the same identifier is
// registered twice.
func RegisterProfCaptureHook(name string, c CaptureHookFactory) {
if _, exists := profCaptureHookRegistry[name]; exists {
panic(fmt.Sprintf("RegisterProfCaptureHook: %s registered twice", name))
}
profCaptureHookRegistry[name] = c
}
// EnableProfCaptureHook actives a registered profile capture hook for a given pipeline.
func EnableProfCaptureHook(name string, opts ...string) {
_, exists := profCaptureHookRegistry[name]
if !exists {
panic(fmt.Sprintf("EnableProfCaptureHook: %s not registered", name))
}
enc := hooks.Encode(name, opts)
for i, h := range enabledProfCaptureHooks {
n, _ := hooks.Decode(h)
if h == n {
// Rewrite the registration with the current arguments
enabledProfCaptureHooks[i] = enc
hooks.EnableHook("prof", enabledProfCaptureHooks...)
return
}
}
enabledProfCaptureHooks = append(enabledProfCaptureHooks, enc)
hooks.EnableHook("prof", enabledProfCaptureHooks...)
}
var traceCaptureHookRegistry = make(map[string]CaptureHookFactory)
var enabledTraceCaptureHooks []string
// RegisterTraceCaptureHook registers a CaptureHookFactory for the
// supplied identifier. It panics if the same identifier is
// registered twice.
func RegisterTraceCaptureHook(name string, c CaptureHookFactory) {
if _, exists := traceCaptureHookRegistry[name]; exists {
panic(fmt.Sprintf("RegisterTraceCaptureHook: %s registered twice", name))
}
traceCaptureHookRegistry[name] = c
}
// EnableTraceCaptureHook actives a registered profile capture hook for a given pipeline.
func EnableTraceCaptureHook(name string, opts ...string) {
if _, exists := traceCaptureHookRegistry[name]; !exists {
panic(fmt.Sprintf("EnableTraceCaptureHook: %s not registered", name))
}
enc := hooks.Encode(name, opts)
for i, h := range enabledTraceCaptureHooks {
n, _ := hooks.Decode(h)
if h == n {
// Rewrite the registration with the current arguments
enabledTraceCaptureHooks[i] = enc
hooks.EnableHook("trace", enabledTraceCaptureHooks...)
return
}
}
enabledTraceCaptureHooks = append(enabledTraceCaptureHooks, enc)
hooks.EnableHook("trace", enabledTraceCaptureHooks...)
}