-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
69 lines (59 loc) · 1.5 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
package main
import (
"flag"
"fmt"
"log"
"os"
"sort"
"strconv"
"github.com/grandecola/smaps/smaps"
)
func main() {
pidVar := flag.Int("pid", 0, "process pid to compute mem usage for (default this pid)")
filterVar := flag.String("filter", "", "filter mapped files using regular expression")
flag.Parse()
if *pidVar == 0 {
*pidVar = os.Getpid()
}
sf, err := smaps.ReadSmaps(*pidVar, *filterVar)
if err != nil {
log.Fatal(err)
}
printSmapsInfo(sf)
printTop10Maps(sf)
}
func printSmapsInfo(sf *smaps.ProcInfo) {
fmt.Println("Summary:")
fmt.Printf(" Total mappings: %v\n", sf.Count)
fmt.Printf(" Total size: %v\n", toStringMemory(sf.Total))
fmt.Printf(" Total RSS: %v\n", toStringMemory(sf.RSS))
fmt.Printf(" Total PSS: %v\n", toStringMemory(sf.PSS))
}
func printTop10Maps(sf *smaps.ProcInfo) {
fmt.Println("Top 10 mappings:")
sort.Slice(sf.Maps, func(i, j int) bool {
return sf.Maps[i].PSS > sf.Maps[j].PSS
})
for i, mf := range sf.Maps[:min(10, len(sf.Maps))] {
fmt.Printf(" %v. {%v} PSS: %v, RSS: %v, Size: %v\n", i+1, mf.Name, toStringMemory(mf.PSS),
toStringMemory(mf.RSS), toStringMemory(mf.Size))
}
}
func toStringMemory(m uint64) string {
switch {
case m > 1024*1024*1024:
return strconv.Itoa(int(m)/1024/1024/1024) + " GB"
case m > 1024*1024:
return strconv.Itoa(int(m/1024/1024)) + " MB"
case m > 1024:
return strconv.Itoa(int(m/1024)) + " KB"
default:
return strconv.Itoa(int(m)) + " Bytes"
}
}
func min(a, b int) int {
if a < b {
return a
}
return b
}