Skip to content

Commit

Permalink
feat: added cpu usage to process table
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Oct 3, 2024
1 parent 9508b31 commit 6576fb2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
20 changes: 14 additions & 6 deletions src/app/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ func (p *Process) updateProcState() {
p.procState.SystemTime = durationToString(dur)
p.procState.Age = dur
p.procState.Name = p.getName()
p.procState.Mem = p.getMemUsage()
p.procState.Mem, p.procState.CPU = p.getResourceUsage()
}
p.procState.IsRunning = isRunning
p.procState.IsElevated = p.procConf.IsElevated
Expand All @@ -532,24 +532,32 @@ func (p *Process) getStartTime() time.Time {
return p.startTime
}

func (p *Process) getMemUsage() int64 {
func (p *Process) getResourceUsage() (int64, float64) {
if p.procConf.IsDaemon {
return 0
return -1, -1
}
proc, err := puproc.NewProcess(int32(p.procState.Pid))
if err != nil {
log.Err(err).Msgf("Could not find process")
return -1
return -1, -1
}
meminfo, err := proc.MemoryInfo()
if err != nil {
log.Err(err).
Str("process", p.getName()).
Int("pid", p.procState.Pid).
Msg("Error retrieving memory stats")
return -1
return -1, -1
}
cpuPercent, err := proc.CPUPercentWithContext(context.Background())
if err != nil {
log.Err(err).
Str("process", p.getName()).
Int("pid", p.procState.Pid).
Msg("Error retrieving cpu stats")
return int64(meminfo.RSS), -1
}
return int64(meminfo.RSS)
return int64(meminfo.RSS), cpuPercent
}

func (p *Process) handleInput(pipe io.WriteCloser) {
Expand Down
25 changes: 22 additions & 3 deletions src/tui/proc-table.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type tableRowValues struct {
status string
age string
mem string
cpu string
health string
restarts string
exitCode string
Expand Down Expand Up @@ -113,6 +114,7 @@ func setRowValues(procTable *tview.Table, row int, rowVals tableRowValues) {
procTable.SetCell(row, int(ProcessStateAge), tview.NewTableCell(rowVals.age).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(rowVals.fgColor))
procTable.SetCell(row, int(ProcessStateHealth), tview.NewTableCell(rowVals.health).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(rowVals.fgColor))
procTable.SetCell(row, int(ProcessStateMem), tview.NewTableCell(rowVals.mem).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(rowVals.fgColor))
procTable.SetCell(row, int(ProcessStateCPU), tview.NewTableCell(rowVals.cpu).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(rowVals.fgColor))
procTable.SetCell(row, int(ProcessStateRestarts), tview.NewTableCell(rowVals.restarts).SetAlign(tview.AlignRight).SetExpansion(0).SetTextColor(rowVals.fgColor))
procTable.SetCell(row, int(ProcessStateExit), tview.NewTableCell(rowVals.exitCode).SetAlign(tview.AlignRight).SetExpansion(0).SetTextColor(rowVals.fgColor))
}
Expand Down Expand Up @@ -176,6 +178,7 @@ func (pv *pcView) createProcTable() *tview.Table {
ProcessStateAge: "AGE(A)",
ProcessStateHealth: "HEALTH(H)",
ProcessStateMem: "MEM(M)",
ProcessStateCPU: "CPU(U)",
ProcessStateRestarts: "RESTARTS(R)",
ProcessStateExit: "EXIT CODE(E)",
}
Expand All @@ -195,6 +198,8 @@ func (pv *pcView) createProcTable() *tview.Table {
pv.setTableSorter(ProcessStateHealth)
} else if event.Rune() == 'M' {
pv.setTableSorter(ProcessStateMem)
} else if event.Rune() == 'U' {
pv.setTableSorter(ProcessStateCPU)
} else if event.Rune() == 'R' {
pv.setTableSorter(ProcessStateRestarts)
} else if event.Rune() == 'E' {
Expand Down Expand Up @@ -371,16 +376,29 @@ func byteCountIEC(b int64) string {
float64(b)/float64(div), "KMGTPE"[exp])
}

func getStrForMem(mem int64) string {
func getStrForMem(mem int64, running bool) string {
if !running {
return types.PlaceHolderValue
}
if mem < 0 {
return "unknown"
}
if mem == 0 {
return "-"
return types.PlaceHolderValue
}
return byteCountIEC(mem)
}

func getStrForCPU(cpu float64, running bool) string {
if !running {
return types.PlaceHolderValue
}
if cpu < 0 {
return "unknown"
}
return fmt.Sprintf("%.1f%%", cpu)
}

func getStrForRestarts(restarts int) string {
if restarts == 0 {
return types.PlaceHolderValue
Expand Down Expand Up @@ -414,7 +432,8 @@ func (pv *pcView) getTableRowValues(state types.ProcessState) tableRowValues {
status: state.Status,
age: state.SystemTime,
health: state.Health,
mem: getStrForMem(state.Mem),
mem: getStrForMem(state.Mem, state.IsRunning),
cpu: getStrForCPU(state.CPU, state.IsRunning),
restarts: getStrForRestarts(state.Restarts),
exitCode: getStrForExitCode(state),
}
Expand Down
2 changes: 1 addition & 1 deletion src/tui/proc-table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestMemToString(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getStrForMem(tt.args.mem); got != tt.want {
if got := getStrForMem(tt.args.mem, false); got != tt.want {
t.Errorf("getStrForMem() = %v, want %v", got, tt.want)
}
})
Expand Down
11 changes: 9 additions & 2 deletions src/tui/procstate_sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ const (
ProcessStateAge ColumnID = 5
ProcessStateHealth ColumnID = 6
ProcessStateMem ColumnID = 7
ProcessStateRestarts ColumnID = 8
ProcessStateExit ColumnID = 9
ProcessStateCPU ColumnID = 8
ProcessStateRestarts ColumnID = 9
ProcessStateExit ColumnID = 10
)

var columnNames = map[ColumnID]string{
Expand All @@ -33,6 +34,7 @@ var columnNames = map[ColumnID]string{
ProcessStateAge: "AGE",
ProcessStateHealth: "HEALTH",
ProcessStateMem: "MEM",
ProcessStateCPU: "CPU",
ProcessStateRestarts: "RESTARTS",
ProcessStateExit: "EXIT",
}
Expand All @@ -46,6 +48,7 @@ var columnIDs = map[string]ColumnID{
"AGE": ProcessStateAge,
"HEALTH": ProcessStateHealth,
"MEM": ProcessStateMem,
"CPU": ProcessStateCPU,
"RESTARTS": ProcessStateRestarts,
"EXIT": ProcessStateExit,
}
Expand Down Expand Up @@ -156,6 +159,10 @@ func getSorter(sortBy ColumnID, states *types.ProcessesState) sortFn {
return func(i, j int) bool {
return states.States[i].Mem < states.States[j].Mem
}
case ProcessStateCPU:
return func(i, j int) bool {
return states.States[i].CPU < states.States[j].CPU
}
case ProcessStateName:
fallthrough
default:
Expand Down
2 changes: 2 additions & 0 deletions src/types/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func NewProcessState(proc *ProcessConfig) *ProcessState {
Restarts: 0,
ExitCode: 0,
Mem: 0,
CPU: 0,
Pid: 0,
}
if proc.Disabled {
Expand All @@ -210,6 +211,7 @@ type ProcessState struct {
IsElevated bool `json:"is_elevated"`
PasswordProvided bool `json:"password_provided"`
Mem int64 `json:"mem"`
CPU float64 `json:"cpu"`
IsRunning bool
}

Expand Down

0 comments on commit 6576fb2

Please sign in to comment.