golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/trace/testdata/generators/go122-confuse-seq-across-generations.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Code generated by "gen.bash" from internal/trace/v2; DO NOT EDIT.
     6  
     7  //go:build go1.21
     8  
     9  // Regression test for an issue found in development.
    10  //
    11  // The core of the issue is that if generation counters
    12  // aren't considered as part of sequence numbers, then
    13  // it's possible to accidentally advance without a
    14  // GoStatus event.
    15  //
    16  // The situation is one in which it just so happens that
    17  // an event on the frontier for a following generation
    18  // has a sequence number exactly one higher than the last
    19  // sequence number for e.g. a goroutine in the previous
    20  // generation. The parser should wait to find a GoStatus
    21  // event before advancing into the next generation at all.
    22  // It turns out this situation is pretty rare; the GoStatus
    23  // event almost always shows up first in practice. But it
    24  // can and did happen.
    25  
    26  package main
    27  
    28  import (
    29  	"golang.org/x/exp/trace"
    30  	"golang.org/x/exp/trace/internal/event/go122"
    31  	testgen "golang.org/x/exp/trace/internal/testgen/go122"
    32  )
    33  
    34  func main() {
    35  	testgen.Main(gen)
    36  }
    37  
    38  func gen(t *testgen.Trace) {
    39  	g1 := t.Generation(1)
    40  
    41  	// A running goroutine blocks.
    42  	b10 := g1.Batch(trace.ThreadID(0), 0)
    43  	b10.Event("ProcStatus", trace.ProcID(0), go122.ProcRunning)
    44  	b10.Event("GoStatus", trace.GoID(1), trace.ThreadID(0), go122.GoRunning)
    45  	b10.Event("GoStop", "whatever", testgen.NoStack)
    46  
    47  	// The running goroutine gets unblocked.
    48  	b11 := g1.Batch(trace.ThreadID(1), 0)
    49  	b11.Event("ProcStatus", trace.ProcID(1), go122.ProcRunning)
    50  	b11.Event("GoStart", trace.GoID(1), testgen.Seq(1))
    51  	b11.Event("GoStop", "whatever", testgen.NoStack)
    52  
    53  	g2 := t.Generation(2)
    54  
    55  	// Start running the goroutine, but later.
    56  	b21 := g2.Batch(trace.ThreadID(1), 3)
    57  	b21.Event("ProcStatus", trace.ProcID(1), go122.ProcRunning)
    58  	b21.Event("GoStart", trace.GoID(1), testgen.Seq(2))
    59  
    60  	// The goroutine starts running, then stops, then starts again.
    61  	b20 := g2.Batch(trace.ThreadID(0), 5)
    62  	b20.Event("ProcStatus", trace.ProcID(0), go122.ProcRunning)
    63  	b20.Event("GoStatus", trace.GoID(1), trace.ThreadID(0), go122.GoRunnable)
    64  	b20.Event("GoStart", trace.GoID(1), testgen.Seq(1))
    65  	b20.Event("GoStop", "whatever", testgen.NoStack)
    66  }