...
Run Format

Source file src/runtime/pprof/pprof.go

Documentation: runtime/pprof

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

View as plain text