From f608f636cefbc864a6dcc69ec1663f4d42d7865c Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Thu, 13 Dec 2018 19:24:34 -0800 Subject: [PATCH] Use old 'ps' parsing method on freebsd --- src/widgets/proc_darwin.go | 55 ------------------------------------- src/widgets/proc_linux.go | 56 ++++++++++++++++++++++++++++++++++++++ src/widgets/proc_other.go | 29 ++++++++++---------- 3 files changed, 70 insertions(+), 70 deletions(-) delete mode 100644 src/widgets/proc_darwin.go create mode 100644 src/widgets/proc_linux.go diff --git a/src/widgets/proc_darwin.go b/src/widgets/proc_darwin.go deleted file mode 100644 index 7fce6b0c..00000000 --- a/src/widgets/proc_darwin.go +++ /dev/null @@ -1,55 +0,0 @@ -package widgets - -import ( - "log" - "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, err := exec.Command("ps", "-axo", "pid,comm,pcpu,pmem,args").Output() - if err != nil { - log.Printf("failed to execute 'ps' command: %v", err) - } - // converts to []string and removes the header - strOutput := strings.Split(strings.TrimSpace(string(output)), "\n")[1:] - processes := []Process{} - for _, line := range strOutput { - split := strings.Fields(line) - pid, err := strconv.Atoi(split[0]) - if err != nil { - log.Printf("failed to convert first field to int: %v. split: %v", err, split) - } - cpu, err := strconv.ParseFloat(split[2], 64) - if err != nil { - log.Printf("failed to convert third field to float: %v. split: %v", err, split) - } - mem, err := strconv.ParseFloat(split[3], 64) - if err != nil { - log.Printf("failed to convert fourth field to float: %v. split: %v", err, split) - } - process := Process{ - PID: pid, - Command: split[1], - CPU: cpu, - Mem: mem, - Args: strings.Join(split[4:], " "), - } - processes = append(processes, process) - } - return processes -} diff --git a/src/widgets/proc_linux.go b/src/widgets/proc_linux.go new file mode 100644 index 00000000..dd3cae59 --- /dev/null +++ b/src/widgets/proc_linux.go @@ -0,0 +1,56 @@ +package widgets + +import ( + "log" + "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, err := exec.Command("ps", "-axo", "pid:10,comm:50,pcpu:5,pmem:5,args").Output() + if err != nil { + log.Printf("failed to execute 'ps' command: %v", err) + } + + // converts to []string, removing trailing newline and header + processStrArr := strings.Split(strings.TrimSuffix(string(output), "\n"), "\n")[1:] + + processes := []Process{} + for _, line := range processStrArr { + pid, err := strconv.Atoi(strings.TrimSpace(line[0:10])) + if err != nil { + log.Printf("failed to convert PID to int: %v. line: %v", err, line) + } + cpu, err := strconv.ParseFloat(strings.TrimSpace(line[63:68]), 64) + if err != nil { + log.Printf("failed to convert CPU usage to float: %v. line: %v", err, line) + } + mem, err := strconv.ParseFloat(strings.TrimSpace(line[69:74]), 64) + if err != nil { + log.Printf("failed to convert Mem usage to float: %v. line: %v", err, line) + } + process := Process{ + PID: pid, + Command: strings.TrimSpace(line[11:61]), + CPU: cpu, + Mem: mem, + Args: line[74:], + } + processes = append(processes, process) + } + return processes +} diff --git a/src/widgets/proc_other.go b/src/widgets/proc_other.go index d5431ac3..6f66dfaf 100644 --- a/src/widgets/proc_other.go +++ b/src/widgets/proc_other.go @@ -1,4 +1,4 @@ -// +build linux freebsd +// +build freebsd darwin package widgets @@ -23,34 +23,33 @@ func (self *Proc) update() { } func Processes() []Process { - output, err := exec.Command("ps", "-axo", "pid:10,comm:50,pcpu:5,pmem:5,args").Output() + output, err := exec.Command("ps", "-axo", "pid,comm,pcpu,pmem,args").Output() if err != nil { log.Printf("failed to execute 'ps' command: %v", err) } - - // converts to []string, removing trailing newline and header - processStrArr := strings.Split(strings.TrimSuffix(string(output), "\n"), "\n")[1:] - + // converts to []string and removes the header + strOutput := strings.Split(strings.TrimSpace(string(output)), "\n")[1:] processes := []Process{} - for _, line := range processStrArr { - pid, err := strconv.Atoi(strings.TrimSpace(line[0:10])) + for _, line := range strOutput { + split := strings.Fields(line) + pid, err := strconv.Atoi(split[0]) if err != nil { - log.Printf("failed to convert PID to int: %v. line: %v", err, line) + log.Printf("failed to convert first field to int: %v. split: %v", err, split) } - cpu, err := strconv.ParseFloat(strings.TrimSpace(line[63:68]), 64) + cpu, err := strconv.ParseFloat(split[2], 64) if err != nil { - log.Printf("failed to convert CPU usage to float: %v. line: %v", err, line) + log.Printf("failed to convert third field to float: %v. split: %v", err, split) } - mem, err := strconv.ParseFloat(strings.TrimSpace(line[69:74]), 64) + mem, err := strconv.ParseFloat(split[3], 64) if err != nil { - log.Printf("failed to convert Mem usage to float: %v. line: %v", err, line) + log.Printf("failed to convert fourth field to float: %v. split: %v", err, split) } process := Process{ PID: pid, - Command: strings.TrimSpace(line[11:61]), + Command: split[1], CPU: cpu, Mem: mem, - Args: line[74:], + Args: strings.Join(split[4:], " "), } processes = append(processes, process) }