...
Run Format

Source file src/runtime/pprof/pprof.go

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

View as plain text