summaryrefslogtreecommitdiffstats
path: root/src/widgets/proc_unix.go
blob: 2253aca42406d736f77ba292ea2f24ebd6495473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// +build freebsd linux darwin

package widgets

import (
	"os/exec"
	"strconv"
	"strings"
)

func (self *Proc) update() {
	processes := Processes()
	// have to iterate like this in order to actually change the value
	for i, _ := range processes {
		processes[i].CPU /= self.cpuCount
	}

	self.ungroupedProcs = processes
	self.groupedProcs = Group(processes)

	self.Sort()
}

func Processes() []Process {
	output, _ := exec.Command("ps", "--no-headers", "-axo", "pid,comm,pcpu,pmem,args").Output()
	strOutput := strings.TrimSpace(string(output))
	processes := []Process{}
	for _, line := range strings.Split(strOutput, "\n") {
		split := strings.Fields(line)
		pid, _ := strconv.Atoi(split[0])
		cpu, _ := strconv.ParseFloat(split[2], 64)
		mem, _ := strconv.ParseFloat(split[3], 64)
		process := Process{
			PID:     pid,
			Command: split[1],
			CPU:     cpu,
			Mem:     mem,
			Args:    strings.Join(split[4:], " "),
		}
		processes = append(processes, process)
	}
	return processes
}