Skip to content

Commit 8918f15

Browse files
committed
Allow to specify CPU usage period as duration string
The current implementation assumes the given duration in seconds. Instead, also allow to specify a duration in the format as expected by `time.ParseDuration` and use that if found. If a plain integer is specified, it is still parsed as a duration in seconds.
1 parent f870d01 commit 8918f15

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ The output displays:
7979

8080
Note that processes running the agent are marked with `*` next to the PID (e.g. `4132*`).
8181

82-
#### $ gops \<pid\>
82+
#### $ gops \<pid\> [duration]
8383

8484
To report more information about a process, run `gops` followed by a PID:
8585

@@ -96,6 +96,24 @@ local/remote: 127.0.0.1:56765 <-> 127.0.0.1:50955 (ESTABLISHED)
9696
local/remote: 100.76.175.164:52353 <-> 54.241.191.232:443 (ESTABLISHED)
9797
```
9898

99+
If an optional duration is specified in the format as expected by
100+
[`time.ParseDuration`](https://golang.org/pkg/time/#ParseDuration), the CPU
101+
usage for the given time period is reported in addition:
102+
103+
```sh
104+
$ gops <pid> 2s
105+
parent PID: 5985
106+
threads: 27
107+
memory usage: 0.199%
108+
cpu usage: 0.139%
109+
cpu usage (2s): 0.271%
110+
username: jbd
111+
cmd+args: /Applications/Splice.app/Contents/Resources/Splice Helper.app/Contents/MacOS/Splice Helper -pid 5985
112+
local/remote: 127.0.0.1:56765 <-> :0 (LISTEN)
113+
local/remote: 127.0.0.1:56765 <-> 127.0.0.1:50955 (ESTABLISHED)
114+
local/remote: 100.76.175.164:52353 <-> 54.241.191.232:443 (ESTABLISHED)
115+
```
116+
99117
#### $ gops tree
100118

101119
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
199217
```sh
200218
$ gops trace (<pid>|<addr>)
201219
```
202-

main.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ func main() {
5555
// See if it is a PID.
5656
pid, err := strconv.Atoi(cmd)
5757
if err == nil {
58-
var interval int
58+
var period time.Duration
5959
if len(os.Args) >= 3 {
60-
interval, _ = strconv.Atoi(os.Args[2])
60+
period, err = time.ParseDuration(os.Args[2])
61+
if err != nil {
62+
secs, _ := strconv.Atoi(os.Args[2])
63+
period = time.Duration(secs) * time.Second
64+
}
6165
}
62-
processInfo(pid, interval)
66+
processInfo(pid, period)
6367
return
6468
}
6569

@@ -131,7 +135,7 @@ func processes() {
131135
}
132136
}
133137

134-
func processInfo(pid, interval int) {
138+
func processInfo(pid int, period time.Duration) {
135139
p, err := process.NewProcess(int32(pid))
136140
if err != nil {
137141
log.Fatalf("Cannot read process info: %v", err)
@@ -148,9 +152,9 @@ func processInfo(pid, interval int) {
148152
if v, err := p.CPUPercent(); err == nil {
149153
fmt.Printf("cpu usage:\t%.3f%%\n", v)
150154
}
151-
if interval > 0 {
152-
if v, err := cpuPercentWithinTime(p, time.Second*time.Duration(interval)); err == nil {
153-
fmt.Printf("cpu usage (%ds):\t%.3f%%\n", interval, v)
155+
if period > 0 {
156+
if v, err := cpuPercentWithinTime(p, period); err == nil {
157+
fmt.Printf("cpu usage (%v):\t%.3f%%\n", period, v)
154158
}
155159
}
156160
if v, err := p.Username(); err == nil {

0 commit comments

Comments
 (0)