-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
113 lines (104 loc) · 2.77 KB
/
profile.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
// Copyright 2022 Google LLC
//
// 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 profiling
import (
"bytes"
"errors"
"fmt"
"sync"
"github.com/google/pprof/profile"
)
// ProfileGroups returns the approximate sum of the profiles of all members of all groups.
// If a group has more than one member, only a subset of the group may be profiled and the
// result scaled up assuming non-profiled members of the group have similar profiles.
// Each group member is represented by a function that profiles that group member.
func ProfileGroups(groups [][]func() ([]byte, error)) ([]byte, error) {
type groupState struct {
profile *profile.Profile
errs []error
}
profiles := make([]groupState, len(groups))
// Profile each group in parallel.
var wait sync.WaitGroup
for i, group := range groups {
if len(group) == 0 {
// Skip empty groups.
continue
}
i := i
group := group
wait.Add(1)
go func() {
defer wait.Done()
// Try each member until we get a profile.
var lastErr error
var p *profile.Profile
for _, fn := range group {
data, err := fn()
if err != nil {
lastErr = err
continue
}
if len(data) == 0 {
p = nil
break
}
// Parse the profile data and scale it based on the number of replicas
// for the given process
prof, err := profile.ParseData(data)
if err != nil {
lastErr = err
continue
}
if len(group) > 1 {
prof.Scale(float64(len(group)))
}
p = prof
break
}
if lastErr != nil {
profiles[i].errs = append(profiles[i].errs, lastErr)
}
profiles[i].profile = p
}()
}
wait.Wait()
// Fill the final merged profile.
var profs []*profile.Profile
var errs []error
for _, p := range profiles {
errs = append(errs, p.errs...)
if p.profile != nil {
profs = append(profs, p.profile)
}
}
if len(profs) == 0 && len(errs) > 0 {
// No profile data but have errors: that's an error.
return nil, fmt.Errorf("error collecting profile: %v", errs)
}
var buf bytes.Buffer
if len(profs) > 0 {
merged := profs[0]
if len(profs) > 1 {
var err error
if merged, err = profile.Merge(profs); err != nil {
return nil, err
}
}
if err := merged.Write(&buf); err != nil {
return nil, err
}
}
return buf.Bytes(), errors.Join(errs...)
}