diff --git a/README.md b/README.md index 5bccd654..0f9a370b 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ The output displays: Note that processes running the agent are marked with `*` next to the PID (e.g. `4132*`). -#### $ gops \ +#### $ gops \ [duration] To report more information about a process, run `gops` followed by a PID: @@ -96,6 +96,24 @@ local/remote: 127.0.0.1:56765 <-> 127.0.0.1:50955 (ESTABLISHED) local/remote: 100.76.175.164:52353 <-> 54.241.191.232:443 (ESTABLISHED) ``` +If an optional duration is specified in the format as expected by +[`time.ParseDuration`](https://golang.org/pkg/time/#ParseDuration), the CPU +usage for the given time period is reported in addition: + +```sh +$ gops 2s +parent PID: 5985 +threads: 27 +memory usage: 0.199% +cpu usage: 0.139% +cpu usage (2s): 0.271% +username: jbd +cmd+args: /Applications/Splice.app/Contents/Resources/Splice Helper.app/Contents/MacOS/Splice Helper -pid 5985 +local/remote: 127.0.0.1:56765 <-> :0 (LISTEN) +local/remote: 127.0.0.1:56765 <-> 127.0.0.1:50955 (ESTABLISHED) +local/remote: 100.76.175.164:52353 <-> 54.241.191.232:443 (ESTABLISHED) +``` + #### $ gops tree To display a process tree with all the running Go processes, run the following command: @@ -199,4 +217,3 @@ gops allows you to start the runtime tracer for 5 seconds and examine the result ```sh $ gops trace (|) ``` - diff --git a/main.go b/main.go index 0a465027..1b73131c 100644 --- a/main.go +++ b/main.go @@ -55,11 +55,15 @@ func main() { // See if it is a PID. pid, err := strconv.Atoi(cmd) if err == nil { - var interval int + var period time.Duration if len(os.Args) >= 3 { - interval, _ = strconv.Atoi(os.Args[2]) + period, err = time.ParseDuration(os.Args[2]) + if err != nil { + secs, _ := strconv.Atoi(os.Args[2]) + period = time.Duration(secs) * time.Second + } } - processInfo(pid, interval) + processInfo(pid, period) return } @@ -131,7 +135,7 @@ func processes() { } } -func processInfo(pid, interval int) { +func processInfo(pid int, period time.Duration) { p, err := process.NewProcess(int32(pid)) if err != nil { log.Fatalf("Cannot read process info: %v", err) @@ -148,9 +152,9 @@ func processInfo(pid, interval int) { if v, err := p.CPUPercent(); err == nil { fmt.Printf("cpu usage:\t%.3f%%\n", v) } - if interval > 0 { - if v, err := cpuPercentWithinTime(p, time.Second*time.Duration(interval)); err == nil { - fmt.Printf("cpu usage (%ds):\t%.3f%%\n", interval, v) + if period > 0 { + if v, err := cpuPercentWithinTime(p, period); err == nil { + fmt.Printf("cpu usage (%v):\t%.3f%%\n", period, v) } } if v, err := p.Username(); err == nil {