fs: add --dump goroutines and --dump openfiles

These are developer flags useful for tracking down resource leaks.
This commit is contained in:
Nick Craig-Wood 2018-04-11 14:16:55 +01:00
parent 37be78705d
commit dcf8334673
3 changed files with 37 additions and 0 deletions

View File

@ -10,10 +10,12 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"os/exec"
"path" "path"
"regexp" "regexp"
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
"strconv"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -322,6 +324,26 @@ func Run(Retry bool, showStats bool, cmd *cobra.Command, f func() error) {
accounting.Stats.Log() accounting.Stats.Log()
} }
fs.Debugf(nil, "%d go routines active\n", runtime.NumGoroutine()) fs.Debugf(nil, "%d go routines active\n", runtime.NumGoroutine())
// dump all running go-routines
if fs.Config.Dump&fs.DumpGoRoutines != 0 {
err = pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
if err != nil {
fs.Errorf(nil, "Failed to dump goroutines: %v", err)
}
}
// dump open files
if fs.Config.Dump&fs.DumpOpenFiles != 0 {
c := exec.Command("lsof", "-p", strconv.Itoa(os.Getpid()))
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err = c.Run()
if err != nil {
fs.Errorf(nil, "Failed to list open files: %v", err)
}
}
if accounting.Stats.Errored() { if accounting.Stats.Errored() {
resolveExitCode(accounting.Stats.GetLastError()) resolveExitCode(accounting.Stats.GetLastError())
} }

View File

@ -953,6 +953,17 @@ only.
Dump the filters to the output. Useful to see exactly what include Dump the filters to the output. Useful to see exactly what include
and exclude options are filtering on. and exclude options are filtering on.
#### --dump goroutines ####
This dumps a list of the running go-routines at the end of the command
to standard output.
#### --dump openfiles ####
This dumps a list of the open files at the end of the command. It
uses the `lsof` command to do that so you'll need that installed to
use it.
### --memprofile=FILE ### ### --memprofile=FILE ###
Write memory profile to file. This can be analysed with `go tool pprof`. Write memory profile to file. This can be analysed with `go tool pprof`.

View File

@ -18,6 +18,8 @@ const (
DumpResponses DumpResponses
DumpAuth DumpAuth
DumpFilters DumpFilters
DumpGoRoutines
DumpOpenFiles
) )
var dumpFlags = []struct { var dumpFlags = []struct {
@ -30,6 +32,8 @@ var dumpFlags = []struct {
{DumpResponses, "responses"}, {DumpResponses, "responses"},
{DumpAuth, "auth"}, {DumpAuth, "auth"},
{DumpFilters, "filters"}, {DumpFilters, "filters"},
{DumpGoRoutines, "goroutines"},
{DumpOpenFiles, "openfiles"},
} }
// DumpFlagsList is a list of dump flags used in the help // DumpFlagsList is a list of dump flags used in the help