From f243be7e69737582779292515f6a3a48fd2f14b9 Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Tue, 15 May 2018 13:13:22 -0700 Subject: [PATCH] Re-add windows gopsutil process info retrieval --- src/widgets/proc.go | 44 ++++++++----------- .../process.go => widgets/proc_unix.go} | 21 +++++---- src/widgets/proc_windows.go | 28 ++++++++++++ 3 files changed, 59 insertions(+), 34 deletions(-) rename src/{psutil/process.go => widgets/proc_unix.go} (67%) create mode 100644 src/widgets/proc_windows.go diff --git a/src/widgets/proc.go b/src/widgets/proc.go index 3034aa0..47e50d1 100644 --- a/src/widgets/proc.go +++ b/src/widgets/proc.go @@ -7,7 +7,6 @@ import ( "strconv" "time" - "github.com/cjbassi/gotop/src/psutil" ui "github.com/cjbassi/termui" psCPU "github.com/shirou/gopsutil/cpu" ) @@ -17,13 +16,21 @@ const ( DOWN = "▼" ) +// Process represents each process. +type Process struct { + PID int + Command string + CPU float64 + Mem float64 +} + type Proc struct { *ui.Table cpuCount float64 interval time.Duration sortMethod string - groupedProcs []psutil.Process - ungroupedProcs []psutil.Process + groupedProcs []Process + ungroupedProcs []Process group bool KeyPressed chan bool DefaultColWidths []int @@ -63,19 +70,6 @@ func NewProc(keyPressed chan bool) *Proc { return self } -func (self *Proc) update() { - processes := psutil.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() -} - // Sort sorts either the grouped or ungrouped []Process based on the sortMethod. // Called with every update, when the sort method is changed, and when processes are grouped and ungrouped. func (self *Proc) Sort() { @@ -216,19 +210,19 @@ func (self *Proc) keyBinds() { // Group groupes a []Process based on command name. // The first field changes from PID to count. // CPU and Mem are added together for each Process. -func Group(P []psutil.Process) []psutil.Process { - groupedP := make(map[string]psutil.Process) +func Group(P []Process) []Process { + groupedP := make(map[string]Process) for _, process := range P { val, ok := groupedP[process.Command] if ok { - groupedP[process.Command] = psutil.Process{ + groupedP[process.Command] = Process{ val.PID + 1, val.Command, val.CPU + process.CPU, val.Mem + process.Mem, } } else { - groupedP[process.Command] = psutil.Process{ + groupedP[process.Command] = Process{ 1, process.Command, process.CPU, @@ -237,7 +231,7 @@ func Group(P []psutil.Process) []psutil.Process { } } - groupList := make([]psutil.Process, len(groupedP)) + groupList := make([]Process, len(groupedP)) var i int for _, val := range groupedP { groupList[i] = val @@ -248,7 +242,7 @@ func Group(P []psutil.Process) []psutil.Process { } // FieldsToStrings converts a []Process to a [][]string -func FieldsToStrings(P []psutil.Process) [][]string { +func FieldsToStrings(P []Process) [][]string { strings := make([][]string, len(P)) for i, p := range P { strings[i] = make([]string, 4) @@ -275,7 +269,7 @@ func (self *Proc) Kill() { // []Process Sorting // ///////////////////////////////////////////////////////////////////////////////// -type ProcessByCPU []psutil.Process +type ProcessByCPU []Process // Len implements Sort interface func (P ProcessByCPU) Len() int { @@ -292,7 +286,7 @@ func (P ProcessByCPU) Less(i, j int) bool { return P[i].CPU < P[j].CPU } -type ProcessByPID []psutil.Process +type ProcessByPID []Process // Len implements Sort interface func (P ProcessByPID) Len() int { @@ -309,7 +303,7 @@ func (P ProcessByPID) Less(i, j int) bool { return P[i].PID < P[j].PID } -type ProcessByMem []psutil.Process +type ProcessByMem []Process // Len implements Sort interface func (P ProcessByMem) Len() int { diff --git a/src/psutil/process.go b/src/widgets/proc_unix.go similarity index 67% rename from src/psutil/process.go rename to src/widgets/proc_unix.go index fcb683e..a485f5f 100644 --- a/src/psutil/process.go +++ b/src/widgets/proc_unix.go @@ -1,18 +1,22 @@ -package psutil +package widgets import ( - // "fmt" "os/exec" "strconv" "strings" ) -// Process represents each process. -type Process struct { - PID int - Command string - CPU float64 - Mem float64 +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 { @@ -21,7 +25,6 @@ func Processes() []Process { processes := []Process{} for _, line := range strings.Split(strOutput, "\n")[1:] { split := strings.Fields(line) - // fmt.Println(split) pid, _ := strconv.Atoi(split[0]) cpu, _ := strconv.ParseFloat(split[2], 64) mem, _ := strconv.ParseFloat(split[3], 64) diff --git a/src/widgets/proc_windows.go b/src/widgets/proc_windows.go new file mode 100644 index 0000000..e59ea27 --- /dev/null +++ b/src/widgets/proc_windows.go @@ -0,0 +1,28 @@ +package widgets + +import ( + psProc "github.com/shirou/gopsutil/process" +) + +func (self *Proc) update() { + psProcesses, _ := psProc.Processes() + processes := make([]Process, len(psProcesses)) + for i, psProcess := range psProcesses { + pid := psProcess.Pid + command, _ := psProcess.Name() + cpu, _ := psProcess.CPUPercent() + mem, _ := psProcess.MemoryPercent() + + processes[i] = Process{ + int(pid), + command, + cpu / self.cpuCount, + float64(mem), + } + } + + self.ungroupedProcs = processes + self.groupedProcs = Group(processes) + + self.Sort() +}