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