]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/cpuprof.go
all: REVERSE MERGE dev.typeparams (4d3cc84) into master
[gostls13.git] / 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         "runtime/internal/atomic"
18         "runtime/internal/sys"
19         "unsafe"
20 )
21
22 const maxCPUProfStack = 64
23
24 type cpuProfile struct {
25         lock mutex
26         on   bool     // profiling is on
27         log  *profBuf // profile events written here
28
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
33         // signal handler.
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.
40         extra      [1000]uintptr
41         numExtra   int
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
44 }
45
46 var cpuprof cpuProfile
47
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.
51 //
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.
57         if hz < 0 {
58                 hz = 0
59         }
60         if hz > 1000000 {
61                 hz = 1000000
62         }
63
64         lock(&cpuprof.lock)
65         if hz > 0 {
66                 if cpuprof.on || cpuprof.log != nil {
67                         print("runtime: cannot set cpu profile rate until previous profile has finished.\n")
68                         unlock(&cpuprof.lock)
69                         return
70                 }
71
72                 cpuprof.on = true
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 {
78                 setcpuprofilerate(0)
79                 cpuprof.on = false
80                 cpuprof.addExtra()
81                 cpuprof.log.close()
82         }
83         unlock(&cpuprof.lock)
84 }
85
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
90 // of stack.
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) {
95                 osyield()
96         }
97
98         if prof.hz != 0 { // implies cpuprof.log != nil
99                 if p.numExtra > 0 || p.lostExtra > 0 || p.lostAtomic > 0 {
100                         p.addExtra()
101                 }
102                 hdr := [1]uint64{1}
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.
107                 //
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
113                 if gp != nil {
114                         tagPtr = &gp.labels
115                 }
116                 cpuprof.log.write(tagPtr, nanotime(), hdr[:], stk)
117         }
118
119         atomic.Store(&prof.signalLock, 0)
120 }
121
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.
129 //go:nosplit
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) {
137                 osyield()
138         }
139
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)
145         } else {
146                 cpuprof.lostExtra++
147         }
148
149         atomic.Store(&prof.signalLock, 0)
150 }
151
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.
159         hdr := [1]uint64{1}
160         for i := 0; i < p.numExtra; {
161                 p.log.write(nil, 0, hdr[:], p.extra[i+1:i+int(p.extra[i])])
162                 i += int(p.extra[i])
163         }
164         p.numExtra = 0
165
166         // Report any lost events.
167         if p.lostExtra > 0 {
168                 hdr := [1]uint64{p.lostExtra}
169                 lostStk := [2]uintptr{
170                         abi.FuncPCABIInternal(_LostExternalCode) + sys.PCQuantum,
171                         abi.FuncPCABIInternal(_ExternalCode) + sys.PCQuantum,
172                 }
173                 p.log.write(nil, 0, hdr[:], lostStk[:])
174                 p.lostExtra = 0
175         }
176
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,
182                 }
183                 p.log.write(nil, 0, hdr[:], lostStk[:])
184                 p.lostAtomic = 0
185         }
186
187 }
188
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.
194 //
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")
200 }
201
202 //go:linkname runtime_pprof_runtime_cyclesPerSecond runtime/pprof.runtime_cyclesPerSecond
203 func runtime_pprof_runtime_cyclesPerSecond() int64 {
204         return tickspersecond()
205 }
206
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.
212 //
213 //go:linkname runtime_pprof_readProfile runtime/pprof.readProfile
214 func runtime_pprof_readProfile() ([]uint64, []unsafe.Pointer, bool) {
215         lock(&cpuprof.lock)
216         log := cpuprof.log
217         unlock(&cpuprof.lock)
218         data, tags, eof := log.read(profBufBlocking)
219         if len(data) == 0 && eof {
220                 lock(&cpuprof.lock)
221                 cpuprof.log = nil
222                 unlock(&cpuprof.lock)
223         }
224         return data, tags, eof
225 }