Source File
trace.go
Belonging Package
runtime
package runtime
import (
)
const (
traceEvNone = 0 // unused
traceEvBatch = 1 // start of per-P batch of events [pid, timestamp]
traceEvFrequency = 2 // contains tracer timer frequency [frequency (ticks per second)]
traceEvStack = 3 // stack [stack id, number of PCs, array of {PC, func string ID, file string ID, line}]
traceEvGomaxprocs = 4 // current value of GOMAXPROCS [timestamp, GOMAXPROCS, stack id]
traceEvProcStart = 5 // start of P [timestamp, thread id]
traceEvProcStop = 6 // stop of P [timestamp]
traceEvGCStart = 7 // GC start [timestamp, seq, stack id]
traceEvGCDone = 8 // GC done [timestamp]
traceEvGCSTWStart = 9 // GC STW start [timestamp, kind]
traceEvGCSTWDone = 10 // GC STW done [timestamp]
traceEvGCSweepStart = 11 // GC sweep start [timestamp, stack id]
traceEvGCSweepDone = 12 // GC sweep done [timestamp, swept, reclaimed]
traceEvGoCreate = 13 // goroutine creation [timestamp, new goroutine id, new stack id, stack id]
traceEvGoStart = 14 // goroutine starts running [timestamp, goroutine id, seq]
traceEvGoEnd = 15 // goroutine ends [timestamp]
traceEvGoStop = 16 // goroutine stops (like in select{}) [timestamp, stack]
traceEvGoSched = 17 // goroutine calls Gosched [timestamp, stack]
traceEvGoPreempt = 18 // goroutine is preempted [timestamp, stack]
traceEvGoSleep = 19 // goroutine calls Sleep [timestamp, stack]
traceEvGoBlock = 20 // goroutine blocks [timestamp, stack]
traceEvGoUnblock = 21 // goroutine is unblocked [timestamp, goroutine id, seq, stack]
traceEvGoBlockSend = 22 // goroutine blocks on chan send [timestamp, stack]
traceEvGoBlockRecv = 23 // goroutine blocks on chan recv [timestamp, stack]
traceEvGoBlockSelect = 24 // goroutine blocks on select [timestamp, stack]
traceEvGoBlockSync = 25 // goroutine blocks on Mutex/RWMutex [timestamp, stack]
traceEvGoBlockCond = 26 // goroutine blocks on Cond [timestamp, stack]
traceEvGoBlockNet = 27 // goroutine blocks on network [timestamp, stack]
traceEvGoSysCall = 28 // syscall enter [timestamp, stack]
traceEvGoSysExit = 29 // syscall exit [timestamp, goroutine id, seq, real timestamp]
traceEvGoSysBlock = 30 // syscall blocks [timestamp]
traceEvGoWaiting = 31 // denotes that goroutine is blocked when tracing starts [timestamp, goroutine id]
traceEvGoInSyscall = 32 // denotes that goroutine is in syscall when tracing starts [timestamp, goroutine id]
traceEvHeapAlloc = 33 // memstats.heap_live change [timestamp, heap_alloc]
traceEvNextGC = 34 // memstats.next_gc change [timestamp, next_gc]
traceEvTimerGoroutine = 35 // not currently used; previously denoted timer goroutine [timer goroutine id]
traceEvFutileWakeup = 36 // denotes that the previous wakeup of this goroutine was futile [timestamp]
traceEvString = 37 // string dictionary entry [ID, length, string]
traceEvGoStartLocal = 38 // goroutine starts running on the same P as the last event [timestamp, goroutine id]
traceEvGoUnblockLocal = 39 // goroutine is unblocked on the same P as the last event [timestamp, goroutine id, stack]
traceEvGoSysExitLocal = 40 // syscall exit on the same P as the last event [timestamp, goroutine id, real timestamp]
traceEvGoStartLabel = 41 // goroutine starts running with label [timestamp, goroutine id, seq, label string id]
traceEvGoBlockGC = 42 // goroutine blocks on GC assist [timestamp, stack]
traceEvGCMarkAssistStart = 43 // GC mark assist start [timestamp, stack]
traceEvGCMarkAssistDone = 44 // GC mark assist done [timestamp]
traceEvUserTaskCreate = 45 // trace.NewContext [timestamp, internal task id, internal parent task id, stack, name string]
traceEvUserTaskEnd = 46 // end of a task [timestamp, internal task id, stack]
traceEvUserRegion = 47 // trace.WithRegion [timestamp, internal task id, mode(0:start, 1:end), stack, name string]
traceEvUserLog = 48 // trace.Log [timestamp, internal task id, key string id, stack, value string]
)
traceFutileWakeup byte = 128
)
var trace struct {
lock mutex // protects the following members
lockOwner *g // to avoid deadlocks during recursive lock locks
enabled bool // when set runtime traces events
shutdown bool // set when we are waiting for trace reader to finish after setting enabled to false
headerWritten bool // whether ReadTrace has emitted trace header
footerWritten bool // whether ReadTrace has emitted trace footer
shutdownSema uint32 // used to wait for ReadTrace completion
seqStart uint64 // sequence number when tracing was started
ticksStart int64 // cputicks when tracing was started
ticksEnd int64 // cputicks when tracing was stopped
timeStart int64 // nanotime when tracing was started
timeEnd int64 // nanotime when tracing was stopped
seqGC uint64 // GC start/done sequencer
reading traceBufPtr // buffer currently handed off to user
empty traceBufPtr // stack of empty buffers
fullHead traceBufPtr // queue of full buffers
fullTail traceBufPtr
reader guintptr // goroutine that called ReadTrace, or nil
stackTab traceStackTable // maps stack traces to unique ids
markWorkerLabels [len(gcMarkWorkerModeStrings)]uint64
bufLock mutex // protects buf
buf traceBufPtr // global trace buffer, used when running without a p
}
type traceBufHeader struct {
link traceBufPtr // in trace.empty/full
lastTicks uint64 // when we wrote the last event
pos int // next write offset in arr
stk [traceStackSize]uintptr // scratch buffer for traceback
}
type traceBuf struct {
traceBufHeader
arr [64<<10 - unsafe.Sizeof(traceBufHeader{})]byte // underlying buffer for traceBufHeader.buf
}
type traceBufPtr uintptr
func ( traceBufPtr) () *traceBuf { return (*traceBuf)(unsafe.Pointer()) }
func ( *traceBufPtr) ( *traceBuf) { * = traceBufPtr(unsafe.Pointer()) }
func ( *traceBuf) traceBufPtr {
return traceBufPtr(unsafe.Pointer())
}
stopTheWorldGC("start tracing")
lock(&trace.bufLock)
if trace.enabled || trace.shutdown {
unlock(&trace.bufLock)
unlock(&sched.sysmonlock)
startTheWorldGC()
return errorString("tracing is already enabled")
}
:= getg()
.m.startingtrace = true
:= acquirem()
:= make([]uintptr, traceStackSize)
:= traceStackID(, , 2)
releasem()
for , := range allgs {
:= readgstatus()
if != _Gdead {
.traceseq = 0
.traceseq++
traceEvent(traceEvGoWaiting, -1, uint64(.goid))
}
if == _Gsyscall {
.traceseq++
traceEvent(traceEvGoInSyscall, -1, uint64(.goid))
} else {
.sysblocktraced = false
}
}
traceProcStart()
trace.ticksStart = cputicks()
trace.timeStart = nanotime()
trace.headerWritten = false
trace.footerWritten = false
, , := traceAcquireBuffer()
for , := range gcMarkWorkerModeStrings[:] {
trace.markWorkerLabels[], = traceString(, , )
}
traceReleaseBuffer()
unlock(&trace.bufLock)
unlock(&sched.sysmonlock)
startTheWorldGC()
return nil
}
stopTheWorldGC("stop tracing")
lock(&trace.bufLock)
if !trace.enabled {
unlock(&trace.bufLock)
unlock(&sched.sysmonlock)
startTheWorldGC()
return
}
traceGoSched()
semacquire(&trace.shutdownSema)
if raceenabled {
raceacquire(unsafe.Pointer(&trace.shutdownSema))
}
lock(&trace.lock)
for , := range allp[:cap(allp)] {
if .tracebuf != 0 {
throw("trace: non-empty trace buffer in proc")
}
}
if trace.buf != 0 {
throw("trace: non-empty global trace buffer")
}
if trace.fullHead != 0 || trace.fullTail != 0 {
throw("trace: non-empty full trace buffer")
}
if trace.reading != 0 || trace.reader != 0 {
throw("trace: reading after shutdown")
}
for trace.empty != 0 {
:= trace.empty
trace.empty = .ptr().link
sysFree(unsafe.Pointer(), unsafe.Sizeof(*.ptr()), &memstats.other_sys)
}
trace.strings = nil
trace.shutdown = false
unlock(&trace.lock)
}
if !trace.footerWritten {
semrelease(&trace.shutdownSema)
return nil
if !trace.enabled && !.startingtrace {
traceReleaseBuffer()
return
}
if > 0 {
if getg() == .curg {
++ // +1 because stack is captured in traceEventLocked.
}
}
traceEventLocked(0, , , , , , ...)
traceReleaseBuffer()
}
func ( int, *m, int32, *traceBufPtr, byte, int, ...uint64) {
:= 2 + 5*traceBytesPerNumber + // event type, length, sequence, timestamp, stack id and two add params
if == nil || len(.arr)-.pos < {
= traceFlush(traceBufPtrOf(), ).ptr()
.set()
}
:= uint64(cputicks()) / traceTickDiv
:= - .lastTicks
.lastTicks =
:= byte(len())
if >= 0 {
++
if > 3 {
= 3
}
:= .pos
.byte( | <<traceArgCountShift)
var *byte
func ( traceBufPtr, int32) traceBufPtr {
:= trace.lockOwner
:= == nil || != getg().m.curg
if {
lock(&trace.lock)
}
if != 0 {
traceFullQueue()
}
if trace.empty != 0 {
= trace.empty
trace.empty = .ptr().link
} else {
= traceBufPtr(sysAlloc(unsafe.Sizeof(traceBuf{}), &memstats.other_sys))
if == 0 {
throw("trace: out of memory")
}
}
:= .ptr()
.link.set(nil)
.pos = 0
:= uint64(cputicks()) / traceTickDiv
.lastTicks =
.byte(traceEvBatch | 1<<traceArgCountShift)
.varint(uint64())
.varint()
if {
unlock(&trace.lock)
}
return
}
func ( *traceBufPtr, int32, string) (uint64, *traceBufPtr) {
if == "" {
return 0,
}
lock(&trace.stringsLock)
raceacquire(unsafe.Pointer(&trace.stringsLock))
}
if , := trace.strings[]; {
if raceenabled {
racerelease(unsafe.Pointer(&trace.stringsLock))
}
unlock(&trace.stringsLock)
return ,
}
trace.stringSeq++
:= trace.stringSeq
trace.strings[] =
if raceenabled {
racerelease(unsafe.Pointer(&trace.stringsLock))
}
unlock(&trace.stringsLock)
:= .ptr()
:= 1 + 2*traceBytesPerNumber + len()
if == nil || len(.arr)-.pos < {
= traceFlush(traceBufPtrOf(), ).ptr()
.set()
}
.byte(traceEvString)
.varint()
type traceStackTable struct {
lock mutex
seq uint32
mem traceAlloc
tab [1 << 13]traceStackPtr
}
type traceStack struct {
link traceStackPtr
hash uintptr
id uint32
n int
stk [0]uintptr // real type [n]uintptr
}
type traceStackPtr uintptr
func ( traceStackPtr) () *traceStack { return (*traceStack)(unsafe.Pointer()) }
func ( *traceStack) () []uintptr {
return (*[traceStackSize]uintptr)(unsafe.Pointer(&.stk))[:.n]
}
func ( *traceStackTable) ( []uintptr) uint32 {
if len() == 0 {
return 0
}
if := .find(, ); != 0 {
return
func ( *traceStackTable) ( int) *traceStack {
return (*traceStack)(.mem.alloc(unsafe.Sizeof(traceStack{}) + uintptr()*sys.PtrSize))
}
func ( *traceStackTable) () {
var [(2 + 4*traceStackSize) * traceBytesPerNumber]byte
:= traceFlush(0, 0)
for , := range .tab {
:= .ptr()
for ; != nil; = .link.ptr() {
:= [:0]
= traceAppend(, uint64(.id))
:= allFrames(.stack())
= traceAppend(, uint64(len()))
for , := range {
var traceFrame
, = traceFrameForPC(, 0, )
= traceAppend(, uint64(.PC))
= traceAppend(, uint64(.funcID))
= traceAppend(, uint64(.fileID))
= traceAppend(, uint64(.line))
:= 1 + traceBytesPerNumber + len()
if := .ptr(); len(.arr)-.pos < {
= traceFlush(, 0)
}
:= .ptr()
.byte(traceEvStack | 3<<traceArgCountShift)
.varint(uint64(len()))
.pos += copy(.arr[.pos:], )
}
}
lock(&trace.lock)
traceFullQueue()
unlock(&trace.lock)
.mem.drop()
* = traceStackTable{}
lockInit(&((*).lock), lockRankTraceStackTab)
}
type traceFrame struct {
funcID uint64
fileID uint64
line uint64
}
func ( traceBufPtr, int32, Frame) (traceFrame, traceBufPtr) {
:= &
var traceFrame
:= .Function
const = 1 << 10
if len() > {
= [len()-:]
}
.funcID, = traceString(, , )
.line = uint64(.Line)
:= .File
if len() > {
= [len()-:]
}
.fileID, = traceString(, , )
return , (*)
}
type traceAlloc struct {
head traceAllocBlockPtr
off uintptr
}
type traceAllocBlock struct {
next traceAllocBlockPtr
data [64<<10 - sys.PtrSize]byte
}
type traceAllocBlockPtr uintptr
func ( traceAllocBlockPtr) () *traceAllocBlock { return (*traceAllocBlock)(unsafe.Pointer()) }
func ( *traceAllocBlockPtr) ( *traceAllocBlock) { * = traceAllocBlockPtr(unsafe.Pointer()) }
func ( *traceAlloc) ( uintptr) unsafe.Pointer {
= alignUp(, sys.PtrSize)
if .head == 0 || .off+ > uintptr(len(.head.ptr().data)) {
if > uintptr(len(.head.ptr().data)) {
throw("trace: alloc too large")
}
:= (*traceAllocBlock)(sysAlloc(unsafe.Sizeof(traceAllocBlock{}), &memstats.other_sys))
if == nil {
throw("trace: out of memory")
}
.next.set(.head.ptr())
.head.set()
.off = 0
}
:= &.head.ptr().data[.off]
.off +=
return unsafe.Pointer()
}
func ( int32) {
traceEvent(traceEvGomaxprocs, 1, uint64())
}
func () {
traceEvent(traceEvProcStart, -1, uint64(getg().m.id))
}
:= acquirem()
:= .p
.p.set()
traceEvent(traceEvProcStop, -1)
.p =
releasem()
}
func () {
traceEvent(traceEvGCStart, 3, trace.seqGC)
trace.seqGC++
}
func () {
traceEvent(traceEvGCDone, -1)
}
func ( int) {
traceEvent(traceEvGCSTWStart, -1, uint64())
}
func () {
traceEvent(traceEvGCSTWDone, -1)
}
:= getg().m.p.ptr()
if .traceSweep {
throw("double traceGCSweepStart")
}
.traceSweep, .traceSwept, .traceReclaimed = true, 0, 0
}
func ( uintptr) {
:= getg().m.p.ptr()
if .traceSweep {
if .traceSwept == 0 {
traceEvent(traceEvGCSweepStart, 1)
}
.traceSwept +=
}
}
func () {
:= getg().m.p.ptr()
if !.traceSweep {
throw("missing traceGCSweepStart")
}
if .traceSwept != 0 {
traceEvent(traceEvGCSweepDone, -1, uint64(.traceSwept), uint64(.traceReclaimed))
}
.traceSweep = false
}
func () {
traceEvent(traceEvGCMarkAssistStart, 1)
}
func () {
traceEvent(traceEvGCMarkAssistDone, -1)
}
func ( *g, uintptr) {
.traceseq = 0
:= trace.stackTab.put([]uintptr{ + sys.PCQuantum})
traceEvent(traceEvGoCreate, 2, uint64(.goid), uint64())
}
func () {
:= getg().m.curg
:= .m.p
.traceseq++
if .ptr().gcMarkWorkerMode != gcMarkWorkerNotWorker {
traceEvent(traceEvGoStartLabel, -1, uint64(.goid), .traceseq, trace.markWorkerLabels[.ptr().gcMarkWorkerMode])
} else if .tracelastp == {
traceEvent(traceEvGoStartLocal, -1, uint64(.goid))
} else {
.tracelastp =
traceEvent(traceEvGoStart, -1, uint64(.goid), .traceseq)
}
}
func () {
traceEvent(traceEvGoEnd, -1)
}
func () {
:= getg()
.tracelastp = .m.p
traceEvent(traceEvGoSched, 1)
}
func () {
:= getg()
.tracelastp = .m.p
traceEvent(traceEvGoPreempt, 1)
}
func ( byte, int) {
if &traceFutileWakeup != 0 {
traceEvent(traceEvFutileWakeup, -1)
}
traceEvent( & ^traceFutileWakeup, )
}
func ( *g, int) {
:= getg().m.p
.traceseq++
if .tracelastp == {
traceEvent(traceEvGoUnblockLocal, , uint64(.goid))
} else {
.tracelastp =
traceEvent(traceEvGoUnblock, , uint64(.goid), .traceseq)
}
}
func () {
traceEvent(traceEvGoSysCall, 1)
}
func ( int64) {
= 0
}
:= getg().m.curg
.traceseq++
.tracelastp = .m.p
traceEvent(traceEvGoSysExit, -1, uint64(.goid), .traceseq, uint64()/traceTickDiv)
}
:= acquirem()
:= .p
.p.set()
traceEvent(traceEvGoSysBlock, -1)
.p =
releasem()
}
func () {
traceEvent(traceEvHeapAlloc, -1, memstats.heap_live)
}
func () {
traceEvent(traceEvNextGC, -1, 0)
} else {
traceEvent(traceEvNextGC, -1, )
}
}
, , := traceAcquireBuffer()
if !trace.enabled && !.startingtrace {
traceReleaseBuffer()
return
}
, := traceString(, , )
traceEventLocked(0, , , , traceEvUserTaskCreate, 3, , , )
traceReleaseBuffer()
}
func ( uint64) {
traceEvent(traceEvUserTaskEnd, 2, )
}
func (, uint64, string) {
if !trace.enabled {
return
}
, , := traceAcquireBuffer()
if !trace.enabled && !.startingtrace {
traceReleaseBuffer()
return
}
, := traceString(, , )
traceEventLocked(0, , , , traceEvUserRegion, 3, , , )
traceReleaseBuffer()
}
func ( uint64, , string) {
if !trace.enabled {
return
}
, , := traceAcquireBuffer()
if !trace.enabled && !.startingtrace {
traceReleaseBuffer()
return
}
, := traceString(, , )
:= traceBytesPerNumber + len() // extraSpace for the value string
:= .ptr()
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |