cmd/trace: handle Sync event at the beginning of the trace

Currently the code assumes that there's no Sync event at the start of
the trace, but this hasn't been correct for some time. Count Syncs and
look for at least one instead of looking for zero.

Fixes #73962.

Change-Id: I2b4199a21c699c5b50b3d5add37dc46a515108c6
Reviewed-on: https://go-review.googlesource.com/c/go/+/678555
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
This commit is contained in:
Michael Anthony Knyszek 2025-06-03 20:30:43 +00:00 committed by Gopher Robot
parent d4bf716793
commit f537061e1b

View File

@ -215,12 +215,12 @@ func (g *stackSampleGenerator[R]) StackSample(ctx *traceContext, ev *trace.Event
// to trace.ResourceNone (the global scope).
type globalRangeGenerator struct {
ranges map[string]activeRange
seenSync bool
seenSync int
}
// Sync notifies the generator of an EventSync event.
func (g *globalRangeGenerator) Sync() {
g.seenSync = true
g.seenSync++
}
// GlobalRange implements a handler for EventRange* events whose Scope.Kind is ResourceNone.
@ -234,8 +234,9 @@ func (g *globalRangeGenerator) GlobalRange(ctx *traceContext, ev *trace.Event) {
case trace.EventRangeBegin:
g.ranges[r.Name] = activeRange{ev.Time(), ev.Stack()}
case trace.EventRangeActive:
// If we've seen a Sync event, then Active events are always redundant.
if !g.seenSync {
// If we've seen at least 2 Sync events (indicating that we're in at least the second
// generation), then Active events are always redundant.
if g.seenSync < 2 {
// Otherwise, they extend back to the start of the trace.
g.ranges[r.Name] = activeRange{ctx.startTime, ev.Stack()}
}
@ -294,12 +295,12 @@ func (g *globalMetricGenerator) GlobalMetric(ctx *traceContext, ev *trace.Event)
// ResourceProc.
type procRangeGenerator struct {
ranges map[trace.Range]activeRange
seenSync bool
seenSync int
}
// Sync notifies the generator of an EventSync event.
func (g *procRangeGenerator) Sync() {
g.seenSync = true
g.seenSync++
}
// ProcRange implements a handler for EventRange* events whose Scope.Kind is ResourceProc.
@ -313,8 +314,9 @@ func (g *procRangeGenerator) ProcRange(ctx *traceContext, ev *trace.Event) {
case trace.EventRangeBegin:
g.ranges[r] = activeRange{ev.Time(), ev.Stack()}
case trace.EventRangeActive:
// If we've seen a Sync event, then Active events are always redundant.
if !g.seenSync {
// If we've seen at least 2 Sync events (indicating that we're in at least the second
// generation), then Active events are always redundant.
if g.seenSync < 2 {
// Otherwise, they extend back to the start of the trace.
g.ranges[r] = activeRange{ctx.startTime, ev.Stack()}
}