2018-12-13 19:24:34 -08:00
|
|
|
// +build freebsd darwin
|
2018-12-13 19:13:28 -08:00
|
|
|
|
2018-05-15 13:13:22 -07:00
|
|
|
package widgets
|
2018-05-15 11:10:30 -07:00
|
|
|
|
|
|
|
import (
|
2018-12-13 21:59:45 -08:00
|
|
|
"fmt"
|
2018-12-04 21:44:25 -08:00
|
|
|
"log"
|
2018-05-15 11:10:30 -07:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-02-09 16:43:09 -06:00
|
|
|
const (
|
|
|
|
// Define column widths for ps output used in Processes()
|
|
|
|
five = "12345"
|
|
|
|
ten = five + five
|
|
|
|
fifty = ten + ten + ten + ten + ten
|
|
|
|
)
|
|
|
|
|
2018-05-15 13:13:22 -07:00
|
|
|
func (self *Proc) update() {
|
2018-12-13 21:59:45 -08:00
|
|
|
processes, err := Processes()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to retrieve processes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-15 13:13:22 -07:00
|
|
|
// have to iterate like this in order to actually change the value
|
2018-12-04 21:44:25 -08:00
|
|
|
for i := range processes {
|
2018-05-15 13:13:22 -07:00
|
|
|
processes[i].CPU /= self.cpuCount
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ungroupedProcs = processes
|
|
|
|
self.groupedProcs = Group(processes)
|
|
|
|
|
|
|
|
self.Sort()
|
2018-05-15 11:10:30 -07:00
|
|
|
}
|
|
|
|
|
2018-12-13 21:59:45 -08:00
|
|
|
func Processes() ([]Process, error) {
|
2019-02-09 16:43:09 -06:00
|
|
|
keywords := fmt.Sprintf("pid=%s,comm=%s,pcpu=%s,pmem=%s,args", ten, fifty, five, five)
|
2019-02-10 08:16:12 -06:00
|
|
|
output, err := exec.Command("ps", "-caxo", keywords).Output()
|
2018-12-04 21:44:25 -08:00
|
|
|
if err != nil {
|
2018-12-13 21:59:45 -08:00
|
|
|
return nil, fmt.Errorf("failed to execute 'ps' command: %v", err)
|
2018-12-04 21:44:25 -08:00
|
|
|
}
|
2019-02-09 16:43:09 -06:00
|
|
|
|
2018-12-13 19:24:34 -08:00
|
|
|
// converts to []string and removes the header
|
|
|
|
strOutput := strings.Split(strings.TrimSpace(string(output)), "\n")[1:]
|
2019-02-09 16:43:09 -06:00
|
|
|
|
2018-05-15 11:10:30 -07:00
|
|
|
processes := []Process{}
|
2018-12-13 19:24:34 -08:00
|
|
|
for _, line := range strOutput {
|
2019-02-09 16:43:09 -06:00
|
|
|
pid, err := strconv.Atoi(strings.TrimSpace(line[0:10]))
|
2018-12-04 21:44:25 -08:00
|
|
|
if err != nil {
|
2019-02-09 16:43:09 -06:00
|
|
|
log.Printf("failed to convert first field to int: %v. split: %v", err, line)
|
2018-12-04 21:44:25 -08:00
|
|
|
}
|
2019-02-09 16:43:09 -06:00
|
|
|
cpu, err := strconv.ParseFloat(strings.TrimSpace(line[63:68]), 64)
|
2018-12-04 21:44:25 -08:00
|
|
|
if err != nil {
|
2019-02-09 16:43:09 -06:00
|
|
|
log.Printf("failed to convert third field to float: %v. split: %v", err, line)
|
2018-12-04 21:44:25 -08:00
|
|
|
}
|
2019-02-09 16:43:09 -06:00
|
|
|
mem, err := strconv.ParseFloat(strings.TrimSpace(line[69:74]), 64)
|
2018-12-04 21:44:25 -08:00
|
|
|
if err != nil {
|
2019-02-09 16:43:09 -06:00
|
|
|
log.Printf("failed to convert fourth field to float: %v. split: %v", err, line)
|
2018-12-04 21:44:25 -08:00
|
|
|
}
|
2018-05-15 11:10:30 -07:00
|
|
|
process := Process{
|
|
|
|
PID: pid,
|
2019-02-09 16:43:09 -06:00
|
|
|
Command: strings.TrimSpace(line[11:61]),
|
2018-05-15 11:10:30 -07:00
|
|
|
CPU: cpu,
|
|
|
|
Mem: mem,
|
2019-02-09 16:43:09 -06:00
|
|
|
Args: line[74:],
|
2018-05-15 11:10:30 -07:00
|
|
|
}
|
|
|
|
processes = append(processes, process)
|
|
|
|
}
|
2018-12-13 21:59:45 -08:00
|
|
|
return processes, nil
|
2018-05-15 11:10:30 -07:00
|
|
|
}
|