Source file src/runtime/traceevent.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 event writing API for trace2runtime.go. 6 7 package runtime 8 9 import ( 10 "internal/abi" 11 "internal/runtime/sys" 12 "internal/trace/tracev2" 13 ) 14 15 // traceArg is a simple wrapper type to help ensure that arguments passed 16 // to traces are well-formed. 17 type traceArg uint64 18 19 // traceEventWriter is the high-level API for writing trace events. 20 // 21 // See the comment on traceWriter about style for more details as to why 22 // this type and its methods are structured the way they are. 23 type traceEventWriter struct { 24 tl traceLocker 25 } 26 27 // eventWriter creates a new traceEventWriter. It is the main entrypoint for writing trace events. 28 // 29 // Before creating the event writer, this method will emit a status for the current goroutine 30 // or proc if it exists, and if it hasn't had its status emitted yet. goStatus and procStatus indicate 31 // what the status of goroutine or P should be immediately *before* the events that are about to 32 // be written using the eventWriter (if they exist). No status will be written if there's no active 33 // goroutine or P. 34 // 35 // Callers can elect to pass a constant value here if the status is clear (e.g. a goroutine must have 36 // been Runnable before a GoStart). Otherwise, callers can query the status of either the goroutine 37 // or P and pass the appropriate status. 38 // 39 // In this case, the default status should be tracev2.GoBad or tracev2.ProcBad to help identify bugs sooner. 40 func (tl traceLocker) eventWriter(goStatus tracev2.GoStatus, procStatus tracev2.ProcStatus) traceEventWriter { 41 if pp := tl.mp.p.ptr(); pp != nil && !pp.trace.statusWasTraced(tl.gen) && pp.trace.acquireStatus(tl.gen) { 42 tl.writer().writeProcStatus(uint64(pp.id), procStatus, pp.trace.inSweep).end() 43 } 44 if gp := tl.mp.curg; gp != nil && !gp.trace.statusWasTraced(tl.gen) && gp.trace.acquireStatus(tl.gen) { 45 tl.writer().writeGoStatus(uint64(gp.goid), int64(tl.mp.procid), goStatus, gp.inMarkAssist, 0 /* no stack */).end() 46 } 47 return traceEventWriter{tl} 48 } 49 50 // event writes out a trace event. 51 func (e traceEventWriter) event(ev tracev2.EventType, args ...traceArg) { 52 e.tl.writer().event(ev, args...).end() 53 } 54 55 // stack takes a stack trace skipping the provided number of frames. 56 // It then returns a traceArg representing that stack which may be 57 // passed to write. 58 func (tl traceLocker) stack(skip int) traceArg { 59 return traceArg(traceStack(skip, nil, tl.gen)) 60 } 61 62 // startPC takes a start PC for a goroutine and produces a unique 63 // stack ID for it. 64 // 65 // It then returns a traceArg representing that stack which may be 66 // passed to write. 67 func (tl traceLocker) startPC(pc uintptr) traceArg { 68 // +PCQuantum because makeTraceFrame expects return PCs and subtracts PCQuantum. 69 return traceArg(trace.stackTab[tl.gen%2].put([]uintptr{ 70 logicalStackSentinel, 71 startPCForTrace(pc) + sys.PCQuantum, 72 })) 73 } 74 75 // string returns a traceArg representing s which may be passed to write. 76 // The string is assumed to be relatively short and popular, so it may be 77 // stored for a while in the string dictionary. 78 func (tl traceLocker) string(s string) traceArg { 79 return traceArg(trace.stringTab[tl.gen%2].put(tl.gen, s)) 80 } 81 82 // uniqueString returns a traceArg representing s which may be passed to write. 83 // The string is assumed to be unique or long, so it will be written out to 84 // the trace eagerly. 85 func (tl traceLocker) uniqueString(s string) traceArg { 86 return traceArg(trace.stringTab[tl.gen%2].emit(tl.gen, s)) 87 } 88 89 // rtype returns a traceArg representing typ which may be passed to write. 90 func (tl traceLocker) rtype(typ *abi.Type) traceArg { 91 return traceArg(trace.typeTab[tl.gen%2].put(typ)) 92 } 93