-
Notifications
You must be signed in to change notification settings - Fork 4
/
top.go
157 lines (125 loc) · 3.68 KB
/
top.go
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
package main
import (
"context"
"fmt"
"sort"
"strconv"
"strings"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/container"
"github.com/mum4k/termdash/widgets/text"
)
// Initializes both a top CPU and top memory box
// We do these together because they depend on the same call to `ps`
func newTopBoxes(ctx context.Context, config *PoptopConfig) ([]container.Option, []container.Option, error) {
cpuTextBox, err := text.New()
if err != nil {
return nil, nil, err
}
memTextBox, err := text.New()
if err != nil {
return nil, nil, err
}
// Sample top less frequently than configured for other charts because it's a point-in-time measure
interval := config.SampleInterval * 4
go periodic(ctx, interval, func() error {
topCpu, topMem, err := topProcesses(ctx, config)
if err != nil {
return err
}
lines := []string{}
for _, proc := range topCpu {
lineItem := fmt.Sprintf("%3.0f%% %-5d %s\n", proc.CpuPerc, proc.Pid, proc.Command)
lines = append(lines, lineItem)
}
fullText := strings.Join(lines, "")
cpuTextBox.Write(fullText, text.WriteReplace())
lines = []string{}
for _, proc := range topMem {
lineItem := fmt.Sprintf("%3.0f%% %-5d %s\n", proc.MemPerc, proc.Pid, proc.Command)
lines = append(lines, lineItem)
}
fullText = strings.Join(lines, "")
memTextBox.Write(fullText, text.WriteReplace())
return nil
})
cpuTitle := cell.NewRichTextString(ColorWidgetTitle).
AddOpt(cell.Bold()).
AddText(" Top CPU Processes (%, pid, command) ")
memTitle := cell.NewRichTextString(ColorWidgetTitle).
AddOpt(cell.Bold()).
AddText(" Top Memory Processes (%, pid, command) ")
cpuOpts := makeContainer(cpuTextBox, cpuTitle)
memOpts := makeContainer(memTextBox, memTitle)
return cpuOpts, memOpts, nil
}
type PsProcess struct {
User string
Pid int
CpuPerc float64
MemPerc float64
Command string
}
func parsePerc(field string) (float64, error) {
// In some locales the ps output uses a `,` rather than a `.`
cleanField := strings.Replace(field, ",", ".", 64)
return strconv.ParseFloat(cleanField, 64)
}
func GetPsProcesses(ctx context.Context) ([]*PsProcess, error) {
args := []string{"auxc"}
out, err := commandWithContext(ctx, "ps", args...)
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
processes := []*PsProcess{}
for _, line := range lines[1:] {
fields := strings.Fields(line)
if len(fields) < 10 {
break
}
cmd := strings.Join(fields[10:], " ")
pid, err := strconv.Atoi(fields[1])
if err != nil {
return nil, err
}
cpuPerc, err := parsePerc(fields[2])
if err != nil {
return nil, err
}
memPerc, err := parsePerc(fields[3])
if err != nil {
return nil, err
}
process := &PsProcess{
User: fields[0],
Pid: pid,
CpuPerc: cpuPerc,
MemPerc: memPerc,
Command: cmd,
}
processes = append(processes, process)
}
return processes, nil
}
func (this *PsProcess) String() string {
return fmt.Sprintf("%s,%d,%f,%f,%s\n", this.User, this.Pid, this.CpuPerc, this.MemPerc, this.Command)
}
// Create CPU and Memory top lists using output from a shared ps command execution.
func topProcesses(ctx context.Context, config *PoptopConfig) ([]*PsProcess, []*PsProcess, error) {
procs, err := GetPsProcesses(ctx)
if err != nil {
return nil, nil, err
}
sort.Slice(procs, func(i, j int) bool {
return procs[i].CpuPerc > procs[j].CpuPerc
})
procsByCpu := make([]*PsProcess, min(config.TopRowsShown, len(procs)))
copy(procsByCpu, procs)
sort.Slice(procs, func(i, j int) bool {
return procs[i].MemPerc > procs[j].MemPerc
})
procsByMem := make([]*PsProcess, min(config.TopRowsShown, len(procs)))
copy(procsByMem, procs)
return procsByCpu, procsByMem, nil
}