| 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; DO NOT EDIT. |
| 6 | |
| 7 | //go:build go1.23 |
| 8 | |
| 9 | // Tests a G being created from within a syscall. |
| 10 | // |
| 11 | // Specifically, it tests a scenario 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/testgen" |
| 33 | "golang.org/x/exp/trace/internal/tracev2" |
| 34 | "golang.org/x/exp/trace/internal/version" |
| 35 | ) |
| 36 | |
| 37 | func main() { |
| 38 | testgen.Main(version.Go122, gen) |
| 39 | } |
| 40 | |
| 41 | func gen(t *testgen.Trace) { |
| 42 | g := t.Generation(1) |
| 43 | |
| 44 | // A C thread calls into Go and acquires a P. It returns |
| 45 | // back to C, destroying the G. |
| 46 | b0 := g.Batch(trace.ThreadID(0), 0) |
| 47 | b0.Event("GoCreateSyscall", trace.GoID(4)) |
| 48 | b0.Event("GoSyscallEndBlocked") |
| 49 | b0.Event("ProcStatus", trace.ProcID(0), tracev2.ProcIdle) |
| 50 | b0.Event("ProcStart", trace.ProcID(0), testgen.Seq(1)) |
| 51 | b0.Event("GoStatus", trace.GoID(4), trace.NoThread, tracev2.GoRunnable) |
| 52 | b0.Event("GoStart", trace.GoID(4), testgen.Seq(1)) |
| 53 | b0.Event("GoSyscallBegin", testgen.Seq(2), testgen.NoStack) |
| 54 | b0.Event("GoDestroySyscall") |
| 55 | |
| 56 | // A new Go-created thread with the same ID appears and |
| 57 | // starts running, then tries to steal the P from the |
| 58 | // first thread. The stealing is interesting because if |
| 59 | // the parser handles GoDestroySyscall wrong, then we |
| 60 | // have a self-steal here potentially that doesn't make |
| 61 | // sense. |
| 62 | b1 := g.Batch(trace.ThreadID(0), 0) |
| 63 | b1.Event("ProcStatus", trace.ProcID(1), tracev2.ProcIdle) |
| 64 | b1.Event("ProcStart", trace.ProcID(1), testgen.Seq(1)) |
| 65 | b1.Event("ProcSteal", trace.ProcID(0), testgen.Seq(3), trace.ThreadID(0)) |
| 66 | } |