Documentation
¶
Index ¶
- Constants
- Variables
- func CreateProfileFile(folder string, name string) (*os.File, error)
- type CallbackFunc
- type FinalizerFunc
- type Mode
- type ProfileOption
- func WithAllocProfiler() ProfileOption
- func WithBlockProfiler() ProfileOption
- func WithCPUProfiler() ProfileOption
- func WithCallback(callback CallbackFunc) ProfileOption
- func WithClockProfiling() ProfileOption
- func WithHeapProfiler() ProfileOption
- func WithMemoryProfilingRate(rate int) ProfileOption
- func WithMutexFraction(rate int) ProfileOption
- func WithProfileFileLocation(path string) ProfileOption
- func WithQuietOutput() ProfileOption
- func WithRealTimeData() ProfileOption
- func WithThreadProfiler() ProfileOption
- func WithTracing() ProfileOption
- func WithoutSignalHandling() ProfileOption
- type Profiler
- type StrategyFunc
Constants ¶
const ( CPUFileName = "cpu.pprof" MemoryFileName = "memory.pprof" // Covers heap and alloc BlockFileName = "block.pprof" GoroutineFileName = "goroutine.pprof" MutexFileName = "mutex.pprof" ThreadCreateFileName = "threadcreate.pprof" TraceFileName = "trace.out" ClockFileName = "clock.pprof" )
Variables ¶
var StrategyMap = map[Mode]StrategyFunc{
CPUMode: cpuStrategyFn,
MemoryHeapMode: heapStrategyFn,
MemoryAllocMode: allocStrategyFn,
MutexMode: mutexStrategyFn,
BlockMode: blockStrategyFn,
GoroutineMode: goroutineStrategyFn,
ThreadCreateMode: threadCreateStrategyFn,
TraceMode: traceStrategyFn,
ClockMode: clockStrategyFn,
}
Functions ¶
func CreateProfileFile ¶ added in v0.2.0
CreateProfileFile takes the user defined folder (or working dir) if omitted and attempts to make the full folder tree. If the folder creation fails, a temp folder is created and the file is written to that location. File names are currently not customisable and are provided by the caller based on the profile mode selected.
Types ¶
type CallbackFunc ¶
type CallbackFunc func(p *Profiler)
CallbackFunc is a function that can be supplied with the WithCallback option to be executed when the profiling instance is performing teardown. It has access to the *Profiler instance.
type FinalizerFunc ¶
type FinalizerFunc func() error
FinalizerFunc is a function that is invokved during the teardown period of the profiling instance.
type ProfileOption ¶
type ProfileOption func(*Profiler)
ProfileOption is a functional option to configure profiler instances.
func WithAllocProfiler ¶ added in v0.2.0
func WithAllocProfiler() ProfileOption
WithAllocProfiler enables the Alloc Profiler. Alloc Profiling is useful for determining where memory is being allocated and where it is being retained. This is different to Heap Profiling as it will show you where memory is being allocated, but not necessarily where it is being retained. This is useful for finding memory leaks. This is only available in Go 1.12 and later. The rate at which the profiler samples memory allocations can be set with the WithMemoryProfilingRate option.
func WithCPUProfiler ¶
func WithCPUProfiler() ProfileOption
WithCPUProfiler enables the CPU Profiler. CPU Profiling is useful for determining where a program is spending CPU cycles (as opposed) to sleeping or waiting for IO.
func WithCallback ¶
func WithCallback(callback CallbackFunc) ProfileOption
WithCallback executes a user defined function when clean up occurs. This function is also fired on sigterm handling when the option is enabled. Callbacks have access to the underlying *Profiler instance, this is typically useful if you wanted to do some logic with the profile files that are written as the callback is only fired when the profile is complete, such as persisting a profile file to a central store etc.
func WithClockProfiling ¶ added in v0.2.0
func WithClockProfiling() ProfileOption
WithClockProfiling utilises wall clock profiling powered by https://github.com/felixge/fgprof. This allows you to profile both CPU ON and OFF wait in tandem, painting a nice picture. Go runtimes built in CPU profiler only displays cpu ON time.
func WithHeapProfiler ¶ added in v0.2.0
func WithHeapProfiler() ProfileOption
WithHeapProfiler enables the Heap Profiler. Heap Profiling is useful for determining where memory is being allocated and where it is being retained.
func WithMemoryProfilingRate ¶
func WithMemoryProfilingRate(rate int) ProfileOption
WithMemoryProfilingRate sets the rate at which the memory profiler samples memory allocations for both Heap and Alloc profiling. By default this is set to the runtime.MemProfileRate value which is 512 * 1024. This can be set to a higher value to increase the resolution of the memory profile.
func WithMutexFraction ¶
func WithMutexFraction(rate int) ProfileOption
WithMutexFraction sets the rate at which the mutex profiler samples mutex contention. By default this is set to 1.
func WithProfileFileLocation ¶
func WithProfileFileLocation(path string) ProfileOption
WithProfileFileLocation allows a custom output path for the profile file that is written to disk.
func WithQuietOutput ¶
func WithQuietOutput() ProfileOption
WithQuietOutput prevents the profiling from writing logger events.
func WithRealTimeData ¶
func WithRealTimeData() ProfileOption
WithLiveTracing enables live tracing of the program as it runs for cases which allow it. This exposes trace data via the runtime/pprof http server.
func WithTracing ¶
func WithTracing() ProfileOption
WithTracing enables the tracing profiler. Tracing is useful for determining the flow of a program and where it is spending time. Utilising the trace api within your code can add some extra context to the trace output (logs, tasks etc). but is not the responsibility of this package.
func WithoutSignalHandling ¶
func WithoutSignalHandling() ProfileOption
WithoutSignalHandling disables the signal handling for the profiler. This is useful for cases where you want to handle the signal yourself. Be sure to invoke profiler.Stop() yourself in your code and handle the os.Exit() yourself etc.
type Profiler ¶
type Profiler struct {
// contains filtered or unexported fields
}
Profiler encapsulates a profiling instance.
func Start ¶
func Start(options ...ProfileOption) *Profiler
Start starts a new profiling instance. If no mode option is provided, the default behavious is to perform CPU profiling. Start returns the underlying profile instance typically deferred in simple scenarios. In more complex scenarios keeping a handle to the stop function and calling it yourself in some of your own signal handling code for example is wise, this should be used with the option: WithNoSignalShutdownHandling.
func (*Profiler) SetProfileFile ¶
SetProfileFile sets the profile file for the profiler instance. not to be confused with the folder location provided by the functional options.
type StrategyFunc ¶
type StrategyFunc func(p *Profiler) (FinalizerFunc, error)
StrategyFunc is the custom type for an implementation that controls pre/post profiling setup and teardown.