mirror of
https://github.com/google/gops.git
synced 2025-07-16 00:14:17 +02:00
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.
This commit is contained in:
committed by
Tobias Klauser
parent
f870d0110a
commit
8918f15c19
21
README.md
21
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*`).
|
Note that processes running the agent are marked with `*` next to the PID (e.g. `4132*`).
|
||||||
|
|
||||||
#### $ gops \<pid\>
|
#### $ gops \<pid\> [duration]
|
||||||
|
|
||||||
To report more information about a process, run `gops` followed by a PID:
|
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)
|
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 <pid> 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
|
#### $ gops tree
|
||||||
|
|
||||||
To display a process tree with all the running Go processes, run the following command:
|
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
|
```sh
|
||||||
$ gops trace (<pid>|<addr>)
|
$ gops trace (<pid>|<addr>)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
18
main.go
18
main.go
@ -55,11 +55,15 @@ func main() {
|
|||||||
// See if it is a PID.
|
// See if it is a PID.
|
||||||
pid, err := strconv.Atoi(cmd)
|
pid, err := strconv.Atoi(cmd)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
var interval int
|
var period time.Duration
|
||||||
if len(os.Args) >= 3 {
|
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
|
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))
|
p, err := process.NewProcess(int32(pid))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Cannot read process info: %v", err)
|
log.Fatalf("Cannot read process info: %v", err)
|
||||||
@ -148,9 +152,9 @@ func processInfo(pid, interval int) {
|
|||||||
if v, err := p.CPUPercent(); err == nil {
|
if v, err := p.CPUPercent(); err == nil {
|
||||||
fmt.Printf("cpu usage:\t%.3f%%\n", v)
|
fmt.Printf("cpu usage:\t%.3f%%\n", v)
|
||||||
}
|
}
|
||||||
if interval > 0 {
|
if period > 0 {
|
||||||
if v, err := cpuPercentWithinTime(p, time.Second*time.Duration(interval)); err == nil {
|
if v, err := cpuPercentWithinTime(p, period); err == nil {
|
||||||
fmt.Printf("cpu usage (%ds):\t%.3f%%\n", interval, v)
|
fmt.Printf("cpu usage (%v):\t%.3f%%\n", period, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if v, err := p.Username(); err == nil {
|
if v, err := p.Username(); err == nil {
|
||||||
|
Reference in New Issue
Block a user