Skip to content

Commit 57ebce6

Browse files
authored
Merge pull request #54 from AeroNotix/allow-override-memory-profile-type
Add MemProfileType to allow overriding type of memory profile
2 parents acd64d4 + 1d0d932 commit 57ebce6

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func main() {
2828
options
2929
-------
3030

31-
What to profile is controlled by config value passed to profile.Start.
31+
What to profile is controlled by config value passed to profile.Start.
3232
By default CPU profiling is enabled.
3333

3434
```go
@@ -39,6 +39,9 @@ func main() {
3939
// ensure profiling information is written to disk.
4040
p := profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook)
4141
...
42+
// You can enable different kinds of memory profiling, either Heap or Allocs where Heap
43+
// profiling is the default with profile.MemProfile.
44+
p := profile.Start(profile.MemProfileAllocs, profile.ProfilePath("."), profile.NoShutdownHook)
4245
}
4346
```
4447

profile.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.
89111
func 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

Comments
 (0)