-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
117 lines (98 loc) · 2.86 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
package main
import (
"bufio"
"flag"
"fmt"
"image/color"
"log"
"math"
"os"
"github.com/nikolaydubina/go-binsize-treemap/fmtbytecount"
"github.com/nikolaydubina/go-binsize-treemap/symtab"
"github.com/nikolaydubina/treemap"
"github.com/nikolaydubina/treemap/render"
)
const doc string = `
Go binary size treemap.
Examples
$ go tool nm -size <binary finename> | go-binsize-treemap > binsize.svg
$ go tool nm -size <binary finename> | c++filt | go-binsize-treemap > binsize.svg
Command options:
`
var grey = color.RGBA{128, 128, 128, 255}
func main() {
var (
w float64
h float64
marginBox float64
paddingBox float64
padding float64
maxDepth uint
outputCSV bool
)
flag.Usage = func() {
fmt.Fprint(flag.CommandLine.Output(), doc)
flag.PrintDefaults()
}
flag.Float64Var(&w, "w", 1024, "width of output")
flag.Float64Var(&h, "h", 1024, "height of output")
flag.Float64Var(&marginBox, "margin-box", 4, "margin between boxes")
flag.Float64Var(&paddingBox, "padding-box", 4, "padding between box border and content")
flag.Float64Var(&padding, "padding", 16, "padding around root content")
flag.UintVar(&maxDepth, "max-depth", 0, "if zero then no max depth is set, else will show only number of levels from root including")
flag.BoolVar(&outputCSV, "csv", false, "print as csv instead")
flag.Parse()
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
parser := symtab.GoSymtabParser{}
symtabFile, err := parser.ParseSymtab(lines)
if err != nil || symtabFile == nil {
log.Fatal(err)
}
converter := BasicSymtabConverter{
MaxDepth: maxDepth,
}
tree := converter.SymtabFileToTreemap(*symtabFile)
sizeImputer := treemap.SumSizeImputer{EmptyLeafSize: 0}
sizeImputer.ImputeSize(tree)
treemap.SetNamesFromPaths(&tree)
treemap.CollapseLongPaths(&tree)
updateNodeNamesWithByteSize(&tree)
if outputCSV {
for name, node := range tree.Nodes {
if name == rootNodeName {
continue
}
fmt.Printf("%s,%f\n", name, node.Size)
}
return
}
uiBuilder := render.UITreeMapBuilder{
Colorer: render.NoneColorer{},
BorderColor: grey,
}
spec := uiBuilder.NewUITreeMap(tree, w, h, marginBox, paddingBox, padding)
renderer := render.SVGRenderer{}
os.Stdout.Write(renderer.Render(spec, w, h))
}
func updateNodeNamesWithByteSize(tree *treemap.Tree) {
for name, node := range tree.Nodes {
count, suffix := fmtbytecount.ByteCountIEC(uint(math.Floor(node.Size)))
nameWithSize := fmt.Sprintf("%s %.2f%sB", node.Name, count, suffix)
// for secret root just size
if name == rootNodeName {
nameWithSize = fmt.Sprintf("%.2f%sB", count, suffix)
}
tree.Nodes[name] = treemap.Node{
Path: node.Path,
Name: nameWithSize,
Size: node.Size,
Heat: node.Heat,
HasHeat: node.HasHeat,
}
}
}