github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/trace/v2/testdata/generators/go122-create-syscall-reuse-thread-id.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  // Tests a G being created from within a syscall.
     6  //
     7  // Specifically, it tests a scenerio wherein a C
     8  // thread is calling into Go, creating a goroutine in
     9  // a syscall (in the tracer's model). The system is free
    10  // to reuse thread IDs, so first a thread ID is used to
    11  // call into Go, and then is used for a Go-created thread.
    12  //
    13  // This is a regression test. The trace parser didn't correctly
    14  // model GoDestroySyscall as dropping its P (even if the runtime
    15  // did). It turns out this is actually fine if all the threads
    16  // in the trace have unique IDs, since the P just stays associated
    17  // with an eternally dead thread, and it's stolen by some other
    18  // thread later. But if thread IDs are reused, then the tracer
    19  // gets confused when trying to advance events on the new thread.
    20  // The now-dead thread which exited on a GoDestroySyscall still has
    21  // its P associated and this transfers to the newly-live thread
    22  // in the parser's state because they share a thread ID.
    23  
    24  package main
    25  
    26  import (
    27  	"github.com/go-asm/go/trace/v2"
    28  	"github.com/go-asm/go/trace/v2/event/go122"
    29  	testgen "github.com/go-asm/go/trace/v2/github.com/go-asm/go/testgen/go122"
    30  )
    31  
    32  func main() {
    33  	testgen.Main(gen)
    34  }
    35  
    36  func gen(t *testgen.Trace) {
    37  	g := t.Generation(1)
    38  
    39  	// A C thread calls into Go and acquires a P. It returns
    40  	// back to C, destroying the G.
    41  	b0 := g.Batch(trace.ThreadID(0), 0)
    42  	b0.Event("GoCreateSyscall", trace.GoID(4))
    43  	b0.Event("GoSyscallEndBlocked")
    44  	b0.Event("ProcStatus", trace.ProcID(0), go122.ProcIdle)
    45  	b0.Event("ProcStart", trace.ProcID(0), testgen.Seq(1))
    46  	b0.Event("GoStatus", trace.GoID(4), trace.NoThread, go122.GoRunnable)
    47  	b0.Event("GoStart", trace.GoID(4), testgen.Seq(1))
    48  	b0.Event("GoSyscallBegin", testgen.Seq(2), testgen.NoStack)
    49  	b0.Event("GoDestroySyscall")
    50  
    51  	// A new Go-created thread with the same ID appears and
    52  	// starts running, then tries to steal the P from the
    53  	// first thread. The stealing is interesting because if
    54  	// the parser handles GoDestroySyscall wrong, then we
    55  	// have a self-steal here potentially that doesn't make
    56  	// sense.
    57  	b1 := g.Batch(trace.ThreadID(0), 0)
    58  	b1.Event("ProcStatus", trace.ProcID(1), go122.ProcIdle)
    59  	b1.Event("ProcStart", trace.ProcID(1), testgen.Seq(1))
    60  	b1.Event("ProcSteal", trace.ProcID(0), testgen.Seq(3), trace.ThreadID(0))
    61  }