cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package commands defines and manages the basic pprof commands
package commands
import (
"bytes"
"fmt"
"io"
2015-07-14 15:43:33 -04:00
"io/ioutil"
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
"os"
"os/exec"
2014-12-04 11:24:23 -05:00
"runtime"
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
"strings"
2015-07-22 17:48:49 -04:00
"time"
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
"cmd/pprof/internal/plugin"
"cmd/pprof/internal/report"
"cmd/pprof/internal/svg"
"cmd/pprof/internal/tempfile"
)
// Commands describes the commands accepted by pprof.
type Commands map [ string ] * Command
// Command describes the actions for a pprof command. Includes a
// function for command-line completion, the report format to use
// during report generation, any postprocessing functions, and whether
// the command expects a regexp parameter (typically a function name).
type Command struct {
Complete Completer // autocomplete for interactive mode
Format int // report format to generate
PostProcess PostProcessor // postprocessing to run on report
HasParam bool // Collect a parameter from the CLI
Usage string // Help text
}
// Completer is a function for command-line autocompletion
type Completer func ( prefix string ) string
// PostProcessor is a function that applies post-processing to the report output
type PostProcessor func ( input * bytes . Buffer , output io . Writer , ui plugin . UI ) error
// PProf returns the basic pprof report-generation commands
2015-04-27 14:40:34 -04:00
func PProf ( c Completer , interactive * * bool ) Commands {
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
return Commands {
// Commands that require no post-processing.
"tags" : { nil , report . Tags , nil , false , "Outputs all tags in the profile" } ,
"raw" : { c , report . Raw , nil , false , "Outputs a text representation of the raw profile" } ,
"dot" : { c , report . Dot , nil , false , "Outputs a graph in DOT format" } ,
"top" : { c , report . Text , nil , false , "Outputs top entries in text form" } ,
"tree" : { c , report . Tree , nil , false , "Outputs a text rendering of call graph" } ,
"text" : { c , report . Text , nil , false , "Outputs top entries in text form" } ,
"disasm" : { c , report . Dis , nil , true , "Output annotated assembly for functions matching regexp or address" } ,
"list" : { c , report . List , nil , true , "Output annotated source for functions matching regexp" } ,
"peek" : { c , report . Tree , nil , true , "Output callers/callees of functions matching regexp" } ,
// Save binary formats to a file
"callgrind" : { c , report . Callgrind , awayFromTTY ( "callgraph.out" ) , false , "Outputs a graph in callgrind format" } ,
"proto" : { c , report . Proto , awayFromTTY ( "pb.gz" ) , false , "Outputs the profile in compressed protobuf format" } ,
// Generate report in DOT format and postprocess with dot
"gif" : { c , report . Dot , invokeDot ( "gif" ) , false , "Outputs a graph image in GIF format" } ,
"pdf" : { c , report . Dot , invokeDot ( "pdf" ) , false , "Outputs a graph in PDF format" } ,
"png" : { c , report . Dot , invokeDot ( "png" ) , false , "Outputs a graph image in PNG format" } ,
"ps" : { c , report . Dot , invokeDot ( "ps" ) , false , "Outputs a graph in PS format" } ,
// Save SVG output into a file after including svgpan library
2015-04-27 14:40:34 -04:00
"svg" : { c , report . Dot , saveSVGToFile ( ) , false , "Outputs a graph in SVG format" } ,
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
// Visualize postprocessed dot output
"eog" : { c , report . Dot , invokeVisualizer ( interactive , invokeDot ( "svg" ) , "svg" , [ ] string { "eog" } ) , false , "Visualize graph through eog" } ,
"evince" : { c , report . Dot , invokeVisualizer ( interactive , invokeDot ( "pdf" ) , "pdf" , [ ] string { "evince" } ) , false , "Visualize graph through evince" } ,
"gv" : { c , report . Dot , invokeVisualizer ( interactive , invokeDot ( "ps" ) , "ps" , [ ] string { "gv --noantialias" } ) , false , "Visualize graph through gv" } ,
2015-04-27 14:40:34 -04:00
"web" : { c , report . Dot , invokeVisualizer ( interactive , saveSVGToFile ( ) , "svg" , browsers ( ) ) , false , "Visualize graph through web browser" } ,
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
// Visualize HTML directly generated by report.
2014-12-04 11:24:23 -05:00
"weblist" : { c , report . WebList , invokeVisualizer ( interactive , awayFromTTY ( "html" ) , "html" , browsers ( ) ) , true , "Output annotated source in HTML for functions matching regexp or address" } ,
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
}
}
2014-12-04 11:24:23 -05:00
// browsers returns a list of commands to attempt for web visualization
// on the current platform
func browsers ( ) [ ] string {
2015-07-14 15:38:46 -04:00
var cmds [ ] string
if exe := os . Getenv ( "BROWSER" ) ; exe != "" {
cmds = append ( cmds , exe )
}
2014-12-04 11:24:23 -05:00
switch runtime . GOOS {
case "darwin" :
cmds = append ( cmds , "/usr/bin/open" )
case "windows" :
cmds = append ( cmds , "cmd /c start" )
default :
cmds = append ( cmds , "xdg-open" )
}
2015-07-14 15:38:46 -04:00
cmds = append ( cmds , "chrome" , "google-chrome" , "firefox" )
2014-12-04 11:24:23 -05:00
return cmds
}
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
// NewCompleter creates an autocompletion function for a set of commands.
func NewCompleter ( cs Commands ) Completer {
return func ( line string ) string {
switch tokens := strings . Fields ( line ) ; len ( tokens ) {
case 0 :
// Nothing to complete
case 1 :
// Single token -- complete command name
found := ""
for c := range cs {
if strings . HasPrefix ( c , tokens [ 0 ] ) {
if found != "" {
return line
}
found = c
}
}
if found != "" {
return found
}
default :
// Multiple tokens -- complete using command completer
if c , ok := cs [ tokens [ 0 ] ] ; ok {
if c . Complete != nil {
lastTokenIdx := len ( tokens ) - 1
lastToken := tokens [ lastTokenIdx ]
if strings . HasPrefix ( lastToken , "-" ) {
lastToken = "-" + c . Complete ( lastToken [ 1 : ] )
} else {
lastToken = c . Complete ( lastToken )
}
return strings . Join ( append ( tokens [ : lastTokenIdx ] , lastToken ) , " " )
}
}
}
return line
}
}
// awayFromTTY saves the output in a file if it would otherwise go to
// the terminal screen. This is used to avoid dumping binary data on
// the screen.
func awayFromTTY ( format string ) PostProcessor {
return func ( input * bytes . Buffer , output io . Writer , ui plugin . UI ) error {
if output == os . Stdout && ui . IsTerminal ( ) {
tempFile , err := tempfile . New ( "" , "profile" , "." + format )
if err != nil {
return err
}
ui . PrintErr ( "Generating report in " , tempFile . Name ( ) )
_ , err = fmt . Fprint ( tempFile , input )
return err
}
_ , err := fmt . Fprint ( output , input )
return err
}
}
func invokeDot ( format string ) PostProcessor {
divert := awayFromTTY ( format )
return func ( input * bytes . Buffer , output io . Writer , ui plugin . UI ) error {
2014-12-04 11:24:23 -05:00
if _ , err := exec . LookPath ( "dot" ) ; err != nil {
ui . PrintErr ( "Cannot find dot, have you installed Graphviz?" )
return err
}
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
cmd := exec . Command ( "dot" , "-T" + format )
var buf bytes . Buffer
cmd . Stdin , cmd . Stdout , cmd . Stderr = input , & buf , os . Stderr
if err := cmd . Run ( ) ; err != nil {
return err
}
return divert ( & buf , output , ui )
}
}
2015-04-27 14:40:34 -04:00
func saveSVGToFile ( ) PostProcessor {
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
generateSVG := invokeDot ( "svg" )
divert := awayFromTTY ( "svg" )
return func ( input * bytes . Buffer , output io . Writer , ui plugin . UI ) error {
baseSVG := & bytes . Buffer { }
generateSVG ( input , baseSVG , ui )
massaged := & bytes . Buffer { }
2015-04-27 14:40:34 -04:00
fmt . Fprint ( massaged , svg . Massage ( * baseSVG ) )
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
return divert ( massaged , output , ui )
}
}
2015-07-14 15:43:33 -04:00
var vizTmpDir string
func makeVizTmpDir ( ) error {
if vizTmpDir != "" {
return nil
}
name , err := ioutil . TempDir ( "" , "pprof-" )
if err != nil {
return err
}
vizTmpDir = name
return nil
}
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
func invokeVisualizer ( interactive * * bool , format PostProcessor , suffix string , visualizers [ ] string ) PostProcessor {
return func ( input * bytes . Buffer , output io . Writer , ui plugin . UI ) error {
2015-07-14 15:43:33 -04:00
if err := makeVizTmpDir ( ) ; err != nil {
return err
}
tempFile , err := tempfile . New ( vizTmpDir , "pprof" , "." + suffix )
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
if err != nil {
return err
}
tempfile . DeferDelete ( tempFile . Name ( ) )
if err = format ( input , tempFile , ui ) ; err != nil {
return err
}
2014-12-04 11:24:23 -05:00
tempFile . Close ( ) // on windows, if the file is Open, start cannot access it.
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
// Try visualizers until one is successful
for _ , v := range visualizers {
// Separate command and arguments for exec.Command.
args := strings . Split ( v , " " )
if len ( args ) == 0 {
continue
}
viewer := exec . Command ( args [ 0 ] , append ( args [ 1 : ] , tempFile . Name ( ) ) ... )
viewer . Stderr = os . Stderr
if err = viewer . Start ( ) ; err == nil {
2015-07-22 17:48:49 -04:00
// The viewer might just send a message to another program
// to open the file. Give that program a little time to open the
// file before we remove it.
time . Sleep ( 1 * time . Second )
cmd/pprof: add Go implementation
Update #8798
This is a new implementation of pprof,
written in Go instead of in Perl.
It was written primarily by Raul Silvera and
is in use for profiling programs of all languages
inside Google.
The internal structure is a bit package-heavy,
but it matches the copy used inside Google, and
since it is in an internal directory, we can make
changes to it later if we need to.
The only "new" file here is src/cmd/pprof/pprof.go,
which stitches together the Google pprof and the
Go command libraries for object file access.
I am explicitly NOT interested in style or review
comments on the rest of the files
(that is, src/cmd/pprof/internal/...).
Those are intended to stay as close to the Google
copies as possible, like we did with the pprof Perl script.
Still to do:
- Basic tests.
- Real command documentation.
- Hook up disassemblers.
LGTM=r
R=r, bradfitz, alex.brainman, dave
CC=golang-codereviews
https://golang.org/cl/153750043
2014-09-30 13:41:54 -04:00
if ! * * interactive {
// In command-line mode, wait for the viewer to be closed
// before proceeding
return viewer . Wait ( )
}
return nil
}
}
return err
}
}