@@ -13,9 +13,6 @@ import (
1313 "sync/atomic"
1414)
1515
16- // started counts the number of times Start has been called
17- var started uint32
18-
1916const (
2017 cpuMode = iota
2118 memMode
@@ -40,7 +37,7 @@ type profile struct {
4037 // memProfileRate holds the rate for the memory profile.
4138 memProfileRate int
4239
43- // closer holds the cleanup function that run after each profile
40+ // closer holds a cleanup function that run after each profile
4441 closer func ()
4542
4643 // stopped records if a call to profile.Stop has been made
@@ -101,6 +98,9 @@ func (p *profile) Stop() {
10198 atomic .StoreUint32 (& started , 0 )
10299}
103100
101+ // started is non zero if a profile is running.
102+ var started uint32
103+
104104// Start starts a new profiling session.
105105// The caller should call the Stop method on the value returned
106106// to cleanly stop profiling.
@@ -127,23 +127,25 @@ func Start(options ...func(*profile)) interface {
127127 log .Fatalf ("profile: could not create initial output directory: %v" , err )
128128 }
129129
130+ logf := func (format string , args ... interface {}) {
131+ if ! prof .quiet {
132+ log .Printf (format , args ... )
133+ }
134+ }
135+
130136 switch prof .mode {
131137 case cpuMode :
132138 fn := filepath .Join (path , "cpu.pprof" )
133139 f , err := os .Create (fn )
134140 if err != nil {
135141 log .Fatalf ("profile: could not create cpu profile %q: %v" , fn , err )
136142 }
137- if ! prof .quiet {
138- log .Printf ("profile: cpu profiling enabled, %s" , fn )
139- }
143+ logf ("profile: cpu profiling enabled, %s" , fn )
140144 pprof .StartCPUProfile (f )
141145 prof .closer = func () {
142146 pprof .StopCPUProfile ()
143147 f .Close ()
144- if ! prof .quiet {
145- log .Printf ("profile: cpu profiling disabled, %s" , fn )
146- }
148+ logf ("profile: cpu profiling disabled, %s" , fn )
147149 }
148150
149151 case memMode :
@@ -154,16 +156,12 @@ func Start(options ...func(*profile)) interface {
154156 }
155157 old := runtime .MemProfileRate
156158 runtime .MemProfileRate = prof .memProfileRate
157- if ! prof .quiet {
158- log .Printf ("profile: memory profiling enabled (rate %d), %s" , runtime .MemProfileRate , fn )
159- }
159+ logf ("profile: memory profiling enabled (rate %d), %s" , runtime .MemProfileRate , fn )
160160 prof .closer = func () {
161161 pprof .Lookup ("heap" ).WriteTo (f , 0 )
162162 f .Close ()
163163 runtime .MemProfileRate = old
164- if ! prof .quiet {
165- log .Printf ("profile: memory profiling disabled, %s" , fn )
166- }
164+ logf ("profile: memory profiling disabled, %s" , fn )
167165 }
168166
169167 case blockMode :
@@ -173,16 +171,12 @@ func Start(options ...func(*profile)) interface {
173171 log .Fatalf ("profile: could not create block profile %q: %v" , fn , err )
174172 }
175173 runtime .SetBlockProfileRate (1 )
176- if ! prof .quiet {
177- log .Printf ("profile: block profiling enabled, %s" , fn )
178- }
174+ logf ("profile: block profiling enabled, %s" , fn )
179175 prof .closer = func () {
180176 pprof .Lookup ("block" ).WriteTo (f , 0 )
181177 f .Close ()
182178 runtime .SetBlockProfileRate (0 )
183- if ! prof .quiet {
184- log .Printf ("profile: block profiling disabled, %s" , fn )
185- }
179+ logf ("profile: block profiling disabled, %s" , fn )
186180 }
187181 }
188182
0 commit comments