Skip to content

Commit 5dde69f

Browse files
committed
internal/trace/v2: redefine NoTask and add BackgroundTask
The v2 trace parser currently handles task inheritance and region task association incorrectly. It assumes that a TaskID of 0 means that there is no task. However, this is only true for task events. A TaskID of 0 means that a region gets assigned to the "background task." The parser currently has no concept of a "background task." Fix this by defining the background task as task ID 0 and redefining NoTask to ^uint64(0). This aligns the TaskID values more closely with other IDs in the parser and also enables disambiguating these two cases. For #60773. For #63960. Change-Id: I09c8217b33b87c8f8f8ea3b0203ed83fd3b61e11 Reviewed-on: https://go-review.googlesource.com/c/go/+/543019 Reviewed-by: Michael Pratt <[email protected]> Auto-Submit: Michael Knyszek <[email protected]> TryBot-Bypass: Michael Knyszek <[email protected]>
1 parent d1dcffd commit 5dde69f

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

src/internal/trace/goroutinesv2.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,7 @@ func (s *GoroutineSummarizer) Event(ev *tracev2.Event) {
299299
if creatorG := s.gs[ev.Goroutine()]; creatorG != nil && len(creatorG.activeRegions) > 0 {
300300
regions := creatorG.activeRegions
301301
s := regions[len(regions)-1]
302-
if s.TaskID != tracev2.NoTask {
303-
g.activeRegions = []*UserRegionSummary{{TaskID: s.TaskID, Start: ev}}
304-
}
302+
g.activeRegions = []*UserRegionSummary{{TaskID: s.TaskID, Start: ev}}
305303
}
306304
s.gs[g.ID] = g
307305
case tracev2.GoRunning:

src/internal/trace/v2/event.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,14 @@ type RangeAttribute struct {
203203
// are of the same type).
204204
type TaskID uint64
205205

206-
// NoTask indicates the lack of a task.
207-
const NoTask = TaskID(0)
206+
const (
207+
// NoTask indicates the lack of a task.
208+
NoTask = TaskID(^uint64(0))
209+
210+
// BackgroundTask is the global task that events are attached to if there was
211+
// no other task in the context at the point the event was emitted.
212+
BackgroundTask = TaskID(0)
213+
)
208214

209215
// Task provides details about a Task event.
210216
type Task struct {

src/internal/trace/v2/order.go

+7
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,13 @@ func (o *ordering) advance(ev *baseEvent, evt *evTable, m ThreadID, gen uint64)
525525
// Get the parent ID, but don't validate it. There's no guarantee
526526
// we actually have information on whether it's active.
527527
parentID := TaskID(ev.args[1])
528+
if parentID == BackgroundTask {
529+
// Note: a value of 0 here actually means no parent, *not* the
530+
// background task. Automatic background task attachment only
531+
// applies to regions.
532+
parentID = NoTask
533+
ev.args[1] = uint64(NoTask)
534+
}
528535

529536
// Validate the name and record it. We'll need to pass it through to
530537
// EvUserTaskEnd.

src/internal/trace/v2/testdata/generators/go122-task-across-generations.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func gen(t *testgen.Trace) {
2929
b1 := g1.Batch(trace.ThreadID(0), 0)
3030
b1.Event("ProcStatus", trace.ProcID(0), go122.ProcRunning)
3131
b1.Event("GoStatus", trace.GoID(1), trace.ThreadID(0), go122.GoRunning)
32-
b1.Event("UserTaskBegin", trace.TaskID(2), trace.NoTask, "my task", testgen.NoStack)
32+
b1.Event("UserTaskBegin", trace.TaskID(2), trace.TaskID(0) /* 0 means no parent, not background */, "my task", testgen.NoStack)
3333

3434
g2 := t.Generation(2)
3535

src/internal/trace/v2/testtrace/validation.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,14 @@ func (v *Validator) Event(ev trace.Event) error {
256256
case trace.EventTaskBegin:
257257
// Validate task begin.
258258
t := ev.Task()
259-
if t.ID == trace.NoTask {
259+
if t.ID == trace.NoTask || t.ID == trace.BackgroundTask {
260+
// The background task should never have an event emitted for it.
260261
e.Errorf("found invalid task ID for task of type %s", t.Type)
261262
}
263+
if t.Parent == trace.BackgroundTask {
264+
// It's not possible for a task to be a subtask of the background task.
265+
e.Errorf("found background task as the parent for task of type %s", t.Type)
266+
}
262267
// N.B. Don't check the task type. Empty string is a valid task type.
263268
v.tasks[t.ID] = t.Type
264269
case trace.EventTaskEnd:

src/internal/trace/v2/trace_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestTraceAnnotations(t *testing.T) {
3535
{trace.EventRegionEnd, trace.TaskID(1), []string{"region0"}},
3636
{trace.EventTaskEnd, trace.TaskID(1), []string{"task0"}},
3737
// Currently, pre-existing region is not recorded to avoid allocations.
38-
{trace.EventRegionBegin, trace.NoTask, []string{"post-existing region"}},
38+
{trace.EventRegionBegin, trace.BackgroundTask, []string{"post-existing region"}},
3939
}
4040
r, err := trace.NewReader(bytes.NewReader(tb))
4141
if err != nil {

0 commit comments

Comments
 (0)