github.com/ice-blockchain/go/src@v0.0.0-20240403114104-1564d284e521/runtime/trace2event.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 //go:build goexperiment.exectracer2 6 7 // Trace event writing API for trace2runtime.go. 8 9 package runtime 10 11 import ( 12 "runtime/internal/sys" 13 ) 14 15 // Event types in the trace, args are given in square brackets. 16 // 17 // Naming scheme: 18 // - Time range event pairs have suffixes "Begin" and "End". 19 // - "Start", "Stop", "Create", "Destroy", "Block", "Unblock" 20 // are suffixes reserved for scheduling resources. 21 // 22 // NOTE: If you add an event type, make sure you also update all 23 // tables in this file! 24 type traceEv uint8 25 26 const ( 27 traceEvNone traceEv = iota // unused 28 29 // Structural events. 30 traceEvEventBatch // start of per-M batch of events [generation, M ID, timestamp, batch length] 31 traceEvStacks // start of a section of the stack table [...traceEvStack] 32 traceEvStack // stack table entry [ID, ...{PC, func string ID, file string ID, line #}] 33 traceEvStrings // start of a section of the string dictionary [...traceEvString] 34 traceEvString // string dictionary entry [ID, length, string] 35 traceEvCPUSamples // start of a section of CPU samples [...traceEvCPUSample] 36 traceEvCPUSample // CPU profiling sample [timestamp, M ID, P ID, goroutine ID, stack ID] 37 traceEvFrequency // timestamp units per sec [freq] 38 39 // Procs. 40 traceEvProcsChange // current value of GOMAXPROCS [timestamp, GOMAXPROCS, stack ID] 41 traceEvProcStart // start of P [timestamp, P ID, P seq] 42 traceEvProcStop // stop of P [timestamp] 43 traceEvProcSteal // P was stolen [timestamp, P ID, P seq, M ID] 44 traceEvProcStatus // P status at the start of a generation [timestamp, P ID, status] 45 46 // Goroutines. 47 traceEvGoCreate // goroutine creation [timestamp, new goroutine ID, new stack ID, stack ID] 48 traceEvGoCreateSyscall // goroutine appears in syscall (cgo callback) [timestamp, new goroutine ID] 49 traceEvGoStart // goroutine starts running [timestamp, goroutine ID, goroutine seq] 50 traceEvGoDestroy // goroutine ends [timestamp] 51 traceEvGoDestroySyscall // goroutine ends in syscall (cgo callback) [timestamp] 52 traceEvGoStop // goroutine yields its time, but is runnable [timestamp, reason, stack ID] 53 traceEvGoBlock // goroutine blocks [timestamp, reason, stack ID] 54 traceEvGoUnblock // goroutine is unblocked [timestamp, goroutine ID, goroutine seq, stack ID] 55 traceEvGoSyscallBegin // syscall enter [timestamp, P seq, stack ID] 56 traceEvGoSyscallEnd // syscall exit [timestamp] 57 traceEvGoSyscallEndBlocked // syscall exit and it blocked at some point [timestamp] 58 traceEvGoStatus // goroutine status at the start of a generation [timestamp, goroutine ID, M ID, status] 59 60 // STW. 61 traceEvSTWBegin // STW start [timestamp, kind] 62 traceEvSTWEnd // STW done [timestamp] 63 64 // GC events. 65 traceEvGCActive // GC active [timestamp, seq] 66 traceEvGCBegin // GC start [timestamp, seq, stack ID] 67 traceEvGCEnd // GC done [timestamp, seq] 68 traceEvGCSweepActive // GC sweep active [timestamp, P ID] 69 traceEvGCSweepBegin // GC sweep start [timestamp, stack ID] 70 traceEvGCSweepEnd // GC sweep done [timestamp, swept bytes, reclaimed bytes] 71 traceEvGCMarkAssistActive // GC mark assist active [timestamp, goroutine ID] 72 traceEvGCMarkAssistBegin // GC mark assist start [timestamp, stack ID] 73 traceEvGCMarkAssistEnd // GC mark assist done [timestamp] 74 traceEvHeapAlloc // gcController.heapLive change [timestamp, heap alloc in bytes] 75 traceEvHeapGoal // gcController.heapGoal() change [timestamp, heap goal in bytes] 76 77 // Annotations. 78 traceEvGoLabel // apply string label to current running goroutine [timestamp, label string ID] 79 traceEvUserTaskBegin // trace.NewTask [timestamp, internal task ID, internal parent task ID, name string ID, stack ID] 80 traceEvUserTaskEnd // end of a task [timestamp, internal task ID, stack ID] 81 traceEvUserRegionBegin // trace.{Start,With}Region [timestamp, internal task ID, name string ID, stack ID] 82 traceEvUserRegionEnd // trace.{End,With}Region [timestamp, internal task ID, name string ID, stack ID] 83 traceEvUserLog // trace.Log [timestamp, internal task ID, key string ID, stack, value string ID] 84 85 // Coroutines. 86 traceEvGoSwitch // goroutine switch (coroswitch) [timestamp, goroutine ID, goroutine seq] 87 traceEvGoSwitchDestroy // goroutine switch and destroy [timestamp, goroutine ID, goroutine seq] 88 traceEvGoCreateBlocked // goroutine creation (starts blocked) [timestamp, new goroutine ID, new stack ID, stack ID] 89 ) 90 91 // traceArg is a simple wrapper type to help ensure that arguments passed 92 // to traces are well-formed. 93 type traceArg uint64 94 95 // traceEventWriter is the high-level API for writing trace events. 96 // 97 // See the comment on traceWriter about style for more details as to why 98 // this type and its methods are structured the way they are. 99 type traceEventWriter struct { 100 w traceWriter 101 } 102 103 // eventWriter creates a new traceEventWriter. It is the main entrypoint for writing trace events. 104 // 105 // Before creating the event writer, this method will emit a status for the current goroutine 106 // or proc if it exists, and if it hasn't had its status emitted yet. goStatus and procStatus indicate 107 // what the status of goroutine or P should be immediately *before* the events that are about to 108 // be written using the eventWriter (if they exist). No status will be written if there's no active 109 // goroutine or P. 110 // 111 // Callers can elect to pass a constant value here if the status is clear (e.g. a goroutine must have 112 // been Runnable before a GoStart). Otherwise, callers can query the status of either the goroutine 113 // or P and pass the appropriate status. 114 // 115 // In this case, the default status should be traceGoBad or traceProcBad to help identify bugs sooner. 116 func (tl traceLocker) eventWriter(goStatus traceGoStatus, procStatus traceProcStatus) traceEventWriter { 117 w := tl.writer() 118 if pp := tl.mp.p.ptr(); pp != nil && !pp.trace.statusWasTraced(tl.gen) && pp.trace.acquireStatus(tl.gen) { 119 w = w.writeProcStatus(uint64(pp.id), procStatus, pp.trace.inSweep) 120 } 121 if gp := tl.mp.curg; gp != nil && !gp.trace.statusWasTraced(tl.gen) && gp.trace.acquireStatus(tl.gen) { 122 w = w.writeGoStatus(uint64(gp.goid), int64(tl.mp.procid), goStatus, gp.inMarkAssist) 123 } 124 return traceEventWriter{w} 125 } 126 127 // commit writes out a trace event and calls end. It's a helper to make the 128 // common case of writing out a single event less error-prone. 129 func (e traceEventWriter) commit(ev traceEv, args ...traceArg) { 130 e = e.write(ev, args...) 131 e.end() 132 } 133 134 // write writes an event into the trace. 135 func (e traceEventWriter) write(ev traceEv, args ...traceArg) traceEventWriter { 136 e.w = e.w.event(ev, args...) 137 return e 138 } 139 140 // end finishes writing to the trace. The traceEventWriter must not be used after this call. 141 func (e traceEventWriter) end() { 142 e.w.end() 143 } 144 145 // traceEventWrite is the part of traceEvent that actually writes the event. 146 func (w traceWriter) event(ev traceEv, args ...traceArg) traceWriter { 147 // Make sure we have room. 148 w, _ = w.ensure(1 + (len(args)+1)*traceBytesPerNumber) 149 150 // Compute the timestamp diff that we'll put in the trace. 151 ts := traceClockNow() 152 if ts <= w.traceBuf.lastTime { 153 ts = w.traceBuf.lastTime + 1 154 } 155 tsDiff := uint64(ts - w.traceBuf.lastTime) 156 w.traceBuf.lastTime = ts 157 158 // Write out event. 159 w.byte(byte(ev)) 160 w.varint(tsDiff) 161 for _, arg := range args { 162 w.varint(uint64(arg)) 163 } 164 return w 165 } 166 167 // stack takes a stack trace skipping the provided number of frames. 168 // It then returns a traceArg representing that stack which may be 169 // passed to write. 170 func (tl traceLocker) stack(skip int) traceArg { 171 return traceArg(traceStack(skip, tl.mp, tl.gen)) 172 } 173 174 // startPC takes a start PC for a goroutine and produces a unique 175 // stack ID for it. 176 // 177 // It then returns a traceArg representing that stack which may be 178 // passed to write. 179 func (tl traceLocker) startPC(pc uintptr) traceArg { 180 // +PCQuantum because makeTraceFrame expects return PCs and subtracts PCQuantum. 181 return traceArg(trace.stackTab[tl.gen%2].put([]uintptr{ 182 logicalStackSentinel, 183 startPCForTrace(pc) + sys.PCQuantum, 184 })) 185 } 186 187 // string returns a traceArg representing s which may be passed to write. 188 // The string is assumed to be relatively short and popular, so it may be 189 // stored for a while in the string dictionary. 190 func (tl traceLocker) string(s string) traceArg { 191 return traceArg(trace.stringTab[tl.gen%2].put(tl.gen, s)) 192 } 193 194 // uniqueString returns a traceArg representing s which may be passed to write. 195 // The string is assumed to be unique or long, so it will be written out to 196 // the trace eagerly. 197 func (tl traceLocker) uniqueString(s string) traceArg { 198 return traceArg(trace.stringTab[tl.gen%2].emit(tl.gen, s)) 199 }