github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/trace/v2/event/go122/event.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 go122
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/go-asm/go/trace/v2/event"
    11  )
    12  
    13  const (
    14  	EvNone event.Type = iota // unused
    15  
    16  	// Structural events.
    17  	EvEventBatch // start of per-M batch of events [generation, M ID, timestamp, batch length]
    18  	EvStacks     // start of a section of the stack table [...EvStack]
    19  	EvStack      // stack table entry [ID, ...{PC, func string ID, file string ID, line #}]
    20  	EvStrings    // start of a section of the string dictionary [...EvString]
    21  	EvString     // string dictionary entry [ID, length, string]
    22  	EvCPUSamples // start of a section of CPU samples [...EvCPUSample]
    23  	EvCPUSample  // CPU profiling sample [timestamp, M ID, P ID, goroutine ID, stack ID]
    24  	EvFrequency  // timestamp units per sec [freq]
    25  
    26  	// Procs.
    27  	EvProcsChange // current value of GOMAXPROCS [timestamp, GOMAXPROCS, stack ID]
    28  	EvProcStart   // start of P [timestamp, P ID, P seq]
    29  	EvProcStop    // stop of P [timestamp]
    30  	EvProcSteal   // P was stolen [timestamp, P ID, P seq, M ID]
    31  	EvProcStatus  // P status at the start of a generation [timestamp, P ID, status]
    32  
    33  	// Goroutines.
    34  	EvGoCreate            // goroutine creation [timestamp, new goroutine ID, new stack ID, stack ID]
    35  	EvGoCreateSyscall     // goroutine appears in syscall (cgo callback) [timestamp, new goroutine ID]
    36  	EvGoStart             // goroutine starts running [timestamp, goroutine ID, goroutine seq]
    37  	EvGoDestroy           // goroutine ends [timestamp]
    38  	EvGoDestroySyscall    // goroutine ends in syscall (cgo callback) [timestamp]
    39  	EvGoStop              // goroutine yields its time, but is runnable [timestamp, reason, stack ID]
    40  	EvGoBlock             // goroutine blocks [timestamp, reason, stack ID]
    41  	EvGoUnblock           // goroutine is unblocked [timestamp, goroutine ID, goroutine seq, stack ID]
    42  	EvGoSyscallBegin      // syscall enter [timestamp, P seq, stack ID]
    43  	EvGoSyscallEnd        // syscall exit [timestamp]
    44  	EvGoSyscallEndBlocked // syscall exit and it blocked at some point [timestamp]
    45  	EvGoStatus            // goroutine status at the start of a generation [timestamp, goroutine ID, status]
    46  
    47  	// STW.
    48  	EvSTWBegin // STW start [timestamp, kind]
    49  	EvSTWEnd   // STW done [timestamp]
    50  
    51  	// GC events.
    52  	EvGCActive           // GC active [timestamp, seq]
    53  	EvGCBegin            // GC start [timestamp, seq, stack ID]
    54  	EvGCEnd              // GC done [timestamp, seq]
    55  	EvGCSweepActive      // GC sweep active [timestamp, P ID]
    56  	EvGCSweepBegin       // GC sweep start [timestamp, stack ID]
    57  	EvGCSweepEnd         // GC sweep done [timestamp, swept bytes, reclaimed bytes]
    58  	EvGCMarkAssistActive // GC mark assist active [timestamp, goroutine ID]
    59  	EvGCMarkAssistBegin  // GC mark assist start [timestamp, stack ID]
    60  	EvGCMarkAssistEnd    // GC mark assist done [timestamp]
    61  	EvHeapAlloc          // gcController.heapLive change [timestamp, heap alloc in bytes]
    62  	EvHeapGoal           // gcController.heapGoal() change [timestamp, heap goal in bytes]
    63  
    64  	// Annotations.
    65  	EvGoLabel         // apply string label to current running goroutine [timestamp, label string ID]
    66  	EvUserTaskBegin   // trace.NewTask [timestamp, internal task ID, internal parent task ID, name string ID, stack ID]
    67  	EvUserTaskEnd     // end of a task [timestamp, internal task ID, stack ID]
    68  	EvUserRegionBegin // trace.{Start,With}Region [timestamp, internal task ID, name string ID, stack ID]
    69  	EvUserRegionEnd   // trace.{End,With}Region [timestamp, internal task ID, name string ID, stack ID]
    70  	EvUserLog         // trace.Log [timestamp, internal task ID, key string ID, stack, value string ID]
    71  )
    72  
    73  // EventString returns the name of a Go 1.22 event.
    74  func EventString(typ event.Type) string {
    75  	if int(typ) < len(specs) {
    76  		return specs[typ].Name
    77  	}
    78  	return fmt.Sprintf("Invalid(%d)", typ)
    79  }
    80  
    81  func Specs() []event.Spec {
    82  	return specs[:]
    83  }
    84  
    85  var specs = [...]event.Spec{
    86  	// "Structural" Events.
    87  	EvEventBatch: {
    88  		Name: "EventBatch",
    89  		Args: []string{"gen", "m", "time", "size"},
    90  	},
    91  	EvStacks: {
    92  		Name: "Stacks",
    93  	},
    94  	EvStack: {
    95  		Name:    "Stack",
    96  		Args:    []string{"id", "nframes"},
    97  		IsStack: true,
    98  	},
    99  	EvStrings: {
   100  		Name: "Strings",
   101  	},
   102  	EvString: {
   103  		Name:    "String",
   104  		Args:    []string{"id"},
   105  		HasData: true,
   106  	},
   107  	EvCPUSamples: {
   108  		Name: "CPUSamples",
   109  	},
   110  	EvCPUSample: {
   111  		Name: "CPUSample",
   112  		Args: []string{"time", "p", "g", "m", "stack"},
   113  		// N.B. There's clearly a timestamp here, but these Events
   114  		// are special in that they don't appear in the regular
   115  		// M streams.
   116  	},
   117  	EvFrequency: {
   118  		Name: "Frequency",
   119  		Args: []string{"freq"},
   120  	},
   121  
   122  	// "Timed" Events.
   123  	EvProcsChange: {
   124  		Name:         "ProcsChange",
   125  		Args:         []string{"dt", "procs_value", "stack"},
   126  		IsTimedEvent: true,
   127  		StackIDs:     []int{2},
   128  	},
   129  	EvProcStart: {
   130  		Name:         "ProcStart",
   131  		Args:         []string{"dt", "p", "p_seq"},
   132  		IsTimedEvent: true,
   133  	},
   134  	EvProcStop: {
   135  		Name:         "ProcStop",
   136  		Args:         []string{"dt"},
   137  		IsTimedEvent: true,
   138  	},
   139  	EvProcSteal: {
   140  		Name:         "ProcSteal",
   141  		Args:         []string{"dt", "p", "p_seq", "m"},
   142  		IsTimedEvent: true,
   143  	},
   144  	EvProcStatus: {
   145  		Name:         "ProcStatus",
   146  		Args:         []string{"dt", "p", "pstatus"},
   147  		IsTimedEvent: true,
   148  	},
   149  	EvGoCreate: {
   150  		Name:         "GoCreate",
   151  		Args:         []string{"dt", "new_g", "new_stack", "stack"},
   152  		IsTimedEvent: true,
   153  		StackIDs:     []int{3, 2},
   154  	},
   155  	EvGoCreateSyscall: {
   156  		Name:         "GoCreateSyscall",
   157  		Args:         []string{"dt", "new_g"},
   158  		IsTimedEvent: true,
   159  	},
   160  	EvGoStart: {
   161  		Name:         "GoStart",
   162  		Args:         []string{"dt", "g", "g_seq"},
   163  		IsTimedEvent: true,
   164  	},
   165  	EvGoDestroy: {
   166  		Name:         "GoDestroy",
   167  		Args:         []string{"dt"},
   168  		IsTimedEvent: true,
   169  	},
   170  	EvGoDestroySyscall: {
   171  		Name:         "GoDestroySyscall",
   172  		Args:         []string{"dt"},
   173  		IsTimedEvent: true,
   174  	},
   175  	EvGoStop: {
   176  		Name:         "GoStop",
   177  		Args:         []string{"dt", "reason_string", "stack"},
   178  		IsTimedEvent: true,
   179  		StackIDs:     []int{2},
   180  		StringIDs:    []int{1},
   181  	},
   182  	EvGoBlock: {
   183  		Name:         "GoBlock",
   184  		Args:         []string{"dt", "reason_string", "stack"},
   185  		IsTimedEvent: true,
   186  		StackIDs:     []int{2},
   187  		StringIDs:    []int{1},
   188  	},
   189  	EvGoUnblock: {
   190  		Name:         "GoUnblock",
   191  		Args:         []string{"dt", "g", "g_seq", "stack"},
   192  		IsTimedEvent: true,
   193  		StackIDs:     []int{3},
   194  	},
   195  	EvGoSyscallBegin: {
   196  		Name:         "GoSyscallBegin",
   197  		Args:         []string{"dt", "p_seq", "stack"},
   198  		IsTimedEvent: true,
   199  		StackIDs:     []int{2},
   200  	},
   201  	EvGoSyscallEnd: {
   202  		Name:         "GoSyscallEnd",
   203  		Args:         []string{"dt"},
   204  		StartEv:      EvGoSyscallBegin,
   205  		IsTimedEvent: true,
   206  	},
   207  	EvGoSyscallEndBlocked: {
   208  		Name:         "GoSyscallEndBlocked",
   209  		Args:         []string{"dt"},
   210  		StartEv:      EvGoSyscallBegin,
   211  		IsTimedEvent: true,
   212  	},
   213  	EvGoStatus: {
   214  		Name:         "GoStatus",
   215  		Args:         []string{"dt", "g", "m", "gstatus"},
   216  		IsTimedEvent: true,
   217  	},
   218  	EvSTWBegin: {
   219  		Name:         "STWBegin",
   220  		Args:         []string{"dt", "kind_string", "stack"},
   221  		IsTimedEvent: true,
   222  		StackIDs:     []int{2},
   223  		StringIDs:    []int{1},
   224  	},
   225  	EvSTWEnd: {
   226  		Name:         "STWEnd",
   227  		Args:         []string{"dt"},
   228  		StartEv:      EvSTWBegin,
   229  		IsTimedEvent: true,
   230  	},
   231  	EvGCActive: {
   232  		Name:         "GCActive",
   233  		Args:         []string{"dt", "gc_seq"},
   234  		IsTimedEvent: true,
   235  		StartEv:      EvGCBegin,
   236  	},
   237  	EvGCBegin: {
   238  		Name:         "GCBegin",
   239  		Args:         []string{"dt", "gc_seq", "stack"},
   240  		IsTimedEvent: true,
   241  		StackIDs:     []int{2},
   242  	},
   243  	EvGCEnd: {
   244  		Name:         "GCEnd",
   245  		Args:         []string{"dt", "gc_seq"},
   246  		StartEv:      EvGCBegin,
   247  		IsTimedEvent: true,
   248  	},
   249  	EvGCSweepActive: {
   250  		Name:         "GCSweepActive",
   251  		Args:         []string{"dt", "p"},
   252  		StartEv:      EvGCSweepBegin,
   253  		IsTimedEvent: true,
   254  	},
   255  	EvGCSweepBegin: {
   256  		Name:         "GCSweepBegin",
   257  		Args:         []string{"dt", "stack"},
   258  		IsTimedEvent: true,
   259  		StackIDs:     []int{1},
   260  	},
   261  	EvGCSweepEnd: {
   262  		Name:         "GCSweepEnd",
   263  		Args:         []string{"dt", "swept_value", "reclaimed_value"},
   264  		StartEv:      EvGCSweepBegin,
   265  		IsTimedEvent: true,
   266  	},
   267  	EvGCMarkAssistActive: {
   268  		Name:         "GCMarkAssistActive",
   269  		Args:         []string{"dt", "g"},
   270  		StartEv:      EvGCMarkAssistBegin,
   271  		IsTimedEvent: true,
   272  	},
   273  	EvGCMarkAssistBegin: {
   274  		Name:         "GCMarkAssistBegin",
   275  		Args:         []string{"dt", "stack"},
   276  		IsTimedEvent: true,
   277  		StackIDs:     []int{1},
   278  	},
   279  	EvGCMarkAssistEnd: {
   280  		Name:         "GCMarkAssistEnd",
   281  		Args:         []string{"dt"},
   282  		StartEv:      EvGCMarkAssistBegin,
   283  		IsTimedEvent: true,
   284  	},
   285  	EvHeapAlloc: {
   286  		Name:         "HeapAlloc",
   287  		Args:         []string{"dt", "heapalloc_value"},
   288  		IsTimedEvent: true,
   289  	},
   290  	EvHeapGoal: {
   291  		Name:         "HeapGoal",
   292  		Args:         []string{"dt", "heapgoal_value"},
   293  		IsTimedEvent: true,
   294  	},
   295  	EvGoLabel: {
   296  		Name:         "GoLabel",
   297  		Args:         []string{"dt", "label_string"},
   298  		IsTimedEvent: true,
   299  		StringIDs:    []int{1},
   300  	},
   301  	EvUserTaskBegin: {
   302  		Name:         "UserTaskBegin",
   303  		Args:         []string{"dt", "task", "parent_task", "name_string", "stack"},
   304  		IsTimedEvent: true,
   305  		StackIDs:     []int{4},
   306  		StringIDs:    []int{3},
   307  	},
   308  	EvUserTaskEnd: {
   309  		Name:         "UserTaskEnd",
   310  		Args:         []string{"dt", "task", "stack"},
   311  		IsTimedEvent: true,
   312  		StackIDs:     []int{2},
   313  	},
   314  	EvUserRegionBegin: {
   315  		Name:         "UserRegionBegin",
   316  		Args:         []string{"dt", "task", "name_string", "stack"},
   317  		IsTimedEvent: true,
   318  		StackIDs:     []int{3},
   319  		StringIDs:    []int{2},
   320  	},
   321  	EvUserRegionEnd: {
   322  		Name:         "UserRegionEnd",
   323  		Args:         []string{"dt", "task", "name_string", "stack"},
   324  		StartEv:      EvUserRegionBegin,
   325  		IsTimedEvent: true,
   326  		StackIDs:     []int{3},
   327  		StringIDs:    []int{2},
   328  	},
   329  	EvUserLog: {
   330  		Name:         "UserLog",
   331  		Args:         []string{"dt", "task", "key_string", "value_string", "stack"},
   332  		IsTimedEvent: true,
   333  		StackIDs:     []int{4},
   334  		StringIDs:    []int{2, 3},
   335  	},
   336  }
   337  
   338  type GoStatus uint8
   339  
   340  const (
   341  	GoBad GoStatus = iota
   342  	GoRunnable
   343  	GoRunning
   344  	GoSyscall
   345  	GoWaiting
   346  )
   347  
   348  func (s GoStatus) String() string {
   349  	switch s {
   350  	case GoRunnable:
   351  		return "Runnable"
   352  	case GoRunning:
   353  		return "Running"
   354  	case GoSyscall:
   355  		return "Syscall"
   356  	case GoWaiting:
   357  		return "Waiting"
   358  	}
   359  	return "Bad"
   360  }
   361  
   362  type ProcStatus uint8
   363  
   364  const (
   365  	ProcBad ProcStatus = iota
   366  	ProcRunning
   367  	ProcIdle
   368  	ProcSyscall
   369  	ProcSyscallAbandoned
   370  )
   371  
   372  func (s ProcStatus) String() string {
   373  	switch s {
   374  	case ProcRunning:
   375  		return "Running"
   376  	case ProcIdle:
   377  		return "Idle"
   378  	case ProcSyscall:
   379  		return "Syscall"
   380  	}
   381  	return "Bad"
   382  }
   383  
   384  const (
   385  	// Various format-specific constants.
   386  	MaxBatchSize      = 64 << 10
   387  	MaxFramesPerStack = 128
   388  	MaxStringSize     = 1 << 10
   389  )