| 1 |
// 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 |
// |
| 8 |
// Profiling a Go program |
| 9 |
// |
| 10 |
// The first step to profiling a Go program is to enable profiling. |
| 11 |
// Support for profiling benchmarks built with the standard testing |
| 12 |
// package is built into go test. For example, the following command |
| 13 |
// runs benchmarks in the current directory and writes the CPU and |
| 14 |
// memory profiles to cpu.prof and mem.prof: |
| 15 |
// |
| 16 |
// go test -cpuprofile cpu.prof -memprofile mem.prof -bench . |
| 17 |
// |
| 18 |
// To add equivalent profiling support to a standalone program, add |
| 19 |
// code like the following to your main function: |
| 20 |
// |
| 21 |
// var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") |
| 22 |
// var memprofile = flag.String("memprofile", "", "write memory profile to `file`") |
| 23 |
// |
| 24 |
// func main() { |
| 25 |
// flag.Parse() |
| 26 |
// if *cpuprofile != "" { |
| 27 |
// f, err := os.Create(*cpuprofile) |
| 28 |
// if err != nil { |
| 29 |
// log.Fatal("could not create CPU profile: ", err) |
| 30 |
// } |
| 31 |
// defer f.Close() // error handling omitted for example |
| 32 |
// if err := pprof.StartCPUProfile(f); err != nil { |
| 33 |
// log.Fatal("could not start CPU profile: ", err) |
| 34 |
// } |
| 35 |
// defer pprof.StopCPUProfile() |
| 36 |
// } |
| 37 |
// |
| 38 |
// // ... rest of the program ... |
| 39 |
// |
| 40 |
// if *memprofile != "" { |
| 41 |
// f, err := os.Create(*memprofile) |
| 42 |
// if err != nil { |
| 43 |
// log.Fatal("could not create memory profile: ", err) |
| 44 |
// } |
| 45 |
// defer f.Close() // error handling omitted for example |
| 46 |
// runtime.GC() // get up-to-date statistics |
| 47 |
// if err := pprof.WriteHeapProfile(f); err != nil { |
| 48 |
// log.Fatal("could not write memory profile: ", err) |
| 49 |
// } |
| 50 |
// } |
| 51 |
// } |
| 52 |
// |
| 53 |
// There is also a standard HTTP interface to profiling data. Adding |
| 54 |
// the following line will install handlers under the /debug/pprof/ |
| 55 |
// URL to download live profiles: |
| 56 |
// |
| 57 |
// import _ "net/http/pprof" |
| 58 |
// |
| 59 |
// See the net/http/pprof package for more details. |
| 60 |
// |
| 61 |
// Profiles can then be visualized with the pprof tool: |
| 62 |
// |
| 63 |
// go tool pprof cpu.prof |
| 64 |
// |
| 65 |
// There are many commands available from the pprof command line. |
| 66 |
// Commonly used commands include "top", which prints a summary of the |
| 67 |
// top program hot-spots, and "web", which opens an interactive graph |
| 68 |
// of hot-spots and their call graphs. Use "help" for information on |
| 69 |
// all pprof commands. |
| 70 |
// |
| 71 |
// For more information about pprof, see |
| 72 |
// https://github.com/google/pprof/blob/master/doc/README.md. |
| 73 |
package pprof |
| 74 |
|
| 75 |
import ( |
| 76 |
"bufio" |
| 77 |
"bytes" |
| 78 |
"fmt" |
| 79 |
"internal/abi" |
| 80 |
"io" |
| 81 |
"runtime" |
| 82 |
"sort" |
| 83 |
"strings" |
| 84 |
"sync" |
| 85 |
"text/tabwriter" |
| 86 |
"time" |
| 87 |
"unsafe" |
| 88 |
) |
| 89 |
|
| 90 |
// BUG(rsc): Profiles are only as good as the kernel support used to generate them. |
| 91 |
// See https://golang.org/issue/13841 for details about known problems. |
| 92 |
|
| 93 |
// A Profile is a collection of stack traces showing the call sequences |
| 94 |
// that led to instances of a particular event, such as allocation. |
| 95 |
// Packages can create and maintain their own profiles; the most common |
| 96 |
// use is for tracking resources that must be explicitly closed, such as files |
| 97 |
// or network connections. |
| 98 |
// |
| 99 |
// A Profile's methods can be called from multiple goroutines simultaneously. |
| 100 |
// |
| 101 |
// Each Profile has a unique name. A few profiles are predefined: |
| 102 |
// |
| 103 |
// goroutine - stack traces of all current goroutines |
| 104 |
// heap - a sampling of memory allocations of live objects |
| 105 |
// allocs - a sampling of all past memory allocations |
| 106 |
// threadcreate - stack traces that led to the creation of new OS threads |
| 107 |
// block - stack traces that led to blocking on synchronization primitives |
| 108 |
// mutex - stack traces of holders of contended mutexes |
| 109 |
// |
| 110 |
// These predefined profiles maintain themselves and panic on an explicit |
| 111 |
// Add or Remove method call. |
| 112 |
// |
| 113 |
// The heap profile reports statistics as of the most recently completed |
| 114 |
// garbage collection; it elides more recent allocation to avoid skewing |
| 115 |
// the profile away from live data and toward garbage. |
| 116 |
// If there has been no garbage collection at all, the heap profile reports |
| 117 |
// all known allocations. This exception helps mainly in programs running |
| 118 |
// without garbage collection enabled, usually for debugging purposes. |
| 119 |
// |
| 120 |
// The heap profile tracks both the allocation sites for all live objects in |
| 121 |
// the application memory and for all objects allocated since the program start. |
| 122 |
// Pprof's -inuse_space, -inuse_objects, -alloc_space, and -alloc_objects |
| 123 |
// flags select which to display, defaulting to -inuse_space (live objects, |
| 124 |
// scaled by size). |
| 125 |
// |
| 126 |
// The allocs profile is the same as the heap profile but changes the default |
| 127 |
// pprof display to -alloc_space, the total number of bytes allocated since |
| 128 |
// the program began (including garbage-collected bytes). |
| 129 |
// |
| 130 |
// The CPU profile is not available as a Profile. It has a special API, |
| 131 |
// the StartCPUProfile and StopCPUProfile functions, because it streams |
| 132 |
// output to a writer during profiling. |
| 133 |
// |
| 134 |
type Profile struct { |
| 135 |
name string |
| 136 |
mu sync.Mutex |
| 137 |
m map[any][]uintptr |
| 138 |
count func() int |
| 139 |
write func(io.Writer, int) error |
| 140 |
} |
| 141 |
|
| 142 |
// profiles records all registered profiles. |
| 143 |
var profiles struct { |
| 144 |
mu sync.Mutex |
| 145 |
m map[string]*Profile |
| 146 |
} |
| 147 |
|
| 148 |
var goroutineProfile = &Profile{ |
| 149 |
name: "goroutine", |
| 150 |
count: countGoroutine, |
| 151 |
write: writeGoroutine, |
| 152 |
} |
| 153 |
|
| 154 |
var threadcreateProfile = &Profile{ |
| 155 |
name: "threadcreate", |
| 156 |
count: countThreadCreate, |
| 157 |
write: writeThreadCreate, |
| 158 |
} |
| 159 |
|
| 160 |
var heapProfile = &Profile{ |
| 161 |
name: "heap", |
| 162 |
count: countHeap, |
| 163 |
write: writeHeap, |
| 164 |
} |
| 165 |
|
| 166 |
var allocsProfile = &Profile{ |
| 167 |
name: "allocs", |
| 168 |
count: countHeap, // identical to heap profile |
| 169 |
write: writeAlloc, |
| 170 |
} |
| 171 |
|
| 172 |
var blockProfile = &Profile{ |
| 173 |
name: "block", |
| 174 |
count: countBlock, |
| 175 |
write: writeBlock, |
| 176 |
} |
| 177 |
|
| 178 |
var mutexProfile = &Profile{ |
| 179 |
name: "mutex", |
| 180 |
count: countMutex, |
| 181 |
write: writeMutex, |
| 182 |
} |
| 183 |
|
| 184 |
func lockProfiles() { |
| 185 |
profiles.mu.Lock() |
| 186 |
if profiles.m == nil { |
| 187 |
// Initial built-in profiles. |
| 188 |
profiles.m = map[string]*Profile{ |
| 189 |
"goroutine": goroutineProfile, |
| 190 |
"threadcreate": threadcreateProfile, |
| 191 |
"heap": heapProfile, |
| 192 |
"allocs": allocsProfile, |
| 193 |
"block": blockProfile, |
| 194 |
"mutex": mutexProfile, |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func unlockProfiles() { |
| 200 |
profiles.mu.Unlock() |
| 201 |
} |
| 202 |
|
| 203 |
// NewProfile creates a new profile with the given name. |
| 204 |
// If a profile with that name already exists, NewProfile panics. |
| 205 |
// The convention is to use a 'import/path.' prefix to create |
| 206 |
// separate name spaces for each package. |
| 207 |
// For compatibility with various tools that read pprof data, |
| 208 |
// profile names should not contain spaces. |
| 209 |
func NewProfile(name string) *Profile { |
| 210 |
lockProfiles() |
| 211 |
defer unlockProfiles() |
| 212 |
if name == "" { |
| 213 |
panic("pprof: NewProfile with empty name") |
| 214 |
} |
| 215 |
if profiles.m[name] != nil { |
| 216 |
panic("pprof: NewProfile name already in use: " + name) |
| 217 |
} |
| 218 |
p := &Profile{ |
| 219 |
name: name, |
| 220 |
m: map[any][]uintptr{}, |
| 221 |
} |
| 222 |
profiles.m[name] = p |
| 223 |
return p |
| 224 |
} |
| 225 |
|
| 226 |
// Lookup returns the profile with the given name, or nil if no such profile exists. |
| 227 |
func Lookup(name string) *Profile { |
| 228 |
lockProfiles() |
| 229 |
defer unlockProfiles() |
| 230 |
return profiles.m[name] |
| 231 |
} |
| 232 |
|
| 233 |
// Profiles returns a slice of all the known profiles, sorted by name. |
| 234 |
func Profiles() []*Profile { |
| 235 |
lockProfiles() |
| 236 |
defer unlockProfiles() |
| 237 |
|
| 238 |
all := make([]*Profile, 0, len(profiles.m)) |
| 239 |
for _, p := range profiles.m { |
| 240 |
all = append(all, p) |
| 241 |
} |
| 242 |
|
| 243 |
sort.Slice(all, func(i, j int) bool { return all[i].name < all[j].name }) |
| 244 |
return all |
| 245 |
} |
| 246 |
|
| 247 |
// Name returns this profile's name, which can be passed to Lookup to reobtain the profile. |
| 248 |
func (p *Profile) Name() string { |
| 249 |
return p.name |
| 250 |
} |
| 251 |
|
| 252 |
// Count returns the number of execution stacks currently in the profile. |
| 253 |
func (p *Profile) Count() int { |
| 254 |
p.mu.Lock() |
| 255 |
defer p.mu.Unlock() |
| 256 |
if p.count != nil { |
| 257 |
return p.count() |
| 258 |
} |
| 259 |
return len(p.m) |
| 260 |
} |
| 261 |
|
| 262 |
// Add adds the current execution stack to the profile, associated with value. |
| 263 |
// Add stores value in an internal map, so value must be suitable for use as |
| 264 |
// a map key and will not be garbage collected until the corresponding |
| 265 |
// call to Remove. Add panics if the profile already contains a stack for value. |
| 266 |
// |
| 267 |
// The skip parameter has the same meaning as runtime.Caller's skip |
| 268 |
// and controls where the stack trace begins. Passing skip=0 begins the |
| 269 |
// trace in the function calling Add. For example, given this |
| 270 |
// execution stack: |
| 271 |
// |
| 272 |
// Add |
| 273 |
// called from rpc.NewClient |
| 274 |
// called from mypkg.Run |
| 275 |
// called from main.main |
| 276 |
// |
| 277 |
// Passing skip=0 begins the stack trace at the call to Add inside rpc.NewClient. |
| 278 |
// Passing skip=1 begins the stack trace at the call to NewClient inside mypkg.Run. |
| 279 |
// |
| 280 |
func (p *Profile) Add(value any, skip int) { |
| 281 |
if p.name == "" { |
| 282 |
panic("pprof: use of uninitialized Profile") |
| 283 |
} |
| 284 |
if p.write != nil { |
| 285 |
panic("pprof: Add called on built-in Profile " + p.name) |
| 286 |
} |
| 287 |
|
| 288 |
stk := make([]uintptr, 32) |
| 289 |
n := runtime.Callers(skip+1, stk[:]) |
| 290 |
stk = stk[:n] |
| 291 |
if len(stk) == 0 { |
| 292 |
// The value for skip is too large, and there's no stack trace to record. |
| 293 |
stk = []uintptr{abi.FuncPCABIInternal(lostProfileEvent) + 1} |
| 294 |
} |
| 295 |
|
| 296 |
p.mu.Lock() |
| 297 |
defer p.mu.Unlock() |
| 298 |
if p.m[value] != nil { |
| 299 |
panic("pprof: Profile.Add of duplicate value") |
| 300 |
} |
| 301 |
p.m[value] = stk |
| 302 |
} |
| 303 |
|
| 304 |
// Remove removes the execution stack associated with value from the profile. |
| 305 |
// It is a no-op if the value is not in the profile. |
| 306 |
func (p *Profile) Remove(value any) { |
| 307 |
p.mu.Lock() |
| 308 |
defer p.mu.Unlock() |
| 309 |
delete(p.m, value) |
| 310 |
} |
| 311 |
|
| 312 |
// WriteTo writes a pprof-formatted snapshot of the profile to w. |
| 313 |
// If a write to w returns an error, WriteTo returns that error. |
| 314 |
// Otherwise, WriteTo returns nil. |
| 315 |
// |
| 316 |
// The debug parameter enables additional output. |
| 317 |
// Passing debug=0 writes the gzip-compressed protocol buffer described |
| 318 |
// in https://github.com/google/pprof/tree/master/proto#overview. |
| 319 |
// Passing debug=1 writes the legacy text format with comments |
| 320 |
// translating addresses to function names and line numbers, so that a |
| 321 |
// programmer can read the profile without tools. |
| 322 |
// |
| 323 |
// The predefined profiles may assign meaning to other debug values; |
| 324 |
// for example, when printing the "goroutine" profile, debug=2 means to |
| 325 |
// print the goroutine stacks in the same form that a Go program uses |
| 326 |
// when dying due to an unrecovered panic. |
| 327 |
func (p *Profile) WriteTo(w io.Writer, debug int) error { |
| 328 |
if p.name == "" { |
| 329 |
panic("pprof: use of zero Profile") |
| 330 |
} |
| 331 |
if p.write != nil { |
| 332 |
return p.write(w, debug) |
| 333 |
} |
| 334 |
|
| 335 |
// Obtain consistent snapshot under lock; then process without lock. |
| 336 |
p.mu.Lock() |
| 337 |
all := make([][]uintptr, 0, len(p.m)) |
| 338 |
for _, stk := range p.m { |
| 339 |
all = append(all, stk) |
| 340 |
} |
| 341 |
p.mu.Unlock() |
| 342 |
|
| 343 |
// Map order is non-deterministic; make output deterministic. |
| 344 |
sort.Slice(all, func(i, j int) bool { |
| 345 |
t, u := all[i], all[j] |
| 346 |
for k := 0; k < len(t) && k < len(u); k++ { |
| 347 |
if t[k] != u[k] { |
| 348 |
return t[k] < u[k] |
| 349 |
} |
| 350 |
} |
| 351 |
return len(t) < len(u) |
| 352 |
}) |
| 353 |
|
| 354 |
return printCountProfile(w, debug, p.name, stackProfile(all)) |
| 355 |
} |
| 356 |
|
| 357 |
type stackProfile [][]uintptr |
| 358 |
|
| 359 |
func (x stackProfile) Len() int { return len(x) } |
| 360 |
func (x stackProfile) Stack(i int) []uintptr { return x[i] } |
| 361 |
func (x stackProfile) Label(i int) *labelMap { return nil } |
| 362 |
|
| 363 |
// A countProfile is a set of stack traces to be printed as counts |
| 364 |
// grouped by stack trace. There are multiple implementations: |
| 365 |
// all that matters is that we can find out how many traces there are |
| 366 |
// and obtain each trace in turn. |
| 367 |
type countProfile interface { |
| 368 |
Len() int |
| 369 |
Stack(i int) []uintptr |
| 370 |
Label(i int) *labelMap |
| 371 |
} |
| 372 |
|
| 373 |
// printCountCycleProfile outputs block profile records (for block or mutex profiles) |
| 374 |
// as the pprof-proto format output. Translations from cycle count to time duration |
| 375 |
// are done because The proto expects count and time (nanoseconds) instead of count |
| 376 |
// and the number of cycles for block, contention profiles. |
| 377 |
// Possible 'scaler' functions are scaleBlockProfile and scaleMutexProfile. |
| 378 |
func printCountCycleProfile(w io.Writer, countName, cycleName string, scaler func(int64, float64) (int64, float64), records []runtime.BlockProfileRecord) error { |
| 379 |
// Output profile in protobuf form. |
| 380 |
b := newProfileBuilder(w) |
| 381 |
b.pbValueType(tagProfile_PeriodType, countName, "count") |
| 382 |
b.pb.int64Opt(tagProfile_Period, 1) |
| 383 |
b.pbValueType(tagProfile_SampleType, countName, "count") |
| 384 |
b.pbValueType(tagProfile_SampleType, cycleName, "nanoseconds") |
| 385 |
|
| 386 |
cpuGHz := float64(runtime_cyclesPerSecond()) / 1e9 |
| 387 |
|
| 388 |
values := []int64{0, 0} |
| 389 |
var locs []uint64 |
| 390 |
for _, r := range records { |
| 391 |
count, nanosec := scaler(r.Count, float64(r.Cycles)/cpuGHz) |
| 392 |
values[0] = count |
| 393 |
values[1] = int64(nanosec) |
| 394 |
// For count profiles, all stack addresses are |
| 395 |
// return PCs, which is what appendLocsForStack expects. |
| 396 |
locs = b.appendLocsForStack(locs[:0], r.Stack()) |
| 397 |
b.pbSample(values, locs, nil) |
| 398 |
} |
| 399 |
b.build() |
| 400 |
return nil |
| 401 |
} |
| 402 |
|
| 403 |
// printCountProfile prints a countProfile at the specified debug level. |
| 404 |
// The profile will be in compressed proto format unless debug is nonzero. |
| 405 |
func printCountProfile(w io.Writer, debug int, name string, p countProfile) error { |
| 406 |
// Build count of each stack. |
| 407 |
var buf bytes.Buffer |
| 408 |
key := func(stk []uintptr, lbls *labelMap) string { |
| 409 |
buf.Reset() |
| 410 |
fmt.Fprintf(&buf, "@") |
| 411 |
for _, pc := range stk { |
| 412 |
fmt.Fprintf(&buf, " %#x", pc) |
| 413 |
} |
| 414 |
if lbls != nil { |
| 415 |
buf.WriteString("\n# labels: ") |
| 416 |
buf.WriteString(lbls.String()) |
| 417 |
} |
| 418 |
return buf.String() |
| 419 |
} |
| 420 |
count := map[string]int{} |
| 421 |
index := map[string]int{} |
| 422 |
var keys []string |
| 423 |
n := p.Len() |
| 424 |
for i := 0; i < n; i++ { |
| 425 |
k := key(p.Stack(i), p.Label(i)) |
| 426 |
if count[k] == 0 { |
| 427 |
index[k] = i |
| 428 |
keys = append(keys, k) |
| 429 |
} |
| 430 |
count[k]++ |
| 431 |
} |
| 432 |
|
| 433 |
sort.Sort(&keysByCount{keys, count}) |
| 434 |
|
| 435 |
if debug > 0 { |
| 436 |
// Print debug profile in legacy format |
| 437 |
tw := tabwriter.NewWriter(w, 1, 8, 1, '\t', 0) |
| 438 |
fmt.Fprintf(tw, "%s profile: total %d\n", name, p.Len()) |
| 439 |
for _, k := range keys { |
| 440 |
fmt.Fprintf(tw, "%d %s\n", count[k], k) |
| 441 |
printStackRecord(tw, p.Stack(index[k]), false) |
| 442 |
} |
| 443 |
return tw.Flush() |
| 444 |
} |
| 445 |
|
| 446 |
// Output profile in protobuf form. |
| 447 |
b := newProfileBuilder(w) |
| 448 |
b.pbValueType(tagProfile_PeriodType, name, "count") |
| 449 |
b.pb.int64Opt(tagProfile_Period, 1) |
| 450 |
b.pbValueType(tagProfile_SampleType, name, "count") |
| 451 |
|
| 452 |
values := []int64{0} |
| 453 |
var locs []uint64 |
| 454 |
for _, k := range keys { |
| 455 |
values[0] = int64(count[k]) |
| 456 |
// For count profiles, all stack addresses are |
| 457 |
// return PCs, which is what appendLocsForStack expects. |
| 458 |
locs = b.appendLocsForStack(locs[:0], p.Stack(index[k])) |
| 459 |
idx := index[k] |
| 460 |
var labels func() |
| 461 |
if p.Label(idx) != nil { |
| 462 |
labels = func() { |
| 463 |
for k, v := range *p.Label(idx) { |
| 464 |
b.pbLabel(tagSample_Label, k, v, 0) |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
b.pbSample(values, locs, labels) |
| 469 |
} |
| 470 |
b.build() |
| 471 |
return nil |
| 472 |
} |
| 473 |
|
| 474 |
// keysByCount sorts keys with higher counts first, breaking ties by key string order. |
| 475 |
type keysByCount struct { |
| 476 |
keys []string |
| 477 |
count map[string]int |
| 478 |
} |
| 479 |
|
| 480 |
func (x *keysByCount) Len() int { return len(x.keys) } |
| 481 |
func (x *keysByCount) Swap(i, j int) { x.keys[i], x.keys[j] = x.keys[j], x.keys[i] } |
| 482 |
func (x *keysByCount) Less(i, j int) bool { |
| 483 |
ki, kj := x.keys[i], x.keys[j] |
| 484 |
ci, cj := x.count[ki], x.count[kj] |
| 485 |
if ci != cj { |
| 486 |
return ci > cj |
| 487 |
} |
| 488 |
return ki < kj |
| 489 |
} |
| 490 |
|
| 491 |
// printStackRecord prints the function + source line information |
| 492 |
// for a single stack trace. |
| 493 |
func printStackRecord(w io.Writer, stk []uintptr, allFrames bool) { |
| 494 |
show := allFrames |
| 495 |
frames := runtime.CallersFrames(stk) |
| 496 |
for { |
| 497 |
frame, more := frames.Next() |
| 498 |
name := frame.Function |
| 499 |
|
| 500 |
// Hide runtime.goexit and any runtime functions at the beginning. |
| 501 |
// This is useful mainly for allocation traces. |
| 502 |
skip := name == "runtime.goexit" || name == "runtime.kickoff" |
| 503 |
if !show { |
| 504 |
switch { |
| 505 |
case strings.HasPrefix(name, "runtime."): |
| 506 |
skip = true |
| 507 |
case strings.HasPrefix(name, "runtime_"): |
| 508 |
skip = true |
| 509 |
case !strings.Contains(name, ".") && strings.HasPrefix(name, "__go_"): |
| 510 |
skip = true |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
if !show && name == "" { |
| 515 |
// This can happen due to http://gcc.gnu.org/PR65797. |
| 516 |
} else if name == "" { |
| 517 |
fmt.Fprintf(w, "#\t%#x\n", frame.PC) |
| 518 |
} else if !skip { |
| 519 |
show = true |
| 520 |
fmt.Fprintf(w, "#\t%#x\t%s+%#x\t%s:%d\n", frame.PC, name, frame.PC-frame.Entry, frame.File, frame.Line) |
| 521 |
} |
| 522 |
if !more { |
| 523 |
break |
| 524 |
} |
| 525 |
} |
| 526 |
if !show { |
| 527 |
// We didn't print anything; do it again, |
| 528 |
// and this time include runtime functions. |
| 529 |
printStackRecord(w, stk, true) |
| 530 |
return |
| 531 |
} |
| 532 |
fmt.Fprintf(w, "\n") |
| 533 |
} |
| 534 |
|
| 535 |
// Interface to system profiles. |
| 536 |
|
| 537 |
// WriteHeapProfile is shorthand for Lookup("heap").WriteTo(w, 0). |
| 538 |
// It is preserved for backwards compatibility. |
| 539 |
func WriteHeapProfile(w io.Writer) error { |
| 540 |
return writeHeap(w, 0) |
| 541 |
} |
| 542 |
|
| 543 |
// countHeap returns the number of records in the heap profile. |
| 544 |
func countHeap() int { |
| 545 |
n, _ := runtime.MemProfile(nil, true) |
| 546 |
return n |
| 547 |
} |
| 548 |
|
| 549 |
// writeHeap writes the current runtime heap profile to w. |
| 550 |
func writeHeap(w io.Writer, debug int) error { |
| 551 |
return writeHeapInternal(w, debug, "") |
| 552 |
} |
| 553 |
|
| 554 |
// writeAlloc writes the current runtime heap profile to w |
| 555 |
// with the total allocation space as the default sample type. |
| 556 |
func writeAlloc(w io.Writer, debug int) error { |
| 557 |
return writeHeapInternal(w, debug, "alloc_space") |
| 558 |
} |
| 559 |
|
| 560 |
func writeHeapInternal(w io.Writer, debug int, defaultSampleType string) error { |
| 561 |
var memStats *runtime.MemStats |
| 562 |
if debug != 0 { |
| 563 |
// Read mem stats first, so that our other allocations |
| 564 |
// do not appear in the statistics. |
| 565 |
memStats = new(runtime.MemStats) |
| 566 |
runtime.ReadMemStats(memStats) |
| 567 |
} |
| 568 |
|
| 569 |
// Find out how many records there are (MemProfile(nil, true)), |
| 570 |
// allocate that many records, and get the data. |
| 571 |
// There's a race—more records might be added between |
| 572 |
// the two calls—so allocate a few extra records for safety |
| 573 |
// and also try again if we're very unlucky. |
| 574 |
// The loop should only execute one iteration in the common case. |
| 575 |
var p []runtime.MemProfileRecord |
| 576 |
n, ok := runtime.MemProfile(nil, true) |
| 577 |
for { |
| 578 |
// Allocate room for a slightly bigger profile, |
| 579 |
// in case a few more entries have been added |
| 580 |
// since the call to MemProfile. |
| 581 |
p = make([]runtime.MemProfileRecord, n+50) |
| 582 |
n, ok = runtime.MemProfile(p, true) |
| 583 |
if ok { |
| 584 |
p = p[0:n] |
| 585 |
break |
| 586 |
} |
| 587 |
// Profile grew; try again. |
| 588 |
} |
| 589 |
|
| 590 |
if debug == 0 { |
| 591 |
return writeHeapProto(w, p, int64(runtime.MemProfileRate), defaultSampleType) |
| 592 |
} |
| 593 |
|
| 594 |
sort.Slice(p, func(i, j int) bool { return p[i].InUseBytes() > p[j].InUseBytes() }) |
| 595 |
|
| 596 |
b := bufio.NewWriter(w) |
| 597 |
tw := tabwriter.NewWriter(b, 1, 8, 1, '\t', 0) |
| 598 |
w = tw |
| 599 |
|
| 600 |
var total runtime.MemProfileRecord |
| 601 |
for i := range p { |
| 602 |
r := &p[i] |
| 603 |
total.AllocBytes += r.AllocBytes |
| 604 |
total.AllocObjects += r.AllocObjects |
| 605 |
total.FreeBytes += r.FreeBytes |
| 606 |
total.FreeObjects += r.FreeObjects |
| 607 |
} |
| 608 |
|
| 609 |
// Technically the rate is MemProfileRate not 2*MemProfileRate, |
| 610 |
// but early versions of the C++ heap profiler reported 2*MemProfileRate, |
| 611 |
// so that's what pprof has come to expect. |
| 612 |
fmt.Fprintf(w, "heap profile: %d: %d [%d: %d] @ heap/%d\n", |
| 613 |
total.InUseObjects(), total.InUseBytes(), |
| 614 |
total.AllocObjects, total.AllocBytes, |
| 615 |
2*runtime.MemProfileRate) |
| 616 |
|
| 617 |
for i := range p { |
| 618 |
r := &p[i] |
| 619 |
fmt.Fprintf(w, "%d: %d [%d: %d] @", |
| 620 |
r.InUseObjects(), r.InUseBytes(), |
| 621 |
r.AllocObjects, r.AllocBytes) |
| 622 |
for _, pc := range r.Stack() { |
| 623 |
fmt.Fprintf(w, " %#x", pc) |
| 624 |
} |
| 625 |
fmt.Fprintf(w, "\n") |
| 626 |
printStackRecord(w, r.Stack(), false) |
| 627 |
} |
| 628 |
|
| 629 |
// Print memstats information too. |
| 630 |
// Pprof will ignore, but useful for people |
| 631 |
s := memStats |
| 632 |
fmt.Fprintf(w, "\n# runtime.MemStats\n") |
| 633 |
fmt.Fprintf(w, "# Alloc = %d\n", s.Alloc) |
| 634 |
fmt.Fprintf(w, "# TotalAlloc = %d\n", s.TotalAlloc) |
| 635 |
fmt.Fprintf(w, "# Sys = %d\n", s.Sys) |
| 636 |
fmt.Fprintf(w, "# Lookups = %d\n", s.Lookups) |
| 637 |
fmt.Fprintf(w, "# Mallocs = %d\n", s.Mallocs) |
| 638 |
fmt.Fprintf(w, "# Frees = %d\n", s.Frees) |
| 639 |
|
| 640 |
fmt.Fprintf(w, "# HeapAlloc = %d\n", s.HeapAlloc) |
| 641 |
fmt.Fprintf(w, "# HeapSys = %d\n", s.HeapSys) |
| 642 |
fmt.Fprintf(w, "# HeapIdle = %d\n", s.HeapIdle) |
| 643 |
fmt.Fprintf(w, "# HeapInuse = %d\n", s.HeapInuse) |
| 644 |
fmt.Fprintf(w, "# HeapReleased = %d\n", s.HeapReleased) |
| 645 |
fmt.Fprintf(w, "# HeapObjects = %d\n", s.HeapObjects) |
| 646 |
|
| 647 |
fmt.Fprintf(w, "# Stack = %d / %d\n", s.StackInuse, s.StackSys) |
| 648 |
fmt.Fprintf(w, "# MSpan = %d / %d\n", s.MSpanInuse, s.MSpanSys) |
| 649 |
fmt.Fprintf(w, "# MCache = %d / %d\n", s.MCacheInuse, s.MCacheSys) |
| 650 |
fmt.Fprintf(w, "# BuckHashSys = %d\n", s.BuckHashSys) |
| 651 |
fmt.Fprintf(w, "# GCSys = %d\n", s.GCSys) |
| 652 |
fmt.Fprintf(w, "# OtherSys = %d\n", s.OtherSys) |
| 653 |
|
| 654 |
fmt.Fprintf(w, "# NextGC = %d\n", s.NextGC) |
| 655 |
fmt.Fprintf(w, "# LastGC = %d\n", s.LastGC) |
| 656 |
fmt.Fprintf(w, "# PauseNs = %d\n", s.PauseNs) |
| 657 |
fmt.Fprintf(w, "# PauseEnd = %d\n", s.PauseEnd) |
| 658 |
fmt.Fprintf(w, "# NumGC = %d\n", s.NumGC) |
| 659 |
fmt.Fprintf(w, "# NumForcedGC = %d\n", s.NumForcedGC) |
| 660 |
fmt.Fprintf(w, "# GCCPUFraction = %v\n", s.GCCPUFraction) |
| 661 |
fmt.Fprintf(w, "# DebugGC = %v\n", s.DebugGC) |
| 662 |
|
| 663 |
// Also flush out MaxRSS on supported platforms. |
| 664 |
addMaxRSS(w) |
| 665 |
|
| 666 |
tw.Flush() |
| 667 |
return b.Flush() |
| 668 |
} |
| 669 |
|
| 670 |
// countThreadCreate returns the size of the current ThreadCreateProfile. |
| 671 |
func countThreadCreate() int { |
| 672 |
n, _ := runtime.ThreadCreateProfile(nil) |
| 673 |
return n |
| 674 |
} |
| 675 |
|
| 676 |
// writeThreadCreate writes the current runtime ThreadCreateProfile to w. |
| 677 |
func writeThreadCreate(w io.Writer, debug int) error { |
| 678 |
// Until https://golang.org/issues/6104 is addressed, wrap |
| 679 |
// ThreadCreateProfile because there's no point in tracking labels when we |
| 680 |
// don't get any stack-traces. |
| 681 |
return writeRuntimeProfile(w, debug, "threadcreate", func(p []runtime.StackRecord, _ []unsafe.Pointer) (n int, ok bool) { |
| 682 |
return runtime.ThreadCreateProfile(p) |
| 683 |
}) |
| 684 |
} |
| 685 |
|
| 686 |
// countGoroutine returns the number of goroutines. |
| 687 |
func countGoroutine() int { |
| 688 |
return runtime.NumGoroutine() |
| 689 |
} |
| 690 |
|
| 691 |
// runtime_goroutineProfileWithLabels is defined in runtime/mprof.go |
| 692 |
func runtime_goroutineProfileWithLabels(p []runtime.StackRecord, labels []unsafe.Pointer) (n int, ok bool) |
| 693 |
|
| 694 |
// writeGoroutine writes the current runtime GoroutineProfile to w. |
| 695 |
func writeGoroutine(w io.Writer, debug int) error { |
| 696 |
if debug >= 2 { |
| 697 |
return writeGoroutineStacks(w) |
| 698 |
} |
| 699 |
return writeRuntimeProfile(w, debug, "goroutine", runtime_goroutineProfileWithLabels) |
| 700 |
} |
| 701 |
|
| 702 |
func writeGoroutineStacks(w io.Writer) error { |
| 703 |
// We don't know how big the buffer needs to be to collect |
| 704 |
// all the goroutines. Start with 1 MB and try a few times, doubling each time. |
| 705 |
// Give up and use a truncated trace if 64 MB is not enough. |
| 706 |
buf := make([]byte, 1<<20) |
| 707 |
for i := 0; ; i++ { |
| 708 |
n := runtime.Stack(buf, true) |
| 709 |
if n < len(buf) { |
| 710 |
buf = buf[:n] |
| 711 |
break |
| 712 |
} |
| 713 |
if len(buf) >= 64<<20 { |
| 714 |
// Filled 64 MB - stop there. |
| 715 |
break |
| 716 |
} |
| 717 |
buf = make([]byte, 2*len(buf)) |
| 718 |
} |
| 719 |
_, err := w.Write(buf) |
| 720 |
return err |
| 721 |
} |
| 722 |
|
| 723 |
func writeRuntimeProfile(w io.Writer, debug int, name string, fetch func([]runtime.StackRecord, []unsafe.Pointer) (int, bool)) error { |
| 724 |
// Find out how many records there are (fetch(nil)), |
| 725 |
// allocate that many records, and get the data. |
| 726 |
// There's a race—more records might be added between |
| 727 |
// the two calls—so allocate a few extra records for safety |
| 728 |
// and also try again if we're very unlucky. |
| 729 |
// The loop should only execute one iteration in the common case. |
| 730 |
var p []runtime.StackRecord |
| 731 |
var labels []unsafe.Pointer |
| 732 |
n, ok := fetch(nil, nil) |
| 733 |
for { |
| 734 |
// Allocate room for a slightly bigger profile, |
| 735 |
// in case a few more entries have been added |
| 736 |
// since the call to ThreadProfile. |
| 737 |
p = make([]runtime.StackRecord, n+10) |
| 738 |
labels = make([]unsafe.Pointer, n+10) |
| 739 |
n, ok = fetch(p, labels) |
| 740 |
if ok { |
| 741 |
p = p[0:n] |
| 742 |
break |
| 743 |
} |
| 744 |
// Profile grew; try again. |
| 745 |
} |
| 746 |
|
| 747 |
return printCountProfile(w, debug, name, &runtimeProfile{p, labels}) |
| 748 |
} |
| 749 |
|
| 750 |
type runtimeProfile struct { |
| 751 |
stk []runtime.StackRecord |
| 752 |
labels []unsafe.Pointer |
| 753 |
} |
| 754 |
|
| 755 |
func (p *runtimeProfile) Len() int { return len(p.stk) } |
| 756 |
func (p *runtimeProfile) Stack(i int) []uintptr { return p.stk[i].Stack() } |
| 757 |
func (p *runtimeProfile) Label(i int) *labelMap { return (*labelMap)(p.labels[i]) } |
| 758 |
|
| 759 |
var cpu struct { |
| 760 |
sync.Mutex |
| 761 |
profiling bool |
| 762 |
done chan bool |
| 763 |
} |
| 764 |
|
| 765 |
// StartCPUProfile enables CPU profiling for the current process. |
| 766 |
// While profiling, the profile will be buffered and written to w. |
| 767 |
// StartCPUProfile returns an error if profiling is already enabled. |
| 768 |
// |
| 769 |
// On Unix-like systems, StartCPUProfile does not work by default for |
| 770 |
// Go code built with -buildmode=c-archive or -buildmode=c-shared. |
| 771 |
// StartCPUProfile relies on the SIGPROF signal, but that signal will |
| 772 |
// be delivered to the main program's SIGPROF signal handler (if any) |
| 773 |
// not to the one used by Go. To make it work, call os/signal.Notify |
| 774 |
// for syscall.SIGPROF, but note that doing so may break any profiling |
| 775 |
// being done by the main program. |
| 776 |
func StartCPUProfile(w io.Writer) error { |
| 777 |
// The runtime routines allow a variable profiling rate, |
| 778 |
// but in practice operating systems cannot trigger signals |
| 779 |
// at more than about 500 Hz, and our processing of the |
| 780 |
// signal is not cheap (mostly getting the stack trace). |
| 781 |
// 100 Hz is a reasonable choice: it is frequent enough to |
| 782 |
// produce useful data, rare enough not to bog down the |
| 783 |
// system, and a nice round number to make it easy to |
| 784 |
// convert sample counts to seconds. Instead of requiring |
| 785 |
// each client to specify the frequency, we hard code it. |
| 786 |
const hz = 100 |
| 787 |
|
| 788 |
cpu.Lock() |
| 789 |
defer cpu.Unlock() |
| 790 |
if cpu.done == nil { |
| 791 |
cpu.done = make(chan bool) |
| 792 |
} |
| 793 |
// Double-check. |
| 794 |
if cpu.profiling { |
| 795 |
return fmt.Errorf("cpu profiling already in use") |
| 796 |
} |
| 797 |
cpu.profiling = true |
| 798 |
runtime.SetCPUProfileRate(hz) |
| 799 |
go profileWriter(w) |
| 800 |
return nil |
| 801 |
} |
| 802 |
|
| 803 |
// readProfile, provided by the runtime, returns the next chunk of |
| 804 |
// binary CPU profiling stack trace data, blocking until data is available. |
| 805 |
// If profiling is turned off and all the profile data accumulated while it was |
| 806 |
// on has been returned, readProfile returns eof=true. |
| 807 |
// The caller must save the returned data and tags before calling readProfile again. |
| 808 |
func readProfile() (data []uint64, tags []unsafe.Pointer, eof bool) |
| 809 |
|
| 810 |
func profileWriter(w io.Writer) { |
| 811 |
b := newProfileBuilder(w) |
| 812 |
var err error |
| 813 |
for { |
| 814 |
time.Sleep(100 * time.Millisecond) |
| 815 |
data, tags, eof := readProfile() |
| 816 |
if e := b.addCPUData(data, tags); e != nil && err == nil { |
| 817 |
err = e |
| 818 |
} |
| 819 |
if eof { |
| 820 |
break |
| 821 |
} |
| 822 |
} |
| 823 |
if err != nil { |
| 824 |
// The runtime should never produce an invalid or truncated profile. |
| 825 |
// It drops records that can't fit into its log buffers. |
| 826 |
panic("runtime/pprof: converting profile: " + err.Error()) |
| 827 |
} |
| 828 |
b.build() |
| 829 |
cpu.done <- true |
| 830 |
} |
| 831 |
|
| 832 |
// StopCPUProfile stops the current CPU profile, if any. |
| 833 |
// StopCPUProfile only returns after all the writes for the |
| 834 |
// profile have completed. |
| 835 |
func StopCPUProfile() { |
| 836 |
cpu.Lock() |
| 837 |
defer cpu.Unlock() |
| 838 |
|
| 839 |
if !cpu.profiling { |
| 840 |
return |
| 841 |
} |
| 842 |
cpu.profiling = false |
| 843 |
runtime.SetCPUProfileRate(0) |
| 844 |
<-cpu.done |
| 845 |
} |
| 846 |
|
| 847 |
// countBlock returns the number of records in the blocking profile. |
| 848 |
func countBlock() int { |
| 849 |
n, _ := runtime.BlockProfile(nil) |
| 850 |
return n |
| 851 |
} |
| 852 |
|
| 853 |
// countMutex returns the number of records in the mutex profile. |
| 854 |
func countMutex() int { |
| 855 |
n, _ := runtime.MutexProfile(nil) |
| 856 |
return n |
| 857 |
} |
| 858 |
|
| 859 |
// writeBlock writes the current blocking profile to w. |
| 860 |
func writeBlock(w io.Writer, debug int) error { |
| 861 |
return writeProfileInternal(w, debug, "contention", runtime.BlockProfile, scaleBlockProfile) |
| 862 |
} |
| 863 |
|
| 864 |
func scaleBlockProfile(cnt int64, ns float64) (int64, float64) { |
| 865 |
// Do nothing. |
| 866 |
// The current way of block profile sampling makes it |
| 867 |
// hard to compute the unsampled number. The legacy block |
| 868 |
// profile parse doesn't attempt to scale or unsample. |
| 869 |
return cnt, ns |
| 870 |
} |
| 871 |
|
| 872 |
// writeMutex writes the current mutex profile to w. |
| 873 |
func writeMutex(w io.Writer, debug int) error { |
| 874 |
return writeProfileInternal(w, debug, "mutex", runtime.MutexProfile, scaleMutexProfile) |
| 875 |
} |
| 876 |
|
| 877 |
// writeProfileInternal writes the current blocking or mutex profile depending on the passed parameters |
| 878 |
func writeProfileInternal(w io.Writer, debug int, name string, runtimeProfile func([]runtime.BlockProfileRecord) (int, bool), scaleProfile func(int64, float64) (int64, float64)) error { |
| 879 |
var p []runtime.BlockProfileRecord |
| 880 |
n, ok := runtimeProfile(nil) |
| 881 |
for { |
| 882 |
p = make([]runtime.BlockProfileRecord, n+50) |
| 883 |
n, ok = runtimeProfile(p) |
| 884 |
if ok { |
| 885 |
p = p[:n] |
| 886 |
break |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
sort.Slice(p, func(i, j int) bool { return p[i].Cycles > p[j].Cycles }) |
| 891 |
|
| 892 |
if debug <= 0 { |
| 893 |
return printCountCycleProfile(w, "contentions", "delay", scaleProfile, p) |
| 894 |
} |
| 895 |
|
| 896 |
b := bufio.NewWriter(w) |
| 897 |
tw := tabwriter.NewWriter(w, 1, 8, 1, '\t', 0) |
| 898 |
w = tw |
| 899 |
|
| 900 |
fmt.Fprintf(w, "--- %v:\n", name) |
| 901 |
fmt.Fprintf(w, "cycles/second=%v\n", runtime_cyclesPerSecond()) |
| 902 |
if name == "mutex" { |
| 903 |
fmt.Fprintf(w, "sampling period=%d\n", runtime.SetMutexProfileFraction(-1)) |
| 904 |
} |
| 905 |
for i := range p { |
| 906 |
r := &p[i] |
| 907 |
fmt.Fprintf(w, "%v %v @", r.Cycles, r.Count) |
| 908 |
for _, pc := range r.Stack() { |
| 909 |
fmt.Fprintf(w, " %#x", pc) |
| 910 |
} |
| 911 |
fmt.Fprint(w, "\n") |
| 912 |
if debug > 0 { |
| 913 |
printStackRecord(w, r.Stack(), true) |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
if tw != nil { |
| 918 |
tw.Flush() |
| 919 |
} |
| 920 |
return b.Flush() |
| 921 |
} |
| 922 |
|
| 923 |
func scaleMutexProfile(cnt int64, ns float64) (int64, float64) { |
| 924 |
period := runtime.SetMutexProfileFraction(-1) |
| 925 |
return cnt * int64(period), ns * float64(period) |
| 926 |
} |
| 927 |
|
| 928 |
func runtime_cyclesPerSecond() int64 |