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// Trace goroutine and P status management.
6
7package runtime
8
9import "internal/runtime/atomic"
10
11// traceGoStatus is the status of a goroutine.
12//
13// They correspond directly to the various goroutine
14// statuses.
15type traceGoStatus uint8
16
17const (
18	traceGoBad traceGoStatus = iota
19	traceGoRunnable
20	traceGoRunning
21	traceGoSyscall
22	traceGoWaiting
23)
24
25// traceProcStatus is the status of a P.
26//
27// They mostly correspond to the various P statuses.
28type traceProcStatus uint8
29
30const (
31	traceProcBad traceProcStatus = iota
32	traceProcRunning
33	traceProcIdle
34	traceProcSyscall
35
36	// traceProcSyscallAbandoned is a special case of
37	// traceProcSyscall. It's used in the very specific case
38	// where the first a P is mentioned in a generation is
39	// part of a ProcSteal event. If that's the first time
40	// it's mentioned, then there's no GoSyscallBegin to
41	// connect the P stealing back to at that point. This
42	// special state indicates this to the parser, so it
43	// doesn't try to find a GoSyscallEndBlocked that
44	// corresponds with the ProcSteal.
45	traceProcSyscallAbandoned
46)
47
48// writeGoStatus emits a GoStatus event as well as any active ranges on the goroutine.
49func (w traceWriter) writeGoStatus(goid uint64, mid int64, status traceGoStatus, markAssist bool, stackID uint64) traceWriter {
50	// The status should never be bad. Some invariant must have been violated.
51	if status == traceGoBad {
52		print("runtime: goid=", goid, "\n")
53		throw("attempted to trace a bad status for a goroutine")
54	}
55
56	// Trace the status.
57	if stackID == 0 {
58		w = w.event(traceEvGoStatus, traceArg(goid), traceArg(uint64(mid)), traceArg(status))
59	} else {
60		w = w.event(traceEvGoStatusStack, traceArg(goid), traceArg(uint64(mid)), traceArg(status), traceArg(stackID))
61	}
62
63	// Trace any special ranges that are in-progress.
64	if markAssist {
65		w = w.event(traceEvGCMarkAssistActive, traceArg(goid))
66	}
67	return w
68}
69
70// writeProcStatusForP emits a ProcStatus event for the provided p based on its status.
71//
72// The caller must fully own pp and it must be prevented from transitioning (e.g. this can be
73// called by a forEachP callback or from a STW).
74func (w traceWriter) writeProcStatusForP(pp *p, inSTW bool) traceWriter {
75	if !pp.trace.acquireStatus(w.gen) {
76		return w
77	}
78	var status traceProcStatus
79	switch pp.status {
80	case _Pidle, _Pgcstop:
81		status = traceProcIdle
82		if pp.status == _Pgcstop && inSTW {
83			// N.B. a P that is running and currently has the world stopped will be
84			// in _Pgcstop, but we model it as running in the tracer.
85			status = traceProcRunning
86		}
87	case _Prunning:
88		status = traceProcRunning
89		// There's a short window wherein the goroutine may have entered _Gsyscall
90		// but it still owns the P (it's not in _Psyscall yet). The goroutine entering
91		// _Gsyscall is the tracer's signal that the P its bound to is also in a syscall,
92		// so we need to emit a status that matches. See #64318.
93		if w.mp.p.ptr() == pp && w.mp.curg != nil && readgstatus(w.mp.curg)&^_Gscan == _Gsyscall {
94			status = traceProcSyscall
95		}
96	case _Psyscall:
97		status = traceProcSyscall
98	default:
99		throw("attempt to trace invalid or unsupported P status")
100	}
101	w = w.writeProcStatus(uint64(pp.id), status, pp.trace.inSweep)
102	return w
103}
104
105// writeProcStatus emits a ProcStatus event with all the provided information.
106//
107// The caller must have taken ownership of a P's status writing, and the P must be
108// prevented from transitioning.
109func (w traceWriter) writeProcStatus(pid uint64, status traceProcStatus, inSweep bool) traceWriter {
110	// The status should never be bad. Some invariant must have been violated.
111	if status == traceProcBad {
112		print("runtime: pid=", pid, "\n")
113		throw("attempted to trace a bad status for a proc")
114	}
115
116	// Trace the status.
117	w = w.event(traceEvProcStatus, traceArg(pid), traceArg(status))
118
119	// Trace any special ranges that are in-progress.
120	if inSweep {
121		w = w.event(traceEvGCSweepActive, traceArg(pid))
122	}
123	return w
124}
125
126// goStatusToTraceGoStatus translates the internal status to tracGoStatus.
127//
128// status must not be _Gdead or any status whose name has the suffix "_unused."
129func goStatusToTraceGoStatus(status uint32, wr waitReason) traceGoStatus {
130	// N.B. Ignore the _Gscan bit. We don't model it in the tracer.
131	var tgs traceGoStatus
132	switch status &^ _Gscan {
133	case _Grunnable:
134		tgs = traceGoRunnable
135	case _Grunning, _Gcopystack:
136		tgs = traceGoRunning
137	case _Gsyscall:
138		tgs = traceGoSyscall
139	case _Gwaiting, _Gpreempted:
140		// There are a number of cases where a G might end up in
141		// _Gwaiting but it's actually running in a non-preemptive
142		// state but needs to present itself as preempted to the
143		// garbage collector. In these cases, we're not going to
144		// emit an event, and we want these goroutines to appear in
145		// the final trace as if they're running, not blocked.
146		tgs = traceGoWaiting
147		if status == _Gwaiting && wr.isWaitingForGC() {
148			tgs = traceGoRunning
149		}
150	case _Gdead:
151		throw("tried to trace dead goroutine")
152	default:
153		throw("tried to trace goroutine with invalid or unsupported status")
154	}
155	return tgs
156}
157
158// traceSchedResourceState is shared state for scheduling resources (i.e. fields common to
159// both Gs and Ps).
160type traceSchedResourceState struct {
161	// statusTraced indicates whether a status event was traced for this resource
162	// a particular generation.
163	//
164	// There are 3 of these because when transitioning across generations, traceAdvance
165	// needs to be able to reliably observe whether a status was traced for the previous
166	// generation, while we need to clear the value for the next generation.
167	statusTraced [3]atomic.Uint32
168
169	// seq is the sequence counter for this scheduling resource's events.
170	// The purpose of the sequence counter is to establish a partial order between
171	// events that don't obviously happen serially (same M) in the stream ofevents.
172	//
173	// There are two of these so that we can reset the counter on each generation.
174	// This saves space in the resulting trace by keeping the counter small and allows
175	// GoStatus and GoCreate events to omit a sequence number (implicitly 0).
176	seq [2]uint64
177}
178
179// acquireStatus acquires the right to emit a Status event for the scheduling resource.
180func (r *traceSchedResourceState) acquireStatus(gen uintptr) bool {
181	if !r.statusTraced[gen%3].CompareAndSwap(0, 1) {
182		return false
183	}
184	r.readyNextGen(gen)
185	return true
186}
187
188// readyNextGen readies r for the generation following gen.
189func (r *traceSchedResourceState) readyNextGen(gen uintptr) {
190	nextGen := traceNextGen(gen)
191	r.seq[nextGen%2] = 0
192	r.statusTraced[nextGen%3].Store(0)
193}
194
195// statusWasTraced returns true if the sched resource's status was already acquired for tracing.
196func (r *traceSchedResourceState) statusWasTraced(gen uintptr) bool {
197	return r.statusTraced[gen%3].Load() != 0
198}
199
200// setStatusTraced indicates that the resource's status was already traced, for example
201// when a goroutine is created.
202func (r *traceSchedResourceState) setStatusTraced(gen uintptr) {
203	r.statusTraced[gen%3].Store(1)
204}
205
206// nextSeq returns the next sequence number for the resource.
207func (r *traceSchedResourceState) nextSeq(gen uintptr) traceArg {
208	r.seq[gen%2]++
209	return traceArg(r.seq[gen%2])
210}
211