-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
311 lines (282 loc) · 7.26 KB
/
main.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"syscall"
"time"
ui "github.com/gizak/termui"
"github.com/gizak/termui/widgets"
)
// Program version
const version = "1.0"
// UID->username map cache
var ucache = make(map[uint32]string)
// Regex to get the process name out of stat file
var namergx = regexp.MustCompile(`\((.*?)\)`)
// Default sort key
var sortKey = "rss"
// Process holds information about a process
type Process struct {
Basepath string
PID int
Name, User, Command string
RSS, PSS, USS, Swap int
}
// scrapeSmaps sums select memory fields from /proc/<int>/smaps
func (p *Process) scrapeSmaps() error {
if p.Basepath == "" {
return fmt.Errorf("no path for Process")
}
leaf := filepath.Base(p.Basepath)
pid, err := strconv.Atoi(leaf)
if err != nil {
return err
}
p.PID = pid
smap := filepath.Join(p.Basepath, "smaps")
file, err := os.Open(smap)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
p.RSS += getSmapMem(line, "Rss")
p.PSS += getSmapMem(line, "Pss")
p.Swap += getSmapMem(line, "SwapPss")
p.USS += getSmapMem(line, "Private_Clean")
p.USS += getSmapMem(line, "Private_Dirty")
}
return nil
}
// PopulateInfo fills in the Process attributes
func (p *Process) PopulateInfo() error {
if err := p.scrapeSmaps(); err != nil {
return err
}
p.Name = getProcName(p.Basepath)
user, err := lookupUsername(p.Basepath)
if err != nil {
return err
}
p.User = user
return nil
}
// lookupUsername looks up username for uid if not already in cache
func lookupUsername(file string) (string, error) {
fileInfo, err := os.Stat(file)
if err != nil {
return "", err
}
uid := fileInfo.Sys().(*syscall.Stat_t).Uid
if ucache[uid] != "" {
return ucache[uid], nil
}
u, err := user.LookupId(strconv.FormatUint(uint64(uid), 10))
if err != nil {
return "", err
}
ucache[uid] = u.Username
return u.Username, nil
}
// getSmapMem is a helper function to read particular values from smaps
func getSmapMem(line, mment string) int {
if strings.HasPrefix(line, mment) {
f, err := strconv.Atoi(strings.Fields(line)[1])
if err != nil {
return 0
}
return f
}
return 0
}
// Determine if it's a process dir by checking if the dirname is an int
func isProc(path string) bool {
basename := filepath.Base(path)
_, err := strconv.Atoi(basename)
if err != nil {
return false
}
return true
}
// Get process name from stat file
func getProcName(path string) string {
namepath := filepath.Join(path, "stat")
statp, err := ioutil.ReadFile(namepath)
if err != nil {
// fmt.Printf("error reading stat [%v]\n", err)
return ""
}
match := namergx.FindStringSubmatch(string(statp))
if len(match) < 2 {
return ""
}
return match[1]
}
// Returns the process cmdline
func getCmdline(path string) string {
cmdpath := filepath.Join(path, "cmdline")
cmdline, err := ioutil.ReadFile(cmdpath)
if err != nil {
//fmt.Printf("read error [%v]\n", err)
return ""
}
cmdstring := string(cmdline)
return strings.Replace(cmdstring, "\x00", " ", -1)
}
// GetProcesses returns a collection of Processes
func GetProcesses(rootpath string) []*Process {
box := []*Process{}
dirs, err := ioutil.ReadDir(rootpath)
if err != nil {
fmt.Printf("readdir error [%v]\n", err)
}
for _, f := range dirs {
fname := filepath.Join(rootpath, f.Name())
if isProc(fname) {
p, ok := processIt(fname)
if ok {
box = append(box, p)
}
}
}
// Name sorts ascending, all else sorts descending
switch sortKey {
case "name":
sort.Slice(box, func(i, j int) bool { return box[i].Name < box[j].Name })
case "rss":
sort.Slice(box, func(i, j int) bool { return box[i].RSS > box[j].RSS })
case "pss":
sort.Slice(box, func(i, j int) bool { return box[i].PSS > box[j].PSS })
case "uss":
sort.Slice(box, func(i, j int) bool { return box[i].USS > box[j].USS })
case "swap":
sort.Slice(box, func(i, j int) bool { return box[i].Swap > box[j].Swap })
}
return box
}
// processIt returns a populated Process pointer
func processIt(fpath string) (*Process, bool) {
cmdline := getCmdline(fpath)
if cmdline != "" {
p := &Process{Basepath: fpath, Command: cmdline}
if err := p.PopulateInfo(); err != nil {
return nil, false
}
return p, true
}
return nil, false
}
// Fix this
// Print header and then the contents of each Process
// func printProcesses(a []*Process) {
// fmt.Printf("%6s %-16s %-14s %5s %5s %5s %5s %-80s",
// "PID", "Name", "User", "Swap", "USS", "PSS", "RSS", "Command")
// for _, p := range a {
// fmt.Printf("%6d %-16s %-14s %5d %5d %5d %5d %-80s",
// p.PID, p.Name, p.User, p.Swap, p.USS, p.PSS, p.RSS, p.Command)
// }
// }
// Formats the processes for the termui table
func tableFormat(a []*Process) [][]string {
tab := [][]string{{"PID", "Name", "User", "SwapPSS", "USS", "PSS", "RSS", "Command"},
{"---", "----", "----", "----", "---", "------", "---", "-------"}}
for _, p := range a {
tab = append(tab, []string{strconv.Itoa(p.PID), p.Name, p.User, strconv.Itoa(p.Swap),
strconv.Itoa(p.USS), strconv.Itoa(p.PSS), strconv.Itoa(p.RSS), p.Command})
}
return tab
}
func runTermui() {
if err := ui.Init(); err != nil {
log.Fatalln("cannot initialize termui")
}
defer ui.Close()
procs := GetProcesses("/proc")
termWidth, termHeight := ui.TerminalDimensions()
tb := widgets.NewTable()
tb.RowSeparator = false
tb.SetRect(0, 0, termWidth, termHeight)
tb.BorderStyle = ui.NewStyle(ui.ColorBlack)
tb.Border = false
tb.ColumnWidths = []int{6, 18, 10, 8, 8, 8, 8, termWidth - 66}
tb.Rows = tableFormat(procs)
ui.Render(tb)
// Event Handlers
uiEvents := ui.PollEvents()
ticker := time.NewTicker(time.Second).C
for {
select {
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return
case "r":
sortKey = "rss"
procs := GetProcesses("/proc")
tb.Rows = tableFormat(procs)
ui.Render(tb)
case "u":
sortKey = "uss"
procs := GetProcesses("/proc")
tb.Rows = tableFormat(procs)
ui.Render(tb)
case "p":
sortKey = "pss"
procs := GetProcesses("/proc")
tb.Rows = tableFormat(procs)
ui.Render(tb)
case "n":
sortKey = "name"
procs := GetProcesses("/proc")
tb.Rows = tableFormat(procs)
ui.Render(tb)
case "s":
sortKey = "swap"
procs := GetProcesses("/proc")
tb.Rows = tableFormat(procs)
ui.Render(tb)
case "<Resize>":
payload := e.Payload.(ui.Resize)
width, height := payload.Width, payload.Height
tb.SetRect(0, 0, width, height)
}
case <-ticker:
procs := GetProcesses("/proc")
tb.Rows = tableFormat(procs)
ui.Render(tb)
}
}
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Once running, hit n, r, p, s, or u to sort by Name, RSS, PSS, SwapPSS, or USS. RSS is default.\n")
flag.PrintDefaults()
os.Exit(0)
}
wantVersion := flag.Bool("version", false, "Print the version")
// wantOnce := flag.Bool("once", false, "Print table once and exit")
flag.StringVar(&sortKey, "sort", "rss", "Start sorted by name, rss, pss, swap, or uss")
flag.Parse()
if *wantVersion {
fmt.Println(version)
os.Exit(0)
}
// if *wantOnce {
// procs := GetProcesses("/proc")
// printProcesses(procs)
// os.Exit(0)
// }
runTermui()
}