Skip to content

Commit 84fbdf0

Browse files
prattmicgopherbot
authored andcommitted
cmd/go: use cache for PGO preprocessing
This is the final CL in the series adding PGO preprocessing support to cmd/go. Now that the tool is hooked up, we integrate with the build cache to cache the result. This is fairly straightforward. One difference is that the compile and link do caching through updateBuildID. However, preprocessed PGO files don't have a build ID, so it doesn't make much sense to hack our way through that function when it is simple to just add to the cache ourselves. As as aside, we could add a build ID to the preproccessed file format, though it is not clear if it is worthwhile. The one place a build ID could be used is in buildActionID, which currently compute the file hash of the preprocessed profile. With a build ID it could simply read the build ID. This would save one complete read of the file per build (cmd/go caches the hash), but each compile process also reads the entire file, so this is a small change overall. Fixes #58102. Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest Change-Id: I86e2999a08ccd264230fbb1c983192259b7288e9 Reviewed-on: https://go-review.googlesource.com/c/go/+/569425 Auto-Submit: Michael Pratt <mpratt@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 081dc9f commit 84fbdf0

File tree

7 files changed

+92
-1
lines changed

7 files changed

+92
-1
lines changed

doc/next/5-toolchain.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Compiler {#compiler}
22

3+
The build time overhead to building with [Profile Guided Optimization](/doc/pgo) has been reduced significantly.
4+
Previously, large builds could see 100%+ build time increase from enabling PGO.
5+
In Go 1.23, overhead should be in the single digit percentages.
6+
37
## Assembler {#assembler}
48

59
## Linker {#linker}

src/cmd/go/internal/work/action.go

+41-1
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,28 @@ func (ba *buildActor) Act(b *Builder, ctx context.Context, a *Action) error {
461461
return b.build(ctx, a)
462462
}
463463

464+
// pgoActionID computes the action ID for a preprocess PGO action.
465+
func (b *Builder) pgoActionID(input string) cache.ActionID {
466+
h := cache.NewHash("preprocess PGO profile " + input)
467+
468+
fmt.Fprintf(h, "preprocess PGO profile\n")
469+
fmt.Fprintf(h, "preprofile %s\n", b.toolID("preprofile"))
470+
fmt.Fprintf(h, "input %q\n", b.fileHash(input))
471+
472+
return h.Sum()
473+
}
474+
464475
// pgoActor implements the Actor interface for preprocessing PGO profiles.
465476
type pgoActor struct {
466477
// input is the path to the original pprof profile.
467478
input string
468479
}
469480

470481
func (p *pgoActor) Act(b *Builder, ctx context.Context, a *Action) error {
471-
// TODO(prattmic): Integrate with build cache to cache output.
482+
if b.useCache(a, b.pgoActionID(p.input), a.Target, !b.IsCmdList) || b.IsCmdList {
483+
return nil
484+
}
485+
defer b.flushOutput(a)
472486

473487
sh := b.Shell(a)
474488

@@ -480,7 +494,33 @@ func (p *pgoActor) Act(b *Builder, ctx context.Context, a *Action) error {
480494
return err
481495
}
482496

497+
// N.B. Builder.build looks for the out in a.built, regardless of
498+
// whether this came from cache.
483499
a.built = a.Target
500+
501+
if !cfg.BuildN {
502+
// Cache the output.
503+
//
504+
// N.B. We don't use updateBuildID here, as preprocessed PGO profiles
505+
// do not contain a build ID. updateBuildID is typically responsible
506+
// for adding to the cache, thus we must do so ourselves instead.
507+
508+
r, err := os.Open(a.Target)
509+
if err != nil {
510+
return fmt.Errorf("error opening target for caching: %w", err)
511+
}
512+
513+
c := cache.Default()
514+
outputID, _, err := c.Put(a.actionID, r)
515+
r.Close()
516+
if err != nil {
517+
return fmt.Errorf("error adding target to cache: %w", err)
518+
}
519+
if cfg.BuildX {
520+
sh.ShowCmd("", "%s # internal", joinUnambiguously(str.StringList("cp", a.Target, c.OutputFile(outputID))))
521+
}
522+
}
523+
484524
return nil
485525
}
486526

src/cmd/go/internal/work/buildid.go

+8
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,14 @@ func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string,
527527

528528
// Check to see if the action output is cached.
529529
if file, _, err := cache.GetFile(c, actionHash); err == nil {
530+
if a.Mode == "preprocess PGO profile" {
531+
// Preprocessed PGO profiles don't embed a build ID, so
532+
// skip the build ID lookup.
533+
// TODO(prattmic): better would be to add a build ID to the format.
534+
a.built = file
535+
a.Target = "DO NOT USE - using cache"
536+
return true
537+
}
530538
if buildID, err := buildid.ReadFile(file); err == nil {
531539
if printOutput {
532540
showStdout(b, c, a, "stdout")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[short] skip
2+
3+
# Set up fresh GOCACHE.
4+
env GOCACHE=$WORK/gocache
5+
mkdir $GOCACHE
6+
7+
# Building trivial non-main package should run preprofile the first time.
8+
go build -x -pgo=default.pgo lib.go
9+
stderr 'preprofile.*default\.pgo'
10+
11+
# ... but not again ...
12+
go build -x -pgo=default.pgo lib.go
13+
! stderr 'preprofile.*default\.pgo'
14+
15+
# ... unless we use -a.
16+
go build -a -x -pgo=default.pgo lib.go
17+
stderr 'preprofile.*default\.pgo'
18+
19+
# ... building a different package should not run preprofile again, instead using a profile from cache.
20+
go build -x -pgo=default.pgo lib2.go
21+
! stderr 'preprofile.*default\.pgo'
22+
stderr 'compile.*-pgoprofile=.*'$GOCACHE'.*lib2.go'
23+
24+
-- lib.go --
25+
package lib
26+
-- lib2.go --
27+
package lib2
28+
-- default.pgo --

src/cmd/go/testdata/script/build_pgo.txt

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
[short] skip 'compiles and links executables'
55

6+
# Set up fresh GOCACHE.
7+
env GOCACHE=$WORK/gocache
8+
mkdir $GOCACHE
9+
610
# build without PGO
711
go build triv.go
812

src/cmd/go/testdata/script/build_pgo_auto.txt

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
[short] skip 'compiles and links executables'
44

5+
# Set up fresh GOCACHE.
6+
env GOCACHE=$WORK/gocache
7+
mkdir $GOCACHE
8+
59
# use default.pgo for a single main package
610
go build -n -pgo=auto -o a1.exe ./a/a1
711
stderr 'preprofile.*-i.*default\.pgo'

src/cmd/preprofile/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616

1717
import (
1818
"bufio"
19+
"cmd/internal/objabi"
1920
"cmd/internal/pgo"
2021
"flag"
2122
"fmt"
@@ -67,6 +68,8 @@ func preprocess(profileFile string, outputFile string) error {
6768
}
6869

6970
func main() {
71+
objabi.AddVersionFlag()
72+
7073
log.SetFlags(0)
7174
log.SetPrefix("preprofile: ")
7275

0 commit comments

Comments
 (0)