| 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 main |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"internal/trace" |
| 10 |
"internal/trace/traceviewer" |
| 11 |
"internal/trace/traceviewer/format" |
| 12 |
) |
| 13 |
|
| 14 |
var _ generator = &threadGenerator{} |
| 15 |
|
| 16 |
type threadGenerator struct { |
| 17 |
globalRangeGenerator |
| 18 |
globalMetricGenerator |
| 19 |
stackSampleGenerator[trace.ThreadID] |
| 20 |
logEventGenerator[trace.ThreadID] |
| 21 |
|
| 22 |
gStates map[trace.GoID]*gState[trace.ThreadID] |
| 23 |
threads map[trace.ThreadID]struct{} |
| 24 |
} |
| 25 |
|
| 26 |
func newThreadGenerator() *threadGenerator { |
| 27 |
tg := new(threadGenerator) |
| 28 |
rg := func(ev *trace.Event) trace.ThreadID { |
| 29 |
return ev.Thread() |
| 30 |
} |
| 31 |
tg.stackSampleGenerator.getResource = rg |
| 32 |
tg.logEventGenerator.getResource = rg |
| 33 |
tg.gStates = make(map[trace.GoID]*gState[trace.ThreadID]) |
| 34 |
tg.threads = make(map[trace.ThreadID]struct{}) |
| 35 |
return tg |
| 36 |
} |
| 37 |
|
| 38 |
func (g *threadGenerator) Sync() { |
| 39 |
g.globalRangeGenerator.Sync() |
| 40 |
} |
| 41 |
|
| 42 |
func (g *threadGenerator) GoroutineLabel(ctx *traceContext, ev *trace.Event) { |
| 43 |
l := ev.Label() |
| 44 |
g.gStates[l.Resource.Goroutine()].setLabel(l.Label) |
| 45 |
} |
| 46 |
|
| 47 |
func (g *threadGenerator) GoroutineRange(ctx *traceContext, ev *trace.Event) { |
| 48 |
r := ev.Range() |
| 49 |
switch ev.Kind() { |
| 50 |
case trace.EventRangeBegin: |
| 51 |
g.gStates[r.Scope.Goroutine()].rangeBegin(ev.Time(), r.Name, ev.Stack()) |
| 52 |
case trace.EventRangeActive: |
| 53 |
g.gStates[r.Scope.Goroutine()].rangeActive(r.Name) |
| 54 |
case trace.EventRangeEnd: |
| 55 |
gs := g.gStates[r.Scope.Goroutine()] |
| 56 |
gs.rangeEnd(ev.Time(), r.Name, ev.Stack(), ctx) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func (g *threadGenerator) GoroutineTransition(ctx *traceContext, ev *trace.Event) { |
| 61 |
if ev.Thread() != trace.NoThread { |
| 62 |
if _, ok := g.threads[ev.Thread()]; !ok { |
| 63 |
g.threads[ev.Thread()] = struct{}{} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
st := ev.StateTransition() |
| 68 |
goID := st.Resource.Goroutine() |
| 69 |
|
| 70 |
// If we haven't seen this goroutine before, create a new |
| 71 |
// gState for it. |
| 72 |
gs, ok := g.gStates[goID] |
| 73 |
if !ok { |
| 74 |
gs = newGState[trace.ThreadID](goID) |
| 75 |
g.gStates[goID] = gs |
| 76 |
} |
| 77 |
// If we haven't already named this goroutine, try to name it. |
| 78 |
gs.augmentName(st.Stack) |
| 79 |
|
| 80 |
// Handle the goroutine state transition. |
| 81 |
from, to := st.Goroutine() |
| 82 |
if from == to { |
| 83 |
// Filter out no-op events. |
| 84 |
return |
| 85 |
} |
| 86 |
if from.Executing() && !to.Executing() { |
| 87 |
if to == trace.GoWaiting { |
| 88 |
// Goroutine started blocking. |
| 89 |
gs.block(ev.Time(), ev.Stack(), st.Reason, ctx) |
| 90 |
} else { |
| 91 |
gs.stop(ev.Time(), ev.Stack(), ctx) |
| 92 |
} |
| 93 |
} |
| 94 |
if !from.Executing() && to.Executing() { |
| 95 |
start := ev.Time() |
| 96 |
if from == trace.GoUndetermined { |
| 97 |
// Back-date the event to the start of the trace. |
| 98 |
start = ctx.startTime |
| 99 |
} |
| 100 |
gs.start(start, ev.Thread(), ctx) |
| 101 |
} |
| 102 |
|
| 103 |
if from == trace.GoWaiting { |
| 104 |
// Goroutine was unblocked. |
| 105 |
gs.unblock(ev.Time(), ev.Stack(), ev.Thread(), ctx) |
| 106 |
} |
| 107 |
if from == trace.GoNotExist && to == trace.GoRunnable { |
| 108 |
// Goroutine was created. |
| 109 |
gs.created(ev.Time(), ev.Thread(), ev.Stack()) |
| 110 |
} |
| 111 |
if from == trace.GoSyscall { |
| 112 |
// Exiting syscall. |
| 113 |
gs.syscallEnd(ev.Time(), to != trace.GoRunning, ctx) |
| 114 |
} |
| 115 |
|
| 116 |
// Handle syscalls. |
| 117 |
if to == trace.GoSyscall { |
| 118 |
start := ev.Time() |
| 119 |
if from == trace.GoUndetermined { |
| 120 |
// Back-date the event to the start of the trace. |
| 121 |
start = ctx.startTime |
| 122 |
} |
| 123 |
// Write down that we've entered a syscall. Note: we might have no P here |
| 124 |
// if we're in a cgo callback or this is a transition from GoUndetermined |
| 125 |
// (i.e. the G has been blocked in a syscall). |
| 126 |
gs.syscallBegin(start, ev.Thread(), ev.Stack()) |
| 127 |
} |
| 128 |
|
| 129 |
// Note down the goroutine transition. |
| 130 |
_, inMarkAssist := gs.activeRanges["GC mark assist"] |
| 131 |
ctx.GoroutineTransition(ctx.elapsed(ev.Time()), viewerGState(from, inMarkAssist), viewerGState(to, inMarkAssist)) |
| 132 |
} |
| 133 |
|
| 134 |
func (g *threadGenerator) ProcTransition(ctx *traceContext, ev *trace.Event) { |
| 135 |
if ev.Thread() != trace.NoThread { |
| 136 |
if _, ok := g.threads[ev.Thread()]; !ok { |
| 137 |
g.threads[ev.Thread()] = struct{}{} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
st := ev.StateTransition() |
| 142 |
viewerEv := traceviewer.InstantEvent{ |
| 143 |
Resource: uint64(ev.Thread()), |
| 144 |
Stack: ctx.Stack(viewerFrames(ev.Stack())), |
| 145 |
|
| 146 |
// Annotate with the thread and proc. The thread is redundant, but this is to |
| 147 |
// stay consistent with the proc view. |
| 148 |
Arg: format.SchedCtxArg{ |
| 149 |
ProcID: uint64(st.Resource.Proc()), |
| 150 |
ThreadID: uint64(ev.Thread()), |
| 151 |
}, |
| 152 |
} |
| 153 |
|
| 154 |
from, to := st.Proc() |
| 155 |
if from == to { |
| 156 |
// Filter out no-op events. |
| 157 |
return |
| 158 |
} |
| 159 |
if to.Executing() { |
| 160 |
start := ev.Time() |
| 161 |
if from == trace.ProcUndetermined { |
| 162 |
start = ctx.startTime |
| 163 |
} |
| 164 |
viewerEv.Name = "proc start" |
| 165 |
viewerEv.Ts = ctx.elapsed(start) |
| 166 |
// TODO(mknyszek): We don't have a state machine for threads, so approximate |
| 167 |
// running threads with running Ps. |
| 168 |
ctx.IncThreadStateCount(ctx.elapsed(start), traceviewer.ThreadStateRunning, 1) |
| 169 |
} |
| 170 |
if from.Executing() { |
| 171 |
start := ev.Time() |
| 172 |
viewerEv.Name = "proc stop" |
| 173 |
viewerEv.Ts = ctx.elapsed(start) |
| 174 |
// TODO(mknyszek): We don't have a state machine for threads, so approximate |
| 175 |
// running threads with running Ps. |
| 176 |
ctx.IncThreadStateCount(ctx.elapsed(start), traceviewer.ThreadStateRunning, -1) |
| 177 |
} |
| 178 |
// TODO(mknyszek): Consider modeling procs differently and have them be |
| 179 |
// transition to and from NotExist when GOMAXPROCS changes. We can emit |
| 180 |
// events for this to clearly delineate GOMAXPROCS changes. |
| 181 |
|
| 182 |
if viewerEv.Name != "" { |
| 183 |
ctx.Instant(viewerEv) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
func (g *threadGenerator) ProcRange(ctx *traceContext, ev *trace.Event) { |
| 188 |
// TODO(mknyszek): Extend procRangeGenerator to support rendering proc ranges on threads. |
| 189 |
} |
| 190 |
|
| 191 |
func (g *threadGenerator) Finish(ctx *traceContext) { |
| 192 |
ctx.SetResourceType("OS THREADS") |
| 193 |
|
| 194 |
// Finish off global ranges. |
| 195 |
g.globalRangeGenerator.Finish(ctx) |
| 196 |
|
| 197 |
// Finish off all the goroutine slices. |
| 198 |
for _, gs := range g.gStates { |
| 199 |
gs.finish(ctx) |
| 200 |
} |
| 201 |
|
| 202 |
// Name all the threads to the emitter. |
| 203 |
for id := range g.threads { |
| 204 |
ctx.Resource(uint64(id), fmt.Sprintf("Thread %d", id)) |
| 205 |
} |
| 206 |
} |