github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/trace/traceviewer/emitter.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 traceviewer 6 7 import ( 8 "github.com/shogo82148/std/internal/trace" 9 "github.com/shogo82148/std/internal/trace/traceviewer/format" 10 "github.com/shogo82148/std/io" 11 "github.com/shogo82148/std/time" 12 ) 13 14 type TraceConsumer struct { 15 ConsumeTimeUnit func(unit string) 16 ConsumeViewerEvent func(v *format.Event, required bool) 17 ConsumeViewerFrame func(key string, f format.Frame) 18 Flush func() 19 } 20 21 // ViewerDataTraceConsumer returns a TraceConsumer that writes to w. The 22 // startIdx and endIdx are used for splitting large traces. They refer to 23 // indexes in the traceEvents output array, not the events in the trace input. 24 func ViewerDataTraceConsumer(w io.Writer, startIdx, endIdx int64) TraceConsumer 25 26 func SplittingTraceConsumer(max int) (*splitter, TraceConsumer) 27 28 // WalkStackFrames calls fn for id and all of its parent frames from allFrames. 29 func WalkStackFrames(allFrames map[string]format.Frame, id int, fn func(id int)) 30 31 type Mode int 32 33 const ( 34 ModeGoroutineOriented Mode = 1 << iota 35 ModeTaskOriented 36 ModeThreadOriented 37 ) 38 39 // NewEmitter returns a new Emitter that writes to c. The rangeStart and 40 // rangeEnd args are used for splitting large traces. 41 func NewEmitter(c TraceConsumer, rangeStart, rangeEnd time.Duration) *Emitter 42 43 type Emitter struct { 44 c TraceConsumer 45 rangeStart time.Duration 46 rangeEnd time.Duration 47 48 heapStats, prevHeapStats heapStats 49 gstates, prevGstates [gStateCount]int64 50 threadStats, prevThreadStats [threadStateCount]int64 51 gomaxprocs uint64 52 frameTree frameNode 53 frameSeq int 54 arrowSeq uint64 55 filter func(uint64) bool 56 resourceType string 57 resources map[uint64]string 58 focusResource uint64 59 tasks map[uint64]task 60 asyncSliceSeq uint64 61 } 62 63 func (e *Emitter) Gomaxprocs(v uint64) 64 65 func (e *Emitter) Resource(id uint64, name string) 66 67 func (e *Emitter) SetResourceType(name string) 68 69 func (e *Emitter) SetResourceFilter(filter func(uint64) bool) 70 71 func (e *Emitter) Task(id uint64, name string, sortIndex int) 72 73 func (e *Emitter) Slice(s SliceEvent) 74 75 func (e *Emitter) TaskSlice(s SliceEvent) 76 77 type SliceEvent struct { 78 Name string 79 Ts time.Duration 80 Dur time.Duration 81 Resource uint64 82 Stack int 83 EndStack int 84 Arg any 85 } 86 87 func (e *Emitter) AsyncSlice(s AsyncSliceEvent) 88 89 type AsyncSliceEvent struct { 90 SliceEvent 91 Category string 92 Scope string 93 TaskColorIndex uint64 94 } 95 96 func (e *Emitter) Instant(i InstantEvent) 97 98 type InstantEvent struct { 99 Ts time.Duration 100 Name string 101 Category string 102 Resource uint64 103 Stack int 104 Arg any 105 } 106 107 func (e *Emitter) Arrow(a ArrowEvent) 108 109 func (e *Emitter) TaskArrow(a ArrowEvent) 110 111 type ArrowEvent struct { 112 Name string 113 Start time.Duration 114 End time.Duration 115 FromResource uint64 116 FromStack int 117 ToResource uint64 118 } 119 120 func (e *Emitter) Event(ev *format.Event) 121 122 func (e *Emitter) HeapAlloc(ts time.Duration, v uint64) 123 124 func (e *Emitter) Focus(id uint64) 125 126 func (e *Emitter) GoroutineTransition(ts time.Duration, from, to GState) 127 128 func (e *Emitter) IncThreadStateCount(ts time.Duration, state ThreadState, delta int64) 129 130 func (e *Emitter) HeapGoal(ts time.Duration, v uint64) 131 132 // Err returns an error if the emitter is in an invalid state. 133 func (e *Emitter) Err() error 134 135 // OptionalEvent emits ev if it's within the time range of of the consumer, i.e. 136 // the selected trace split range. 137 func (e *Emitter) OptionalEvent(ev *format.Event) 138 139 func (e *Emitter) Flush() 140 141 // Stack emits the given frames and returns a unique id for the stack. No 142 // pointers to the given data are being retained beyond the call to Stack. 143 func (e *Emitter) Stack(stk []*trace.Frame) int 144 145 type GState int 146 147 const ( 148 GDead GState = iota 149 GRunnable 150 GRunning 151 GWaiting 152 GWaitingGC 153 ) 154 155 type ThreadState int 156 157 const ( 158 ThreadStateInSyscall ThreadState = iota 159 ThreadStateInSyscallRuntime 160 ThreadStateRunning 161 )