-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
basic_converter.go
129 lines (103 loc) Β· 2.58 KB
/
basic_converter.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
package main
import (
"errors"
"log"
"strings"
"github.com/nikolaydubina/go-binsize-treemap/symtab"
"github.com/nikolaydubina/treemap"
)
const (
rootNodeName = "some-secret-string-binsize"
)
// BasicSymtabConverter converts parsed symtab file into treemap.
// Has no heat.
// Size is bytes.
type BasicSymtabConverter struct {
MaxDepth uint // number of levels from root, including, if 0 then no limit
}
func (s BasicSymtabConverter) SymtabFileToTreemap(sf symtab.SymtabFile) treemap.Tree {
if len(sf.Entries) == 0 {
return treemap.Tree{}
}
tree := treemap.Tree{
Nodes: map[string]treemap.Node{},
To: map[string][]string{},
}
hasParent := map[string]bool{}
for _, entry := range sf.Entries {
// skip unrecognized. mostly this is is C/C++ or something else. TODO: what is this?
if entry.Type == symtab.Undefined {
continue
}
symbolName := symtab.ParseSymbolName(entry.SymbolName)
var parts []string
// strange symbols. non-go like.
// append them to still to single root unknown
// TODO: what is this? C/C++?
if len(symbolName.PackageParts) == 0 {
parts = append(parts, "unknown")
} else {
parts = append(parts, symbolName.PackageParts...)
}
parts = append(parts, symbolName.SymbolParts...)
if s.MaxDepth > 0 && len(parts) > int(s.MaxDepth) {
parts = parts[:s.MaxDepth]
}
nodeName := strings.Join(parts, "/")
if node, ok := tree.Nodes[nodeName]; ok {
// accumulate reported size if duplicate
tree.Nodes[nodeName] = treemap.Node{
Path: node.Path,
Name: node.Name,
Size: node.Size + float64(entry.Size),
Heat: node.Heat,
HasHeat: node.HasHeat,
}
continue
}
tree.Nodes[nodeName] = treemap.Node{
Path: nodeName,
Size: float64(entry.Size),
}
hasParent[parts[0]] = false
for parent, i := parts[0], 1; i < len(parts); i++ {
child := parent + "/" + parts[i]
tree.Nodes[parent] = treemap.Node{
Path: parent,
}
tree.To[parent] = append(tree.To[parent], child)
hasParent[child] = true
parent = child
}
}
for node, v := range tree.To {
tree.To[node] = unique(v)
}
var roots []string
for node, has := range hasParent {
if !has {
roots = append(roots, node)
}
}
switch {
case len(roots) == 0:
log.Fatalf(errors.New("no roots, possible cycle in graph").Error())
case len(roots) > 1:
tree.Root = rootNodeName
tree.To[tree.Root] = roots
default:
tree.Root = roots[0]
}
return tree
}
func unique(a []string) []string {
u := map[string]bool{}
var b []string
for _, q := range a {
if _, ok := u[q]; !ok {
u[q] = true
b = append(b, q)
}
}
return b
}