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.
6 // Based on algorithms and data structures used in
7 // http://code.google.com/p/google-perftools/.
9 // The main difference between this code and the google-perftools
10 // code is that this code is written to allow copying the profile data
11 // to an arbitrary io.Writer, while the google-perftools code always
12 // writes to an operating system file.
14 // The signal handler for the profiling clock tick adds a new stack trace
15 // to a hash table tracking counts for recent traces. Most clock ticks
16 // hit in the cache. In the event of a cache miss, an entry must be
17 // evicted from the hash table, copied to a log that will eventually be
18 // written as profile data. The google-perftools code flushed the
19 // log itself during the signal handler. This code cannot do that, because
20 // the io.Writer might block or need system calls or locks that are not
21 // safe to use from within the signal handler. Instead, we split the log
22 // into two halves and let the signal handler fill one half while a goroutine
23 // is writing out the other half. When the signal handler fills its half, it
24 // offers to swap with the goroutine. If the writer is not done with its half,
25 // we lose the stack trace for this clock tick (and record that loss).
26 // The goroutine interacts with the signal handler by calling getprofile() to
27 // get the next log piece to write, implicitly handing back the last log
30 // The state of this dance between the signal handler and the goroutine
31 // is encoded in the Profile.handoff field. If handoff == 0, then the goroutine
32 // is not using either log half and is waiting (or will soon be waiting) for
33 // a new piece by calling notesleep(&p->wait). If the signal handler
34 // changes handoff from 0 to non-zero, it must call notewakeup(&p->wait)
35 // to wake the goroutine. The value indicates the number of entries in the
36 // log half being handed off. The goroutine leaves the non-zero value in
37 // place until it has finished processing the log half and then flips the number
38 // back to zero. Setting the high bit in handoff means that the profiling is over,
39 // and the goroutine is now in charge of flushing the data left in the hash table
40 // to the log and returning that data.
42 // The handoff field is manipulated using atomic operations.
43 // For the most part, the manipulation of handoff is orderly: if handoff == 0
44 // then the signal handler owns it and can change it to non-zero.
45 // If handoff != 0 then the goroutine owns it and can change it to zero.
46 // If that were the end of the story then we would not need to manipulate
47 // handoff using atomic operations. The operations are needed, however,
48 // in order to let the log closer set the high bit to indicate "EOF" safely
49 // in the situation when normally the goroutine "owns" handoff.
62 type cpuprofEntry struct {
65 stack [maxCPUProfStack]uintptr
68 type cpuProfile struct {
69 on bool // profiling is on
70 wait note // goroutine waits here
71 count uintptr // tick count
72 evicts uintptr // eviction count
73 lost uintptr // lost ticks that need to be logged
75 // Active recent stack traces.
76 hash [numBuckets]struct {
77 entry [assoc]cpuprofEntry
80 // Log of traces evicted from hash.
81 // Signal handler has filled log[toggle][:nlog].
82 // Goroutine is writing log[1-toggle][:handoff].
83 log [2][logSize / 2]uintptr
89 // Writer maintains its own toggle to avoid races
90 // looking at signal handler's toggle.
92 wholding bool // holding & need to release a log half
93 flushing bool // flushing hash table - profile is over
94 eodSent bool // special end-of-data record sent; => flushing
101 eod = [3]uintptr{0, 1, 0}
104 func setcpuprofilerate_m() // proc.c
106 func setcpuprofilerate(hz int32) {
108 g.m.scalararg[0] = uintptr(hz)
109 onM(setcpuprofilerate_m)
112 // lostProfileData is a no-op function used in profiles
113 // to mark the number of profiling stack traces that were
114 // discarded due to slow data writers.
115 func lostProfileData() {}
117 // SetCPUProfileRate sets the CPU profiling rate to hz samples per second.
118 // If hz <= 0, SetCPUProfileRate turns off profiling.
119 // If the profiler is on, the rate cannot be changed without first turning it off.
121 // Most clients should use the runtime/pprof package or
122 // the testing package's -test.cpuprofile flag instead of calling
123 // SetCPUProfileRate directly.
124 func SetCPUProfileRate(hz int) {
125 // Clamp hz to something reasonable.
136 cpuprof = (*cpuProfile)(sysAlloc(unsafe.Sizeof(cpuProfile{}), &memstats.other_sys))
138 print("runtime: cpu profiling cannot allocate memory\n")
143 if cpuprof.on || cpuprof.handoff != 0 {
144 print("runtime: cannot set cpu profile rate until previous profile has finished.\n")
150 // pprof binary header format.
151 // http://code.google.com/p/google-perftools/source/browse/trunk/src/profiledata.cc#117
153 p[0] = 0 // count for header
154 p[1] = 3 // depth for header
155 p[2] = 0 // version number
156 p[3] = uintptr(1e6 / hz) // period (microseconds)
160 cpuprof.wholding = false
162 cpuprof.flushing = false
163 cpuprof.eodSent = false
164 noteclear(&cpuprof.wait)
166 setcpuprofilerate(int32(hz))
167 } else if cpuprof != nil && cpuprof.on {
171 // Now add is not running anymore, and getprofile owns the entire log.
172 // Set the high bit in prof->handoff to tell getprofile.
175 if n&0x80000000 != 0 {
176 print("runtime: setcpuprofile(off) twice\n")
178 if cas(&cpuprof.handoff, n, n|0x80000000) {
180 // we did the transition from 0 -> nonzero so we wake getprofile
181 notewakeup(&cpuprof.wait)
190 func cpuproftick(pc *uintptr, n int32) {
191 if n > maxCPUProfStack {
194 s := (*[maxCPUProfStack]uintptr)(unsafe.Pointer(pc))[:n]
198 // add adds the stack trace to the profile.
199 // It is called from signal handlers and other limited environments
200 // and cannot allocate memory or acquire locks that might be
201 // held at the time of the signal, nor can it use substantial amounts
202 // of stack. It is allowed to call evict.
203 func (p *cpuProfile) add(pc []uintptr) {
206 for _, x := range pc {
207 h = h<<8 | (h >> (8 * (unsafe.Sizeof(h) - 1)))
208 h += x*31 + x*7 + x*3
212 // Add to entry count if already present in table.
213 b := &p.hash[h%numBuckets]
215 for i := range b.entry {
217 if e.depth != uintptr(len(pc)) {
221 if e.stack[j] != pc[j] {
229 // Evict entry with smallest count.
231 for i := range b.entry {
232 if e == nil || b.entry[i].count < e.count {
238 // Could not evict entry. Record lost stack.
245 // Reuse the newly evicted entry.
246 e.depth = uintptr(len(pc))
251 // evict copies the given entry's data into the log, so that
252 // the entry can be reused. evict is called from add, which
253 // is called from the profiling signal handler, so it must not
254 // allocate memory or block. It is safe to call flushlog.
255 // evict returns true if the entry was copied to the log,
256 // false if there was no room available.
257 func (p *cpuProfile) evict(e *cpuprofEntry) bool {
260 log := &p.log[p.toggle]
261 if p.nlog+nslot > uintptr(len(p.log[0])) {
265 log = &p.log[p.toggle]
273 copy(log[q:], e.stack[:d])
280 // flushlog tries to flush the current log and switch to the other one.
281 // flushlog is called from evict, called from add, called from the signal handler,
282 // so it cannot allocate memory or block. It can try to swap logs with
283 // the writing goroutine, as explained in the comment at the top of this file.
284 func (p *cpuProfile) flushlog() bool {
285 if !cas(&p.handoff, 0, uint32(p.nlog)) {
290 p.toggle = 1 - p.toggle
291 log := &p.log[p.toggle]
294 lostPC := funcPC(lostProfileData)
305 // getprofile blocks until the next block of profiling data is available
306 // and returns it as a []byte. It is called from the writing goroutine.
307 func (p *cpuProfile) getprofile() []byte {
313 // Release previous log to signal handling side.
314 // Loop because we are racing against SetCPUProfileRate(0).
318 print("runtime: phase error during cpu profile handoff\n")
321 if n&0x80000000 != 0 {
322 p.wtoggle = 1 - p.wtoggle
327 if cas(&p.handoff, n, 0) {
331 p.wtoggle = 1 - p.wtoggle
339 if !p.on && p.handoff == 0 {
344 notetsleepg(&p.wait, -1)
347 switch n := p.handoff; {
349 print("runtime: phase error during cpu profile wait\n")
351 case n == 0x80000000:
357 // Return new log to caller.
360 return uintptrBytes(p.log[p.wtoggle][:n])
364 // Add is no longer being called. We own the log.
365 // Also, p->handoff is non-zero, so flushlog will return false.
366 // Evict the hash table into the log and return it.
368 for i := range p.hash {
370 for j := range b.entry {
372 if e.count > 0 && !p.evict(e) {
373 // Filled the log. Stop the loop and return what we've got.
379 // Return pending log data.
381 // Note that we're using toggle now, not wtoggle,
382 // because we're working on the log directly.
385 return uintptrBytes(p.log[p.toggle][:n])
388 // Made it through the table without finding anything to log.
390 // We may not have space to append this to the partial log buf,
391 // so we always return a new slice for the end-of-data marker.
393 return uintptrBytes(eod[:])
396 // Finally done. Clean up and return nil.
398 if !cas(&p.handoff, p.handoff, 0) {
399 print("runtime: profile flush racing with something\n")
404 func uintptrBytes(p []uintptr) (ret []byte) {
405 pp := (*sliceStruct)(unsafe.Pointer(&p))
406 rp := (*sliceStruct)(unsafe.Pointer(&ret))
409 rp.len = pp.len * int(unsafe.Sizeof(p[0]))
415 // CPUProfile returns the next chunk of binary CPU profiling stack trace data,
416 // blocking until data is available. If profiling is turned off and all the profile
417 // data accumulated while it was on has been returned, CPUProfile returns nil.
418 // The caller must save the returned data before calling CPUProfile again.
420 // Most clients should use the runtime/pprof package or
421 // the testing package's -test.cpuprofile flag instead of calling
422 // CPUProfile directly.
423 func CPUProfile() []byte {
424 return cpuprof.getprofile()