summaryrefslogtreecommitdiff
path: root/internal/tui/dashboard/processes.go
blob: 34fdbc815303c620de893a58dcce099ec5049bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package dashboard

import (
	"fmt"
	"slices"
	"strconv"
	"strings"

	"ior/internal/statsengine"
	common "ior/internal/tui/common"
)

type processSortKey uint8

const (
	processSortKeyPID processSortKey = iota
	processSortKeyComm
	processSortKeySyscalls
	processSortKeyRate
	processSortKeyBytes
	processSortKeyAvgLatency
)

func renderProcesses(snap *statsengine.Snapshot, width, height int) string {
	return renderProcessesWithSort(snap, width, height, 0, 0, -1, tableSortState[processSortKey]{})
}

func renderProcessesWithOffset(snap *statsengine.Snapshot, width, height, offset, selectedCol, pidFilter int) string {
	return renderProcessesWithSort(snap, width, height, offset, selectedCol, pidFilter, tableSortState[processSortKey]{})
}

func renderProcessesWithSort(snap *statsengine.Snapshot, width, height, offset, selectedCol, pidFilter int, sortState tableSortState[processSortKey]) string {
	if snap == nil {
		return "Processes: waiting for stats..."
	}

	rows := processRows(sortedProcessTableRows(snap.Processes(), sortState))
	if len(rows) == 0 {
		return "Processes: no data"
	}

	columns := processColumns()
	out := renderSelectableTable(columns, rows, height, offset, selectedCol, "enter:filter", "s/S:sort", processSortHint(sortState), "v:mode", "b:metric")
	if pidFilter > 0 {
		out += "\n" + "Note: this tab is most useful with All PIDs."
	}
	return out
}

func processColumns() []common.TableColumn {
	return []common.TableColumn{
		{Title: "PID", Width: 8},
		{Title: "Comm", Width: 18},
		{Title: "Syscalls", Width: 10},
		{Title: "Rate/s", Width: 8},
		{Title: "Total Bytes", Width: 12},
		{Title: "Avg Latency", Width: 12},
	}
}

func sortedProcessTableRows(rows []statsengine.ProcessSnapshot, sortState tableSortState[processSortKey]) []statsengine.ProcessSnapshot {
	if len(rows) == 0 {
		return nil
	}
	if !sortState.active {
		return rows
	}

	sorted := slices.Clone(rows)
	slices.SortFunc(sorted, func(left, right statsengine.ProcessSnapshot) int {
		cmp := compareProcessBySort(left, right, sortState.key)
		if cmp == 0 {
			cmp = compareProcessDefault(left, right)
		}
		return sortState.apply(cmp)
	})
	return sorted
}

func compareProcessBySort(left, right statsengine.ProcessSnapshot, key processSortKey) int {
	switch key {
	case processSortKeyPID:
		return compareUint64Asc(uint64(left.PID), uint64(right.PID))
	case processSortKeyComm:
		return compareStringAsc(left.Comm, right.Comm)
	case processSortKeySyscalls:
		return compareUint64Desc(left.Syscalls, right.Syscalls)
	case processSortKeyRate:
		return compareFloat64Desc(left.RatePerSec, right.RatePerSec)
	case processSortKeyBytes:
		return compareUint64Desc(left.Bytes, right.Bytes)
	case processSortKeyAvgLatency:
		return compareFloat64Desc(left.AvgLatencyNs, right.AvgLatencyNs)
	default:
		return 0
	}
}

func compareProcessDefault(left, right statsengine.ProcessSnapshot) int {
	if cmp := compareUint64Desc(left.Syscalls, right.Syscalls); cmp != 0 {
		return cmp
	}
	if cmp := compareUint64Desc(left.Bytes, right.Bytes); cmp != 0 {
		return cmp
	}
	return compareUint64Asc(uint64(left.PID), uint64(right.PID))
}

func processSortKeyForColumn(column int) (processSortKey, bool) {
	switch column {
	case 0:
		return processSortKeyPID, true
	case 1:
		return processSortKeyComm, true
	case 2:
		return processSortKeySyscalls, true
	case 3:
		return processSortKeyRate, true
	case 4:
		return processSortKeyBytes, true
	case 5:
		return processSortKeyAvgLatency, true
	default:
		return 0, false
	}
}

func processSortHint(sortState tableSortState[processSortKey]) string {
	return "sort: " + processSortLabel(sortState)
}

func processSortLabel(sortState tableSortState[processSortKey]) string {
	if !sortState.active {
		return "default"
	}
	switch sortState.key {
	case processSortKeyPID:
		return sortLabelWithDirection("PID", true, sortState.reverse)
	case processSortKeyComm:
		return sortLabelWithDirection("Comm", true, sortState.reverse)
	case processSortKeySyscalls:
		return sortLabelWithDirection("Syscalls", false, sortState.reverse)
	case processSortKeyRate:
		return sortLabelWithDirection("Rate/s", false, sortState.reverse)
	case processSortKeyBytes:
		return sortLabelWithDirection("Total Bytes", false, sortState.reverse)
	case processSortKeyAvgLatency:
		return sortLabelWithDirection("Avg Latency", false, sortState.reverse)
	default:
		return "default"
	}
}

func findProcessOffset(rows []statsengine.ProcessSnapshot, pid uint32) (int, bool) {
	for idx, row := range rows {
		if row.PID == pid {
			return idx, true
		}
	}
	return 0, false
}

func processRows(processes []statsengine.ProcessSnapshot) [][]string {
	rows := make([][]string, 0, len(processes))
	for _, p := range processes {
		rows = append(rows, []string{
			strconv.FormatUint(uint64(p.PID), 10),
			truncateText(p.Comm, 18),
			strconv.FormatUint(p.Syscalls, 10),
			fmt.Sprintf("%.1f", p.RatePerSec),
			formatBytes(float64(p.Bytes)),
			formatDurationNs(p.AvgLatencyNs),
		})
	}
	return rows
}

func truncateText(value string, limit int) string {
	if len(value) <= limit {
		return value
	}
	if limit <= 3 {
		return value[:limit]
	}
	return strings.TrimSpace(value[:limit-3]) + "..."
}