"sort"
"strings"
"sync"
- "sync/atomic"
"text/tabwriter"
"time"
"unsafe"
var cpu struct {
sync.Mutex
- profiling uint32 // bool, accessed atomically
+ profiling bool
done chan bool
}
-func cpuProfiling() bool {
- return atomic.LoadUint32(&cpu.profiling) != 0
-}
-
-func setCPUProfiling(b bool) {
- if b {
- atomic.StoreUint32(&cpu.profiling, 1)
- } else {
- atomic.StoreUint32(&cpu.profiling, 0)
- }
-}
-
// StartCPUProfile enables CPU profiling for the current process.
// While profiling, the profile will be buffered and written to w.
// StartCPUProfile returns an error if profiling is already enabled.
cpu.done = make(chan bool)
}
// Double-check.
- if cpuProfiling() {
+ if cpu.profiling {
return fmt.Errorf("cpu profiling already in use")
}
- setCPUProfiling(true)
+ cpu.profiling = true
runtime.SetCPUProfileRate(hz)
go profileWriter(w)
return nil
b := newProfileBuilder(w)
var err error
for {
- if cpuProfiling() {
- time.Sleep(100 * time.Millisecond)
- }
+ time.Sleep(100 * time.Millisecond)
data, tags, eof := readProfile()
if e := b.addCPUData(data, tags); e != nil && err == nil {
err = e
cpu.Lock()
defer cpu.Unlock()
- if !cpuProfiling() {
+ if !cpu.profiling {
return
}
- setCPUProfiling(false)
+ cpu.profiling = false
runtime.SetCPUProfileRate(0)
<-cpu.done
}