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 (
    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  		// A P is considered to be in a syscall if its attached G is. Since we fully
    66  		// own P, then the goroutine isn't going to transition and we can trivially
    67  		// check if the goroutine is in a syscall. This used to be just a small problematic
    68  		// window, but this is now the default since _Psyscall no longer exists. See #64318
    69  		// for the history on why it was needed while _Psyscall still existed.
    70  		if w.mp.p.ptr() == pp && w.mp.curg != nil && readgstatus(w.mp.curg)&^_Gscan == _Gsyscall {
    71  			status = tracev2.ProcSyscall
    72  		}
    73  	default:
    74  		throw("attempt to trace invalid or unsupported P status")
    75  	}
    76  	w = w.writeProcStatus(uint64(pp.id), status, pp.trace.inSweep)
    77  	return w
    78  }
    79  
    80  // writeProcStatus emits a ProcStatus event with all the provided information.
    81  //
    82  // The caller must have taken ownership of a P's status writing, and the P must be
    83  // prevented from transitioning.
    84  //
    85  // nosplit because it's part of writing an event for an M, which must not
    86  // have any stack growth.
    87  //
    88  //go:nosplit
    89  func (w traceWriter) writeProcStatus(pid uint64, status tracev2.ProcStatus, inSweep bool) traceWriter {
    90  	// The status should never be bad. Some invariant must have been violated.
    91  	if status == tracev2.ProcBad {
    92  		print("runtime: pid=", pid, "\n")
    93  		throw("attempted to trace a bad status for a proc")
    94  	}
    95  
    96  	// Trace the status.
    97  	w = w.event(tracev2.EvProcStatus, traceArg(pid), traceArg(status))
    98  
    99  	// Trace any special ranges that are in-progress.
   100  	if inSweep {
   101  		w = w.event(tracev2.EvGCSweepActive, traceArg(pid))
   102  	}
   103  	return w
   104  }
   105  
   106  // goStatusToTraceGoStatus translates the internal status to tracGoStatus.
   107  //
   108  // status must not be _Gdead or any status whose name has the suffix "_unused."
   109  //
   110  // nosplit because it's part of writing an event for an M, which must not
   111  // have any stack growth.
   112  //
   113  //go:nosplit
   114  func goStatusToTraceGoStatus(status uint32, wr waitReason) tracev2.GoStatus {
   115  	// N.B. Ignore the _Gscan bit. We don't model it in the tracer.
   116  	var tgs tracev2.GoStatus
   117  	switch status &^ _Gscan {
   118  	case _Grunnable:
   119  		tgs = tracev2.GoRunnable
   120  	case _Grunning, _Gcopystack:
   121  		tgs = tracev2.GoRunning
   122  	case _Gsyscall:
   123  		tgs = tracev2.GoSyscall
   124  	case _Gwaiting, _Gpreempted, _Gleaked:
   125  		// There are a number of cases where a G might end up in
   126  		// _Gwaiting but it's actually running in a non-preemptive
   127  		// state but needs to present itself as preempted to the
   128  		// garbage collector and traceAdvance (via suspendG). In
   129  		// these cases, we're not going to emit an event, and we
   130  		// want these goroutines to appear in the final trace as
   131  		// if they're running, not blocked.
   132  		tgs = tracev2.GoWaiting
   133  		if status == _Gwaiting && wr.isWaitingForSuspendG() {
   134  			tgs = tracev2.GoRunning
   135  		}
   136  	case _Gdead, _Gdeadextra:
   137  		throw("tried to trace dead goroutine")
   138  	default:
   139  		throw("tried to trace goroutine with invalid or unsupported status")
   140  	}
   141  	return tgs
   142  }
   143  
   144  // traceSchedResourceState is shared state for scheduling resources (i.e. fields common to
   145  // both Gs and Ps).
   146  type traceSchedResourceState struct {
   147  	// statusTraced indicates whether a status event was traced for this resource
   148  	// a particular generation.
   149  	//
   150  	// There are 3 of these because when transitioning across generations, traceAdvance
   151  	// needs to be able to reliably observe whether a status was traced for the previous
   152  	// generation, while we need to clear the value for the next generation.
   153  	statusTraced [3]atomic.Uint32
   154  
   155  	// seq is the sequence counter for this scheduling resource's events.
   156  	// The purpose of the sequence counter is to establish a partial order between
   157  	// events that don't obviously happen serially (same M) in the stream ofevents.
   158  	//
   159  	// There are two of these so that we can reset the counter on each generation.
   160  	// This saves space in the resulting trace by keeping the counter small and allows
   161  	// GoStatus and GoCreate events to omit a sequence number (implicitly 0).
   162  	seq [2]uint64
   163  }
   164  
   165  // acquireStatus acquires the right to emit a Status event for the scheduling resource.
   166  //
   167  // nosplit because it's part of writing an event for an M, which must not
   168  // have any stack growth.
   169  //
   170  //go:nosplit
   171  func (r *traceSchedResourceState) acquireStatus(gen uintptr) bool {
   172  	if !r.statusTraced[gen%3].CompareAndSwap(0, 1) {
   173  		return false
   174  	}
   175  	r.readyNextGen(gen)
   176  	return true
   177  }
   178  
   179  // readyNextGen readies r for the generation following gen.
   180  func (r *traceSchedResourceState) readyNextGen(gen uintptr) {
   181  	nextGen := traceNextGen(gen)
   182  	r.seq[nextGen%2] = 0
   183  	r.statusTraced[nextGen%3].Store(0)
   184  }
   185  
   186  // statusWasTraced returns true if the sched resource's status was already acquired for tracing.
   187  func (r *traceSchedResourceState) statusWasTraced(gen uintptr) bool {
   188  	return r.statusTraced[gen%3].Load() != 0
   189  }
   190  
   191  // setStatusTraced indicates that the resource's status was already traced, for example
   192  // when a goroutine is created.
   193  func (r *traceSchedResourceState) setStatusTraced(gen uintptr) {
   194  	r.statusTraced[gen%3].Store(1)
   195  }
   196  
   197  // nextSeq returns the next sequence number for the resource.
   198  func (r *traceSchedResourceState) nextSeq(gen uintptr) traceArg {
   199  	r.seq[gen%2]++
   200  	return traceArg(r.seq[gen%2])
   201  }
   202  

View as plain text