summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCaleb Bassi <calebjbassi@gmail.com>2018-05-15 13:13:22 -0700
committerCaleb Bassi <calebjbassi@gmail.com>2018-05-15 13:13:22 -0700
commitf243be7e69737582779292515f6a3a48fd2f14b9 (patch)
tree17140a33b219c779d31e838334af7dc9d321f202
parent3ab6ea23803046abbafac5fe4aa4749e750bbe27 (diff)
Re-add windows gopsutil process info retrieval
-rw-r--r--src/widgets/proc.go44
-rw-r--r--src/widgets/proc_unix.go (renamed from src/psutil/process.go)21
-rw-r--r--src/widgets/proc_windows.go28
3 files changed, 59 insertions, 34 deletions
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
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()
+}