|
| 1 | +// Copyright 2017 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. |
| 4 | + |
| 5 | +package test |
| 6 | + |
| 7 | +import ( |
| 8 | + "bufio" |
| 9 | + "bytes" |
| 10 | + "internal/testenv" |
| 11 | + "io" |
| 12 | + "io/ioutil" |
| 13 | + "os/exec" |
| 14 | + "path/filepath" |
| 15 | + "regexp" |
| 16 | + "strings" |
| 17 | + "testing" |
| 18 | +) |
| 19 | + |
| 20 | +// TestIntendedInlining tests that specific functions are inlined. |
| 21 | +// This allows refactoring for code clarity and re-use without fear that |
| 22 | +// changes to the compiler will cause silent performance regressions. |
| 23 | +func TestPGOIntendedInlining(t *testing.T) { |
| 24 | + testenv.MustHaveGoRun(t) |
| 25 | + t.Parallel() |
| 26 | + |
| 27 | + // Make a temporary directory to work in. |
| 28 | + tmpdir, err := ioutil.TempDir("", "TestCode") |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("Failed to create temporary directory: %v", err) |
| 31 | + } |
| 32 | + //defer os.RemoveAll(tmpdir) |
| 33 | + |
| 34 | + want := map[string][]string{ |
| 35 | + "pgo/inline": { |
| 36 | + "(*BS).NS", |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + // The functions which are not expected to be inlined are as follows. |
| 41 | + wantNot := map[string][]string{ |
| 42 | + "pgo/inline": { |
| 43 | + // The calling edge main->A is hot and the cost of A is large than |
| 44 | + // inlineHotCalleeMaxBudget. |
| 45 | + "A", |
| 46 | + // The calling edge BenchmarkA" -> benchmarkB is cold |
| 47 | + // and the cost of A is large than inlineMaxBudget. |
| 48 | + "benchmarkB", |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + must := map[string]bool{ |
| 53 | + "(*BS).NS": true, |
| 54 | + } |
| 55 | + |
| 56 | + notInlinedReason := make(map[string]string) |
| 57 | + pkgs := make([]string, 0, len(want)) |
| 58 | + for pname, fnames := range want { |
| 59 | + pkgs = append(pkgs, pname) |
| 60 | + for _, fname := range fnames { |
| 61 | + fullName := pname + "." + fname |
| 62 | + if _, ok := notInlinedReason[fullName]; ok { |
| 63 | + t.Errorf("duplicate func: %s", fullName) |
| 64 | + } |
| 65 | + notInlinedReason[fullName] = "unknown reason" |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // If the compiler emit "cannot inline for function A", the entry A |
| 70 | + // in expectedNotInlinedList will be removed. |
| 71 | + expectedNotInlinedList := make(map[string]struct{}) |
| 72 | + for pname, fnames := range wantNot { |
| 73 | + for _, fname := range fnames { |
| 74 | + fullName := pname + "." + fname |
| 75 | + expectedNotInlinedList[fullName] = struct{}{} |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + args := append([]string{"test", "-o", filepath.Join(tmpdir, "inline_hot.test"), "-bench=.", "-cpuprofile", filepath.Join(tmpdir, "inline_hot.pprof")}, pkgs...) |
| 80 | + gotool := testenv.GoToolPath(t) |
| 81 | + cmd := exec.Command(gotool, args...) |
| 82 | + var stdout, stderr bytes.Buffer |
| 83 | + cmd.Stdout = &stdout |
| 84 | + cmd.Stderr = &stderr |
| 85 | + err = cmd.Run() |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("Failed: %v:\nOut: %s\nStderr: %s\n", err, &stdout, &stderr) |
| 88 | + } |
| 89 | + |
| 90 | + args = append([]string{"test", "-run=nope", "-tags=", "-timeout=9m0s", "-gcflags=-m -m -profileuse " + filepath.Join(tmpdir, "inline_hot.pprof")}, pkgs...) |
| 91 | + cmd = testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), args...)) |
| 92 | + |
| 93 | + pr, pw := io.Pipe() |
| 94 | + cmd.Stdout = pw |
| 95 | + cmd.Stderr = pw |
| 96 | + cmdErr := make(chan error, 1) |
| 97 | + go func() { |
| 98 | + cmdErr <- cmd.Run() |
| 99 | + pw.Close() |
| 100 | + }() |
| 101 | + scanner := bufio.NewScanner(pr) |
| 102 | + curPkg := "" |
| 103 | + canInline := regexp.MustCompile(`: can inline ([^ ]*)`) |
| 104 | + haveInlined := regexp.MustCompile(`: inlining call to ([^ ]*)`) |
| 105 | + cannotInline := regexp.MustCompile(`: cannot inline ([^ ]*): (.*)`) |
| 106 | + for scanner.Scan() { |
| 107 | + line := scanner.Text() |
| 108 | + if strings.HasPrefix(line, "# ") { |
| 109 | + curPkg = line[2:] |
| 110 | + splits := strings.Split(curPkg, " ") |
| 111 | + curPkg = splits[0] |
| 112 | + continue |
| 113 | + } |
| 114 | + if m := haveInlined.FindStringSubmatch(line); m != nil { |
| 115 | + fname := m[1] |
| 116 | + delete(notInlinedReason, curPkg+"."+fname) |
| 117 | + continue |
| 118 | + } |
| 119 | + if m := canInline.FindStringSubmatch(line); m != nil { |
| 120 | + fname := m[1] |
| 121 | + fullname := curPkg + "." + fname |
| 122 | + // If function must be inlined somewhere, being inlinable is not enough |
| 123 | + if _, ok := must[fullname]; !ok { |
| 124 | + delete(notInlinedReason, fullname) |
| 125 | + continue |
| 126 | + } |
| 127 | + } |
| 128 | + if m := cannotInline.FindStringSubmatch(line); m != nil { |
| 129 | + fname, reason := m[1], m[2] |
| 130 | + fullName := curPkg + "." + fname |
| 131 | + if _, ok := notInlinedReason[fullName]; ok { |
| 132 | + // cmd/compile gave us a reason why |
| 133 | + notInlinedReason[fullName] = reason |
| 134 | + } |
| 135 | + delete(expectedNotInlinedList, fullName) |
| 136 | + continue |
| 137 | + } |
| 138 | + } |
| 139 | + if err := <-cmdErr; err != nil { |
| 140 | + t.Fatal(err) |
| 141 | + } |
| 142 | + if err := scanner.Err(); err != nil { |
| 143 | + t.Fatal(err) |
| 144 | + } |
| 145 | + for fullName, reason := range notInlinedReason { |
| 146 | + t.Errorf("%s was not inlined: %s", fullName, reason) |
| 147 | + } |
| 148 | + |
| 149 | + // If the list expectedNotInlinedList is not empty, it indicates |
| 150 | + // the functions in the expectedNotInlinedList are marked with caninline. |
| 151 | + for fullName, _ := range expectedNotInlinedList { |
| 152 | + t.Errorf("%s was expected not inlined", fullName) |
| 153 | + } |
| 154 | +} |
0 commit comments