Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Switch to custom process info retrieval
Browse files Browse the repository at this point in the history
* Switch from gopsutil to using 'ps' for process info retrieval.
* Much faster now since we are getting all info in a batch.
* Should hopefully fix the issue on OSX where process info retrieval was
taking too long.
* CPU percentage reporting should also be imporoved, since it is more
realtime.
  • Loading branch information
cjbassi committed May 15, 2018
1 parent 716c642 commit b42d098
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
37 changes: 37 additions & 0 deletions src/psutil/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package psutil

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

// Process represents each process.
type Process struct {
PID int
Command string
CPU float64
Mem float64
}

func Processes() []Process {
output, _ := exec.Command("ps", "-acxo", "pid,comm,pcpu,pmem").Output()
strOutput := strings.TrimSpace(string(output))
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)
process := Process{
PID: pid,
Command: split[1],
CPU: cpu,
Mem: mem,
}
processes = append(processes, process)
}
return processes
}
54 changes: 18 additions & 36 deletions src/widgets/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,23 @@ import (
"strconv"
"time"

"github.com/cjbassi/gotop/src/psutil"
ui "github.com/cjbassi/termui"
psCPU "github.com/shirou/gopsutil/cpu"
psProc "github.com/shirou/gopsutil/process"
)

const (
UP = "▲"
DOWN = "▼"
)

// Process represents each process.
type Process struct {
PID int32
Command string
CPU float64
Mem float32
}

type Proc struct {
*ui.Table
cpuCount int
cpuCount float64
interval time.Duration
sortMethod string
groupedProcs []Process
ungroupedProcs []Process
groupedProcs []psutil.Process
ungroupedProcs []psutil.Process
group bool
KeyPressed chan bool
DefaultColWidths []int
Expand All @@ -42,7 +34,7 @@ func NewProc(keyPressed chan bool) *Proc {
self := &Proc{
Table: ui.NewTable(),
interval: time.Second,
cpuCount: cpuCount,
cpuCount: float64(cpuCount),
sortMethod: "c",
group: true,
KeyPressed: keyPressed,
Expand Down Expand Up @@ -72,21 +64,11 @@ func NewProc(keyPressed chan bool) *Proc {
}

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{
pid,
command,
cpu / float64(self.cpuCount),
mem,
}
processes := psutil.Processes()
for _, process := range processes {
process.CPU /= self.cpuCount
}

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

Expand Down Expand Up @@ -233,19 +215,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 []Process) []Process {
groupedP := make(map[string]Process)
func Group(P []psutil.Process) []psutil.Process {
groupedP := make(map[string]psutil.Process)
for _, process := range P {
val, ok := groupedP[process.Command]
if ok {
groupedP[process.Command] = Process{
groupedP[process.Command] = psutil.Process{
val.PID + 1,
val.Command,
val.CPU + process.CPU,
val.Mem + process.Mem,
}
} else {
groupedP[process.Command] = Process{
groupedP[process.Command] = psutil.Process{
1,
process.Command,
process.CPU,
Expand All @@ -254,7 +236,7 @@ func Group(P []Process) []Process {
}
}

groupList := make([]Process, len(groupedP))
groupList := make([]psutil.Process, len(groupedP))
var i int
for _, val := range groupedP {
groupList[i] = val
Expand All @@ -265,7 +247,7 @@ func Group(P []Process) []Process {
}

// FieldsToStrings converts a []Process to a [][]string
func FieldsToStrings(P []Process) [][]string {
func FieldsToStrings(P []psutil.Process) [][]string {
strings := make([][]string, len(P))
for i, p := range P {
strings[i] = make([]string, 4)
Expand All @@ -292,7 +274,7 @@ func (self *Proc) Kill() {
// []Process Sorting //
/////////////////////////////////////////////////////////////////////////////////

type ProcessByCPU []Process
type ProcessByCPU []psutil.Process

// Len implements Sort interface
func (P ProcessByCPU) Len() int {
Expand All @@ -309,7 +291,7 @@ func (P ProcessByCPU) Less(i, j int) bool {
return P[i].CPU < P[j].CPU
}

type ProcessByPID []Process
type ProcessByPID []psutil.Process

// Len implements Sort interface
func (P ProcessByPID) Len() int {
Expand All @@ -326,7 +308,7 @@ func (P ProcessByPID) Less(i, j int) bool {
return P[i].PID < P[j].PID
}

type ProcessByMem []Process
type ProcessByMem []psutil.Process

// Len implements Sort interface
func (P ProcessByMem) Len() int {
Expand Down

0 comments on commit b42d098

Please sign in to comment.