github.com/m10x/go/src@v0.0.0-20220112094212-ba61592315da/runtime/pprof/pprof.go (about) 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)} 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 if name == "" { 500 show = true 501 fmt.Fprintf(w, "#\t%#x\n", frame.PC) 502 } else if name != "runtime.goexit" && (show || !strings.HasPrefix(name, "runtime.")) { 503 // Hide runtime.goexit and any runtime functions at the beginning. 504 // This is useful mainly for allocation traces. 505 show = true 506 fmt.Fprintf(w, "#\t%#x\t%s+%#x\t%s:%d\n", frame.PC, name, frame.PC-frame.Entry, frame.File, frame.Line) 507 } 508 if !more { 509 break 510 } 511 } 512 if !show { 513 // We didn't print anything; do it again, 514 // and this time include runtime functions. 515 printStackRecord(w, stk, true) 516 return 517 } 518 fmt.Fprintf(w, "\n") 519 } 520 521 // Interface to system profiles. 522 523 // WriteHeapProfile is shorthand for Lookup("heap").WriteTo(w, 0). 524 // It is preserved for backwards compatibility. 525 func WriteHeapProfile(w io.Writer) error { 526 return writeHeap(w, 0) 527 } 528 529 // countHeap returns the number of records in the heap profile. 530 func countHeap() int { 531 n, _ := runtime.MemProfile(nil, true) 532 return n 533 } 534 535 // writeHeap writes the current runtime heap profile to w. 536 func writeHeap(w io.Writer, debug int) error { 537 return writeHeapInternal(w, debug, "") 538 } 539 540 // writeAlloc writes the current runtime heap profile to w 541 // with the total allocation space as the default sample type. 542 func writeAlloc(w io.Writer, debug int) error { 543 return writeHeapInternal(w, debug, "alloc_space") 544 } 545 546 func writeHeapInternal(w io.Writer, debug int, defaultSampleType string) error { 547 var memStats *runtime.MemStats 548 if debug != 0 { 549 // Read mem stats first, so that our other allocations 550 // do not appear in the statistics. 551 memStats = new(runtime.MemStats) 552 runtime.ReadMemStats(memStats) 553 } 554 555 // Find out how many records there are (MemProfile(nil, true)), 556 // allocate that many records, and get the data. 557 // There's a race—more records might be added between 558 // the two calls—so allocate a few extra records for safety 559 // and also try again if we're very unlucky. 560 // The loop should only execute one iteration in the common case. 561 var p []runtime.MemProfileRecord 562 n, ok := runtime.MemProfile(nil, true) 563 for { 564 // Allocate room for a slightly bigger profile, 565 // in case a few more entries have been added 566 // since the call to MemProfile. 567 p = make([]runtime.MemProfileRecord, n+50) 568 n, ok = runtime.MemProfile(p, true) 569 if ok { 570 p = p[0:n] 571 break 572 } 573 // Profile grew; try again. 574 } 575 576 if debug == 0 { 577 return writeHeapProto(w, p, int64(runtime.MemProfileRate), defaultSampleType) 578 } 579 580 sort.Slice(p, func(i, j int) bool { return p[i].InUseBytes() > p[j].InUseBytes() }) 581 582 b := bufio.NewWriter(w) 583 tw := tabwriter.NewWriter(b, 1, 8, 1, '\t', 0) 584 w = tw 585 586 var total runtime.MemProfileRecord 587 for i := range p { 588 r := &p[i] 589 total.AllocBytes += r.AllocBytes 590 total.AllocObjects += r.AllocObjects 591 total.FreeBytes += r.FreeBytes 592 total.FreeObjects += r.FreeObjects 593 } 594 595 // Technically the rate is MemProfileRate not 2*MemProfileRate, 596 // but early versions of the C++ heap profiler reported 2*MemProfileRate, 597 // so that's what pprof has come to expect. 598 fmt.Fprintf(w, "heap profile: %d: %d [%d: %d] @ heap/%d\n", 599 total.InUseObjects(), total.InUseBytes(), 600 total.AllocObjects, total.AllocBytes, 601 2*runtime.MemProfileRate) 602 603 for i := range p { 604 r := &p[i] 605 fmt.Fprintf(w, "%d: %d [%d: %d] @", 606 r.InUseObjects(), r.InUseBytes(), 607 r.AllocObjects, r.AllocBytes) 608 for _, pc := range r.Stack() { 609 fmt.Fprintf(w, " %#x", pc) 610 } 611 fmt.Fprintf(w, "\n") 612 printStackRecord(w, r.Stack(), false) 613 } 614 615 // Print memstats information too. 616 // Pprof will ignore, but useful for people 617 s := memStats 618 fmt.Fprintf(w, "\n# runtime.MemStats\n") 619 fmt.Fprintf(w, "# Alloc = %d\n", s.Alloc) 620 fmt.Fprintf(w, "# TotalAlloc = %d\n", s.TotalAlloc) 621 fmt.Fprintf(w, "# Sys = %d\n", s.Sys) 622 fmt.Fprintf(w, "# Lookups = %d\n", s.Lookups) 623 fmt.Fprintf(w, "# Mallocs = %d\n", s.Mallocs) 624 fmt.Fprintf(w, "# Frees = %d\n", s.Frees) 625 626 fmt.Fprintf(w, "# HeapAlloc = %d\n", s.HeapAlloc) 627 fmt.Fprintf(w, "# HeapSys = %d\n", s.HeapSys) 628 fmt.Fprintf(w, "# HeapIdle = %d\n", s.HeapIdle) 629 fmt.Fprintf(w, "# HeapInuse = %d\n", s.HeapInuse) 630 fmt.Fprintf(w, "# HeapReleased = %d\n", s.HeapReleased) 631 fmt.Fprintf(w, "# HeapObjects = %d\n", s.HeapObjects) 632 633 fmt.Fprintf(w, "# Stack = %d / %d\n", s.StackInuse, s.StackSys) 634 fmt.Fprintf(w, "# MSpan = %d / %d\n", s.MSpanInuse, s.MSpanSys) 635 fmt.Fprintf(w, "# MCache = %d / %d\n", s.MCacheInuse, s.MCacheSys) 636 fmt.Fprintf(w, "# BuckHashSys = %d\n", s.BuckHashSys) 637 fmt.Fprintf(w, "# GCSys = %d\n", s.GCSys) 638 fmt.Fprintf(w, "# OtherSys = %d\n", s.OtherSys) 639 640 fmt.Fprintf(w, "# NextGC = %d\n", s.NextGC) 641 fmt.Fprintf(w, "# LastGC = %d\n", s.LastGC) 642 fmt.Fprintf(w, "# PauseNs = %d\n", s.PauseNs) 643 fmt.Fprintf(w, "# PauseEnd = %d\n", s.PauseEnd) 644 fmt.Fprintf(w, "# NumGC = %d\n", s.NumGC) 645 fmt.Fprintf(w, "# NumForcedGC = %d\n", s.NumForcedGC) 646 fmt.Fprintf(w, "# GCCPUFraction = %v\n", s.GCCPUFraction) 647 fmt.Fprintf(w, "# DebugGC = %v\n", s.DebugGC) 648 649 // Also flush out MaxRSS on supported platforms. 650 addMaxRSS(w) 651 652 tw.Flush() 653 return b.Flush() 654 } 655 656 // countThreadCreate returns the size of the current ThreadCreateProfile. 657 func countThreadCreate() int { 658 n, _ := runtime.ThreadCreateProfile(nil) 659 return n 660 } 661 662 // writeThreadCreate writes the current runtime ThreadCreateProfile to w. 663 func writeThreadCreate(w io.Writer, debug int) error { 664 // Until https://golang.org/issues/6104 is addressed, wrap 665 // ThreadCreateProfile because there's no point in tracking labels when we 666 // don't get any stack-traces. 667 return writeRuntimeProfile(w, debug, "threadcreate", func(p []runtime.StackRecord, _ []unsafe.Pointer) (n int, ok bool) { 668 return runtime.ThreadCreateProfile(p) 669 }) 670 } 671 672 // countGoroutine returns the number of goroutines. 673 func countGoroutine() int { 674 return runtime.NumGoroutine() 675 } 676 677 // runtime_goroutineProfileWithLabels is defined in runtime/mprof.go 678 func runtime_goroutineProfileWithLabels(p []runtime.StackRecord, labels []unsafe.Pointer) (n int, ok bool) 679 680 // writeGoroutine writes the current runtime GoroutineProfile to w. 681 func writeGoroutine(w io.Writer, debug int) error { 682 if debug >= 2 { 683 return writeGoroutineStacks(w) 684 } 685 return writeRuntimeProfile(w, debug, "goroutine", runtime_goroutineProfileWithLabels) 686 } 687 688 func writeGoroutineStacks(w io.Writer) error { 689 // We don't know how big the buffer needs to be to collect 690 // all the goroutines. Start with 1 MB and try a few times, doubling each time. 691 // Give up and use a truncated trace if 64 MB is not enough. 692 buf := make([]byte, 1<<20) 693 for i := 0; ; i++ { 694 n := runtime.Stack(buf, true) 695 if n < len(buf) { 696 buf = buf[:n] 697 break 698 } 699 if len(buf) >= 64<<20 { 700 // Filled 64 MB - stop there. 701 break 702 } 703 buf = make([]byte, 2*len(buf)) 704 } 705 _, err := w.Write(buf) 706 return err 707 } 708 709 func writeRuntimeProfile(w io.Writer, debug int, name string, fetch func([]runtime.StackRecord, []unsafe.Pointer) (int, bool)) error { 710 // Find out how many records there are (fetch(nil)), 711 // allocate that many records, and get the data. 712 // There's a race—more records might be added between 713 // the two calls—so allocate a few extra records for safety 714 // and also try again if we're very unlucky. 715 // The loop should only execute one iteration in the common case. 716 var p []runtime.StackRecord 717 var labels []unsafe.Pointer 718 n, ok := fetch(nil, nil) 719 for { 720 // Allocate room for a slightly bigger profile, 721 // in case a few more entries have been added 722 // since the call to ThreadProfile. 723 p = make([]runtime.StackRecord, n+10) 724 labels = make([]unsafe.Pointer, n+10) 725 n, ok = fetch(p, labels) 726 if ok { 727 p = p[0:n] 728 break 729 } 730 // Profile grew; try again. 731 } 732 733 return printCountProfile(w, debug, name, &runtimeProfile{p, labels}) 734 } 735 736 type runtimeProfile struct { 737 stk []runtime.StackRecord 738 labels []unsafe.Pointer 739 } 740 741 func (p *runtimeProfile) Len() int { return len(p.stk) } 742 func (p *runtimeProfile) Stack(i int) []uintptr { return p.stk[i].Stack() } 743 func (p *runtimeProfile) Label(i int) *labelMap { return (*labelMap)(p.labels[i]) } 744 745 var cpu struct { 746 sync.Mutex 747 profiling bool 748 done chan bool 749 } 750 751 // StartCPUProfile enables CPU profiling for the current process. 752 // While profiling, the profile will be buffered and written to w. 753 // StartCPUProfile returns an error if profiling is already enabled. 754 // 755 // On Unix-like systems, StartCPUProfile does not work by default for 756 // Go code built with -buildmode=c-archive or -buildmode=c-shared. 757 // StartCPUProfile relies on the SIGPROF signal, but that signal will 758 // be delivered to the main program's SIGPROF signal handler (if any) 759 // not to the one used by Go. To make it work, call os/signal.Notify 760 // for syscall.SIGPROF, but note that doing so may break any profiling 761 // being done by the main program. 762 func StartCPUProfile(w io.Writer) error { 763 // The runtime routines allow a variable profiling rate, 764 // but in practice operating systems cannot trigger signals 765 // at more than about 500 Hz, and our processing of the 766 // signal is not cheap (mostly getting the stack trace). 767 // 100 Hz is a reasonable choice: it is frequent enough to 768 // produce useful data, rare enough not to bog down the 769 // system, and a nice round number to make it easy to 770 // convert sample counts to seconds. Instead of requiring 771 // each client to specify the frequency, we hard code it. 772 const hz = 100 773 774 cpu.Lock() 775 defer cpu.Unlock() 776 if cpu.done == nil { 777 cpu.done = make(chan bool) 778 } 779 // Double-check. 780 if cpu.profiling { 781 return fmt.Errorf("cpu profiling already in use") 782 } 783 cpu.profiling = true 784 runtime.SetCPUProfileRate(hz) 785 go profileWriter(w) 786 return nil 787 } 788 789 // readProfile, provided by the runtime, returns the next chunk of 790 // binary CPU profiling stack trace data, blocking until data is available. 791 // If profiling is turned off and all the profile data accumulated while it was 792 // on has been returned, readProfile returns eof=true. 793 // The caller must save the returned data and tags before calling readProfile again. 794 func readProfile() (data []uint64, tags []unsafe.Pointer, eof bool) 795 796 func profileWriter(w io.Writer) { 797 b := newProfileBuilder(w) 798 var err error 799 for { 800 time.Sleep(100 * time.Millisecond) 801 data, tags, eof := readProfile() 802 if e := b.addCPUData(data, tags); e != nil && err == nil { 803 err = e 804 } 805 if eof { 806 break 807 } 808 } 809 if err != nil { 810 // The runtime should never produce an invalid or truncated profile. 811 // It drops records that can't fit into its log buffers. 812 panic("runtime/pprof: converting profile: " + err.Error()) 813 } 814 b.build() 815 cpu.done <- true 816 } 817 818 // StopCPUProfile stops the current CPU profile, if any. 819 // StopCPUProfile only returns after all the writes for the 820 // profile have completed. 821 func StopCPUProfile() { 822 cpu.Lock() 823 defer cpu.Unlock() 824 825 if !cpu.profiling { 826 return 827 } 828 cpu.profiling = false 829 runtime.SetCPUProfileRate(0) 830 <-cpu.done 831 } 832 833 // countBlock returns the number of records in the blocking profile. 834 func countBlock() int { 835 n, _ := runtime.BlockProfile(nil) 836 return n 837 } 838 839 // countMutex returns the number of records in the mutex profile. 840 func countMutex() int { 841 n, _ := runtime.MutexProfile(nil) 842 return n 843 } 844 845 // writeBlock writes the current blocking profile to w. 846 func writeBlock(w io.Writer, debug int) error { 847 return writeProfileInternal(w, debug, "contention", runtime.BlockProfile, scaleBlockProfile) 848 } 849 850 func scaleBlockProfile(cnt int64, ns float64) (int64, float64) { 851 // Do nothing. 852 // The current way of block profile sampling makes it 853 // hard to compute the unsampled number. The legacy block 854 // profile parse doesn't attempt to scale or unsample. 855 return cnt, ns 856 } 857 858 // writeMutex writes the current mutex profile to w. 859 func writeMutex(w io.Writer, debug int) error { 860 return writeProfileInternal(w, debug, "mutex", runtime.MutexProfile, scaleMutexProfile) 861 } 862 863 // writeProfileInternal writes the current blocking or mutex profile depending on the passed parameters 864 func writeProfileInternal(w io.Writer, debug int, name string, runtimeProfile func([]runtime.BlockProfileRecord) (int, bool), scaleProfile func(int64, float64) (int64, float64)) error { 865 var p []runtime.BlockProfileRecord 866 n, ok := runtimeProfile(nil) 867 for { 868 p = make([]runtime.BlockProfileRecord, n+50) 869 n, ok = runtimeProfile(p) 870 if ok { 871 p = p[:n] 872 break 873 } 874 } 875 876 sort.Slice(p, func(i, j int) bool { return p[i].Cycles > p[j].Cycles }) 877 878 if debug <= 0 { 879 return printCountCycleProfile(w, "contentions", "delay", scaleProfile, p) 880 } 881 882 b := bufio.NewWriter(w) 883 tw := tabwriter.NewWriter(w, 1, 8, 1, '\t', 0) 884 w = tw 885 886 fmt.Fprintf(w, "--- %v:\n", name) 887 fmt.Fprintf(w, "cycles/second=%v\n", runtime_cyclesPerSecond()) 888 if name == "mutex" { 889 fmt.Fprintf(w, "sampling period=%d\n", runtime.SetMutexProfileFraction(-1)) 890 } 891 for i := range p { 892 r := &p[i] 893 fmt.Fprintf(w, "%v %v @", r.Cycles, r.Count) 894 for _, pc := range r.Stack() { 895 fmt.Fprintf(w, " %#x", pc) 896 } 897 fmt.Fprint(w, "\n") 898 if debug > 0 { 899 printStackRecord(w, r.Stack(), true) 900 } 901 } 902 903 if tw != nil { 904 tw.Flush() 905 } 906 return b.Flush() 907 } 908 909 func scaleMutexProfile(cnt int64, ns float64) (int64, float64) { 910 period := runtime.SetMutexProfileFraction(-1) 911 return cnt * int64(period), ns * float64(period) 912 } 913 914 func runtime_cyclesPerSecond() int64