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"
22 const maxCPUProfStack = 64
24 type cpuProfile struct {
26 on bool // profiling is on
27 log *profBuf // profile events written here
29 // extra holds extra stacks accumulated in addNonGo
30 // corresponding to profiling signals arriving on
31 // non-Go-created threads. Those stacks are written
32 // to log the next time a normal Go thread gets the
34 // Assuming the stacks are 2 words each (we don't get
35 // a full traceback from those threads), plus one word
36 // size for framing, 100 Hz profiling would generate
37 // 300 words per second.
38 // Hopefully a normal Go thread will get the profiling
39 // signal at least once every few seconds.
42 lostExtra uint64 // count of frames lost because extra is full
43 lostAtomic uint64 // count of frames lost because of being in atomic64 on mips/arm; updated racily
46 var cpuprof cpuProfile
48 // SetCPUProfileRate sets the CPU profiling rate to hz samples per second.
49 // If hz <= 0, SetCPUProfileRate turns off profiling.
50 // If the profiler is on, the rate cannot be changed without first turning it off.
52 // Most clients should use the runtime/pprof package or
53 // the testing package's -test.cpuprofile flag instead of calling
54 // SetCPUProfileRate directly.
55 func SetCPUProfileRate(hz int) {
56 // Clamp hz to something reasonable.
66 if cpuprof.on || cpuprof.log != nil {
67 print("runtime: cannot set cpu profile rate until previous profile has finished.\n")
73 cpuprof.log = newProfBuf(1, 1<<17, 1<<14)
74 hdr := [1]uint64{uint64(hz)}
75 cpuprof.log.write(nil, nanotime(), hdr[:], nil)
76 setcpuprofilerate(int32(hz))
77 } else if cpuprof.on {
86 // add adds the stack trace to the profile.
87 // It is called from signal handlers and other limited environments
88 // and cannot allocate memory or acquire locks that might be
89 // held at the time of the signal, nor can it use substantial amounts
91 //go:nowritebarrierrec
92 func (p *cpuProfile) add(gp *g, stk []uintptr) {
93 // Simple cas-lock to coordinate with setcpuprofilerate.
94 for !atomic.Cas(&prof.signalLock, 0, 1) {
98 if prof.hz != 0 { // implies cpuprof.log != nil
99 if p.numExtra > 0 || p.lostExtra > 0 || p.lostAtomic > 0 {
103 // Note: write "knows" that the argument is &gp.labels,
104 // because otherwise its write barrier behavior may not
105 // be correct. See the long comment there before
106 // changing the argument here.
108 // Note: it can happen on Windows, where we are calling
109 // p.add with a gp that is not the current g, that gp is nil,
110 // meaning we interrupted a system thread with no g.
111 // Avoid faulting in that case.
112 var tagPtr *unsafe.Pointer
116 cpuprof.log.write(tagPtr, nanotime(), hdr[:], stk)
119 atomic.Store(&prof.signalLock, 0)
122 // addNonGo adds the non-Go stack trace to the profile.
123 // It is called from a non-Go thread, so we cannot use much stack at all,
124 // nor do anything that needs a g or an m.
125 // In particular, we can't call cpuprof.log.write.
126 // Instead, we copy the stack into cpuprof.extra,
127 // which will be drained the next time a Go thread
128 // gets the signal handling event.
130 //go:nowritebarrierrec
131 func (p *cpuProfile) addNonGo(stk []uintptr) {
132 // Simple cas-lock to coordinate with SetCPUProfileRate.
133 // (Other calls to add or addNonGo should be blocked out
134 // by the fact that only one SIGPROF can be handled by the
135 // process at a time. If not, this lock will serialize those too.)
136 for !atomic.Cas(&prof.signalLock, 0, 1) {
140 if cpuprof.numExtra+1+len(stk) < len(cpuprof.extra) {
141 i := cpuprof.numExtra
142 cpuprof.extra[i] = uintptr(1 + len(stk))
143 copy(cpuprof.extra[i+1:], stk)
144 cpuprof.numExtra += 1 + len(stk)
149 atomic.Store(&prof.signalLock, 0)
152 // addExtra adds the "extra" profiling events,
153 // queued by addNonGo, to the profile log.
154 // addExtra is called either from a signal handler on a Go thread
155 // or from an ordinary goroutine; either way it can use stack
156 // and has a g. The world may be stopped, though.
157 func (p *cpuProfile) addExtra() {
158 // Copy accumulated non-Go profile events.
160 for i := 0; i < p.numExtra; {
161 p.log.write(nil, 0, hdr[:], p.extra[i+1:i+int(p.extra[i])])
166 // Report any lost events.
168 hdr := [1]uint64{p.lostExtra}
169 lostStk := [2]uintptr{
170 abi.FuncPCABIInternal(_LostExternalCode) + sys.PCQuantum,
171 abi.FuncPCABIInternal(_ExternalCode) + sys.PCQuantum,
173 p.log.write(nil, 0, hdr[:], lostStk[:])
177 if p.lostAtomic > 0 {
178 hdr := [1]uint64{p.lostAtomic}
179 lostStk := [2]uintptr{
180 abi.FuncPCABIInternal(_LostSIGPROFDuringAtomic64) + sys.PCQuantum,
181 abi.FuncPCABIInternal(_System) + sys.PCQuantum,
183 p.log.write(nil, 0, hdr[:], lostStk[:])
189 // CPUProfile panics.
190 // It formerly provided raw access to chunks of
191 // a pprof-format profile generated by the runtime.
192 // The details of generating that format have changed,
193 // so this functionality has been removed.
195 // Deprecated: Use the runtime/pprof package,
196 // or the handlers in the net/http/pprof package,
197 // or the testing package's -test.cpuprofile flag instead.
198 func CPUProfile() []byte {
199 panic("CPUProfile no longer available")
202 //go:linkname runtime_pprof_runtime_cyclesPerSecond runtime/pprof.runtime_cyclesPerSecond
203 func runtime_pprof_runtime_cyclesPerSecond() int64 {
204 return tickspersecond()
207 // readProfile, provided to runtime/pprof, returns the next chunk of
208 // binary CPU profiling stack trace data, blocking until data is available.
209 // If profiling is turned off and all the profile data accumulated while it was
210 // on has been returned, readProfile returns eof=true.
211 // The caller must save the returned data and tags before calling readProfile again.
213 //go:linkname runtime_pprof_readProfile runtime/pprof.readProfile
214 func runtime_pprof_readProfile() ([]uint64, []unsafe.Pointer, bool) {
217 unlock(&cpuprof.lock)
218 data, tags, eof := log.read(profBufBlocking)
219 if len(data) == 0 && eof {
222 unlock(&cpuprof.lock)
224 return data, tags, eof