This repository has been archived by the owner on Jan 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nm.go
84 lines (74 loc) · 1.59 KB
/
nm.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
package main
import (
"bufio"
"fmt"
"go/build"
"os/exec"
"path"
"strconv"
"strings"
)
func collectSymbols() error {
if !ctx.symsizes {
return nil
}
// check if pkgName is command first
builtPkg, err := build.Import(ctx.pkgName, "", 0)
if err != nil {
return err
}
if !builtPkg.IsCommand() {
fmt.Println("Warning: symbol sizes can only be determined for commands. (" + ctx.pkgName + ")")
ctx.symsizes = false
return nil
}
// call 'go tool nm' to collect symbol sizes
binary := path.Join(builtPkg.BinDir, path.Base(ctx.pkgName))
cmd := exec.Command("go", "tool", "nm", "-size", binary)
out, err := cmd.StdoutPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
// parse output of 'go tool nm'
s := bufio.NewScanner(out)
for s.Scan() {
line := s.Text()
if strings.HasPrefix(line, " ") {
continue
}
fields := strings.Fields(line)
size, err := strconv.ParseInt(fields[1], 10, 64)
if err != nil {
return err
}
if size > 0 {
symname := fields[3]
ctx.symbols[symname] += size
ctx.flatSymSize += size
}
}
return cmd.Wait()
}
func analyzeSymbols(pkg *Package) {
for sym, size := range ctx.symbols {
if strings.HasPrefix(sym, pkg.Name+".") {
pkg.SymSize += size
delete(ctx.symbols, sym)
}
}
}
func analyzeRemainingSymbols(mainPkgName string) {
var mainSize int64
var remainingSize int64
for sym, size := range ctx.symbols {
if strings.HasPrefix(sym, "main") {
mainSize += size
}
remainingSize += size
}
ctx.pkgs[mainPkgName].SymSize += mainSize
ctx.pkgs["runtime"].SymSize += remainingSize
}