Skip to content

Commit 023d497

Browse files
committed
cmd/go: add trace events for each action
This change adds a trace event for each action and also annotates each of the action execution goroutines with trace.Goroutine so that the actions eaxecuted by each goroutine appear on different threads in the chrome trace viewer. Updates #38714 Change-Id: I2e58dc5606b2e3f7f87076a61e1cc6a2014255c5 Reviewed-on: https://go-review.googlesource.com/c/go/+/248320 Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 49003da commit 023d497

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/cmd/go/internal/trace/trace.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ func StartSpan(ctx context.Context, name string) (context.Context, *Span) {
4545
return ctx, childSpan
4646
}
4747

48-
// Goroutine associates the context with a new Thread ID. The Chrome trace viewer associates each
48+
// StartGoroutine associates the context with a new Thread ID. The Chrome trace viewer associates each
4949
// trace event with a thread, and doesn't expect events with the same thread id to happen at the
5050
// same time.
51-
func Goroutine(ctx context.Context) context.Context {
51+
func StartGoroutine(ctx context.Context) context.Context {
5252
tc, ok := getTraceContext(ctx)
5353
if !ok {
5454
return ctx

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,21 @@ func (b *Builder) Do(ctx context.Context, root *Action) {
115115

116116
// Handle runs a single action and takes care of triggering
117117
// any actions that are runnable as a result.
118-
handle := func(a *Action) {
118+
handle := func(ctx context.Context, a *Action) {
119119
if a.json != nil {
120120
a.json.TimeStart = time.Now()
121121
}
122122
var err error
123123
if a.Func != nil && (!a.Failed || a.IgnoreFail) {
124+
// TODO(matloob): Better action descriptions
125+
desc := "Executing action "
126+
if a.Package != nil {
127+
desc += "(" + a.Mode + " " + a.Package.Desc() + ")"
128+
}
129+
ctx, span := trace.StartSpan(ctx, desc)
130+
_ = ctx
124131
err = a.Func(b, a)
132+
span.Done()
125133
}
126134
if a.json != nil {
127135
a.json.TimeDone = time.Now()
@@ -169,6 +177,7 @@ func (b *Builder) Do(ctx context.Context, root *Action) {
169177
for i := 0; i < par; i++ {
170178
wg.Add(1)
171179
go func() {
180+
ctx := trace.StartGoroutine(ctx)
172181
defer wg.Done()
173182
for {
174183
select {
@@ -181,7 +190,7 @@ func (b *Builder) Do(ctx context.Context, root *Action) {
181190
b.exec.Lock()
182191
a := b.ready.pop()
183192
b.exec.Unlock()
184-
handle(a)
193+
handle(ctx, a)
185194
case <-base.Interrupted:
186195
base.SetExitStatus(1)
187196
return

0 commit comments

Comments
 (0)