2019-05-06 16:11:38 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"github.com/shirou/gopsutil/process"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2019-05-08 13:39:08 +02:00
|
|
|
var currUser, prevUser, currSystem, prevSystem, cpuUsage float64
|
|
|
|
|
2019-05-07 14:49:20 +02:00
|
|
|
flagPID := flag.Int("p", 1, "PID for process to monitor")
|
|
|
|
flagNUM := flag.Int("n", -1, "Stop after n times of displaying stat")
|
2019-05-06 16:11:38 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
p, _ := process.NewProcess(int32(*flagPID))
|
2019-05-07 15:37:53 +02:00
|
|
|
name, _ := p.Name()
|
2019-05-07 14:50:03 +02:00
|
|
|
cnt := 0
|
2019-05-06 16:11:38 +02:00
|
|
|
|
2019-05-08 13:39:08 +02:00
|
|
|
v, _ := p.Times()
|
|
|
|
currUser = v.User
|
|
|
|
currSystem = v.System
|
|
|
|
|
|
|
|
for !(*flagNUM >= 0 && cnt >= *flagNUM) {
|
|
|
|
|
2019-05-08 11:32:29 +02:00
|
|
|
v, err := p.Times()
|
2019-05-07 14:49:20 +02:00
|
|
|
|
2019-05-06 16:11:38 +02:00
|
|
|
if err == nil {
|
2019-05-08 13:39:08 +02:00
|
|
|
// keep old counters, needed to calculated variance
|
|
|
|
prevUser = currUser
|
|
|
|
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)
|
2019-05-06 16:11:38 +02:00
|
|
|
} else {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
2019-05-07 14:49:20 +02:00
|
|
|
cnt++
|
2019-05-06 16:11:38 +02:00
|
|
|
}
|
|
|
|
}
|