Skip to content

Commit

Permalink
Various cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
davecheney committed Jun 1, 2016
1 parent c506c3f commit d02f3ae
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import (
"sync/atomic"
)

// started counts the number of times Start has been called
var started uint32

const (
cpuMode = iota
memMode
Expand All @@ -40,7 +37,7 @@ type profile struct {
// memProfileRate holds the rate for the memory profile.
memProfileRate int

// closer holds the cleanup function that run after each profile
// closer holds a cleanup function that run after each profile
closer func()

// stopped records if a call to profile.Stop has been made
Expand Down Expand Up @@ -101,6 +98,9 @@ func (p *profile) Stop() {
atomic.StoreUint32(&started, 0)
}

// started is non zero if a profile is running.
var started uint32

// Start starts a new profiling session.
// The caller should call the Stop method on the value returned
// to cleanly stop profiling.
Expand All @@ -127,23 +127,25 @@ func Start(options ...func(*profile)) interface {
log.Fatalf("profile: could not create initial output directory: %v", err)
}

logf := func(format string, args ...interface{}) {
if !prof.quiet {
log.Printf(format, args...)
}
}

switch prof.mode {
case cpuMode:
fn := filepath.Join(path, "cpu.pprof")
f, err := os.Create(fn)
if err != nil {
log.Fatalf("profile: could not create cpu profile %q: %v", fn, err)
}
if !prof.quiet {
log.Printf("profile: cpu profiling enabled, %s", fn)
}
logf("profile: cpu profiling enabled, %s", fn)
pprof.StartCPUProfile(f)
prof.closer = func() {
pprof.StopCPUProfile()
f.Close()
if !prof.quiet {
log.Printf("profile: cpu profiling disabled, %s", fn)
}
logf("profile: cpu profiling disabled, %s", fn)
}

case memMode:
Expand All @@ -154,16 +156,12 @@ func Start(options ...func(*profile)) interface {
}
old := runtime.MemProfileRate
runtime.MemProfileRate = prof.memProfileRate
if !prof.quiet {
log.Printf("profile: memory profiling enabled (rate %d), %s", runtime.MemProfileRate, fn)
}
logf("profile: memory profiling enabled (rate %d), %s", runtime.MemProfileRate, fn)
prof.closer = func() {
pprof.Lookup("heap").WriteTo(f, 0)
f.Close()
runtime.MemProfileRate = old
if !prof.quiet {
log.Printf("profile: memory profiling disabled, %s", fn)
}
logf("profile: memory profiling disabled, %s", fn)
}

case blockMode:
Expand All @@ -173,16 +171,12 @@ func Start(options ...func(*profile)) interface {
log.Fatalf("profile: could not create block profile %q: %v", fn, err)
}
runtime.SetBlockProfileRate(1)
if !prof.quiet {
log.Printf("profile: block profiling enabled, %s", fn)
}
logf("profile: block profiling enabled, %s", fn)
prof.closer = func() {
pprof.Lookup("block").WriteTo(f, 0)
f.Close()
runtime.SetBlockProfileRate(0)
if !prof.quiet {
log.Printf("profile: block profiling disabled, %s", fn)
}
logf("profile: block profiling disabled, %s", fn)
}
}

Expand Down

0 comments on commit d02f3ae

Please sign in to comment.