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.
16 "runtime/internal/atomic"
17 "runtime/internal/sys"
21 const maxCPUProfStack
= 64
23 type cpuProfile
struct {
25 on
bool // profiling is on
26 log
*profBuf
// profile events written here
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
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.
41 lostExtra
uint64 // count of frames lost because extra is full
44 var cpuprof cpuProfile
46 // SetCPUProfileRate sets the CPU profiling rate to hz samples per second.
47 // If hz <= 0, SetCPUProfileRate turns off profiling.
48 // If the profiler is on, the rate cannot be changed without first turning it off.
50 // Most clients should use the runtime/pprof package or
51 // the testing package's -test.cpuprofile flag instead of calling
52 // SetCPUProfileRate directly.
53 func SetCPUProfileRate(hz
int) {
54 // Clamp hz to something reasonable.
64 if cpuprof
.on || cpuprof
.log
!= nil {
65 print("runtime: cannot set cpu profile rate until previous profile has finished.\n")
71 cpuprof
.log
= newProfBuf(1, 1<<17, 1<<14)
72 hdr
:= [1]uint64{uint64(hz
)}
73 cpuprof
.log
.write(nil, nanotime(), hdr
[:], nil)
74 setcpuprofilerate(int32(hz
))
75 } else if cpuprof
.on
{
84 // add adds the stack trace to the profile.
85 // It is called from signal handlers and other limited environments
86 // and cannot allocate memory or acquire locks that might be
87 // held at the time of the signal, nor can it use substantial amounts
89 //go:nowritebarrierrec
90 func (p
*cpuProfile
) add(gp
*g
, stk
[]uintptr) {
91 // Simple cas-lock to coordinate with setcpuprofilerate.
92 for !atomic
.Cas(&prof
.signalLock
, 0, 1) {
96 if prof
.hz
!= 0 { // implies cpuprof.log != nil
97 if p
.numExtra
> 0 || p
.lostExtra
> 0 {
101 // Note: write "knows" that the argument is &gp.labels,
102 // because otherwise its write barrier behavior may not
103 // be correct. See the long comment there before
104 // changing the argument here.
105 cpuprof
.log
.write(&gp
.labels
, nanotime(), hdr
[:], stk
)
108 atomic
.Store(&prof
.signalLock
, 0)
111 // addNonGo adds the non-Go stack trace to the profile.
112 // It is called from a non-Go thread, so we cannot use much stack at all,
113 // nor do anything that needs a g or an m.
114 // In particular, we can't call cpuprof.log.write.
115 // Instead, we copy the stack into cpuprof.extra,
116 // which will be drained the next time a Go thread
117 // gets the signal handling event.
119 //go:nowritebarrierrec
120 func (p
*cpuProfile
) addNonGo(stk
[]uintptr) {
121 // Simple cas-lock to coordinate with SetCPUProfileRate.
122 // (Other calls to add or addNonGo should be blocked out
123 // by the fact that only one SIGPROF can be handled by the
124 // process at a time. If not, this lock will serialize those too.)
125 for !atomic
.Cas(&prof
.signalLock
, 0, 1) {
129 if cpuprof
.numExtra
+1+len(stk
) < len(cpuprof
.extra
) {
130 i
:= cpuprof
.numExtra
131 cpuprof
.extra
[i
] = uintptr(1 + len(stk
))
132 copy(cpuprof
.extra
[i
+1:], stk
)
133 cpuprof
.numExtra
+= 1 + len(stk
)
138 atomic
.Store(&prof
.signalLock
, 0)
141 // addExtra adds the "extra" profiling events,
142 // queued by addNonGo, to the profile log.
143 // addExtra is called either from a signal handler on a Go thread
144 // or from an ordinary goroutine; either way it can use stack
145 // and has a g. The world may be stopped, though.
146 func (p
*cpuProfile
) addExtra() {
147 // Copy accumulated non-Go profile events.
149 for i
:= 0; i
< p
.numExtra
; {
150 p
.log
.write(nil, 0, hdr
[:], p
.extra
[i
+1:i
+int(p
.extra
[i
])])
155 // Report any lost events.
157 hdr
:= [1]uint64{p
.lostExtra
}
158 lostStk
:= [2]uintptr{
159 funcPC(_LostExternalCode
) + sys
.PCQuantum
,
160 funcPC(_ExternalCode
) + sys
.PCQuantum
,
162 cpuprof
.log
.write(nil, 0, hdr
[:], lostStk
[:])
166 func (p
*cpuProfile
) addLostAtomic64(count
uint64) {
167 hdr
:= [1]uint64{count
}
168 lostStk
:= [2]uintptr{
169 funcPC(_LostSIGPROFDuringAtomic64
) + sys
.PCQuantum
,
170 funcPC(_System
) + sys
.PCQuantum
,
172 cpuprof
.log
.write(nil, 0, hdr
[:], lostStk
[:])
175 // CPUProfile panics.
176 // It formerly provided raw access to chunks of
177 // a pprof-format profile generated by the runtime.
178 // The details of generating that format have changed,
179 // so this functionality has been removed.
181 // Deprecated: use the runtime/pprof package,
182 // or the handlers in the net/http/pprof package,
183 // or the testing package's -test.cpuprofile flag instead.
184 func CPUProfile() []byte {
185 panic("CPUProfile no longer available")
188 //go:linkname runtime_pprof_runtime_cyclesPerSecond runtime_pprof.runtime_cyclesPerSecond
189 func runtime_pprof_runtime_cyclesPerSecond() int64 {
190 return tickspersecond()
193 // readProfile, provided to runtime/pprof, returns the next chunk of
194 // binary CPU profiling stack trace data, blocking until data is available.
195 // If profiling is turned off and all the profile data accumulated while it was
196 // on has been returned, readProfile returns eof=true.
197 // The caller must save the returned data and tags before calling readProfile again.
199 //go:linkname runtime_pprof_readProfile runtime_pprof.readProfile
200 func runtime_pprof_readProfile() ([]uint64, []unsafe
.Pointer
, bool) {
203 unlock(&cpuprof
.lock
)
204 data
, tags
, eof
:= log
.read(profBufBlocking
)
205 if len(data
) == 0 && eof
{
208 unlock(&cpuprof
.lock
)
210 return data
, tags
, eof