github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/trace/v2/resources.go (about)

     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  package trace
     6  
     7  // ThreadID is the runtime-internal M structure's ID. This is unique
     8  // for each OS thread.
     9  type ThreadID int64
    10  
    11  // NoThread indicates that the relevant events don't correspond to any
    12  // thread in particular.
    13  const NoThread = ThreadID(-1)
    14  
    15  // ProcID is the runtime-internal G structure's id field. This is unique
    16  // for each P.
    17  type ProcID int64
    18  
    19  // NoProc indicates that the relevant events don't correspond to any
    20  // P in particular.
    21  const NoProc = ProcID(-1)
    22  
    23  // GoID is the runtime-internal G structure's goid field. This is unique
    24  // for each goroutine.
    25  type GoID int64
    26  
    27  // NoGoroutine indicates that the relevant events don't correspond to any
    28  // goroutine in particular.
    29  const NoGoroutine = GoID(-1)
    30  
    31  // GoState represents the state of a goroutine.
    32  //
    33  // New GoStates may be added in the future. Users of this type must be robust
    34  // to that possibility.
    35  type GoState uint8
    36  
    37  const (
    38  	GoUndetermined GoState = iota
    39  	GoNotExist
    40  	GoRunnable
    41  	GoRunning
    42  	GoWaiting
    43  	GoSyscall
    44  )
    45  
    46  // Executing returns true if the state indicates that the goroutine is executing
    47  // and bound to its thread.
    48  func (s GoState) Executing() bool
    49  
    50  // String returns a human-readable representation of a GoState.
    51  //
    52  // The format of the returned string is for debugging purposes and is subject to change.
    53  func (s GoState) String() string
    54  
    55  // ProcState represents the state of a proc.
    56  //
    57  // New ProcStates may be added in the future. Users of this type must be robust
    58  // to that possibility.
    59  type ProcState uint8
    60  
    61  const (
    62  	ProcUndetermined ProcState = iota
    63  	ProcNotExist
    64  	ProcRunning
    65  	ProcIdle
    66  )
    67  
    68  // Executing returns true if the state indicates that the proc is executing
    69  // and bound to its thread.
    70  func (s ProcState) Executing() bool
    71  
    72  // String returns a human-readable representation of a ProcState.
    73  //
    74  // The format of the returned string is for debugging purposes and is subject to change.
    75  func (s ProcState) String() string
    76  
    77  // ResourceKind indicates a kind of resource that has a state machine.
    78  //
    79  // New ResourceKinds may be added in the future. Users of this type must be robust
    80  // to that possibility.
    81  type ResourceKind uint8
    82  
    83  const (
    84  	ResourceNone      ResourceKind = iota
    85  	ResourceGoroutine
    86  	ResourceProc
    87  	ResourceThread
    88  )
    89  
    90  // String returns a human-readable representation of a ResourceKind.
    91  //
    92  // The format of the returned string is for debugging purposes and is subject to change.
    93  func (r ResourceKind) String() string
    94  
    95  // ResourceID represents a generic resource ID.
    96  type ResourceID struct {
    97  	// Kind is the kind of resource this ID is for.
    98  	Kind ResourceKind
    99  	id   int64
   100  }
   101  
   102  // MakeResourceID creates a general resource ID from a specific resource's ID.
   103  func MakeResourceID[T interface{ GoID | ProcID | ThreadID }](id T) ResourceID
   104  
   105  // Goroutine obtains a GoID from the resource ID.
   106  //
   107  // r.Kind must be ResourceGoroutine or this function will panic.
   108  func (r ResourceID) Goroutine() GoID
   109  
   110  // Proc obtains a ProcID from the resource ID.
   111  //
   112  // r.Kind must be ResourceProc or this function will panic.
   113  func (r ResourceID) Proc() ProcID
   114  
   115  // Thread obtains a ThreadID from the resource ID.
   116  //
   117  // r.Kind must be ResourceThread or this function will panic.
   118  func (r ResourceID) Thread() ThreadID
   119  
   120  // String returns a human-readable string representation of the ResourceID.
   121  //
   122  // This representation is subject to change and is intended primarily for debugging.
   123  func (r ResourceID) String() string
   124  
   125  // StateTransition provides details about a StateTransition event.
   126  type StateTransition struct {
   127  	// Resource is the resource this state transition is for.
   128  	Resource ResourceID
   129  
   130  	// Reason is a human-readable reason for the state transition.
   131  	Reason string
   132  
   133  	// Stack is the stack trace of the resource making the state transition.
   134  	//
   135  	// This is distinct from the result (Event).Stack because it pertains to
   136  	// the transitioning resource, not any of the ones executing the event
   137  	// this StateTransition came from.
   138  	//
   139  	// An example of this difference is the NotExist -> Runnable transition for
   140  	// goroutines, which indicates goroutine creation. In this particular case,
   141  	// a Stack here would refer to the starting stack of the new goroutine, and
   142  	// an (Event).Stack would refer to the stack trace of whoever created the
   143  	// goroutine.
   144  	Stack Stack
   145  
   146  	// The actual transition data. Stored in a neutral form so that
   147  	// we don't need fields for every kind of resource.
   148  	id       int64
   149  	oldState uint8
   150  	newState uint8
   151  }
   152  
   153  // Goroutine returns the state transition for a goroutine.
   154  //
   155  // Transitions to and from states that are Executing are special in that
   156  // they change the future execution context. In other words, future events
   157  // on the same thread will feature the same goroutine until it stops running.
   158  //
   159  // Panics if d.Resource.Kind is not ResourceGoroutine.
   160  func (d StateTransition) Goroutine() (from, to GoState)
   161  
   162  // Proc returns the state transition for a proc.
   163  //
   164  // Transitions to and from states that are Executing are special in that
   165  // they change the future execution context. In other words, future events
   166  // on the same thread will feature the same goroutine until it stops running.
   167  //
   168  // Panics if d.Resource.Kind is not ResourceProc.
   169  func (d StateTransition) Proc() (from, to ProcState)