1 |
747 |
jeremybenn |
// Copyright 2010 The Go Authors. All rights reserved.
|
2 |
|
|
// Use of this source code is governed by a BSD-style
|
3 |
|
|
// license that can be found in the LICENSE file.
|
4 |
|
|
|
5 |
|
|
// Package pprof writes runtime profiling data in the format expected
|
6 |
|
|
// by the pprof visualization tool.
|
7 |
|
|
// For more information about pprof, see
|
8 |
|
|
// http://code.google.com/p/google-perftools/.
|
9 |
|
|
package pprof
|
10 |
|
|
|
11 |
|
|
import (
|
12 |
|
|
"bufio"
|
13 |
|
|
"fmt"
|
14 |
|
|
"io"
|
15 |
|
|
"runtime"
|
16 |
|
|
"sync"
|
17 |
|
|
)
|
18 |
|
|
|
19 |
|
|
// BUG(rsc): CPU profiling is broken on OS X, due to an Apple kernel bug.
|
20 |
|
|
// For details, see http://code.google.com/p/go/source/detail?r=35b716c94225.
|
21 |
|
|
|
22 |
|
|
// WriteHeapProfile writes a pprof-formatted heap profile to w.
|
23 |
|
|
// If a write to w returns an error, WriteHeapProfile returns that error.
|
24 |
|
|
// Otherwise, WriteHeapProfile returns nil.
|
25 |
|
|
func WriteHeapProfile(w io.Writer) error {
|
26 |
|
|
// Find out how many records there are (MemProfile(nil, false)),
|
27 |
|
|
// allocate that many records, and get the data.
|
28 |
|
|
// There's a race—more records might be added between
|
29 |
|
|
// the two calls—so allocate a few extra records for safety
|
30 |
|
|
// and also try again if we're very unlucky.
|
31 |
|
|
// The loop should only execute one iteration in the common case.
|
32 |
|
|
var p []runtime.MemProfileRecord
|
33 |
|
|
n, ok := runtime.MemProfile(nil, false)
|
34 |
|
|
for {
|
35 |
|
|
// Allocate room for a slightly bigger profile,
|
36 |
|
|
// in case a few more entries have been added
|
37 |
|
|
// since the call to MemProfile.
|
38 |
|
|
p = make([]runtime.MemProfileRecord, n+50)
|
39 |
|
|
n, ok = runtime.MemProfile(p, false)
|
40 |
|
|
if ok {
|
41 |
|
|
p = p[0:n]
|
42 |
|
|
break
|
43 |
|
|
}
|
44 |
|
|
// Profile grew; try again.
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
var total runtime.MemProfileRecord
|
48 |
|
|
for i := range p {
|
49 |
|
|
r := &p[i]
|
50 |
|
|
total.AllocBytes += r.AllocBytes
|
51 |
|
|
total.AllocObjects += r.AllocObjects
|
52 |
|
|
total.FreeBytes += r.FreeBytes
|
53 |
|
|
total.FreeObjects += r.FreeObjects
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
// Technically the rate is MemProfileRate not 2*MemProfileRate,
|
57 |
|
|
// but early versions of the C++ heap profiler reported 2*MemProfileRate,
|
58 |
|
|
// so that's what pprof has come to expect.
|
59 |
|
|
b := bufio.NewWriter(w)
|
60 |
|
|
fmt.Fprintf(b, "heap profile: %d: %d [%d: %d] @ heap/%d\n",
|
61 |
|
|
total.InUseObjects(), total.InUseBytes(),
|
62 |
|
|
total.AllocObjects, total.AllocBytes,
|
63 |
|
|
2*runtime.MemProfileRate)
|
64 |
|
|
|
65 |
|
|
for i := range p {
|
66 |
|
|
r := &p[i]
|
67 |
|
|
fmt.Fprintf(b, "%d: %d [%d: %d] @",
|
68 |
|
|
r.InUseObjects(), r.InUseBytes(),
|
69 |
|
|
r.AllocObjects, r.AllocBytes)
|
70 |
|
|
for _, pc := range r.Stack() {
|
71 |
|
|
fmt.Fprintf(b, " %#x", pc)
|
72 |
|
|
}
|
73 |
|
|
fmt.Fprintf(b, "\n")
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
// Print memstats information too.
|
77 |
|
|
// Pprof will ignore, but useful for people.
|
78 |
|
|
s := new(runtime.MemStats)
|
79 |
|
|
runtime.ReadMemStats(s)
|
80 |
|
|
fmt.Fprintf(b, "\n# runtime.MemStats\n")
|
81 |
|
|
fmt.Fprintf(b, "# Alloc = %d\n", s.Alloc)
|
82 |
|
|
fmt.Fprintf(b, "# TotalAlloc = %d\n", s.TotalAlloc)
|
83 |
|
|
fmt.Fprintf(b, "# Sys = %d\n", s.Sys)
|
84 |
|
|
fmt.Fprintf(b, "# Lookups = %d\n", s.Lookups)
|
85 |
|
|
fmt.Fprintf(b, "# Mallocs = %d\n", s.Mallocs)
|
86 |
|
|
|
87 |
|
|
fmt.Fprintf(b, "# HeapAlloc = %d\n", s.HeapAlloc)
|
88 |
|
|
fmt.Fprintf(b, "# HeapSys = %d\n", s.HeapSys)
|
89 |
|
|
fmt.Fprintf(b, "# HeapIdle = %d\n", s.HeapIdle)
|
90 |
|
|
fmt.Fprintf(b, "# HeapInuse = %d\n", s.HeapInuse)
|
91 |
|
|
|
92 |
|
|
fmt.Fprintf(b, "# Stack = %d / %d\n", s.StackInuse, s.StackSys)
|
93 |
|
|
fmt.Fprintf(b, "# MSpan = %d / %d\n", s.MSpanInuse, s.MSpanSys)
|
94 |
|
|
fmt.Fprintf(b, "# MCache = %d / %d\n", s.MCacheInuse, s.MCacheSys)
|
95 |
|
|
fmt.Fprintf(b, "# BuckHashSys = %d\n", s.BuckHashSys)
|
96 |
|
|
|
97 |
|
|
fmt.Fprintf(b, "# NextGC = %d\n", s.NextGC)
|
98 |
|
|
fmt.Fprintf(b, "# PauseNs = %d\n", s.PauseNs)
|
99 |
|
|
fmt.Fprintf(b, "# NumGC = %d\n", s.NumGC)
|
100 |
|
|
fmt.Fprintf(b, "# EnableGC = %v\n", s.EnableGC)
|
101 |
|
|
fmt.Fprintf(b, "# DebugGC = %v\n", s.DebugGC)
|
102 |
|
|
|
103 |
|
|
fmt.Fprintf(b, "# BySize = Size * (Active = Mallocs - Frees)\n")
|
104 |
|
|
fmt.Fprintf(b, "# (Excluding large blocks.)\n")
|
105 |
|
|
for _, t := range s.BySize {
|
106 |
|
|
if t.Mallocs > 0 {
|
107 |
|
|
fmt.Fprintf(b, "# %d * (%d = %d - %d)\n", t.Size, t.Mallocs-t.Frees, t.Mallocs, t.Frees)
|
108 |
|
|
}
|
109 |
|
|
}
|
110 |
|
|
return b.Flush()
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
var cpu struct {
|
114 |
|
|
sync.Mutex
|
115 |
|
|
profiling bool
|
116 |
|
|
done chan bool
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
// StartCPUProfile enables CPU profiling for the current process.
|
120 |
|
|
// While profiling, the profile will be buffered and written to w.
|
121 |
|
|
// StartCPUProfile returns an error if profiling is already enabled.
|
122 |
|
|
func StartCPUProfile(w io.Writer) error {
|
123 |
|
|
// The runtime routines allow a variable profiling rate,
|
124 |
|
|
// but in practice operating systems cannot trigger signals
|
125 |
|
|
// at more than about 500 Hz, and our processing of the
|
126 |
|
|
// signal is not cheap (mostly getting the stack trace).
|
127 |
|
|
// 100 Hz is a reasonable choice: it is frequent enough to
|
128 |
|
|
// produce useful data, rare enough not to bog down the
|
129 |
|
|
// system, and a nice round number to make it easy to
|
130 |
|
|
// convert sample counts to seconds. Instead of requiring
|
131 |
|
|
// each client to specify the frequency, we hard code it.
|
132 |
|
|
const hz = 100
|
133 |
|
|
|
134 |
|
|
// Avoid queueing behind StopCPUProfile.
|
135 |
|
|
// Could use TryLock instead if we had it.
|
136 |
|
|
if cpu.profiling {
|
137 |
|
|
return fmt.Errorf("cpu profiling already in use")
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
cpu.Lock()
|
141 |
|
|
defer cpu.Unlock()
|
142 |
|
|
if cpu.done == nil {
|
143 |
|
|
cpu.done = make(chan bool)
|
144 |
|
|
}
|
145 |
|
|
// Double-check.
|
146 |
|
|
if cpu.profiling {
|
147 |
|
|
return fmt.Errorf("cpu profiling already in use")
|
148 |
|
|
}
|
149 |
|
|
cpu.profiling = true
|
150 |
|
|
runtime.SetCPUProfileRate(hz)
|
151 |
|
|
go profileWriter(w)
|
152 |
|
|
return nil
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
func profileWriter(w io.Writer) {
|
156 |
|
|
for {
|
157 |
|
|
data := runtime.CPUProfile()
|
158 |
|
|
if data == nil {
|
159 |
|
|
break
|
160 |
|
|
}
|
161 |
|
|
w.Write(data)
|
162 |
|
|
}
|
163 |
|
|
cpu.done <- true
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
// StopCPUProfile stops the current CPU profile, if any.
|
167 |
|
|
// StopCPUProfile only returns after all the writes for the
|
168 |
|
|
// profile have completed.
|
169 |
|
|
func StopCPUProfile() {
|
170 |
|
|
cpu.Lock()
|
171 |
|
|
defer cpu.Unlock()
|
172 |
|
|
|
173 |
|
|
if !cpu.profiling {
|
174 |
|
|
return
|
175 |
|
|
}
|
176 |
|
|
cpu.profiling = false
|
177 |
|
|
runtime.SetCPUProfileRate(0)
|
178 |
|
|
<-cpu.done
|
179 |
|
|
}
|