1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Incomplete source tree on Android.
11 // This file runs the SSA builder in sanity-checking mode on all
12 // packages beneath $GOROOT and prints some summary information.
14 // Run with "go test -cpu=8 to" set GOMAXPROCS.
24 "golang.org/x/tools/go/buildutil"
25 "golang.org/x/tools/go/loader"
26 "golang.org/x/tools/go/ssa"
27 "golang.org/x/tools/go/ssa/ssautil"
28 "golang.org/x/tools/internal/testenv"
31 func bytesAllocated() uint64 {
33 var stats runtime.MemStats
34 runtime.ReadMemStats(&stats)
38 func TestStdlib(t *testing.T) {
40 t.Skip("skipping in short mode; too slow (https://golang.org/issue/14113)")
42 testenv.NeedsTool(t, "go")
44 // Load, parse and type-check the program.
46 alloc0 := bytesAllocated()
48 // Load, parse and type-check the program.
49 ctxt := build.Default // copy
50 ctxt.GOPATH = "" // disable GOPATH
51 conf := loader.Config{Build: &ctxt}
52 for _, path := range buildutil.AllPackages(conf.Build) {
53 conf.ImportWithTests(path)
56 iprog, err := conf.Load()
58 t.Fatalf("Load failed: %v", err)
62 alloc1 := bytesAllocated()
64 // Create SSA packages.
65 var mode ssa.BuilderMode
66 // Comment out these lines during benchmarking. Approx SSA build costs are noted.
67 mode |= ssa.SanityCheckFunctions // + 2% space, + 4% time
68 mode |= ssa.GlobalDebug // +30% space, +18% time
69 prog := ssautil.CreateProgram(iprog, mode)
77 alloc3 := bytesAllocated()
79 numPkgs := len(prog.AllPackages())
80 if want := 140; numPkgs < want {
81 t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
84 // Keep iprog reachable until after we've measured memory usage.
85 if len(iprog.AllPackages) == 0 {
89 allFuncs := ssautil.AllFunctions(prog)
91 // Check that all non-synthetic functions have distinct names.
92 // Synthetic wrappers for exported methods should be distinct too,
93 // except for unexported ones (explained at (*Function).RelString).
94 byName := make(map[string]*ssa.Function)
95 for fn := range allFuncs {
96 if fn.Synthetic == "" || ast.IsExported(fn.Name()) {
101 t.Errorf("%s: duplicate function named %s",
102 prog.Fset.Position(fn.Pos()), str)
103 t.Errorf("%s: (previously defined here)",
104 prog.Fset.Position(prev.Pos()))
109 // Dump some statistics.
111 for fn := range allFuncs {
112 for _, b := range fn.Blocks {
113 numInstrs += len(b.Instrs)
117 // determine line count
119 prog.Fset.Iterate(func(f *token.File) bool {
120 lineCount += f.LineCount()
124 // NB: when benchmarking, don't forget to clear the debug +
125 // sanity builder flags for better performance.
127 t.Log("GOMAXPROCS: ", runtime.GOMAXPROCS(0))
128 t.Log("#Source lines: ", lineCount)
129 t.Log("Load/parse/typecheck: ", t1.Sub(t0))
130 t.Log("SSA create: ", t2.Sub(t1))
131 t.Log("SSA build: ", t3.Sub(t2))
134 t.Log("#Packages: ", numPkgs)
135 t.Log("#Functions: ", len(allFuncs))
136 t.Log("#Instructions: ", numInstrs)
137 t.Log("#MB AST+types: ", int64(alloc1-alloc0)/1e6)
138 t.Log("#MB SSA: ", int64(alloc3-alloc1)/1e6)