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