dump

package
v0.0.0-...-418cbda Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

package dump is a utility for printing memory allocations info using printf. This allows instrumentation of code for memory allocation analysis with very little effort.

- MemStats: Uses `runtime.ReadMemStats`. The numbers shown include "garbage" not yet collected by GC according to Go docs.

- MemProf: Uses code from pprof package to show stats as would be shown by pprof tool. The pprof itself uses some probabilistic approach. Hopefully this is more accurate than `MemStats` and do not include "garbage".

- WriteHeapDump(): Write pprof heap profiles at any point in code.

Sample usage:

func Example() []int64 {
	// Write pprof heap profile at the start and end of function
	dump.WriteHeapDump(fmt.Sprintf("heap-example-before"))
	defer dump.WriteHeapDump(fmt.Sprintf("heap-example-after"))

	// Take a snapshot at the start of a function
	// Capture and print deltas at the end
	memProf := dump.NewMemProf("example")
	defer memProf.PrintDiff()

	// Similar for memStats
	memStats := dump.NewMemStats("example")
	defer memStats.PrintDiff()

	// allocate memory
	allocateMem := func () []int64 {
		return make([]int64, 100000)
	}

	var result []int64
	for i := 0; i < 1000; i++{
		result = append(result, allocateMem()...)
	}

	return result
}

This will produce results similar to this:

WRITTEN HEAP DUMP TO /Users/philip/thanos/github.com/ppanyukov/go-dump/heap-example-before.pb.gz
MEM STATS DIFF:   	example 	example - AFTER 	-> Delta
	HeapAlloc  : 	219.76K 	963.52M 		-> 963.30M
	HeapObjects: 	203 		408 			-> 205

MEM PROF DIFF:    	example 	example - AFTER 	-> Delta
	InUseBytes  : 	0 		816.39M 		-> 816.39M
	InUseObjects: 	0 		1 			-> 1
	AllocBytes  : 	1.26M 		4.69G 			-> 4.69G
	AllocObjects: 	6 		808 			-> 802

WRITTEN HEAP DUMP TO /Users/philip/thanos/github.com/ppanyukov/go-dump/heap-example-after.pb.gz

The size of 1,000,000 int64 values is `800MB`, and can be seen `MemProf` reports this quite close to this number. The MemStats are slightly off with its `963.30M` as this number includes objects not yet collected by GC.

To use pprof tool to see allocations use this command:

	go tool pprof \
		-sample_index=inuse_space \
		-edgefraction=0 \
		-functions \
		-http=:8081 \
		-base /Users/philip/thanos/github.com/ppanyukov/go-dump/heap-example-before.pb.gz \
 		/Users/philip/thanos/github.com/ppanyukov/go-dump/heap-example-after.pb.gz

This would report figures like so:

Showing nodes accounting for 778.57MB, 100% of 778.57MB total

Also close to `800MB`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteHeapDump

func WriteHeapDump(name string)

WriteHeapDump is a convenience method to write a pprof heap dump which can be examined with pprof tool. The file `{name}.pb.gz` is written to `heapDumpDir`.

Call at the start and end of the function to see how much and where things were allocated within that function.

Types

type MemProf

type MemProf struct {
	Name         string
	AllocObjects int64
	AllocBytes   int64
	InUseObjects int64
	InUseBytes   int64
}

MemProf is memory heap profile as would be shown by pprof tool.

func NewMemProf

func NewMemProf(name string) *MemProf

NewMemProf creates memory heap profile as would be shown by pprof tool. Code here is based largely on pprof package with some copy/paste.

func (*MemProf) Print

func (m *MemProf) Print()

func (*MemProf) PrintDiff

func (m *MemProf) PrintDiff()

PrintDiff creates new snapshot and prints the diff against it.

func (*MemProf) String

func (m *MemProf) String() string

type MemStats

type MemStats struct {
	Name        string
	HeapAlloc   int64
	HeapSys     int64
	HeapObjects int64
}

func NewMemStats

func NewMemStats(name string) *MemStats

NewMemStats creates new snapshot of runtime.MemStats.

func (*MemStats) Print

func (m *MemStats) Print()

func (*MemStats) PrintDiff

func (m *MemStats) PrintDiff()

PrintDiff creates new snapshot diff and prints it. Here to avoid pitfalls of defer etc.

func (*MemStats) String

func (m *MemStats) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL