Skip to content

Commit

Permalink
Extend gops trace to allow duration parameter
Browse files Browse the repository at this point in the history
`gops trace :port 10m` will now run a trace for 10 minutes,
as opposed to always doing 5 seconds.

For backwards compatibility both server and the client
default to 5s if nothing is specified.
  • Loading branch information
glibsm committed Mar 29, 2022
1 parent 4e16ac7 commit 532a3d4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -226,8 +226,10 @@ $ gops pprof-heap (<pid>|<addr>)

##### Execution trace

gops allows you to start the runtime tracer for 5 seconds and examine the results.
gops allows you to start the runtime tracer for a specified duration and
examine the results.

```sh
$ gops trace (<pid>|<addr>)
$ gops trace (<pid>|<addr>) 90s
Tracing now, will take 1m30s...
```
13 changes: 12 additions & 1 deletion agent/agent.go
Expand Up @@ -269,10 +269,21 @@ func handle(conn io.ReadWriter, msg []byte) error {
_, err = bufio.NewReader(f).WriteTo(conn)
return err
case signal.Trace:
intd, err := binary.ReadVarint(bufio.NewReader(conn))
if err != nil {
return err
}
d := time.Duration(intd)
if intd == 0 {
// also default to 5 seconds if the client didn't send any duration to
// keep compatibility between older clients contacting a newer server.
d = 5 * time.Second
}

if err := trace.Start(conn); err != nil {
return err
}
time.Sleep(5 * time.Second)
time.Sleep(d)
trace.Stop()
case signal.SetGCPercent:
perc, err := binary.ReadVarint(bufio.NewReader(conn))
Expand Down
20 changes: 17 additions & 3 deletions cmd.go
Expand Up @@ -15,6 +15,7 @@ import (
"os/exec"
"strconv"
"strings"
"time"

"github.com/google/gops/internal"
"github.com/google/gops/signal"
Expand Down Expand Up @@ -71,9 +72,22 @@ func pprofCPU(addr net.TCPAddr, _ []string) error {
return pprof(addr, signal.CPUProfile, "cpu")
}

func trace(addr net.TCPAddr, _ []string) error {
fmt.Println("Tracing now, will take 5 secs...")
out, err := cmd(addr, signal.Trace)
func trace(addr net.TCPAddr, params []string) error {
// keep the original duration of 5 seconds by default
duration := 5 * time.Second
buf := make([]byte, binary.MaxVarintLen64)

if len(params) > 0 {
d, err := time.ParseDuration(params[0])
if err != nil {
return fmt.Errorf("failed to parse duration: %v", params[0])
}
duration = d
binary.PutVarint(buf, int64(duration))
}

fmt.Printf("Tracing now, will take %v...\n", duration)
out, err := cmd(addr, signal.Trace, buf...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -35,7 +35,7 @@ Commands:
memstats Prints the allocation and garbage collection stats.
version Prints the Go version used to build the program.
stats Prints runtime stats.
trace Runs the runtime tracer for 5 secs and launches "go tool trace".
trace Runs the runtime tracer and launches "go tool trace".
pprof-heap Reads the heap profile and launches "go tool pprof".
pprof-cpu Reads the CPU profile and launches "go tool pprof".
Expand Down

0 comments on commit 532a3d4

Please sign in to comment.