]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/runtime/cpuprof.go
runtime: improve tickspersecond
[gostls13.git] / src / runtime / cpuprof.go
index 2f7f6b4153eb15aa4ed3d8362d5ff7c4d944740f..331484b1ff2640b64336b321d4ab7a19fc926f5f 100644 (file)
@@ -14,7 +14,6 @@ package runtime
 
 import (
        "internal/abi"
-       "runtime/internal/atomic"
        "runtime/internal/sys"
        "unsafe"
 )
@@ -106,12 +105,12 @@ func SetCPUProfileRate(hz int) {
 //go:nowritebarrierrec
 func (p *cpuProfile) add(tagPtr *unsafe.Pointer, stk []uintptr) {
        // Simple cas-lock to coordinate with setcpuprofilerate.
-       for !atomic.Cas(&prof.signalLock, 0, 1) {
+       for !prof.signalLock.CompareAndSwap(0, 1) {
                // TODO: Is it safe to osyield here? https://go.dev/issue/52672
                osyield()
        }
 
-       if prof.hz != 0 { // implies cpuprof.log != nil
+       if prof.hz.Load() != 0 { // implies cpuprof.log != nil
                if p.numExtra > 0 || p.lostExtra > 0 || p.lostAtomic > 0 {
                        p.addExtra()
                }
@@ -123,7 +122,7 @@ func (p *cpuProfile) add(tagPtr *unsafe.Pointer, stk []uintptr) {
                cpuprof.log.write(tagPtr, nanotime(), hdr[:], stk)
        }
 
-       atomic.Store(&prof.signalLock, 0)
+       prof.signalLock.Store(0)
 }
 
 // addNonGo adds the non-Go stack trace to the profile.
@@ -143,7 +142,7 @@ func (p *cpuProfile) addNonGo(stk []uintptr) {
        // process at a time. If not, this lock will serialize those too.
        // The use of timer_create(2) on Linux to request process-targeted
        // signals may have changed this.)
-       for !atomic.Cas(&prof.signalLock, 0, 1) {
+       for !prof.signalLock.CompareAndSwap(0, 1) {
                // TODO: Is it safe to osyield here? https://go.dev/issue/52672
                osyield()
        }
@@ -157,7 +156,7 @@ func (p *cpuProfile) addNonGo(stk []uintptr) {
                cpuprof.lostExtra++
        }
 
-       atomic.Store(&prof.signalLock, 0)
+       prof.signalLock.Store(0)
 }
 
 // addExtra adds the "extra" profiling events,
@@ -203,8 +202,8 @@ func (p *cpuProfile) addExtra() {
 // The details of generating that format have changed,
 // so this functionality has been removed.
 //
-// Deprecated: Use the runtime/pprof package,
-// or the handlers in the net/http/pprof package,
+// Deprecated: Use the [runtime/pprof] package,
+// or the handlers in the [net/http/pprof] package,
 // or the testing package's -test.cpuprofile flag instead.
 func CPUProfile() []byte {
        panic("CPUProfile no longer available")
@@ -212,7 +211,7 @@ func CPUProfile() []byte {
 
 //go:linkname runtime_pprof_runtime_cyclesPerSecond runtime/pprof.runtime_cyclesPerSecond
 func runtime_pprof_runtime_cyclesPerSecond() int64 {
-       return tickspersecond()
+       return ticksPerSecond()
 }
 
 // readProfile, provided to runtime/pprof, returns the next chunk of
@@ -228,7 +227,11 @@ func runtime_pprof_readProfile() ([]uint64, []unsafe.Pointer, bool) {
        lock(&cpuprof.lock)
        log := cpuprof.log
        unlock(&cpuprof.lock)
-       data, tags, eof := log.read(profBufBlocking)
+       readMode := profBufBlocking
+       if GOOS == "darwin" || GOOS == "ios" {
+               readMode = profBufNonBlocking // For #61768; on Darwin notes are not async-signal-safe.  See sigNoteSetup in os_darwin.go.
+       }
+       data, tags, eof := log.read(readMode)
        if len(data) == 0 && eof {
                lock(&cpuprof.lock)
                cpuprof.log = nil