1 // Copyright 2023 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.
15 func TestDumpCallSiteScoreDump(t *testing.T) {
17 testenv.MustHaveGoBuild(t)
19 scenarios := []struct {
22 indirectlyPromoted int
29 indirectlyPromoted: 1,
35 for _, scen := range scenarios {
36 dumpfile, err := gatherInlCallSitesScoresForFile(t, scen.name, td)
38 t.Fatalf("dumping callsite scores for %q: error %v", scen.name, err)
41 if content, err := os.ReadFile(dumpfile); err != nil {
42 t.Fatalf("reading dump %q: error %v", dumpfile, err)
44 lines = strings.Split(string(content), "\n")
46 prom, indprom, dem, unch := 0, 0, 0, 0
47 for _, line := range lines {
49 case strings.TrimSpace(line) == "":
50 case !strings.Contains(line, "|"):
51 case strings.HasPrefix(line, "#"):
52 case strings.Contains(line, "PROMOTED"):
54 case strings.Contains(line, "INDPROM"):
56 case strings.Contains(line, "DEMOTED"):
63 if prom != scen.promoted {
64 t.Errorf("testcase %q, got %d promoted want %d promoted",
65 scen.name, prom, scen.promoted)
68 if indprom != scen.indirectlyPromoted {
69 t.Errorf("testcase %q, got %d indirectly promoted want %d",
70 scen.name, indprom, scen.indirectlyPromoted)
73 if dem != scen.demoted {
74 t.Errorf("testcase %q, got %d demoted want %d demoted",
75 scen.name, dem, scen.demoted)
78 if unch != scen.unchanged {
79 t.Errorf("testcase %q, got %d unchanged want %d unchanged",
80 scen.name, unch, scen.unchanged)
84 t.Logf(">> dump output: %s", strings.Join(lines, "\n"))
89 // gatherInlCallSitesScoresForFile builds the specified testcase 'testcase'
90 // from testdata/props passing the "-d=dumpinlcallsitescores=1"
91 // compiler option, to produce a dump, then returns the path of the
92 // newly created file.
93 func gatherInlCallSitesScoresForFile(t *testing.T, testcase string, td string) (string, error) {
95 gopath := "testdata/" + testcase + ".go"
96 outpath := filepath.Join(td, testcase+".a")
97 dumpfile := filepath.Join(td, testcase+".callsites.txt")
98 run := []string{testenv.GoToolPath(t), "build",
99 "-gcflags=-d=dumpinlcallsitescores=1", "-o", outpath, gopath}
100 out, err := testenv.Command(t, run[0], run[1:]...).CombinedOutput()
101 t.Logf("run: %+v\n", run)
105 if err := os.WriteFile(dumpfile, out, 0666); err != nil {