-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpprof.go
150 lines (140 loc) · 4.03 KB
/
pprof.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
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Serving of pprof-like profiles.
package traceviewer
import (
"bufio"
"fmt"
"internal/profile"
"internal/trace"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
)
type ProfileFunc func(r *http.Request) ([]ProfileRecord, error)
// SVGProfileHandlerFunc serves pprof-like profile generated by prof as svg.
func SVGProfileHandlerFunc(f ProfileFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.FormValue("raw") != "" {
w.Header().Set("Content-Type", "application/octet-stream")
failf := func(s string, args ...any) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Go-Pprof", "1")
http.Error(w, fmt.Sprintf(s, args...), http.StatusInternalServerError)
}
records, err := f(r)
if err != nil {
failf("failed to get records: %v", err)
return
}
if err := BuildProfile(records).Write(w); err != nil {
failf("failed to write profile: %v", err)
return
}
return
}
blockf, err := os.CreateTemp("", "block")
if err != nil {
http.Error(w, fmt.Sprintf("failed to create temp file: %v", err), http.StatusInternalServerError)
return
}
defer func() {
blockf.Close()
os.Remove(blockf.Name())
}()
records, err := f(r)
if err != nil {
http.Error(w, fmt.Sprintf("failed to generate profile: %v", err), http.StatusInternalServerError)
}
blockb := bufio.NewWriter(blockf)
if err := BuildProfile(records).Write(blockb); err != nil {
http.Error(w, fmt.Sprintf("failed to write profile: %v", err), http.StatusInternalServerError)
return
}
if err := blockb.Flush(); err != nil {
http.Error(w, fmt.Sprintf("failed to flush temp file: %v", err), http.StatusInternalServerError)
return
}
if err := blockf.Close(); err != nil {
http.Error(w, fmt.Sprintf("failed to close temp file: %v", err), http.StatusInternalServerError)
return
}
svgFilename := blockf.Name() + ".svg"
if output, err := exec.Command(goCmd(), "tool", "pprof", "-svg", "-output", svgFilename, blockf.Name()).CombinedOutput(); err != nil {
http.Error(w, fmt.Sprintf("failed to execute go tool pprof: %v\n%s", err, output), http.StatusInternalServerError)
return
}
defer os.Remove(svgFilename)
w.Header().Set("Content-Type", "image/svg+xml")
http.ServeFile(w, r, svgFilename)
}
}
type ProfileRecord struct {
Stack []trace.StackFrame
Count uint64
Time time.Duration
}
func BuildProfile(prof []ProfileRecord) *profile.Profile {
p := &profile.Profile{
PeriodType: &profile.ValueType{Type: "trace", Unit: "count"},
Period: 1,
SampleType: []*profile.ValueType{
{Type: "contentions", Unit: "count"},
{Type: "delay", Unit: "nanoseconds"},
},
}
locs := make(map[uint64]*profile.Location)
funcs := make(map[string]*profile.Function)
for _, rec := range prof {
var sloc []*profile.Location
for _, frame := range rec.Stack {
loc := locs[frame.PC]
if loc == nil {
fn := funcs[frame.File+frame.Func]
if fn == nil {
fn = &profile.Function{
ID: uint64(len(p.Function) + 1),
Name: frame.Func,
SystemName: frame.Func,
Filename: frame.File,
}
p.Function = append(p.Function, fn)
funcs[frame.File+frame.Func] = fn
}
loc = &profile.Location{
ID: uint64(len(p.Location) + 1),
Address: frame.PC,
Line: []profile.Line{
{
Function: fn,
Line: int64(frame.Line),
},
},
}
p.Location = append(p.Location, loc)
locs[frame.PC] = loc
}
sloc = append(sloc, loc)
}
p.Sample = append(p.Sample, &profile.Sample{
Value: []int64{int64(rec.Count), int64(rec.Time)},
Location: sloc,
})
}
return p
}
func goCmd() string {
var exeSuffix string
if runtime.GOOS == "windows" {
exeSuffix = ".exe"
}
path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
if _, err := os.Stat(path); err == nil {
return path
}
return "go"
}