Skip to content

Commit 0a33cbc

Browse files
committed
Extend gops trace to allow duration parameter
`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.
1 parent 4e16ac7 commit 0a33cbc

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,10 @@ $ gops pprof-heap (<pid>|<addr>)
226226

227227
##### Execution trace
228228

229-
gops allows you to start the runtime tracer for 5 seconds and examine the results.
229+
gops allows you to start the runtime tracer for a specified duration and
230+
examine the results.
230231

231232
```sh
232-
$ gops trace (<pid>|<addr>)
233+
$ gops trace (<pid>|<addr>) 90s
234+
Tracing now, will take 1m30s...
233235
```

agent/agent.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"bufio"
1111
"context"
1212
"encoding/binary"
13+
"errors"
1314
"fmt"
1415
"io"
1516
"io/ioutil"
@@ -269,10 +270,24 @@ func handle(conn io.ReadWriter, msg []byte) error {
269270
_, err = bufio.NewReader(f).WriteTo(conn)
270271
return err
271272
case signal.Trace:
273+
intd, err := binary.ReadVarint(bufio.NewReader(conn))
274+
if err != nil {
275+
return err
276+
}
277+
d := time.Duration(intd)
278+
if intd < 0 {
279+
return errors.New("invalid duration")
280+
}
281+
if intd == 0 {
282+
// also default to 5 seconds if the client didn't send any duration to
283+
// keep compatibility between older clients contacting a newer server.
284+
d = 5 * time.Second
285+
}
286+
272287
if err := trace.Start(conn); err != nil {
273288
return err
274289
}
275-
time.Sleep(5 * time.Second)
290+
time.Sleep(d)
276291
trace.Stop()
277292
case signal.SetGCPercent:
278293
perc, err := binary.ReadVarint(bufio.NewReader(conn))

cmd.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"os/exec"
1616
"strconv"
1717
"strings"
18+
"time"
1819

1920
"github.com/google/gops/internal"
2021
"github.com/google/gops/signal"
@@ -71,9 +72,25 @@ func pprofCPU(addr net.TCPAddr, _ []string) error {
7172
return pprof(addr, signal.CPUProfile, "cpu")
7273
}
7374

74-
func trace(addr net.TCPAddr, _ []string) error {
75-
fmt.Println("Tracing now, will take 5 secs...")
76-
out, err := cmd(addr, signal.Trace)
75+
func trace(addr net.TCPAddr, params []string) error {
76+
// keep the original duration of 5 seconds by default
77+
duration := 5 * time.Second
78+
buf := make([]byte, binary.MaxVarintLen64)
79+
80+
if len(params) > 0 {
81+
d, err := time.ParseDuration(params[0])
82+
if err != nil {
83+
return fmt.Errorf("failed to parse duration: %v", params[0])
84+
}
85+
if d <= 0 {
86+
return fmt.Errorf("duration has to be positive: %v", d)
87+
}
88+
duration = d
89+
binary.PutVarint(buf, int64(duration))
90+
}
91+
92+
fmt.Printf("Tracing now, will take %v...\n", duration)
93+
out, err := cmd(addr, signal.Trace, buf...)
7794
if err != nil {
7895
return err
7996
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Commands:
3535
memstats Prints the allocation and garbage collection stats.
3636
version Prints the Go version used to build the program.
3737
stats Prints runtime stats.
38-
trace Runs the runtime tracer for 5 secs and launches "go tool trace".
38+
trace Runs the runtime tracer and launches "go tool trace".
3939
pprof-heap Reads the heap profile and launches "go tool pprof".
4040
pprof-cpu Reads the CPU profile and launches "go tool pprof".
4141

0 commit comments

Comments
 (0)