Display variance for user and system counters
This commit is contained in:
parent
72775dbd1c
commit
c66f6bc5a9
@ -4,12 +4,13 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/shirou/gopsutil/process"
|
"github.com/shirou/gopsutil/process"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
var currUser, prevUser, currSystem, prevSystem, cpuUsage float64
|
||||||
|
|
||||||
flagPID := flag.Int("p", 1, "PID for process to monitor")
|
flagPID := flag.Int("p", 1, "PID for process to monitor")
|
||||||
flagNUM := flag.Int("n", -1, "Stop after n times of displaying stat")
|
flagNUM := flag.Int("n", -1, "Stop after n times of displaying stat")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@ -18,16 +19,26 @@ func main() {
|
|||||||
name, _ := p.Name()
|
name, _ := p.Name()
|
||||||
cnt := 0
|
cnt := 0
|
||||||
|
|
||||||
for {
|
v, _ := p.Times()
|
||||||
|
currUser = v.User
|
||||||
|
currSystem = v.System
|
||||||
|
|
||||||
|
for !(*flagNUM >= 0 && cnt >= *flagNUM) {
|
||||||
|
|
||||||
v, err := p.Times()
|
v, err := p.Times()
|
||||||
if *flagNUM >= 0 && cnt >= *flagNUM {
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
cpuUsage, _ := p.CPUPercent()
|
// keep old counters, needed to calculated variance
|
||||||
fmt.Printf("%d %s cpu: %5.2f%% user: %5.2f system: %5.2f iowait: %5.2f irq: %5.2f softirq: %5.2f\n",
|
prevUser = currUser
|
||||||
time.Now().Unix(), name, cpuUsage, v.User, v.System, v.Iowait, v.Irq, v.Softirq)
|
prevSystem = currSystem
|
||||||
|
|
||||||
|
currUser = v.User
|
||||||
|
currSystem = v.System
|
||||||
|
|
||||||
|
cpuUsage, _ = p.CPUPercent()
|
||||||
|
|
||||||
|
fmt.Printf("%d %s cpu: %5.2f%% user: %5.2f (%+5.2f) system: %5.2f (%+5.2f) iowait: %5.2f irq: %5.2f softirq: %5.2f\n",
|
||||||
|
time.Now().Unix(), name, cpuUsage, currUser, currUser-prevUser, currSystem, currSystem-prevSystem, v.Iowait, v.Irq, v.Softirq)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user