Namespace: go.std.runtime.pprof
v1.0Contents
Summary
Provides a low-level interface to the runtime/pprof package.
Package pprof writes runtime profiling data in the format expected
by the pprof visualization tool.
# Profiling a Go program
The first step to profiling a Go program is to enable profiling.
Support for profiling benchmarks built with the standard testing
package is built into go test. For example, the following command
runs benchmarks in the current directory and writes the CPU and
memory profiles to cpu.prof and mem.prof:
go test -cpuprofile cpu.prof -memprofile mem.prof -bench .
To add equivalent profiling support to a standalone program, add
code like the following to your main function:
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
// ... rest of the program ...
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
}
There is also a standard HTTP interface to profiling data. Adding
the following line will install handlers under the /debug/pprof/
URL to download live profiles:
import _ "net/http/pprof"
See the net/http/pprof package for more details.
Profiles can then be visualized with the pprof tool:
go tool pprof cpu.prof
There are many commands available from the pprof command line.
Commonly used commands include "top", which prints a summary of the
top program hot-spots, and "web", which opens an interactive graph
of hot-spots and their call graphs. Use "help" for information on
all pprof commands.
For more information about pprof, see
https://github.com/google/pprof/blob/master/doc/README.md.
Index
- *LabelSet
- *Profile
- Label
- LabelSet
- Labels
- Lookup
- NewProfile
- Profile
- Profiles
- SetGoroutineLabels
- StartCPUProfile
- StopCPUProfile
- WithLabels
- WriteHeapProfile
- arrayOfLabelSet
- arrayOfProfile
Legend
-
Constant
Variable
Function
Macro
Special form
Type
GoVar
Receiver/Method
Constants
Constants are variables with :const true in their metadata. Joker currently does not recognize them as special; as such, it allows redefining them or their values.-
(None.)
Variables
-
(None.)
Functions, Macros, and Special Forms
-
Label
Function v1.0(Label ctx key)
Label returns the value of the label with the given key on ctx, and a boolean indicating
whether that label exists.
Go input arguments: (ctx context.Context, key string)
Go returns: (string, bool)
Joker input arguments: [^go.std.context/Context ctx, ^String key]
Joker returns: [^String, ^Boolean] -
Labels
Function v1.0(Labels & args)
Labels takes an even number of strings representing key-value pairs
and makes a LabelSet containing them.
A label overwrites a prior label with the same key.
Currently only the CPU and goroutine profiles utilize any labels
information.
See https://golang.org/issue/23458 for details.
Go input arguments: (args ...string)
Go returns: LabelSet
Joker input arguments: [& ^String args]
Joker returns: ^LabelSet -
Lookup
Function v1.0(Lookup name)
Lookup returns the profile with the given name, or nil if no such profile exists.
Go input arguments: (name string)
Go returns: *Profile
Joker input arguments: [^String name]
Joker returns: ^*Profile -
NewProfile
Function v1.0(NewProfile name)
NewProfile creates a new profile with the given name.
If a profile with that name already exists, NewProfile panics.
The convention is to use a 'import/path.' prefix to create
separate name spaces for each package.
For compatibility with various tools that read pprof data,
profile names should not contain spaces.
Go input arguments: (name string)
Go returns: *Profile
Joker input arguments: [^String name]
Joker returns: ^*Profile -
Profiles
Function v1.0(Profiles)
Profiles returns a slice of all the known profiles, sorted by name.
Go returns: []*Profile
Joker input arguments: []
Joker returns: ^arrayOf*Profile -
SetGoroutineLabels
Function v1.0(SetGoroutineLabels ctx)
SetGoroutineLabels sets the current goroutine's labels to match ctx.
A new goroutine inherits the labels of the goroutine that created it.
This is a lower-level API than Do, which should be used instead when possible.
Go input arguments: (ctx context.Context)
Joker input arguments: [^go.std.context/Context ctx] -
StartCPUProfile
Function v1.0(StartCPUProfile w)
StartCPUProfile enables CPU profiling for the current process.
While profiling, the profile will be buffered and written to w.
StartCPUProfile returns an error if profiling is already enabled.
On Unix-like systems, StartCPUProfile does not work by default for
Go code built with -buildmode=c-archive or -buildmode=c-shared.
StartCPUProfile relies on the SIGPROF signal, but that signal will
be delivered to the main program's SIGPROF signal handler (if any)
not to the one used by Go. To make it work, call os/signal.Notify
for syscall.SIGPROF, but note that doing so may break any profiling
being done by the main program.
Go input arguments: (w io.Writer)
Go returns: error
Joker input arguments: [^go.std.io/Writer w]
Joker returns: ^Error -
StopCPUProfile
Function v1.0(StopCPUProfile)
StopCPUProfile stops the current CPU profile, if any.
StopCPUProfile only returns after all the writes for the
profile have completed.
Joker input arguments: [] -
WithLabels
Function v1.0(WithLabels ctx labels)
WithLabels returns a new context.Context with the given labels added.
A label overwrites a prior label with the same key.
Go input arguments: (ctx context.Context, labels LabelSet)
Go returns: context.Context
Joker input arguments: [^go.std.context/Context ctx, ^LabelSet labels]
Joker returns: ^go.std.context/Context -
WriteHeapProfile
Function v1.0(WriteHeapProfile w)
WriteHeapProfile is shorthand for Lookup("heap").WriteTo(w, 0).
It is preserved for backwards compatibility.
Go input arguments: (w io.Writer)
Go returns: error
Joker input arguments: [^go.std.io/Writer w]
Joker returns: ^Error
Types
-
*LabelSet
Concrete Type v1.0LabelSet is a set of labels.
-
*Profile
Concrete Type v1.0A Profile is a collection of stack traces showing the call sequences
that led to instances of a particular event, such as allocation.
Packages can create and maintain their own profiles; the most common
use is for tracking resources that must be explicitly closed, such as files
or network connections.
A Profile's methods can be called from multiple goroutines simultaneously.
Each Profile has a unique name. A few profiles are predefined:
goroutine - stack traces of all current goroutines
heap - a sampling of memory allocations of live objects
allocs - a sampling of all past memory allocations
threadcreate - stack traces that led to the creation of new OS threads
block - stack traces that led to blocking on synchronization primitives
mutex - stack traces of holders of contended mutexes
These predefined profiles maintain themselves and panic on an explicit
Add or Remove method call.
The heap profile reports statistics as of the most recently completed
garbage collection; it elides more recent allocation to avoid skewing
the profile away from live data and toward garbage.
If there has been no garbage collection at all, the heap profile reports
all known allocations. This exception helps mainly in programs running
without garbage collection enabled, usually for debugging purposes.
The heap profile tracks both the allocation sites for all live objects in
the application memory and for all objects allocated since the program start.
Pprof's -inuse_space, -inuse_objects, -alloc_space, and -alloc_objects
flags select which to display, defaulting to -inuse_space (live objects,
scaled by size).
The allocs profile is the same as the heap profile but changes the default
pprof display to -alloc_space, the total number of bytes allocated since
the program began (including garbage-collected bytes).
The CPU profile is not available as a Profile. It has a special API,
the StartCPUProfile and StopCPUProfile functions, because it streams
output to a writer during profiling.
-
Add
Receiver for *Profile v1.0([value skip])
Add adds the current execution stack to the profile, associated with value.
Add stores value in an internal map, so value must be suitable for use as
a map key and will not be garbage collected until the corresponding
call to Remove. Add panics if the profile already contains a stack for value.
The skip parameter has the same meaning as runtime.Caller's skip
and controls where the stack trace begins. Passing skip=0 begins the
trace in the function calling Add. For example, given this
execution stack:
Add
called from rpc.NewClient
called from mypkg.Run
called from main.main
Passing skip=0 begins the stack trace at the call to Add inside rpc.NewClient.
Passing skip=1 begins the stack trace at the call to NewClient inside mypkg.Run.
-
Count
Receiver for *Profile v1.0([])
Count returns the number of execution stacks currently in the profile.
-
Name
Receiver for *Profile v1.0([])
Name returns this profile's name, which can be passed to Lookup to reobtain the profile.
-
Remove
Receiver for *Profile v1.0([value])
Remove removes the execution stack associated with value from the profile.
It is a no-op if the value is not in the profile.
-
WriteTo
Receiver for *Profile v1.0([w debug])
WriteTo writes a pprof-formatted snapshot of the profile to w.
If a write to w returns an error, WriteTo returns that error.
Otherwise, WriteTo returns nil.
The debug parameter enables additional output.
Passing debug=0 writes the gzip-compressed protocol buffer described
in https://github.com/google/pprof/tree/master/proto#overview.
Passing debug=1 writes the legacy text format with comments
translating addresses to function names and line numbers, so that a
programmer can read the profile without tools.
The predefined profiles may assign meaning to other debug values;
for example, when printing the "goroutine" profile, debug=2 means to
print the goroutine stacks in the same form that a Go program uses
when dying due to an unrecovered panic.
-
LabelSet
Concrete Type v1.0LabelSet is a set of labels.
-
Profile
Concrete Type v1.0A Profile is a collection of stack traces showing the call sequences
that led to instances of a particular event, such as allocation.
Packages can create and maintain their own profiles; the most common
use is for tracking resources that must be explicitly closed, such as files
or network connections.
A Profile's methods can be called from multiple goroutines simultaneously.
Each Profile has a unique name. A few profiles are predefined:
goroutine - stack traces of all current goroutines
heap - a sampling of memory allocations of live objects
allocs - a sampling of all past memory allocations
threadcreate - stack traces that led to the creation of new OS threads
block - stack traces that led to blocking on synchronization primitives
mutex - stack traces of holders of contended mutexes
These predefined profiles maintain themselves and panic on an explicit
Add or Remove method call.
The heap profile reports statistics as of the most recently completed
garbage collection; it elides more recent allocation to avoid skewing
the profile away from live data and toward garbage.
If there has been no garbage collection at all, the heap profile reports
all known allocations. This exception helps mainly in programs running
without garbage collection enabled, usually for debugging purposes.
The heap profile tracks both the allocation sites for all live objects in
the application memory and for all objects allocated since the program start.
Pprof's -inuse_space, -inuse_objects, -alloc_space, and -alloc_objects
flags select which to display, defaulting to -inuse_space (live objects,
scaled by size).
The allocs profile is the same as the heap profile but changes the default
pprof display to -alloc_space, the total number of bytes allocated since
the program began (including garbage-collected bytes).
The CPU profile is not available as a Profile. It has a special API,
the StartCPUProfile and StopCPUProfile functions, because it streams
output to a writer during profiling.
-
arrayOfLabelSet
Concrete Type v1.0LabelSet is a set of labels.
-
arrayOfProfile
Concrete Type v1.0A Profile is a collection of stack traces showing the call sequences
that led to instances of a particular event, such as allocation.
Packages can create and maintain their own profiles; the most common
use is for tracking resources that must be explicitly closed, such as files
or network connections.
A Profile's methods can be called from multiple goroutines simultaneously.
Each Profile has a unique name. A few profiles are predefined:
goroutine - stack traces of all current goroutines
heap - a sampling of memory allocations of live objects
allocs - a sampling of all past memory allocations
threadcreate - stack traces that led to the creation of new OS threads
block - stack traces that led to blocking on synchronization primitives
mutex - stack traces of holders of contended mutexes
These predefined profiles maintain themselves and panic on an explicit
Add or Remove method call.
The heap profile reports statistics as of the most recently completed
garbage collection; it elides more recent allocation to avoid skewing
the profile away from live data and toward garbage.
If there has been no garbage collection at all, the heap profile reports
all known allocations. This exception helps mainly in programs running
without garbage collection enabled, usually for debugging purposes.
The heap profile tracks both the allocation sites for all live objects in
the application memory and for all objects allocated since the program start.
Pprof's -inuse_space, -inuse_objects, -alloc_space, and -alloc_objects
flags select which to display, defaulting to -inuse_space (live objects,
scaled by size).
The allocs profile is the same as the heap profile but changes the default
pprof display to -alloc_space, the total number of bytes allocated since
the program began (including garbage-collected bytes).
The CPU profile is not available as a Profile. It has a special API,
the StartCPUProfile and StopCPUProfile functions, because it streams
output to a writer during profiling.