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 and traceAdvance (via suspendG). In
   164  		// these cases, we're not going to emit an event, and we
   165  		// want these goroutines to appear in the final trace as
   166  		// if they're running, not blocked.
   167  		tgs = traceGoWaiting
   168  		if status == _Gwaiting && wr.isWaitingForSuspendG() {
   169  			tgs = traceGoRunning
   170  		}
   171  	case _Gdead:
   172  		throw("tried to trace dead goroutine")
   173  	default:
   174  		throw("tried to trace goroutine with invalid or unsupported status")
   175  	}
   176  	return tgs
   177  }
   178  
   179  // traceSchedResourceState is shared state for scheduling resources (i.e. fields common to
   180  // both Gs and Ps).
   181  type traceSchedResourceState struct {
   182  	// statusTraced indicates whether a status event was traced for this resource
   183  	// a particular generation.
   184  	//
   185  	// There are 3 of these because when transitioning across generations, traceAdvance
   186  	// needs to be able to reliably observe whether a status was traced for the previous
   187  	// generation, while we need to clear the value for the next generation.
   188  	statusTraced [3]atomic.Uint32
   189  
   190  	// seq is the sequence counter for this scheduling resource's events.
   191  	// The purpose of the sequence counter is to establish a partial order between
   192  	// events that don't obviously happen serially (same M) in the stream ofevents.
   193  	//
   194  	// There are two of these so that we can reset the counter on each generation.
   195  	// This saves space in the resulting trace by keeping the counter small and allows
   196  	// GoStatus and GoCreate events to omit a sequence number (implicitly 0).
   197  	seq [2]uint64
   198  }
   199  
   200  // acquireStatus acquires the right to emit a Status event for the scheduling resource.
   201  //
   202  // nosplit because it's part of writing an event for an M, which must not
   203  // have any stack growth.
   204  //
   205  //go:nosplit
   206  func (r *traceSchedResourceState) acquireStatus(gen uintptr) bool {
   207  	if !r.statusTraced[gen%3].CompareAndSwap(0, 1) {
   208  		return false
   209  	}
   210  	r.readyNextGen(gen)
   211  	return true
   212  }
   213  
   214  // readyNextGen readies r for the generation following gen.
   215  func (r *traceSchedResourceState) readyNextGen(gen uintptr) {
   216  	nextGen := traceNextGen(gen)
   217  	r.seq[nextGen%2] = 0
   218  	r.statusTraced[nextGen%3].Store(0)
   219  }
   220  
   221  // statusWasTraced returns true if the sched resource's status was already acquired for tracing.
   222  func (r *traceSchedResourceState) statusWasTraced(gen uintptr) bool {
   223  	return r.statusTraced[gen%3].Load() != 0
   224  }
   225  
   226  // setStatusTraced indicates that the resource's status was already traced, for example
   227  // when a goroutine is created.
   228  func (r *traceSchedResourceState) setStatusTraced(gen uintptr) {
   229  	r.statusTraced[gen%3].Store(1)
   230  }
   231  
   232  // nextSeq returns the next sequence number for the resource.
   233  func (r *traceSchedResourceState) nextSeq(gen uintptr) traceArg {
   234  	r.seq[gen%2]++
   235  	return traceArg(r.seq[gen%2])
   236  }
   237  

View as plain text