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.
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.
17 "runtime/internal/atomic"
18 "runtime/internal/sys"
25 // profBufWordCount is the size of the CPU profile buffer's storage for the
26 // header and stack of each sample, measured in 64-bit words. Every sample
27 // has a required header of two words. With a small additional header (a
28 // word or two) and stacks at the profiler's maximum length of 64 frames,
29 // that capacity can support 1900 samples or 19 thread-seconds at a 100 Hz
30 // sample rate, at a cost of 1 MiB.
31 profBufWordCount = 1 << 17
32 // profBufTagCount is the size of the CPU profile buffer's storage for the
33 // goroutine tags associated with each sample. A capacity of 1<<14 means
34 // room for 16k samples, or 160 thread-seconds at a 100 Hz sample rate.
35 profBufTagCount = 1 << 14
38 type cpuProfile struct {
40 on bool // profiling is on
41 log *profBuf // profile events written here
43 // extra holds extra stacks accumulated in addNonGo
44 // corresponding to profiling signals arriving on
45 // non-Go-created threads. Those stacks are written
46 // to log the next time a normal Go thread gets the
48 // Assuming the stacks are 2 words each (we don't get
49 // a full traceback from those threads), plus one word
50 // size for framing, 100 Hz profiling would generate
51 // 300 words per second.
52 // Hopefully a normal Go thread will get the profiling
53 // signal at least once every few seconds.
56 lostExtra uint64 // count of frames lost because extra is full
57 lostAtomic uint64 // count of frames lost because of being in atomic64 on mips/arm; updated racily
60 var cpuprof cpuProfile
62 // SetCPUProfileRate sets the CPU profiling rate to hz samples per second.
63 // If hz <= 0, SetCPUProfileRate turns off profiling.
64 // If the profiler is on, the rate cannot be changed without first turning it off.
66 // Most clients should use the runtime/pprof package or
67 // the testing package's -test.cpuprofile flag instead of calling
68 // SetCPUProfileRate directly.
69 func SetCPUProfileRate(hz int) {
70 // Clamp hz to something reasonable.
80 if cpuprof.on || cpuprof.log != nil {
81 print("runtime: cannot set cpu profile rate until previous profile has finished.\n")
87 cpuprof.log = newProfBuf(1, profBufWordCount, profBufTagCount)
88 hdr := [1]uint64{uint64(hz)}
89 cpuprof.log.write(nil, nanotime(), hdr[:], nil)
90 setcpuprofilerate(int32(hz))
91 } else if cpuprof.on {
100 // add adds the stack trace to the profile.
101 // It is called from signal handlers and other limited environments
102 // and cannot allocate memory or acquire locks that might be
103 // held at the time of the signal, nor can it use substantial amounts
106 //go:nowritebarrierrec
107 func (p *cpuProfile) add(tagPtr *unsafe.Pointer, stk []uintptr) {
108 // Simple cas-lock to coordinate with setcpuprofilerate.
109 for !atomic.Cas(&prof.signalLock, 0, 1) {
110 // TODO: Is it safe to osyield here? https://go.dev/issue/52672
114 if prof.hz != 0 { // implies cpuprof.log != nil
115 if p.numExtra > 0 || p.lostExtra > 0 || p.lostAtomic > 0 {
119 // Note: write "knows" that the argument is &gp.labels,
120 // because otherwise its write barrier behavior may not
121 // be correct. See the long comment there before
122 // changing the argument here.
123 cpuprof.log.write(tagPtr, nanotime(), hdr[:], stk)
126 atomic.Store(&prof.signalLock, 0)
129 // addNonGo adds the non-Go stack trace to the profile.
130 // It is called from a non-Go thread, so we cannot use much stack at all,
131 // nor do anything that needs a g or an m.
132 // In particular, we can't call cpuprof.log.write.
133 // Instead, we copy the stack into cpuprof.extra,
134 // which will be drained the next time a Go thread
135 // gets the signal handling event.
138 //go:nowritebarrierrec
139 func (p *cpuProfile) addNonGo(stk []uintptr) {
140 // Simple cas-lock to coordinate with SetCPUProfileRate.
141 // (Other calls to add or addNonGo should be blocked out
142 // by the fact that only one SIGPROF can be handled by the
143 // process at a time. If not, this lock will serialize those too.
144 // The use of timer_create(2) on Linux to request process-targeted
145 // signals may have changed this.)
146 for !atomic.Cas(&prof.signalLock, 0, 1) {
147 // TODO: Is it safe to osyield here? https://go.dev/issue/52672
151 if cpuprof.numExtra+1+len(stk) < len(cpuprof.extra) {
152 i := cpuprof.numExtra
153 cpuprof.extra[i] = uintptr(1 + len(stk))
154 copy(cpuprof.extra[i+1:], stk)
155 cpuprof.numExtra += 1 + len(stk)
160 atomic.Store(&prof.signalLock, 0)
163 // addExtra adds the "extra" profiling events,
164 // queued by addNonGo, to the profile log.
165 // addExtra is called either from a signal handler on a Go thread
166 // or from an ordinary goroutine; either way it can use stack
167 // and has a g. The world may be stopped, though.
168 func (p *cpuProfile) addExtra() {
169 // Copy accumulated non-Go profile events.
171 for i := 0; i < p.numExtra; {
172 p.log.write(nil, 0, hdr[:], p.extra[i+1:i+int(p.extra[i])])
177 // Report any lost events.
179 hdr := [1]uint64{p.lostExtra}
180 lostStk := [2]uintptr{
181 abi.FuncPCABIInternal(_LostExternalCode) + sys.PCQuantum,
182 abi.FuncPCABIInternal(_ExternalCode) + sys.PCQuantum,
184 p.log.write(nil, 0, hdr[:], lostStk[:])
188 if p.lostAtomic > 0 {
189 hdr := [1]uint64{p.lostAtomic}
190 lostStk := [2]uintptr{
191 abi.FuncPCABIInternal(_LostSIGPROFDuringAtomic64) + sys.PCQuantum,
192 abi.FuncPCABIInternal(_System) + sys.PCQuantum,
194 p.log.write(nil, 0, hdr[:], lostStk[:])
200 // CPUProfile panics.
201 // It formerly provided raw access to chunks of
202 // a pprof-format profile generated by the runtime.
203 // The details of generating that format have changed,
204 // so this functionality has been removed.
206 // Deprecated: Use the runtime/pprof package,
207 // or the handlers in the net/http/pprof package,
208 // or the testing package's -test.cpuprofile flag instead.
209 func CPUProfile() []byte {
210 panic("CPUProfile no longer available")
213 //go:linkname runtime_pprof_runtime_cyclesPerSecond runtime/pprof.runtime_cyclesPerSecond
214 func runtime_pprof_runtime_cyclesPerSecond() int64 {
215 return tickspersecond()
218 // readProfile, provided to runtime/pprof, returns the next chunk of
219 // binary CPU profiling stack trace data, blocking until data is available.
220 // If profiling is turned off and all the profile data accumulated while it was
221 // on has been returned, readProfile returns eof=true.
222 // The caller must save the returned data and tags before calling readProfile again.
223 // The returned data contains a whole number of records, and tags contains
224 // exactly one entry per record.
226 //go:linkname runtime_pprof_readProfile runtime/pprof.readProfile
227 func runtime_pprof_readProfile() ([]uint64, []unsafe.Pointer, bool) {
230 unlock(&cpuprof.lock)
231 data, tags, eof := log.read(profBufBlocking)
232 if len(data) == 0 && eof {
235 unlock(&cpuprof.lock)
237 return data, tags, eof