Debian Codegraph @

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