Skip to content

Commit ca368c6

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 ca368c6

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

agent/agent.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,21 @@ func handle(conn io.ReadWriter, msg []byte) error {
269269
_, err = bufio.NewReader(f).WriteTo(conn)
270270
return err
271271
case signal.Trace:
272+
intd, err := binary.ReadVarint(bufio.NewReader(conn))
273+
if err != nil {
274+
return err
275+
}
276+
d := time.Duration(intd)
277+
if intd == 0 {
278+
// also default to 5 seconds if the client didn't send any duration to
279+
// keep compatibility between older clients contacting a newer server.
280+
d = 5 * time.Second
281+
}
282+
272283
if err := trace.Start(conn); err != nil {
273284
return err
274285
}
275-
time.Sleep(5 * time.Second)
286+
time.Sleep(d)
276287
trace.Stop()
277288
case signal.SetGCPercent:
278289
perc, err := binary.ReadVarint(bufio.NewReader(conn))

cmd.go

Lines changed: 17 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,22 @@ 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+
duration = d
86+
binary.PutVarint(buf, int64(duration))
87+
}
88+
89+
fmt.Printf("Tracing now, will take %v...\n", duration)
90+
out, err := cmd(addr, signal.Trace, buf...)
7791
if err != nil {
7892
return err
7993
}

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)