Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/cpuprof.go

Documentation: runtime

     1  // Copyright 2011 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  // CPU profiling.
     6  //
     7  // The signal handler for the profiling clock tick adds a new stack trace
     8  // to a log of recent traces. The log is read by a user goroutine that
     9  // turns it into formatted profile data. If the reader does not keep up
    10  // with the log, those writes will be recorded as a count of lost records.
    11  // The actual profile buffer is in profbuf.go.
    12  
    13  package runtime
    14  
    15  import (
    16  	"runtime/internal/atomic"
    17  	"runtime/internal/sys"
    18  	"unsafe"
    19  )
    20  
    21  const maxCPUProfStack = 64
    22  
    23  type cpuProfile struct {
    24  	lock mutex
    25  	on   bool     // profiling is on
    26  	log  *profBuf // profile events written here
    27  
    28  	// extra holds extra stacks accumulated in addNonGo
    29  	// corresponding to profiling signals arriving on
    30  	// non-Go-created threads. Those stacks are written
    31  	// to log the next time a normal Go thread gets the
    32  	// signal handler.
    33  	// Assuming the stacks are 2 words each (we don't get
    34  	// a full traceback from those threads), plus one word
    35  	// size for framing, 100 Hz profiling would generate
    36  	// 300 words per second.
    37  	// Hopefully a normal Go thread will get the profiling
    38  	// signal at least once every few seconds.
    39  	extra      [1000]uintptr
    40  	numExtra   int
    41  	lostExtra  uint64 // count of frames lost because extra is full
    42  	lostAtomic uint64 // count of frames lost because of being in atomic64 on mips/arm; updated racily
    43  }
    44  
    45  var cpuprof cpuProfile
    46  
    47  // SetCPUProfileRate sets the CPU profiling rate to hz samples per second.
    48  // If hz <= 0, SetCPUProfileRate turns off profiling.
    49  // If the profiler is on, the rate cannot be changed without first turning it off.
    50  //
    51  // Most clients should use the runtime/pprof package or
    52  // the testing package's -test.cpuprofile flag instead of calling
    53  // SetCPUProfileRate directly.
    54  func SetCPUProfileRate(hz int) {
    55  	// Clamp hz to something reasonable.
    56  	if hz < 0 {
    57  		hz = 0
    58  	}
    59  	if hz > 1000000 {
    60  		hz = 1000000
    61  	}
    62  
    63  	lock(&cpuprof.lock)
    64  	if hz > 0 {
    65  		if cpuprof.on || cpuprof.log != nil {
    66  			print("runtime: cannot set cpu profile rate until previous profile has finished.\n")
    67  			unlock(&cpuprof.lock)
    68  			return
    69  		}
    70  
    71  		cpuprof.on = true
    72  		cpuprof.log = newProfBuf(1, 1<<17, 1<<14)
    73  		hdr := [1]uint64{uint64(hz)}
    74  		cpuprof.log.write(nil, nanotime(), hdr[:], nil)
    75  		setcpuprofilerate(int32(hz))
    76  	} else if cpuprof.on {
    77  		setcpuprofilerate(0)
    78  		cpuprof.on = false
    79  		cpuprof.addExtra()
    80  		cpuprof.log.close()
    81  	}
    82  	unlock(&cpuprof.lock)
    83  }
    84  
    85  // add adds the stack trace to the profile.
    86  // It is called from signal handlers and other limited environments
    87  // and cannot allocate memory or acquire locks that might be
    88  // held at the time of the signal, nor can it use substantial amounts
    89  // of stack.
    90  //go:nowritebarrierrec
    91  func (p *cpuProfile) add(gp *g, stk []uintptr) {
    92  	// Simple cas-lock to coordinate with setcpuprofilerate.
    93  	for !atomic.Cas(&prof.signalLock, 0, 1) {
    94  		osyield()
    95  	}
    96  
    97  	if prof.hz != 0 { // implies cpuprof.log != nil
    98  		if p.numExtra > 0 || p.lostExtra > 0 || p.lostAtomic > 0 {
    99  			p.addExtra()
   100  		}
   101  		hdr := [1]uint64{1}
   102  		// Note: write "knows" that the argument is &gp.labels,
   103  		// because otherwise its write barrier behavior may not
   104  		// be correct. See the long comment there before
   105  		// changing the argument here.
   106  		//
   107  		// Note: it can happen on Windows, where we are calling
   108  		// p.add with a gp that is not the current g, that gp is nil,
   109  		// meaning we interrupted a system thread with no g.
   110  		// Avoid faulting in that case.
   111  		var tagPtr *unsafe.Pointer
   112  		if gp != nil {
   113  			tagPtr = &gp.labels
   114  		}
   115  		cpuprof.log.write(tagPtr, nanotime(), hdr[:], stk)
   116  	}
   117  
   118  	atomic.Store(&prof.signalLock, 0)
   119  }
   120  
   121  // addNonGo adds the non-Go stack trace to the profile.
   122  // It is called from a non-Go thread, so we cannot use much stack at all,
   123  // nor do anything that needs a g or an m.
   124  // In particular, we can't call cpuprof.log.write.
   125  // Instead, we copy the stack into cpuprof.extra,
   126  // which will be drained the next time a Go thread
   127  // gets the signal handling event.
   128  //go:nosplit
   129  //go:nowritebarrierrec
   130  func (p *cpuProfile) addNonGo(stk []uintptr) {
   131  	// Simple cas-lock to coordinate with SetCPUProfileRate.
   132  	// (Other calls to add or addNonGo should be blocked out
   133  	// by the fact that only one SIGPROF can be handled by the
   134  	// process at a time. If not, this lock will serialize those too.)
   135  	for !atomic.Cas(&prof.signalLock, 0, 1) {
   136  		osyield()
   137  	}
   138  
   139  	if cpuprof.numExtra+1+len(stk) < len(cpuprof.extra) {
   140  		i := cpuprof.numExtra
   141  		cpuprof.extra[i] = uintptr(1 + len(stk))
   142  		copy(cpuprof.extra[i+1:], stk)
   143  		cpuprof.numExtra += 1 + len(stk)
   144  	} else {
   145  		cpuprof.lostExtra++
   146  	}
   147  
   148  	atomic.Store(&prof.signalLock, 0)
   149  }
   150  
   151  // addExtra adds the "extra" profiling events,
   152  // queued by addNonGo, to the profile log.
   153  // addExtra is called either from a signal handler on a Go thread
   154  // or from an ordinary goroutine; either way it can use stack
   155  // and has a g. The world may be stopped, though.
   156  func (p *cpuProfile) addExtra() {
   157  	// Copy accumulated non-Go profile events.
   158  	hdr := [1]uint64{1}
   159  	for i := 0; i < p.numExtra; {
   160  		p.log.write(nil, 0, hdr[:], p.extra[i+1:i+int(p.extra[i])])
   161  		i += int(p.extra[i])
   162  	}
   163  	p.numExtra = 0
   164  
   165  	// Report any lost events.
   166  	if p.lostExtra > 0 {
   167  		hdr := [1]uint64{p.lostExtra}
   168  		lostStk := [2]uintptr{
   169  			funcPC(_LostExternalCode) + sys.PCQuantum,
   170  			funcPC(_ExternalCode) + sys.PCQuantum,
   171  		}
   172  		p.log.write(nil, 0, hdr[:], lostStk[:])
   173  		p.lostExtra = 0
   174  	}
   175  
   176  	if p.lostAtomic > 0 {
   177  		hdr := [1]uint64{p.lostAtomic}
   178  		lostStk := [2]uintptr{
   179  			funcPC(_LostSIGPROFDuringAtomic64) + sys.PCQuantum,
   180  			funcPC(_System) + sys.PCQuantum,
   181  		}
   182  		p.log.write(nil, 0, hdr[:], lostStk[:])
   183  		p.lostAtomic = 0
   184  	}
   185  
   186  }
   187  
   188  // CPUProfile panics.
   189  // It formerly provided raw access to chunks of
   190  // a pprof-format profile generated by the runtime.
   191  // The details of generating that format have changed,
   192  // so this functionality has been removed.
   193  //
   194  // Deprecated: Use the runtime/pprof package,
   195  // or the handlers in the net/http/pprof package,
   196  // or the testing package's -test.cpuprofile flag instead.
   197  func CPUProfile() []byte {
   198  	panic("CPUProfile no longer available")
   199  }
   200  
   201  //go:linkname runtime_pprof_runtime_cyclesPerSecond runtime/pprof.runtime_cyclesPerSecond
   202  func runtime_pprof_runtime_cyclesPerSecond() int64 {
   203  	return tickspersecond()
   204  }
   205  
   206  // readProfile, provided to runtime/pprof, returns the next chunk of
   207  // binary CPU profiling stack trace data, blocking until data is available.
   208  // If profiling is turned off and all the profile data accumulated while it was
   209  // on has been returned, readProfile returns eof=true.
   210  // The caller must save the returned data and tags before calling readProfile again.
   211  //
   212  //go:linkname runtime_pprof_readProfile runtime/pprof.readProfile
   213  func runtime_pprof_readProfile() ([]uint64, []unsafe.Pointer, bool) {
   214  	lock(&cpuprof.lock)
   215  	log := cpuprof.log
   216  	unlock(&cpuprof.lock)
   217  	data, tags, eof := log.read(profBufBlocking)
   218  	if len(data) == 0 && eof {
   219  		lock(&cpuprof.lock)
   220  		cpuprof.log = nil
   221  		unlock(&cpuprof.lock)
   222  	}
   223  	return data, tags, eof
   224  }
   225  

View as plain text