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.
12 // This file runs the SSA builder in sanity-checking mode on all
13 // packages beneath $GOROOT and prints some summary information.
15 // Run with "go test -cpu=8 to" set GOMAXPROCS.
25 "golang.org/x/tools/go/buildutil"
26 "golang.org/x/tools/go/loader"
27 "golang.org/x/tools/go/ssa"
28 "golang.org/x/tools/go/ssa/ssautil"
29 "golang.org/x/tools/internal/testenv"
32 func bytesAllocated() uint64 {
34 var stats runtime.MemStats
35 runtime.ReadMemStats(&stats)
39 func TestStdlib(t *testing.T) {
41 t.Skip("skipping in short mode; too slow (https://golang.org/issue/14113)")
43 testenv.NeedsTool(t, "go")
45 // Load, parse and type-check the program.
47 alloc0 := bytesAllocated()
49 // Load, parse and type-check the program.
50 ctxt := build.Default // copy
51 ctxt.GOPATH = "" // disable GOPATH
52 conf := loader.Config{Build: &ctxt}
53 for _, path := range buildutil.AllPackages(conf.Build) {
54 conf.ImportWithTests(path)
57 iprog, err := conf.Load()
59 t.Fatalf("Load failed: %v", err)
63 alloc1 := bytesAllocated()
65 // Create SSA packages.
66 var mode ssa.BuilderMode
67 // Comment out these lines during benchmarking. Approx SSA build costs are noted.
68 mode |= ssa.SanityCheckFunctions // + 2% space, + 4% time
69 mode |= ssa.GlobalDebug // +30% space, +18% time
70 prog := ssautil.CreateProgram(iprog, mode)
78 alloc3 := bytesAllocated()
80 numPkgs := len(prog.AllPackages())
81 if want := 140; numPkgs < want {
82 t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
85 // Keep iprog reachable until after we've measured memory usage.
86 if len(iprog.AllPackages) == 0 {
90 allFuncs := ssautil.AllFunctions(prog)
92 // Check that all non-synthetic functions have distinct names.
93 // Synthetic wrappers for exported methods should be distinct too,
94 // except for unexported ones (explained at (*Function).RelString).
95 byName := make(map[string]*ssa.Function)
96 for fn := range allFuncs {
97 if fn.Synthetic == "" || ast.IsExported(fn.Name()) {
102 t.Errorf("%s: duplicate function named %s",
103 prog.Fset.Position(fn.Pos()), str)
104 t.Errorf("%s: (previously defined here)",
105 prog.Fset.Position(prev.Pos()))
110 // Dump some statistics.
112 for fn := range allFuncs {
113 for _, b := range fn.Blocks {
114 numInstrs += len(b.Instrs)
118 // determine line count
120 prog.Fset.Iterate(func(f *token.File) bool {
121 lineCount += f.LineCount()
125 // NB: when benchmarking, don't forget to clear the debug +
126 // sanity builder flags for better performance.
128 t.Log("GOMAXPROCS: ", runtime.GOMAXPROCS(0))
129 t.Log("#Source lines: ", lineCount)
130 t.Log("Load/parse/typecheck: ", t1.Sub(t0))
131 t.Log("SSA create: ", t2.Sub(t1))
132 t.Log("SSA build: ", t3.Sub(t2))
135 t.Log("#Packages: ", numPkgs)
136 t.Log("#Functions: ", len(allFuncs))
137 t.Log("#Instructions: ", numInstrs)
138 t.Log("#MB AST+types: ", int64(alloc1-alloc0)/1e6)
139 t.Log("#MB SSA: ", int64(alloc3-alloc1)/1e6)