From 533161c2ed3167bfcda59e41a093112e6406d017 Mon Sep 17 00:00:00 2001 From: "Aditya Choudhary (c)" Date: Sun, 23 Jul 2017 02:39:35 +0530 Subject: [PATCH] added funtion for calculation of CPU usage percentage of a process --- process/process.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/process/process.go b/process/process.go index b97f777..f740e42 100644 --- a/process/process.go +++ b/process/process.go @@ -186,3 +186,20 @@ func (p *Process) MemoryPercent() (float32, error) { return (100 * float32(used) / float32(total)), nil } +// CPU_Percent returns how many percent of the CPU time this process uses +func (p *Process) CPUPercent() (float64, error) { + crt_time, err := p.CreateTime() + if err != nil { + return 0, err + } + + + cpu, err := p.Times() + if err != nil { + return 0, err + } + + + return (100 * (cpu.Total()) / float64(time.Now().Unix()-(crt_time/1000))), nil +} +