Debian Codegraph @

src/internal/trace/testdata/generators/go122-create-syscall-reuse-thread-id.go

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