@@ -43,6 +43,10 @@ type Profile struct {
4343 // memProfileRate holds the rate for the memory profile.
4444 memProfileRate int
4545
46+ // memProfileType holds the profile type for memory
47+ // profiles. Allowed values are `heap` and `allocs`.
48+ memProfileType string
49+
4650 // closer holds a cleanup function that run after each profile
4751 closer func ()
4852
@@ -84,6 +88,24 @@ func MemProfileRate(rate int) func(*Profile) {
8488 }
8589}
8690
91+ // MemProfileHeap changes which type of memory profiling to profile
92+ // the heap.
93+ func MemProfileHeap () func (* Profile ) {
94+ return func (p * Profile ) {
95+ p .memProfileType = "heap"
96+ p .mode = memMode
97+ }
98+ }
99+
100+ // MemProfileAllocs changes which type of memory to profile
101+ // allocations.
102+ func MemProfileAllocs () func (* Profile ) {
103+ return func (p * Profile ) {
104+ p .memProfileType = "allocs"
105+ p .mode = memMode
106+ }
107+ }
108+
87109// MutexProfile enables mutex profiling.
88110// It disables any previous profiling settings.
89111func MutexProfile (p * Profile ) { p .mode = mutexMode }
@@ -158,6 +180,10 @@ func Start(options ...func(*Profile)) interface {
158180 }
159181 }
160182
183+ if prof .memProfileType == "" {
184+ prof .memProfileType = "heap"
185+ }
186+
161187 switch prof .mode {
162188 case cpuMode :
163189 fn := filepath .Join (path , "cpu.pprof" )
@@ -183,7 +209,7 @@ func Start(options ...func(*Profile)) interface {
183209 runtime .MemProfileRate = prof .memProfileRate
184210 logf ("profile: memory profiling enabled (rate %d), %s" , runtime .MemProfileRate , fn )
185211 prof .closer = func () {
186- pprof .Lookup ("heap" ).WriteTo (f , 0 )
212+ pprof .Lookup (prof . memProfileType ).WriteTo (f , 0 )
187213 f .Close ()
188214 runtime .MemProfileRate = old
189215 logf ("profile: memory profiling disabled, %s" , fn )
0 commit comments