Skip to content

Commit 2bfa45c

Browse files
committed
cmd/go: propagate context into PackagesForBuild and Do for tracing
This change propagates context into PackagesForErrors and Do for the purpose of tracing, and calls trace.StartSpan on PackagesForErrors and Do, so that the trace now shows the broad outline of where the "Loading" and "Execution" phases are in the build. Updates #38714 Change-Id: Ib9a7cf7030210f68f76663d1c8a7461e0a226611 Reviewed-on: https://go-review.googlesource.com/c/go/+/238541 Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com>
1 parent 14715b2 commit 2bfa45c

File tree

13 files changed

+47
-33
lines changed

13 files changed

+47
-33
lines changed

src/cmd/go/internal/clean/clean.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
117117
}
118118

119119
if cleanPkg {
120-
for _, pkg := range load.PackagesAndErrors(args) {
120+
for _, pkg := range load.PackagesAndErrors(ctx, args) {
121121
clean(pkg)
122122
}
123123
}

src/cmd/go/internal/fix/fix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ See also: go fmt, go vet.
3434

3535
func runFix(ctx context.Context, cmd *base.Command, args []string) {
3636
printed := false
37-
for _, pkg := range load.Packages(args) {
37+
for _, pkg := range load.Packages(ctx, args) {
3838
if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
3939
if !printed {
4040
fmt.Fprintf(os.Stderr, "go: not fixing packages in dependency modules\n")

src/cmd/go/internal/fmtcmd/fmt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func runFmt(ctx context.Context, cmd *base.Command, args []string) {
6464
}
6565
}()
6666
}
67-
for _, pkg := range load.PackagesAndErrors(args) {
67+
for _, pkg := range load.PackagesAndErrors(ctx, args) {
6868
if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
6969
if !printed {
7070
fmt.Fprintf(os.Stderr, "go: not formatting packages in dependency modules\n")

src/cmd/go/internal/generate/generate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
176176

177177
// Even if the arguments are .go files, this loop suffices.
178178
printed := false
179-
for _, pkg := range load.PackagesAndErrors(args) {
179+
for _, pkg := range load.PackagesAndErrors(ctx, args) {
180180
if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
181181
if !printed {
182182
fmt.Fprintf(os.Stderr, "go: not generating in packages in dependency modules\n")

src/cmd/go/internal/get/get.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
172172
// everything.
173173
load.ClearPackageCache()
174174

175-
pkgs := load.PackagesForBuild(args)
175+
pkgs := load.PackagesForBuild(ctx, args)
176176

177177
// Phase 3. Install.
178178
if *getD {
@@ -182,7 +182,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
182182
return
183183
}
184184

185-
work.InstallPackages(args, pkgs)
185+
work.InstallPackages(ctx, args, pkgs)
186186
}
187187

188188
// downloadPaths prepares the list of paths to pass to download.

src/cmd/go/internal/list/list.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,9 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
449449
load.IgnoreImports = *listFind
450450
var pkgs []*load.Package
451451
if *listE {
452-
pkgs = load.PackagesAndErrors(args)
452+
pkgs = load.PackagesAndErrors(ctx, args)
453453
} else {
454-
pkgs = load.Packages(args)
454+
pkgs = load.Packages(ctx, args)
455455
base.ExitIfErrors()
456456
}
457457

@@ -539,7 +539,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
539539
a.Deps = append(a.Deps, b.AutoAction(work.ModeInstall, work.ModeInstall, p))
540540
}
541541
}
542-
b.Do(a)
542+
b.Do(ctx, a)
543543
}
544544

545545
for _, p := range pkgs {

src/cmd/go/internal/load/pkg.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package load
77

88
import (
99
"bytes"
10+
"context"
1011
"encoding/json"
1112
"errors"
1213
"fmt"
@@ -30,6 +31,7 @@ import (
3031
"cmd/go/internal/par"
3132
"cmd/go/internal/search"
3233
"cmd/go/internal/str"
34+
"cmd/go/internal/trace"
3335
)
3436

3537
var (
@@ -2123,9 +2125,9 @@ func LoadImportWithFlags(path, srcDir string, parent *Package, stk *ImportStack,
21232125
// to load dependencies of a named package, the named
21242126
// package is still returned, with p.Incomplete = true
21252127
// and details in p.DepsErrors.
2126-
func Packages(args []string) []*Package {
2128+
func Packages(ctx context.Context, args []string) []*Package {
21272129
var pkgs []*Package
2128-
for _, pkg := range PackagesAndErrors(args) {
2130+
for _, pkg := range PackagesAndErrors(ctx, args) {
21292131
if pkg.Error != nil {
21302132
base.Errorf("%v", pkg.Error)
21312133
continue
@@ -2139,7 +2141,10 @@ func Packages(args []string) []*Package {
21392141
// *Package for every argument, even the ones that
21402142
// cannot be loaded at all.
21412143
// The packages that fail to load will have p.Error != nil.
2142-
func PackagesAndErrors(patterns []string) []*Package {
2144+
func PackagesAndErrors(ctx context.Context, patterns []string) []*Package {
2145+
ctx, span := trace.StartSpan(ctx, "load.PackagesAndErrors")
2146+
defer span.Done()
2147+
21432148
for _, p := range patterns {
21442149
// Listing is only supported with all patterns referring to either:
21452150
// - Files that are part of the same directory.
@@ -2233,8 +2238,8 @@ func ImportPaths(args []string) []*search.Match {
22332238
// PackagesForBuild is like Packages but exits
22342239
// if any of the packages or their dependencies have errors
22352240
// (cannot be built).
2236-
func PackagesForBuild(args []string) []*Package {
2237-
pkgs := PackagesAndErrors(args)
2241+
func PackagesForBuild(ctx context.Context, args []string) []*Package {
2242+
pkgs := PackagesAndErrors(ctx, args)
22382243
printed := map[*PackageError]bool{}
22392244
for _, pkg := range pkgs {
22402245
if pkg.Error != nil {

src/cmd/go/internal/modget/get.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,8 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
715715
return
716716
}
717717
work.BuildInit()
718-
pkgs := load.PackagesForBuild(pkgPatterns)
719-
work.InstallPackages(pkgPatterns, pkgs)
718+
pkgs := load.PackagesForBuild(ctx, pkgPatterns)
719+
work.InstallPackages(ctx, pkgPatterns, pkgs)
720720
}
721721

722722
// runQueries looks up modules at target versions in parallel. Results will be

src/cmd/go/internal/run/run.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
7979
}
8080
p = load.GoFilesPackage(files)
8181
} else if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
82-
pkgs := load.PackagesAndErrors(args[:1])
82+
pkgs := load.PackagesAndErrors(ctx, args[:1])
8383
if len(pkgs) == 0 {
8484
base.Fatalf("go run: no packages loaded from %s", args[0])
8585
}
@@ -141,7 +141,7 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
141141
}
142142
a1 := b.LinkAction(work.ModeBuild, work.ModeBuild, p)
143143
a := &work.Action{Mode: "go run", Func: buildRunProgram, Args: cmdArgs, Deps: []*work.Action{a1}}
144-
b.Do(a)
144+
b.Do(ctx, a)
145145
}
146146

147147
// buildRunProgram is the action for running a binary that has already

src/cmd/go/internal/test/test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
577577
work.VetFlags = testVet.flags
578578
work.VetExplicit = testVet.explicit
579579

580-
pkgs = load.PackagesForBuild(pkgArgs)
580+
pkgs = load.PackagesForBuild(ctx, pkgArgs)
581581
if len(pkgs) == 0 {
582582
base.Fatalf("no packages to test")
583583
}
@@ -659,15 +659,15 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
659659
sort.Strings(all)
660660

661661
a := &work.Action{Mode: "go test -i"}
662-
for _, p := range load.PackagesForBuild(all) {
662+
for _, p := range load.PackagesForBuild(ctx, all) {
663663
if cfg.BuildToolchainName == "gccgo" && p.Standard {
664664
// gccgo's standard library packages
665665
// can not be reinstalled.
666666
continue
667667
}
668668
a.Deps = append(a.Deps, b.CompileAction(work.ModeInstall, work.ModeInstall, p))
669669
}
670-
b.Do(a)
670+
b.Do(ctx, a)
671671
if !testC || a.Failed {
672672
return
673673
}
@@ -787,7 +787,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
787787
}
788788
}
789789

790-
b.Do(root)
790+
b.Do(ctx, root)
791791
}
792792

793793
// ensures that package p imports the named package

src/cmd/go/internal/vet/vet.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func runVet(ctx context.Context, cmd *base.Command, args []string) {
6767
}
6868
}
6969

70-
pkgs := load.PackagesForBuild(pkgArgs)
70+
pkgs := load.PackagesForBuild(ctx, pkgArgs)
7171
if len(pkgs) == 0 {
7272
base.Fatalf("no packages to vet")
7373
}
@@ -93,5 +93,5 @@ func runVet(ctx context.Context, cmd *base.Command, args []string) {
9393
root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, pxtest))
9494
}
9595
}
96-
b.Do(root)
96+
b.Do(ctx, root)
9797
}

src/cmd/go/internal/work/build.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"cmd/go/internal/cfg"
2020
"cmd/go/internal/load"
2121
"cmd/go/internal/search"
22+
"cmd/go/internal/trace"
2223
)
2324

2425
var CmdBuild = &base.Command{
@@ -350,7 +351,7 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
350351
var b Builder
351352
b.Init()
352353

353-
pkgs := load.PackagesForBuild(args)
354+
pkgs := load.PackagesForBuild(ctx, args)
354355

355356
explicitO := len(cfg.BuildO) > 0
356357

@@ -379,7 +380,7 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
379380
depMode = ModeInstall
380381
}
381382

382-
pkgs = omitTestOnly(pkgsFilter(load.Packages(args)))
383+
pkgs = omitTestOnly(pkgsFilter(load.Packages(ctx, args)))
383384

384385
// Special case -o /dev/null by not writing at all.
385386
if cfg.BuildO == os.DevNull {
@@ -409,7 +410,7 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
409410
if len(a.Deps) == 0 {
410411
base.Fatalf("go build: no main packages to build")
411412
}
412-
b.Do(a)
413+
b.Do(ctx, a)
413414
return
414415
}
415416
if len(pkgs) > 1 {
@@ -422,7 +423,7 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
422423
p.Stale = true // must build - not up to date
423424
p.StaleReason = "build -o flag in use"
424425
a := b.AutoAction(ModeInstall, depMode, p)
425-
b.Do(a)
426+
b.Do(ctx, a)
426427
return
427428
}
428429

@@ -433,7 +434,7 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
433434
if cfg.BuildBuildmode == "shared" {
434435
a = b.buildmodeShared(ModeBuild, depMode, args, pkgs, a)
435436
}
436-
b.Do(a)
437+
b.Do(ctx, a)
437438
}
438439

439440
var CmdInstall = &base.Command{
@@ -518,7 +519,7 @@ func libname(args []string, pkgs []*load.Package) (string, error) {
518519

519520
func runInstall(ctx context.Context, cmd *base.Command, args []string) {
520521
BuildInit()
521-
InstallPackages(args, load.PackagesForBuild(args))
522+
InstallPackages(ctx, args, load.PackagesForBuild(ctx, args))
522523
}
523524

524525
// omitTestOnly returns pkgs with test-only packages removed.
@@ -538,7 +539,10 @@ func omitTestOnly(pkgs []*load.Package) []*load.Package {
538539
return list
539540
}
540541

541-
func InstallPackages(patterns []string, pkgs []*load.Package) {
542+
func InstallPackages(ctx context.Context, patterns []string, pkgs []*load.Package) {
543+
ctx, span := trace.StartSpan(ctx, "InstallPackages "+strings.Join(patterns, " "))
544+
defer span.Done()
545+
542546
if cfg.GOBIN != "" && !filepath.IsAbs(cfg.GOBIN) {
543547
base.Fatalf("cannot install, GOBIN must be an absolute path")
544548
}
@@ -607,7 +611,7 @@ func InstallPackages(patterns []string, pkgs []*load.Package) {
607611
a = b.buildmodeShared(ModeInstall, ModeInstall, patterns, pkgs, a)
608612
}
609613

610-
b.Do(a)
614+
b.Do(ctx, a)
611615
base.ExitIfErrors()
612616

613617
// Success. If this command is 'go install' with no arguments

src/cmd/go/internal/work/exec.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package work
88

99
import (
1010
"bytes"
11+
"context"
1112
"encoding/json"
1213
"errors"
1314
"fmt"
@@ -31,6 +32,7 @@ import (
3132
"cmd/go/internal/cfg"
3233
"cmd/go/internal/load"
3334
"cmd/go/internal/str"
35+
"cmd/go/internal/trace"
3436
)
3537

3638
// actionList returns the list of actions in the dag rooted at root
@@ -54,7 +56,10 @@ func actionList(root *Action) []*Action {
5456
}
5557

5658
// do runs the action graph rooted at root.
57-
func (b *Builder) Do(root *Action) {
59+
func (b *Builder) Do(ctx context.Context, root *Action) {
60+
ctx, span := trace.StartSpan(ctx, "exec.Builder.Do ("+root.Mode+" "+root.Target+")")
61+
defer span.Done()
62+
5863
if !b.IsCmdList {
5964
// If we're doing real work, take time at the end to trim the cache.
6065
c := cache.Default()

0 commit comments

Comments
 (0)