Source file src/runtime/cpuprof.go
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 "internal/abi" 17 "internal/runtime/sys" 18 "unsafe" 19 ) 20 21 const ( 22 maxCPUProfStack = 64 23 24 // profBufWordCount is the size of the CPU profile buffer's storage for the 25 // header and stack of each sample, measured in 64-bit words. Every sample 26 // has a required header of two words. With a small additional header (a 27 // word or two) and stacks at the profiler's maximum length of 64 frames, 28 // that capacity can support 1900 samples or 19 thread-seconds at a 100 Hz 29 // sample rate, at a cost of 1 MiB. 30 profBufWordCount = 1 << 17 31 // profBufTagCount is the size of the CPU profile buffer's storage for the 32 // goroutine tags associated with each sample. A capacity of 1<<14 means 33 // room for 16k samples, or 160 thread-seconds at a 100 Hz sample rate. 34 profBufTagCount = 1 << 14 35 ) 36 37 type cpuProfile struct { 38 lock mutex 39 on bool // profiling is on 40 log *profBuf // profile events written here 41 42 // extra holds extra stacks accumulated in addNonGo 43 // corresponding to profiling signals arriving on 44 // non-Go-created threads. Those stacks are written 45 // to log the next time a normal Go thread gets the 46 // signal handler. 47 // Assuming the stacks are 2 words each (we don't get 48 // a full traceback from those threads), plus one word 49 // size for framing, 100 Hz profiling would generate 50 // 300 words per second. 51 // Hopefully a normal Go thread will get the profiling 52 // signal at least once every few seconds. 53 extra [1000]uintptr 54 numExtra int 55 lostExtra uint64 // count of frames lost because extra is full 56 lostAtomic uint64 // count of frames lost because of being in atomic64 on mips/arm; updated racily 57 } 58 59 var cpuprof cpuProfile 60 61 // SetCPUProfileRate sets the CPU profiling rate to hz samples per second. 62 // If hz <= 0, SetCPUProfileRate turns off profiling. 63 // If the profiler is on, the rate cannot be changed without first turning it off. 64 // 65 // Most clients should use the [runtime/pprof] package or 66 // the [testing] package's -test.cpuprofile flag instead of calling 67 // SetCPUProfileRate directly. 68 func SetCPUProfileRate(hz int) { 69 setCPUProfileRate(hz, true) 70 } 71 72 // setCPUProfileRate sets the CPU profiling rate to hz. Setting a non-zero rate 73 // when the rate is already non-zero is a no-op. An error is printed in this case 74 // if warn is true. 75 func setCPUProfileRate(hz int, warn bool) { 76 // Clamp hz to something reasonable. 77 if hz < 0 { 78 hz = 0 79 } 80 if hz > 1000000 { 81 hz = 1000000 82 } 83 84 lock(&cpuprof.lock) 85 if hz > 0 { 86 if cpuprof.on || cpuprof.log != nil { 87 if warn { 88 print("runtime: cannot set cpu profile rate until previous profile has finished.\n") 89 } 90 unlock(&cpuprof.lock) 91 return 92 } 93 94 cpuprof.on = true 95 cpuprof.log = newProfBuf(1, profBufWordCount, profBufTagCount) 96 hdr := [1]uint64{uint64(hz)} 97 cpuprof.log.write(nil, nanotime(), hdr[:], nil) 98 setcpuprofilerate(int32(hz)) 99 } else if cpuprof.on { 100 setcpuprofilerate(0) 101 cpuprof.on = false 102 cpuprof.addExtra() 103 cpuprof.log.close() 104 } 105 unlock(&cpuprof.lock) 106 } 107 108 // add adds the stack trace to the profile. 109 // It is called from signal handlers and other limited environments 110 // and cannot allocate memory or acquire locks that might be 111 // held at the time of the signal, nor can it use substantial amounts 112 // of stack. 113 // 114 //go:nowritebarrierrec 115 func (p *cpuProfile) add(tagPtr *unsafe.Pointer, stk []uintptr) { 116 // Simple cas-lock to coordinate with setcpuprofilerate. 117 for !prof.signalLock.CompareAndSwap(0, 1) { 118 // TODO: Is it safe to osyield here? https://go.dev/issue/52672 119 osyield() 120 } 121 122 if prof.hz.Load() != 0 { // implies cpuprof.log != nil 123 if p.numExtra > 0 || p.lostExtra > 0 || p.lostAtomic > 0 { 124 p.addExtra() 125 } 126 hdr := [1]uint64{1} 127 // Note: write "knows" that the argument is &gp.labels, 128 // because otherwise its write barrier behavior may not 129 // be correct. See the long comment there before 130 // changing the argument here. 131 cpuprof.log.write(tagPtr, nanotime(), hdr[:], stk) 132 } 133 134 prof.signalLock.Store(0) 135 } 136 137 // addNonGo adds the non-Go stack trace to the profile. 138 // It is called from a non-Go thread, so we cannot use much stack at all, 139 // nor do anything that needs a g or an m. 140 // In particular, we can't call cpuprof.log.write. 141 // Instead, we copy the stack into cpuprof.extra, 142 // which will be drained the next time a Go thread 143 // gets the signal handling event. 144 // 145 //go:nosplit 146 //go:nowritebarrierrec 147 func (p *cpuProfile) addNonGo(stk []uintptr) { 148 // Simple cas-lock to coordinate with SetCPUProfileRate. 149 // (Other calls to add or addNonGo should be blocked out 150 // by the fact that only one SIGPROF can be handled by the 151 // process at a time. If not, this lock will serialize those too. 152 // The use of timer_create(2) on Linux to request process-targeted 153 // signals may have changed this.) 154 for !prof.signalLock.CompareAndSwap(0, 1) { 155 // TODO: Is it safe to osyield here? https://go.dev/issue/52672 156 osyield() 157 } 158 159 if cpuprof.numExtra+1+len(stk) < len(cpuprof.extra) { 160 i := cpuprof.numExtra 161 cpuprof.extra[i] = uintptr(1 + len(stk)) 162 copy(cpuprof.extra[i+1:], stk) 163 cpuprof.numExtra += 1 + len(stk) 164 } else { 165 cpuprof.lostExtra++ 166 } 167 168 prof.signalLock.Store(0) 169 } 170 171 // addExtra adds the "extra" profiling events, 172 // queued by addNonGo, to the profile log. 173 // addExtra is called either from a signal handler on a Go thread 174 // or from an ordinary goroutine; either way it can use stack 175 // and has a g. The world may be stopped, though. 176 func (p *cpuProfile) addExtra() { 177 // Copy accumulated non-Go profile events. 178 hdr := [1]uint64{1} 179 for i := 0; i < p.numExtra; { 180 p.log.write(nil, 0, hdr[:], p.extra[i+1:i+int(p.extra[i])]) 181 i += int(p.extra[i]) 182 } 183 p.numExtra = 0 184 185 // Report any lost events. 186 if p.lostExtra > 0 { 187 hdr := [1]uint64{p.lostExtra} 188 lostStk := [2]uintptr{ 189 abi.FuncPCABIInternal(_LostExternalCode) + sys.PCQuantum, 190 abi.FuncPCABIInternal(_ExternalCode) + sys.PCQuantum, 191 } 192 p.log.write(nil, 0, hdr[:], lostStk[:]) 193 p.lostExtra = 0 194 } 195 196 if p.lostAtomic > 0 { 197 hdr := [1]uint64{p.lostAtomic} 198 lostStk := [2]uintptr{ 199 abi.FuncPCABIInternal(_LostSIGPROFDuringAtomic64) + sys.PCQuantum, 200 abi.FuncPCABIInternal(_System) + sys.PCQuantum, 201 } 202 p.log.write(nil, 0, hdr[:], lostStk[:]) 203 p.lostAtomic = 0 204 } 205 206 } 207 208 // CPUProfile panics. 209 // It formerly provided raw access to chunks of 210 // a pprof-format profile generated by the runtime. 211 // The details of generating that format have changed, 212 // so this functionality has been removed. 213 // 214 // Deprecated: Use the [runtime/pprof] package, 215 // or the handlers in the [net/http/pprof] package, 216 // or the [testing] package's -test.cpuprofile flag instead. 217 func CPUProfile() []byte { 218 panic("CPUProfile no longer available") 219 } 220 221 // pprof_setCPUProfileRate is provided to runtime/pprof.StartCPUProfile, 222 // to enable CPU profiling without logging an error if the user has already 223 // configured the profiling rate. 224 // 225 //go:linkname pprof_setCPUProfileRate 226 func pprof_setCPUProfileRate(hz int) { 227 setCPUProfileRate(hz, false) 228 } 229 230 // runtime/pprof.runtime_cyclesPerSecond should be an internal detail, 231 // but widely used packages access it using linkname. 232 // Notable members of the hall of shame include: 233 // - github.com/grafana/pyroscope-go/godeltaprof 234 // - github.com/pyroscope-io/godeltaprof 235 // 236 // Do not remove or change the type signature. 237 // See go.dev/issue/67401. 238 // 239 //go:linkname pprof_cyclesPerSecond runtime/pprof.runtime_cyclesPerSecond 240 func pprof_cyclesPerSecond() int64 { 241 return ticksPerSecond() 242 } 243 244 // readProfile, provided to runtime/pprof, returns the next chunk of 245 // binary CPU profiling stack trace data, blocking until data is available. 246 // If profiling is turned off and all the profile data accumulated while it was 247 // on has been returned, readProfile returns eof=true. 248 // The caller must save the returned data and tags before calling readProfile again. 249 // The returned data contains a whole number of records, and tags contains 250 // exactly one entry per record. 251 // 252 // runtime_pprof_readProfile should be an internal detail, 253 // but widely used packages access it using linkname. 254 // Notable members of the hall of shame include: 255 // - github.com/pyroscope-io/pyroscope 256 // 257 // Do not remove or change the type signature. 258 // See go.dev/issue/67401. 259 // 260 //go:linkname runtime_pprof_readProfile runtime/pprof.readProfile 261 func runtime_pprof_readProfile() ([]uint64, []unsafe.Pointer, bool) { 262 lock(&cpuprof.lock) 263 log := cpuprof.log 264 unlock(&cpuprof.lock) 265 readMode := profBufBlocking 266 if GOOS == "darwin" || GOOS == "ios" { 267 readMode = profBufNonBlocking // For #61768; on Darwin notes are not async-signal-safe. See sigNoteSetup in os_darwin.go. 268 } 269 data, tags, eof := log.read(readMode) 270 if len(data) == 0 && eof { 271 lock(&cpuprof.lock) 272 cpuprof.log = nil 273 unlock(&cpuprof.lock) 274 } 275 return data, tags, eof 276 } 277