xmtop/widgets/proc_windows.go

45 lines
1.2 KiB
Go
Raw Normal View History

package widgets
import (
2019-03-01 08:29:52 +08:00
"fmt"
"log"
psProc "github.com/shirou/gopsutil/process"
)
2019-03-01 08:29:52 +08:00
func getProcs() ([]Proc, error) {
psProcs, err := psProc.Processes()
if err != nil {
2019-03-01 08:29:52 +08:00
return nil, fmt.Errorf("failed to get processes from gopsutil: %v", err)
}
2019-03-01 08:29:52 +08:00
procs := make([]Proc, len(psProcs))
for i, psProc := range psProcs {
pid := psProc.Pid
command, err := psProc.Name()
if err != nil {
2019-03-01 08:29:52 +08:00
log.Printf("failed to get process command from gopsutil: %v. psProc: %v. i: %v. pid: %v", err, psProc, i, pid)
}
2019-03-01 08:29:52 +08:00
cpu, err := psProc.CPUPercent()
if err != nil {
2019-03-01 08:29:52 +08:00
log.Printf("failed to get process cpu usage from gopsutil: %v. psProc: %v. i: %v. pid: %v", err, psProc, i, pid)
}
2019-03-01 08:29:52 +08:00
mem, err := psProc.MemoryPercent()
if err != nil {
2019-03-01 08:29:52 +08:00
log.Printf("failed to get process memeory usage from gopsutil: %v. psProc: %v. i: %v. pid: %v", err, psProc, i, pid)
}
2019-03-01 08:29:52 +08:00
procs[i] = Proc{
2019-03-12 13:52:37 +08:00
Pid: int(pid),
CommandName: command,
CPU: cpu,
2019-03-12 13:52:37 +08:00
Mem: float64(mem),
// getting command args using gopsutil's Cmdline and CmdlineSlice wasn't
// working the last time I tried it, so we're just reusing 'command'
2019-03-12 13:52:37 +08:00
FullCommand: command,
}
}
2019-03-01 08:29:52 +08:00
return procs, nil
}