diff --git a/cmd/cmd.go b/cmd/cmd.go index 2ea397a69..1be3d1a54 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -10,10 +10,12 @@ import ( "fmt" "log" "os" + "os/exec" "path" "regexp" "runtime" "runtime/pprof" + "strconv" "time" "github.com/pkg/errors" @@ -322,6 +324,26 @@ func Run(Retry bool, showStats bool, cmd *cobra.Command, f func() error) { accounting.Stats.Log() } 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() { resolveExitCode(accounting.Stats.GetLastError()) } diff --git a/docs/content/docs.md b/docs/content/docs.md index af80c9986..45a8634d8 100644 --- a/docs/content/docs.md +++ b/docs/content/docs.md @@ -953,6 +953,17 @@ only. Dump the filters to the output. Useful to see exactly what include 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 ### Write memory profile to file. This can be analysed with `go tool pprof`. diff --git a/fs/dump.go b/fs/dump.go index 916628ab0..ad3c98462 100644 --- a/fs/dump.go +++ b/fs/dump.go @@ -18,6 +18,8 @@ const ( DumpResponses DumpAuth DumpFilters + DumpGoRoutines + DumpOpenFiles ) var dumpFlags = []struct { @@ -30,6 +32,8 @@ var dumpFlags = []struct { {DumpResponses, "responses"}, {DumpAuth, "auth"}, {DumpFilters, "filters"}, + {DumpGoRoutines, "goroutines"}, + {DumpOpenFiles, "openfiles"}, } // DumpFlagsList is a list of dump flags used in the help