...

Source file src/runtime/trace.go

Documentation: runtime

     1  // Copyright 2014 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 execution tracer.
     6  // The tracer captures a wide range of execution events like goroutine
     7  // creation/blocking/unblocking, syscall enter/exit/block, GC-related events,
     8  // changes of heap size, processor start/stop, etc and writes them to a buffer
     9  // in a compact form. A precise nanosecond-precision timestamp and a stack
    10  // trace is captured for most events.
    11  // See https://golang.org/s/go15trace for more info.
    12  
    13  package runtime
    14  
    15  import (
    16  	"internal/goarch"
    17  	"runtime/internal/atomic"
    18  	"runtime/internal/sys"
    19  	"unsafe"
    20  )
    21  
    22  // Event types in the trace, args are given in square brackets.
    23  const (
    24  	traceEvNone              = 0  // unused
    25  	traceEvBatch             = 1  // start of per-P batch of events [pid, timestamp]
    26  	traceEvFrequency         = 2  // contains tracer timer frequency [frequency (ticks per second)]
    27  	traceEvStack             = 3  // stack [stack id, number of PCs, array of {PC, func string ID, file string ID, line}]
    28  	traceEvGomaxprocs        = 4  // current value of GOMAXPROCS [timestamp, GOMAXPROCS, stack id]
    29  	traceEvProcStart         = 5  // start of P [timestamp, thread id]
    30  	traceEvProcStop          = 6  // stop of P [timestamp]
    31  	traceEvGCStart           = 7  // GC start [timestamp, seq, stack id]
    32  	traceEvGCDone            = 8  // GC done [timestamp]
    33  	traceEvGCSTWStart        = 9  // GC STW start [timestamp, kind]
    34  	traceEvGCSTWDone         = 10 // GC STW done [timestamp]
    35  	traceEvGCSweepStart      = 11 // GC sweep start [timestamp, stack id]
    36  	traceEvGCSweepDone       = 12 // GC sweep done [timestamp, swept, reclaimed]
    37  	traceEvGoCreate          = 13 // goroutine creation [timestamp, new goroutine id, new stack id, stack id]
    38  	traceEvGoStart           = 14 // goroutine starts running [timestamp, goroutine id, seq]
    39  	traceEvGoEnd             = 15 // goroutine ends [timestamp]
    40  	traceEvGoStop            = 16 // goroutine stops (like in select{}) [timestamp, stack]
    41  	traceEvGoSched           = 17 // goroutine calls Gosched [timestamp, stack]
    42  	traceEvGoPreempt         = 18 // goroutine is preempted [timestamp, stack]
    43  	traceEvGoSleep           = 19 // goroutine calls Sleep [timestamp, stack]
    44  	traceEvGoBlock           = 20 // goroutine blocks [timestamp, stack]
    45  	traceEvGoUnblock         = 21 // goroutine is unblocked [timestamp, goroutine id, seq, stack]
    46  	traceEvGoBlockSend       = 22 // goroutine blocks on chan send [timestamp, stack]
    47  	traceEvGoBlockRecv       = 23 // goroutine blocks on chan recv [timestamp, stack]
    48  	traceEvGoBlockSelect     = 24 // goroutine blocks on select [timestamp, stack]
    49  	traceEvGoBlockSync       = 25 // goroutine blocks on Mutex/RWMutex [timestamp, stack]
    50  	traceEvGoBlockCond       = 26 // goroutine blocks on Cond [timestamp, stack]
    51  	traceEvGoBlockNet        = 27 // goroutine blocks on network [timestamp, stack]
    52  	traceEvGoSysCall         = 28 // syscall enter [timestamp, stack]
    53  	traceEvGoSysExit         = 29 // syscall exit [timestamp, goroutine id, seq, real timestamp]
    54  	traceEvGoSysBlock        = 30 // syscall blocks [timestamp]
    55  	traceEvGoWaiting         = 31 // denotes that goroutine is blocked when tracing starts [timestamp, goroutine id]
    56  	traceEvGoInSyscall       = 32 // denotes that goroutine is in syscall when tracing starts [timestamp, goroutine id]
    57  	traceEvHeapAlloc         = 33 // gcController.heapLive change [timestamp, heap_alloc]
    58  	traceEvHeapGoal          = 34 // gcController.heapGoal() (formerly next_gc) change [timestamp, heap goal in bytes]
    59  	traceEvTimerGoroutine    = 35 // not currently used; previously denoted timer goroutine [timer goroutine id]
    60  	traceEvFutileWakeup      = 36 // denotes that the previous wakeup of this goroutine was futile [timestamp]
    61  	traceEvString            = 37 // string dictionary entry [ID, length, string]
    62  	traceEvGoStartLocal      = 38 // goroutine starts running on the same P as the last event [timestamp, goroutine id]
    63  	traceEvGoUnblockLocal    = 39 // goroutine is unblocked on the same P as the last event [timestamp, goroutine id, stack]
    64  	traceEvGoSysExitLocal    = 40 // syscall exit on the same P as the last event [timestamp, goroutine id, real timestamp]
    65  	traceEvGoStartLabel      = 41 // goroutine starts running with label [timestamp, goroutine id, seq, label string id]
    66  	traceEvGoBlockGC         = 42 // goroutine blocks on GC assist [timestamp, stack]
    67  	traceEvGCMarkAssistStart = 43 // GC mark assist start [timestamp, stack]
    68  	traceEvGCMarkAssistDone  = 44 // GC mark assist done [timestamp]
    69  	traceEvUserTaskCreate    = 45 // trace.NewContext [timestamp, internal task id, internal parent task id, stack, name string]
    70  	traceEvUserTaskEnd       = 46 // end of a task [timestamp, internal task id, stack]
    71  	traceEvUserRegion        = 47 // trace.WithRegion [timestamp, internal task id, mode(0:start, 1:end), stack, name string]
    72  	traceEvUserLog           = 48 // trace.Log [timestamp, internal task id, key string id, stack, value string]
    73  	traceEvCPUSample         = 49 // CPU profiling sample [timestamp, stack, real timestamp, real P id (-1 when absent), goroutine id]
    74  	traceEvCount             = 50
    75  	// Byte is used but only 6 bits are available for event type.
    76  	// The remaining 2 bits are used to specify the number of arguments.
    77  	// That means, the max event type value is 63.
    78  )
    79  
    80  const (
    81  	// Timestamps in trace are cputicks/traceTickDiv.
    82  	// This makes absolute values of timestamp diffs smaller,
    83  	// and so they are encoded in less number of bytes.
    84  	// 64 on x86 is somewhat arbitrary (one tick is ~20ns on a 3GHz machine).
    85  	// The suggested increment frequency for PowerPC's time base register is
    86  	// 512 MHz according to Power ISA v2.07 section 6.2, so we use 16 on ppc64
    87  	// and ppc64le.
    88  	// Tracing won't work reliably for architectures where cputicks is emulated
    89  	// by nanotime, so the value doesn't matter for those architectures.
    90  	traceTickDiv = 16 + 48*(goarch.Is386|goarch.IsAmd64)
    91  	// Maximum number of PCs in a single stack trace.
    92  	// Since events contain only stack id rather than whole stack trace,
    93  	// we can allow quite large values here.
    94  	traceStackSize = 128
    95  	// Identifier of a fake P that is used when we trace without a real P.
    96  	traceGlobProc = -1
    97  	// Maximum number of bytes to encode uint64 in base-128.
    98  	traceBytesPerNumber = 10
    99  	// Shift of the number of arguments in the first event byte.
   100  	traceArgCountShift = 6
   101  	// Flag passed to traceGoPark to denote that the previous wakeup of this
   102  	// goroutine was futile. For example, a goroutine was unblocked on a mutex,
   103  	// but another goroutine got ahead and acquired the mutex before the first
   104  	// goroutine is scheduled, so the first goroutine has to block again.
   105  	// Such wakeups happen on buffered channels and sync.Mutex,
   106  	// but are generally not interesting for end user.
   107  	traceFutileWakeup byte = 128
   108  )
   109  
   110  // trace is global tracing context.
   111  var trace struct {
   112  	lock          mutex       // protects the following members
   113  	lockOwner     *g          // to avoid deadlocks during recursive lock locks
   114  	enabled       bool        // when set runtime traces events
   115  	shutdown      bool        // set when we are waiting for trace reader to finish after setting enabled to false
   116  	headerWritten bool        // whether ReadTrace has emitted trace header
   117  	footerWritten bool        // whether ReadTrace has emitted trace footer
   118  	shutdownSema  uint32      // used to wait for ReadTrace completion
   119  	seqStart      uint64      // sequence number when tracing was started
   120  	ticksStart    int64       // cputicks when tracing was started
   121  	ticksEnd      int64       // cputicks when tracing was stopped
   122  	timeStart     int64       // nanotime when tracing was started
   123  	timeEnd       int64       // nanotime when tracing was stopped
   124  	seqGC         uint64      // GC start/done sequencer
   125  	reading       traceBufPtr // buffer currently handed off to user
   126  	empty         traceBufPtr // stack of empty buffers
   127  	fullHead      traceBufPtr // queue of full buffers
   128  	fullTail      traceBufPtr
   129  	reader        guintptr        // goroutine that called ReadTrace, or nil
   130  	stackTab      traceStackTable // maps stack traces to unique ids
   131  	// cpuLogRead accepts CPU profile samples from the signal handler where
   132  	// they're generated. It uses a two-word header to hold the IDs of the P and
   133  	// G (respectively) that were active at the time of the sample. Because
   134  	// profBuf uses a record with all zeros in its header to indicate overflow,
   135  	// we make sure to make the P field always non-zero: The ID of a real P will
   136  	// start at bit 1, and bit 0 will be set. Samples that arrive while no P is
   137  	// running (such as near syscalls) will set the first header field to 0b10.
   138  	// This careful handling of the first header field allows us to store ID of
   139  	// the active G directly in the second field, even though that will be 0
   140  	// when sampling g0.
   141  	cpuLogRead *profBuf
   142  	// cpuLogBuf is a trace buffer to hold events corresponding to CPU profile
   143  	// samples, which arrive out of band and not directly connected to a
   144  	// specific P.
   145  	cpuLogBuf traceBufPtr
   146  
   147  	signalLock  atomic.Uint32 // protects use of the following member, only usable in signal handlers
   148  	cpuLogWrite *profBuf      // copy of cpuLogRead for use in signal handlers, set without signalLock
   149  
   150  	// Dictionary for traceEvString.
   151  	//
   152  	// TODO: central lock to access the map is not ideal.
   153  	//   option: pre-assign ids to all user annotation region names and tags
   154  	//   option: per-P cache
   155  	//   option: sync.Map like data structure
   156  	stringsLock mutex
   157  	strings     map[string]uint64
   158  	stringSeq   uint64
   159  
   160  	// markWorkerLabels maps gcMarkWorkerMode to string ID.
   161  	markWorkerLabels [len(gcMarkWorkerModeStrings)]uint64
   162  
   163  	bufLock mutex       // protects buf
   164  	buf     traceBufPtr // global trace buffer, used when running without a p
   165  }
   166  
   167  // traceBufHeader is per-P tracing buffer.
   168  type traceBufHeader struct {
   169  	link      traceBufPtr             // in trace.empty/full
   170  	lastTicks uint64                  // when we wrote the last event
   171  	pos       int                     // next write offset in arr
   172  	stk       [traceStackSize]uintptr // scratch buffer for traceback
   173  }
   174  
   175  // traceBuf is per-P tracing buffer.
   176  //
   177  //go:notinheap
   178  type traceBuf struct {
   179  	traceBufHeader
   180  	arr [64<<10 - unsafe.Sizeof(traceBufHeader{})]byte // underlying buffer for traceBufHeader.buf
   181  }
   182  
   183  // traceBufPtr is a *traceBuf that is not traced by the garbage
   184  // collector and doesn't have write barriers. traceBufs are not
   185  // allocated from the GC'd heap, so this is safe, and are often
   186  // manipulated in contexts where write barriers are not allowed, so
   187  // this is necessary.
   188  //
   189  // TODO: Since traceBuf is now go:notinheap, this isn't necessary.
   190  type traceBufPtr uintptr
   191  
   192  func (tp traceBufPtr) ptr() *traceBuf   { return (*traceBuf)(unsafe.Pointer(tp)) }
   193  func (tp *traceBufPtr) set(b *traceBuf) { *tp = traceBufPtr(unsafe.Pointer(b)) }
   194  func traceBufPtrOf(b *traceBuf) traceBufPtr {
   195  	return traceBufPtr(unsafe.Pointer(b))
   196  }
   197  
   198  // StartTrace enables tracing for the current process.
   199  // While tracing, the data will be buffered and available via ReadTrace.
   200  // StartTrace returns an error if tracing is already enabled.
   201  // Most clients should use the runtime/trace package or the testing package's
   202  // -test.trace flag instead of calling StartTrace directly.
   203  func StartTrace() error {
   204  	// Stop the world so that we can take a consistent snapshot
   205  	// of all goroutines at the beginning of the trace.
   206  	// Do not stop the world during GC so we ensure we always see
   207  	// a consistent view of GC-related events (e.g. a start is always
   208  	// paired with an end).
   209  	stopTheWorldGC("start tracing")
   210  
   211  	// Prevent sysmon from running any code that could generate events.
   212  	lock(&sched.sysmonlock)
   213  
   214  	// We are in stop-the-world, but syscalls can finish and write to trace concurrently.
   215  	// Exitsyscall could check trace.enabled long before and then suddenly wake up
   216  	// and decide to write to trace at a random point in time.
   217  	// However, such syscall will use the global trace.buf buffer, because we've
   218  	// acquired all p's by doing stop-the-world. So this protects us from such races.
   219  	lock(&trace.bufLock)
   220  
   221  	if trace.enabled || trace.shutdown {
   222  		unlock(&trace.bufLock)
   223  		unlock(&sched.sysmonlock)
   224  		startTheWorldGC()
   225  		return errorString("tracing is already enabled")
   226  	}
   227  
   228  	// Can't set trace.enabled yet. While the world is stopped, exitsyscall could
   229  	// already emit a delayed event (see exitTicks in exitsyscall) if we set trace.enabled here.
   230  	// That would lead to an inconsistent trace:
   231  	// - either GoSysExit appears before EvGoInSyscall,
   232  	// - or GoSysExit appears for a goroutine for which we don't emit EvGoInSyscall below.
   233  	// To instruct traceEvent that it must not ignore events below, we set startingtrace.
   234  	// trace.enabled is set afterwards once we have emitted all preliminary events.
   235  	_g_ := getg()
   236  	_g_.m.startingtrace = true
   237  
   238  	// Obtain current stack ID to use in all traceEvGoCreate events below.
   239  	mp := acquirem()
   240  	stkBuf := make([]uintptr, traceStackSize)
   241  	stackID := traceStackID(mp, stkBuf, 2)
   242  	releasem(mp)
   243  
   244  	profBuf := newProfBuf(2, profBufWordCount, profBufTagCount) // after the timestamp, header is [pp.id, gp.goid]
   245  	trace.cpuLogRead = profBuf
   246  
   247  	// We must not acquire trace.signalLock outside of a signal handler: a
   248  	// profiling signal may arrive at any time and try to acquire it, leading to
   249  	// deadlock. Because we can't use that lock to protect updates to
   250  	// trace.cpuLogWrite (only use of the structure it references), reads and
   251  	// writes of the pointer must be atomic. (And although this field is never
   252  	// the sole pointer to the profBuf value, it's best to allow a write barrier
   253  	// here.)
   254  	atomicstorep(unsafe.Pointer(&trace.cpuLogWrite), unsafe.Pointer(profBuf))
   255  
   256  	// World is stopped, no need to lock.
   257  	forEachGRace(func(gp *g) {
   258  		status := readgstatus(gp)
   259  		if status != _Gdead {
   260  			gp.traceseq = 0
   261  			gp.tracelastp = getg().m.p
   262  			// +PCQuantum because traceFrameForPC expects return PCs and subtracts PCQuantum.
   263  			id := trace.stackTab.put([]uintptr{startPCforTrace(gp.startpc) + sys.PCQuantum})
   264  			traceEvent(traceEvGoCreate, -1, uint64(gp.goid), uint64(id), stackID)
   265  		}
   266  		if status == _Gwaiting {
   267  			// traceEvGoWaiting is implied to have seq=1.
   268  			gp.traceseq++
   269  			traceEvent(traceEvGoWaiting, -1, uint64(gp.goid))
   270  		}
   271  		if status == _Gsyscall {
   272  			gp.traceseq++
   273  			traceEvent(traceEvGoInSyscall, -1, uint64(gp.goid))
   274  		} else {
   275  			gp.sysblocktraced = false
   276  		}
   277  	})
   278  	traceProcStart()
   279  	traceGoStart()
   280  	// Note: ticksStart needs to be set after we emit traceEvGoInSyscall events.
   281  	// If we do it the other way around, it is possible that exitsyscall will
   282  	// query sysexitticks after ticksStart but before traceEvGoInSyscall timestamp.
   283  	// It will lead to a false conclusion that cputicks is broken.
   284  	trace.ticksStart = cputicks()
   285  	trace.timeStart = nanotime()
   286  	trace.headerWritten = false
   287  	trace.footerWritten = false
   288  
   289  	// string to id mapping
   290  	//  0 : reserved for an empty string
   291  	//  remaining: other strings registered by traceString
   292  	trace.stringSeq = 0
   293  	trace.strings = make(map[string]uint64)
   294  
   295  	trace.seqGC = 0
   296  	_g_.m.startingtrace = false
   297  	trace.enabled = true
   298  
   299  	// Register runtime goroutine labels.
   300  	_, pid, bufp := traceAcquireBuffer()
   301  	for i, label := range gcMarkWorkerModeStrings[:] {
   302  		trace.markWorkerLabels[i], bufp = traceString(bufp, pid, label)
   303  	}
   304  	traceReleaseBuffer(pid)
   305  
   306  	unlock(&trace.bufLock)
   307  
   308  	unlock(&sched.sysmonlock)
   309  
   310  	startTheWorldGC()
   311  	return nil
   312  }
   313  
   314  // StopTrace stops tracing, if it was previously enabled.
   315  // StopTrace only returns after all the reads for the trace have completed.
   316  func StopTrace() {
   317  	// Stop the world so that we can collect the trace buffers from all p's below,
   318  	// and also to avoid races with traceEvent.
   319  	stopTheWorldGC("stop tracing")
   320  
   321  	// See the comment in StartTrace.
   322  	lock(&sched.sysmonlock)
   323  
   324  	// See the comment in StartTrace.
   325  	lock(&trace.bufLock)
   326  
   327  	if !trace.enabled {
   328  		unlock(&trace.bufLock)
   329  		unlock(&sched.sysmonlock)
   330  		startTheWorldGC()
   331  		return
   332  	}
   333  
   334  	traceGoSched()
   335  
   336  	atomicstorep(unsafe.Pointer(&trace.cpuLogWrite), nil)
   337  	trace.cpuLogRead.close()
   338  	traceReadCPU()
   339  
   340  	// Loop over all allocated Ps because dead Ps may still have
   341  	// trace buffers.
   342  	for _, p := range allp[:cap(allp)] {
   343  		buf := p.tracebuf
   344  		if buf != 0 {
   345  			traceFullQueue(buf)
   346  			p.tracebuf = 0
   347  		}
   348  	}
   349  	if trace.buf != 0 {
   350  		buf := trace.buf
   351  		trace.buf = 0
   352  		if buf.ptr().pos != 0 {
   353  			traceFullQueue(buf)
   354  		}
   355  	}
   356  	if trace.cpuLogBuf != 0 {
   357  		buf := trace.cpuLogBuf
   358  		trace.cpuLogBuf = 0
   359  		if buf.ptr().pos != 0 {
   360  			traceFullQueue(buf)
   361  		}
   362  	}
   363  
   364  	for {
   365  		trace.ticksEnd = cputicks()
   366  		trace.timeEnd = nanotime()
   367  		// Windows time can tick only every 15ms, wait for at least one tick.
   368  		if trace.timeEnd != trace.timeStart {
   369  			break
   370  		}
   371  		osyield()
   372  	}
   373  
   374  	trace.enabled = false
   375  	trace.shutdown = true
   376  	unlock(&trace.bufLock)
   377  
   378  	unlock(&sched.sysmonlock)
   379  
   380  	startTheWorldGC()
   381  
   382  	// The world is started but we've set trace.shutdown, so new tracing can't start.
   383  	// Wait for the trace reader to flush pending buffers and stop.
   384  	semacquire(&trace.shutdownSema)
   385  	if raceenabled {
   386  		raceacquire(unsafe.Pointer(&trace.shutdownSema))
   387  	}
   388  
   389  	// The lock protects us from races with StartTrace/StopTrace because they do stop-the-world.
   390  	lock(&trace.lock)
   391  	for _, p := range allp[:cap(allp)] {
   392  		if p.tracebuf != 0 {
   393  			throw("trace: non-empty trace buffer in proc")
   394  		}
   395  	}
   396  	if trace.buf != 0 {
   397  		throw("trace: non-empty global trace buffer")
   398  	}
   399  	if trace.fullHead != 0 || trace.fullTail != 0 {
   400  		throw("trace: non-empty full trace buffer")
   401  	}
   402  	if trace.reading != 0 || trace.reader != 0 {
   403  		throw("trace: reading after shutdown")
   404  	}
   405  	for trace.empty != 0 {
   406  		buf := trace.empty
   407  		trace.empty = buf.ptr().link
   408  		sysFree(unsafe.Pointer(buf), unsafe.Sizeof(*buf.ptr()), &memstats.other_sys)
   409  	}
   410  	trace.strings = nil
   411  	trace.shutdown = false
   412  	trace.cpuLogRead = nil
   413  	unlock(&trace.lock)
   414  }
   415  
   416  // ReadTrace returns the next chunk of binary tracing data, blocking until data
   417  // is available. If tracing is turned off and all the data accumulated while it
   418  // was on has been returned, ReadTrace returns nil. The caller must copy the
   419  // returned data before calling ReadTrace again.
   420  // ReadTrace must be called from one goroutine at a time.
   421  func ReadTrace() []byte {
   422  	// This function may need to lock trace.lock recursively
   423  	// (goparkunlock -> traceGoPark -> traceEvent -> traceFlush).
   424  	// To allow this we use trace.lockOwner.
   425  	// Also this function must not allocate while holding trace.lock:
   426  	// allocation can call heap allocate, which will try to emit a trace
   427  	// event while holding heap lock.
   428  	lock(&trace.lock)
   429  	trace.lockOwner = getg()
   430  
   431  	if trace.reader != 0 {
   432  		// More than one goroutine reads trace. This is bad.
   433  		// But we rather do not crash the program because of tracing,
   434  		// because tracing can be enabled at runtime on prod servers.
   435  		trace.lockOwner = nil
   436  		unlock(&trace.lock)
   437  		println("runtime: ReadTrace called from multiple goroutines simultaneously")
   438  		return nil
   439  	}
   440  	// Recycle the old buffer.
   441  	if buf := trace.reading; buf != 0 {
   442  		buf.ptr().link = trace.empty
   443  		trace.empty = buf
   444  		trace.reading = 0
   445  	}
   446  	// Write trace header.
   447  	if !trace.headerWritten {
   448  		trace.headerWritten = true
   449  		trace.lockOwner = nil
   450  		unlock(&trace.lock)
   451  		return []byte("go 1.19 trace\x00\x00\x00")
   452  	}
   453  	// Optimistically look for CPU profile samples. This may write new stack
   454  	// records, and may write new tracing buffers.
   455  	if !trace.footerWritten && !trace.shutdown {
   456  		traceReadCPU()
   457  	}
   458  	// Wait for new data.
   459  	if trace.fullHead == 0 && !trace.shutdown {
   460  		trace.reader.set(getg())
   461  		goparkunlock(&trace.lock, waitReasonTraceReaderBlocked, traceEvGoBlock, 2)
   462  		lock(&trace.lock)
   463  	}
   464  	// Write a buffer.
   465  	if trace.fullHead != 0 {
   466  		buf := traceFullDequeue()
   467  		trace.reading = buf
   468  		trace.lockOwner = nil
   469  		unlock(&trace.lock)
   470  		return buf.ptr().arr[:buf.ptr().pos]
   471  	}
   472  
   473  	// Write footer with timer frequency.
   474  	if !trace.footerWritten {
   475  		trace.footerWritten = true
   476  		// Use float64 because (trace.ticksEnd - trace.ticksStart) * 1e9 can overflow int64.
   477  		freq := float64(trace.ticksEnd-trace.ticksStart) * 1e9 / float64(trace.timeEnd-trace.timeStart) / traceTickDiv
   478  		if freq <= 0 {
   479  			throw("trace: ReadTrace got invalid frequency")
   480  		}
   481  		trace.lockOwner = nil
   482  		unlock(&trace.lock)
   483  		var data []byte
   484  		data = append(data, traceEvFrequency|0<<traceArgCountShift)
   485  		data = traceAppend(data, uint64(freq))
   486  		// This will emit a bunch of full buffers, we will pick them up
   487  		// on the next iteration.
   488  		trace.stackTab.dump()
   489  		return data
   490  	}
   491  	// Done.
   492  	if trace.shutdown {
   493  		trace.lockOwner = nil
   494  		unlock(&trace.lock)
   495  		if raceenabled {
   496  			// Model synchronization on trace.shutdownSema, which race
   497  			// detector does not see. This is required to avoid false
   498  			// race reports on writer passed to trace.Start.
   499  			racerelease(unsafe.Pointer(&trace.shutdownSema))
   500  		}
   501  		// trace.enabled is already reset, so can call traceable functions.
   502  		semrelease(&trace.shutdownSema)
   503  		return nil
   504  	}
   505  	// Also bad, but see the comment above.
   506  	trace.lockOwner = nil
   507  	unlock(&trace.lock)
   508  	println("runtime: spurious wakeup of trace reader")
   509  	return nil
   510  }
   511  
   512  // traceReader returns the trace reader that should be woken up, if any.
   513  // Callers should first check that trace.enabled or trace.shutdown is set.
   514  func traceReader() *g {
   515  	if !traceReaderAvailable() {
   516  		return nil
   517  	}
   518  	lock(&trace.lock)
   519  	if !traceReaderAvailable() {
   520  		unlock(&trace.lock)
   521  		return nil
   522  	}
   523  	gp := trace.reader.ptr()
   524  	trace.reader.set(nil)
   525  	unlock(&trace.lock)
   526  	return gp
   527  }
   528  
   529  // traceReaderAvailable returns true if the trace reader is not currently
   530  // scheduled and should be. Callers should first check that trace.enabled
   531  // or trace.shutdown is set.
   532  func traceReaderAvailable() bool {
   533  	return trace.reader != 0 && (trace.fullHead != 0 || trace.shutdown)
   534  }
   535  
   536  // traceProcFree frees trace buffer associated with pp.
   537  func traceProcFree(pp *p) {
   538  	buf := pp.tracebuf
   539  	pp.tracebuf = 0
   540  	if buf == 0 {
   541  		return
   542  	}
   543  	lock(&trace.lock)
   544  	traceFullQueue(buf)
   545  	unlock(&trace.lock)
   546  }
   547  
   548  // traceFullQueue queues buf into queue of full buffers.
   549  func traceFullQueue(buf traceBufPtr) {
   550  	buf.ptr().link = 0
   551  	if trace.fullHead == 0 {
   552  		trace.fullHead = buf
   553  	} else {
   554  		trace.fullTail.ptr().link = buf
   555  	}
   556  	trace.fullTail = buf
   557  }
   558  
   559  // traceFullDequeue dequeues from queue of full buffers.
   560  func traceFullDequeue() traceBufPtr {
   561  	buf := trace.fullHead
   562  	if buf == 0 {
   563  		return 0
   564  	}
   565  	trace.fullHead = buf.ptr().link
   566  	if trace.fullHead == 0 {
   567  		trace.fullTail = 0
   568  	}
   569  	buf.ptr().link = 0
   570  	return buf
   571  }
   572  
   573  // traceEvent writes a single event to trace buffer, flushing the buffer if necessary.
   574  // ev is event type.
   575  // If skip > 0, write current stack id as the last argument (skipping skip top frames).
   576  // If skip = 0, this event type should contain a stack, but we don't want
   577  // to collect and remember it for this particular call.
   578  func traceEvent(ev byte, skip int, args ...uint64) {
   579  	mp, pid, bufp := traceAcquireBuffer()
   580  	// Double-check trace.enabled now that we've done m.locks++ and acquired bufLock.
   581  	// This protects from races between traceEvent and StartTrace/StopTrace.
   582  
   583  	// The caller checked that trace.enabled == true, but trace.enabled might have been
   584  	// turned off between the check and now. Check again. traceLockBuffer did mp.locks++,
   585  	// StopTrace does stopTheWorld, and stopTheWorld waits for mp.locks to go back to zero,
   586  	// so if we see trace.enabled == true now, we know it's true for the rest of the function.
   587  	// Exitsyscall can run even during stopTheWorld. The race with StartTrace/StopTrace
   588  	// during tracing in exitsyscall is resolved by locking trace.bufLock in traceLockBuffer.
   589  	//
   590  	// Note trace_userTaskCreate runs the same check.
   591  	if !trace.enabled && !mp.startingtrace {
   592  		traceReleaseBuffer(pid)
   593  		return
   594  	}
   595  
   596  	if skip > 0 {
   597  		if getg() == mp.curg {
   598  			skip++ // +1 because stack is captured in traceEventLocked.
   599  		}
   600  	}
   601  	traceEventLocked(0, mp, pid, bufp, ev, 0, skip, args...)
   602  	traceReleaseBuffer(pid)
   603  }
   604  
   605  // traceEventLocked writes a single event of type ev to the trace buffer bufp,
   606  // flushing the buffer if necessary. pid is the id of the current P, or
   607  // traceGlobProc if we're tracing without a real P.
   608  //
   609  // Preemption is disabled, and if running without a real P the global tracing
   610  // buffer is locked.
   611  //
   612  // Events types that do not include a stack set skip to -1. Event types that
   613  // include a stack may explicitly reference a stackID from the trace.stackTab
   614  // (obtained by an earlier call to traceStackID). Without an explicit stackID,
   615  // this function will automatically capture the stack of the goroutine currently
   616  // running on mp, skipping skip top frames or, if skip is 0, writing out an
   617  // empty stack record.
   618  //
   619  // It records the event's args to the traceBuf, and also makes an effort to
   620  // reserve extraBytes bytes of additional space immediately following the event,
   621  // in the same traceBuf.
   622  func traceEventLocked(extraBytes int, mp *m, pid int32, bufp *traceBufPtr, ev byte, stackID uint32, skip int, args ...uint64) {
   623  	buf := bufp.ptr()
   624  	// TODO: test on non-zero extraBytes param.
   625  	maxSize := 2 + 5*traceBytesPerNumber + extraBytes // event type, length, sequence, timestamp, stack id and two add params
   626  	if buf == nil || len(buf.arr)-buf.pos < maxSize {
   627  		buf = traceFlush(traceBufPtrOf(buf), pid).ptr()
   628  		bufp.set(buf)
   629  	}
   630  
   631  	// NOTE: ticks might be same after tick division, although the real cputicks is
   632  	// linear growth.
   633  	ticks := uint64(cputicks()) / traceTickDiv
   634  	tickDiff := ticks - buf.lastTicks
   635  	if tickDiff == 0 {
   636  		ticks = buf.lastTicks + 1
   637  		tickDiff = 1
   638  	}
   639  
   640  	buf.lastTicks = ticks
   641  	narg := byte(len(args))
   642  	if stackID != 0 || skip >= 0 {
   643  		narg++
   644  	}
   645  	// We have only 2 bits for number of arguments.
   646  	// If number is >= 3, then the event type is followed by event length in bytes.
   647  	if narg > 3 {
   648  		narg = 3
   649  	}
   650  	startPos := buf.pos
   651  	buf.byte(ev | narg<<traceArgCountShift)
   652  	var lenp *byte
   653  	if narg == 3 {
   654  		// Reserve the byte for length assuming that length < 128.
   655  		buf.varint(0)
   656  		lenp = &buf.arr[buf.pos-1]
   657  	}
   658  	buf.varint(tickDiff)
   659  	for _, a := range args {
   660  		buf.varint(a)
   661  	}
   662  	if stackID != 0 {
   663  		buf.varint(uint64(stackID))
   664  	} else if skip == 0 {
   665  		buf.varint(0)
   666  	} else if skip > 0 {
   667  		buf.varint(traceStackID(mp, buf.stk[:], skip))
   668  	}
   669  	evSize := buf.pos - startPos
   670  	if evSize > maxSize {
   671  		throw("invalid length of trace event")
   672  	}
   673  	if lenp != nil {
   674  		// Fill in actual length.
   675  		*lenp = byte(evSize - 2)
   676  	}
   677  }
   678  
   679  // traceCPUSample writes a CPU profile sample stack to the execution tracer's
   680  // profiling buffer. It is called from a signal handler, so is limited in what
   681  // it can do.
   682  func traceCPUSample(gp *g, pp *p, stk []uintptr) {
   683  	if !trace.enabled {
   684  		// Tracing is usually turned off; don't spend time acquiring the signal
   685  		// lock unless it's active.
   686  		return
   687  	}
   688  
   689  	// Match the clock used in traceEventLocked
   690  	now := cputicks()
   691  	// The "header" here is the ID of the P that was running the profiled code,
   692  	// followed by the ID of the goroutine. (For normal CPU profiling, it's
   693  	// usually the number of samples with the given stack.) Near syscalls, pp
   694  	// may be nil. Reporting goid of 0 is fine for either g0 or a nil gp.
   695  	var hdr [2]uint64
   696  	if pp != nil {
   697  		// Overflow records in profBuf have all header values set to zero. Make
   698  		// sure that real headers have at least one bit set.
   699  		hdr[0] = uint64(pp.id)<<1 | 0b1
   700  	} else {
   701  		hdr[0] = 0b10
   702  	}
   703  	if gp != nil {
   704  		hdr[1] = uint64(gp.goid)
   705  	}
   706  
   707  	// Allow only one writer at a time
   708  	for !trace.signalLock.CompareAndSwap(0, 1) {
   709  		// TODO: Is it safe to osyield here? https://go.dev/issue/52672
   710  		osyield()
   711  	}
   712  
   713  	if log := (*profBuf)(atomic.Loadp(unsafe.Pointer(&trace.cpuLogWrite))); log != nil {
   714  		// Note: we don't pass a tag pointer here (how should profiling tags
   715  		// interact with the execution tracer?), but if we did we'd need to be
   716  		// careful about write barriers. See the long comment in profBuf.write.
   717  		log.write(nil, now, hdr[:], stk)
   718  	}
   719  
   720  	trace.signalLock.Store(0)
   721  }
   722  
   723  func traceReadCPU() {
   724  	bufp := &trace.cpuLogBuf
   725  
   726  	for {
   727  		data, tags, _ := trace.cpuLogRead.read(profBufNonBlocking)
   728  		if len(data) == 0 {
   729  			break
   730  		}
   731  		for len(data) > 0 {
   732  			if len(data) < 4 || data[0] > uint64(len(data)) {
   733  				break // truncated profile
   734  			}
   735  			if data[0] < 4 || tags != nil && len(tags) < 1 {
   736  				break // malformed profile
   737  			}
   738  			if len(tags) < 1 {
   739  				break // mismatched profile records and tags
   740  			}
   741  			timestamp := data[1]
   742  			ppid := data[2] >> 1
   743  			if hasP := (data[2] & 0b1) != 0; !hasP {
   744  				ppid = ^uint64(0)
   745  			}
   746  			goid := data[3]
   747  			stk := data[4:data[0]]
   748  			empty := len(stk) == 1 && data[2] == 0 && data[3] == 0
   749  			data = data[data[0]:]
   750  			// No support here for reporting goroutine tags at the moment; if
   751  			// that information is to be part of the execution trace, we'd
   752  			// probably want to see when the tags are applied and when they
   753  			// change, instead of only seeing them when we get a CPU sample.
   754  			tags = tags[1:]
   755  
   756  			if empty {
   757  				// Looks like an overflow record from the profBuf. Not much to
   758  				// do here, we only want to report full records.
   759  				//
   760  				// TODO: should we start a goroutine to drain the profBuf,
   761  				// rather than relying on a high-enough volume of tracing events
   762  				// to keep ReadTrace busy? https://go.dev/issue/52674
   763  				continue
   764  			}
   765  
   766  			buf := bufp.ptr()
   767  			if buf == nil {
   768  				*bufp = traceFlush(*bufp, 0)
   769  				buf = bufp.ptr()
   770  			}
   771  			for i := range stk {
   772  				if i >= len(buf.stk) {
   773  					break
   774  				}
   775  				buf.stk[i] = uintptr(stk[i])
   776  			}
   777  			stackID := trace.stackTab.put(buf.stk[:len(stk)])
   778  
   779  			traceEventLocked(0, nil, 0, bufp, traceEvCPUSample, stackID, 1, timestamp/traceTickDiv, ppid, goid)
   780  		}
   781  	}
   782  }
   783  
   784  func traceStackID(mp *m, buf []uintptr, skip int) uint64 {
   785  	_g_ := getg()
   786  	gp := mp.curg
   787  	var nstk int
   788  	if gp == _g_ {
   789  		nstk = callers(skip+1, buf)
   790  	} else if gp != nil {
   791  		gp = mp.curg
   792  		nstk = gcallers(gp, skip, buf)
   793  	}
   794  	if nstk > 0 {
   795  		nstk-- // skip runtime.goexit
   796  	}
   797  	if nstk > 0 && gp.goid == 1 {
   798  		nstk-- // skip runtime.main
   799  	}
   800  	id := trace.stackTab.put(buf[:nstk])
   801  	return uint64(id)
   802  }
   803  
   804  // traceAcquireBuffer returns trace buffer to use and, if necessary, locks it.
   805  func traceAcquireBuffer() (mp *m, pid int32, bufp *traceBufPtr) {
   806  	mp = acquirem()
   807  	if p := mp.p.ptr(); p != nil {
   808  		return mp, p.id, &p.tracebuf
   809  	}
   810  	lock(&trace.bufLock)
   811  	return mp, traceGlobProc, &trace.buf
   812  }
   813  
   814  // traceReleaseBuffer releases a buffer previously acquired with traceAcquireBuffer.
   815  func traceReleaseBuffer(pid int32) {
   816  	if pid == traceGlobProc {
   817  		unlock(&trace.bufLock)
   818  	}
   819  	releasem(getg().m)
   820  }
   821  
   822  // traceFlush puts buf onto stack of full buffers and returns an empty buffer.
   823  func traceFlush(buf traceBufPtr, pid int32) traceBufPtr {
   824  	owner := trace.lockOwner
   825  	dolock := owner == nil || owner != getg().m.curg
   826  	if dolock {
   827  		lock(&trace.lock)
   828  	}
   829  	if buf != 0 {
   830  		traceFullQueue(buf)
   831  	}
   832  	if trace.empty != 0 {
   833  		buf = trace.empty
   834  		trace.empty = buf.ptr().link
   835  	} else {
   836  		buf = traceBufPtr(sysAlloc(unsafe.Sizeof(traceBuf{}), &memstats.other_sys))
   837  		if buf == 0 {
   838  			throw("trace: out of memory")
   839  		}
   840  	}
   841  	bufp := buf.ptr()
   842  	bufp.link.set(nil)
   843  	bufp.pos = 0
   844  
   845  	// initialize the buffer for a new batch
   846  	ticks := uint64(cputicks()) / traceTickDiv
   847  	if ticks == bufp.lastTicks {
   848  		ticks = bufp.lastTicks + 1
   849  	}
   850  	bufp.lastTicks = ticks
   851  	bufp.byte(traceEvBatch | 1<<traceArgCountShift)
   852  	bufp.varint(uint64(pid))
   853  	bufp.varint(ticks)
   854  
   855  	if dolock {
   856  		unlock(&trace.lock)
   857  	}
   858  	return buf
   859  }
   860  
   861  // traceString adds a string to the trace.strings and returns the id.
   862  func traceString(bufp *traceBufPtr, pid int32, s string) (uint64, *traceBufPtr) {
   863  	if s == "" {
   864  		return 0, bufp
   865  	}
   866  
   867  	lock(&trace.stringsLock)
   868  	if raceenabled {
   869  		// raceacquire is necessary because the map access
   870  		// below is race annotated.
   871  		raceacquire(unsafe.Pointer(&trace.stringsLock))
   872  	}
   873  
   874  	if id, ok := trace.strings[s]; ok {
   875  		if raceenabled {
   876  			racerelease(unsafe.Pointer(&trace.stringsLock))
   877  		}
   878  		unlock(&trace.stringsLock)
   879  
   880  		return id, bufp
   881  	}
   882  
   883  	trace.stringSeq++
   884  	id := trace.stringSeq
   885  	trace.strings[s] = id
   886  
   887  	if raceenabled {
   888  		racerelease(unsafe.Pointer(&trace.stringsLock))
   889  	}
   890  	unlock(&trace.stringsLock)
   891  
   892  	// memory allocation in above may trigger tracing and
   893  	// cause *bufp changes. Following code now works with *bufp,
   894  	// so there must be no memory allocation or any activities
   895  	// that causes tracing after this point.
   896  
   897  	buf := bufp.ptr()
   898  	size := 1 + 2*traceBytesPerNumber + len(s)
   899  	if buf == nil || len(buf.arr)-buf.pos < size {
   900  		buf = traceFlush(traceBufPtrOf(buf), pid).ptr()
   901  		bufp.set(buf)
   902  	}
   903  	buf.byte(traceEvString)
   904  	buf.varint(id)
   905  
   906  	// double-check the string and the length can fit.
   907  	// Otherwise, truncate the string.
   908  	slen := len(s)
   909  	if room := len(buf.arr) - buf.pos; room < slen+traceBytesPerNumber {
   910  		slen = room
   911  	}
   912  
   913  	buf.varint(uint64(slen))
   914  	buf.pos += copy(buf.arr[buf.pos:], s[:slen])
   915  
   916  	bufp.set(buf)
   917  	return id, bufp
   918  }
   919  
   920  // traceAppend appends v to buf in little-endian-base-128 encoding.
   921  func traceAppend(buf []byte, v uint64) []byte {
   922  	for ; v >= 0x80; v >>= 7 {
   923  		buf = append(buf, 0x80|byte(v))
   924  	}
   925  	buf = append(buf, byte(v))
   926  	return buf
   927  }
   928  
   929  // varint appends v to buf in little-endian-base-128 encoding.
   930  func (buf *traceBuf) varint(v uint64) {
   931  	pos := buf.pos
   932  	for ; v >= 0x80; v >>= 7 {
   933  		buf.arr[pos] = 0x80 | byte(v)
   934  		pos++
   935  	}
   936  	buf.arr[pos] = byte(v)
   937  	pos++
   938  	buf.pos = pos
   939  }
   940  
   941  // byte appends v to buf.
   942  func (buf *traceBuf) byte(v byte) {
   943  	buf.arr[buf.pos] = v
   944  	buf.pos++
   945  }
   946  
   947  // traceStackTable maps stack traces (arrays of PC's) to unique uint32 ids.
   948  // It is lock-free for reading.
   949  type traceStackTable struct {
   950  	lock mutex
   951  	seq  uint32
   952  	mem  traceAlloc
   953  	tab  [1 << 13]traceStackPtr
   954  }
   955  
   956  // traceStack is a single stack in traceStackTable.
   957  type traceStack struct {
   958  	link traceStackPtr
   959  	hash uintptr
   960  	id   uint32
   961  	n    int
   962  	stk  [0]uintptr // real type [n]uintptr
   963  }
   964  
   965  type traceStackPtr uintptr
   966  
   967  func (tp traceStackPtr) ptr() *traceStack { return (*traceStack)(unsafe.Pointer(tp)) }
   968  
   969  // stack returns slice of PCs.
   970  func (ts *traceStack) stack() []uintptr {
   971  	return (*[traceStackSize]uintptr)(unsafe.Pointer(&ts.stk))[:ts.n]
   972  }
   973  
   974  // put returns a unique id for the stack trace pcs and caches it in the table,
   975  // if it sees the trace for the first time.
   976  func (tab *traceStackTable) put(pcs []uintptr) uint32 {
   977  	if len(pcs) == 0 {
   978  		return 0
   979  	}
   980  	hash := memhash(unsafe.Pointer(&pcs[0]), 0, uintptr(len(pcs))*unsafe.Sizeof(pcs[0]))
   981  	// First, search the hashtable w/o the mutex.
   982  	if id := tab.find(pcs, hash); id != 0 {
   983  		return id
   984  	}
   985  	// Now, double check under the mutex.
   986  	lock(&tab.lock)
   987  	if id := tab.find(pcs, hash); id != 0 {
   988  		unlock(&tab.lock)
   989  		return id
   990  	}
   991  	// Create new record.
   992  	tab.seq++
   993  	stk := tab.newStack(len(pcs))
   994  	stk.hash = hash
   995  	stk.id = tab.seq
   996  	stk.n = len(pcs)
   997  	stkpc := stk.stack()
   998  	for i, pc := range pcs {
   999  		stkpc[i] = pc
  1000  	}
  1001  	part := int(hash % uintptr(len(tab.tab)))
  1002  	stk.link = tab.tab[part]
  1003  	atomicstorep(unsafe.Pointer(&tab.tab[part]), unsafe.Pointer(stk))
  1004  	unlock(&tab.lock)
  1005  	return stk.id
  1006  }
  1007  
  1008  // find checks if the stack trace pcs is already present in the table.
  1009  func (tab *traceStackTable) find(pcs []uintptr, hash uintptr) uint32 {
  1010  	part := int(hash % uintptr(len(tab.tab)))
  1011  Search:
  1012  	for stk := tab.tab[part].ptr(); stk != nil; stk = stk.link.ptr() {
  1013  		if stk.hash == hash && stk.n == len(pcs) {
  1014  			for i, stkpc := range stk.stack() {
  1015  				if stkpc != pcs[i] {
  1016  					continue Search
  1017  				}
  1018  			}
  1019  			return stk.id
  1020  		}
  1021  	}
  1022  	return 0
  1023  }
  1024  
  1025  // newStack allocates a new stack of size n.
  1026  func (tab *traceStackTable) newStack(n int) *traceStack {
  1027  	return (*traceStack)(tab.mem.alloc(unsafe.Sizeof(traceStack{}) + uintptr(n)*goarch.PtrSize))
  1028  }
  1029  
  1030  // allFrames returns all of the Frames corresponding to pcs.
  1031  func allFrames(pcs []uintptr) []Frame {
  1032  	frames := make([]Frame, 0, len(pcs))
  1033  	ci := CallersFrames(pcs)
  1034  	for {
  1035  		f, more := ci.Next()
  1036  		frames = append(frames, f)
  1037  		if !more {
  1038  			return frames
  1039  		}
  1040  	}
  1041  }
  1042  
  1043  // dump writes all previously cached stacks to trace buffers,
  1044  // releases all memory and resets state.
  1045  func (tab *traceStackTable) dump() {
  1046  	var tmp [(2 + 4*traceStackSize) * traceBytesPerNumber]byte
  1047  	bufp := traceFlush(0, 0)
  1048  	for _, stk := range tab.tab {
  1049  		stk := stk.ptr()
  1050  		for ; stk != nil; stk = stk.link.ptr() {
  1051  			tmpbuf := tmp[:0]
  1052  			tmpbuf = traceAppend(tmpbuf, uint64(stk.id))
  1053  			frames := allFrames(stk.stack())
  1054  			tmpbuf = traceAppend(tmpbuf, uint64(len(frames)))
  1055  			for _, f := range frames {
  1056  				var frame traceFrame
  1057  				frame, bufp = traceFrameForPC(bufp, 0, f)
  1058  				tmpbuf = traceAppend(tmpbuf, uint64(f.PC))
  1059  				tmpbuf = traceAppend(tmpbuf, uint64(frame.funcID))
  1060  				tmpbuf = traceAppend(tmpbuf, uint64(frame.fileID))
  1061  				tmpbuf = traceAppend(tmpbuf, uint64(frame.line))
  1062  			}
  1063  			// Now copy to the buffer.
  1064  			size := 1 + traceBytesPerNumber + len(tmpbuf)
  1065  			if buf := bufp.ptr(); len(buf.arr)-buf.pos < size {
  1066  				bufp = traceFlush(bufp, 0)
  1067  			}
  1068  			buf := bufp.ptr()
  1069  			buf.byte(traceEvStack | 3<<traceArgCountShift)
  1070  			buf.varint(uint64(len(tmpbuf)))
  1071  			buf.pos += copy(buf.arr[buf.pos:], tmpbuf)
  1072  		}
  1073  	}
  1074  
  1075  	lock(&trace.lock)
  1076  	traceFullQueue(bufp)
  1077  	unlock(&trace.lock)
  1078  
  1079  	tab.mem.drop()
  1080  	*tab = traceStackTable{}
  1081  	lockInit(&((*tab).lock), lockRankTraceStackTab)
  1082  }
  1083  
  1084  type traceFrame struct {
  1085  	funcID uint64
  1086  	fileID uint64
  1087  	line   uint64
  1088  }
  1089  
  1090  // traceFrameForPC records the frame information.
  1091  // It may allocate memory.
  1092  func traceFrameForPC(buf traceBufPtr, pid int32, f Frame) (traceFrame, traceBufPtr) {
  1093  	bufp := &buf
  1094  	var frame traceFrame
  1095  
  1096  	fn := f.Function
  1097  	const maxLen = 1 << 10
  1098  	if len(fn) > maxLen {
  1099  		fn = fn[len(fn)-maxLen:]
  1100  	}
  1101  	frame.funcID, bufp = traceString(bufp, pid, fn)
  1102  	frame.line = uint64(f.Line)
  1103  	file := f.File
  1104  	if len(file) > maxLen {
  1105  		file = file[len(file)-maxLen:]
  1106  	}
  1107  	frame.fileID, bufp = traceString(bufp, pid, file)
  1108  	return frame, (*bufp)
  1109  }
  1110  
  1111  // traceAlloc is a non-thread-safe region allocator.
  1112  // It holds a linked list of traceAllocBlock.
  1113  type traceAlloc struct {
  1114  	head traceAllocBlockPtr
  1115  	off  uintptr
  1116  }
  1117  
  1118  // traceAllocBlock is a block in traceAlloc.
  1119  //
  1120  // traceAllocBlock is allocated from non-GC'd memory, so it must not
  1121  // contain heap pointers. Writes to pointers to traceAllocBlocks do
  1122  // not need write barriers.
  1123  //
  1124  //go:notinheap
  1125  type traceAllocBlock struct {
  1126  	next traceAllocBlockPtr
  1127  	data [64<<10 - goarch.PtrSize]byte
  1128  }
  1129  
  1130  // TODO: Since traceAllocBlock is now go:notinheap, this isn't necessary.
  1131  type traceAllocBlockPtr uintptr
  1132  
  1133  func (p traceAllocBlockPtr) ptr() *traceAllocBlock   { return (*traceAllocBlock)(unsafe.Pointer(p)) }
  1134  func (p *traceAllocBlockPtr) set(x *traceAllocBlock) { *p = traceAllocBlockPtr(unsafe.Pointer(x)) }
  1135  
  1136  // alloc allocates n-byte block.
  1137  func (a *traceAlloc) alloc(n uintptr) unsafe.Pointer {
  1138  	n = alignUp(n, goarch.PtrSize)
  1139  	if a.head == 0 || a.off+n > uintptr(len(a.head.ptr().data)) {
  1140  		if n > uintptr(len(a.head.ptr().data)) {
  1141  			throw("trace: alloc too large")
  1142  		}
  1143  		block := (*traceAllocBlock)(sysAlloc(unsafe.Sizeof(traceAllocBlock{}), &memstats.other_sys))
  1144  		if block == nil {
  1145  			throw("trace: out of memory")
  1146  		}
  1147  		block.next.set(a.head.ptr())
  1148  		a.head.set(block)
  1149  		a.off = 0
  1150  	}
  1151  	p := &a.head.ptr().data[a.off]
  1152  	a.off += n
  1153  	return unsafe.Pointer(p)
  1154  }
  1155  
  1156  // drop frees all previously allocated memory and resets the allocator.
  1157  func (a *traceAlloc) drop() {
  1158  	for a.head != 0 {
  1159  		block := a.head.ptr()
  1160  		a.head.set(block.next.ptr())
  1161  		sysFree(unsafe.Pointer(block), unsafe.Sizeof(traceAllocBlock{}), &memstats.other_sys)
  1162  	}
  1163  }
  1164  
  1165  // The following functions write specific events to trace.
  1166  
  1167  func traceGomaxprocs(procs int32) {
  1168  	traceEvent(traceEvGomaxprocs, 1, uint64(procs))
  1169  }
  1170  
  1171  func traceProcStart() {
  1172  	traceEvent(traceEvProcStart, -1, uint64(getg().m.id))
  1173  }
  1174  
  1175  func traceProcStop(pp *p) {
  1176  	// Sysmon and stopTheWorld can stop Ps blocked in syscalls,
  1177  	// to handle this we temporary employ the P.
  1178  	mp := acquirem()
  1179  	oldp := mp.p
  1180  	mp.p.set(pp)
  1181  	traceEvent(traceEvProcStop, -1)
  1182  	mp.p = oldp
  1183  	releasem(mp)
  1184  }
  1185  
  1186  func traceGCStart() {
  1187  	traceEvent(traceEvGCStart, 3, trace.seqGC)
  1188  	trace.seqGC++
  1189  }
  1190  
  1191  func traceGCDone() {
  1192  	traceEvent(traceEvGCDone, -1)
  1193  }
  1194  
  1195  func traceGCSTWStart(kind int) {
  1196  	traceEvent(traceEvGCSTWStart, -1, uint64(kind))
  1197  }
  1198  
  1199  func traceGCSTWDone() {
  1200  	traceEvent(traceEvGCSTWDone, -1)
  1201  }
  1202  
  1203  // traceGCSweepStart prepares to trace a sweep loop. This does not
  1204  // emit any events until traceGCSweepSpan is called.
  1205  //
  1206  // traceGCSweepStart must be paired with traceGCSweepDone and there
  1207  // must be no preemption points between these two calls.
  1208  func traceGCSweepStart() {
  1209  	// Delay the actual GCSweepStart event until the first span
  1210  	// sweep. If we don't sweep anything, don't emit any events.
  1211  	_p_ := getg().m.p.ptr()
  1212  	if _p_.traceSweep {
  1213  		throw("double traceGCSweepStart")
  1214  	}
  1215  	_p_.traceSweep, _p_.traceSwept, _p_.traceReclaimed = true, 0, 0
  1216  }
  1217  
  1218  // traceGCSweepSpan traces the sweep of a single page.
  1219  //
  1220  // This may be called outside a traceGCSweepStart/traceGCSweepDone
  1221  // pair; however, it will not emit any trace events in this case.
  1222  func traceGCSweepSpan(bytesSwept uintptr) {
  1223  	_p_ := getg().m.p.ptr()
  1224  	if _p_.traceSweep {
  1225  		if _p_.traceSwept == 0 {
  1226  			traceEvent(traceEvGCSweepStart, 1)
  1227  		}
  1228  		_p_.traceSwept += bytesSwept
  1229  	}
  1230  }
  1231  
  1232  func traceGCSweepDone() {
  1233  	_p_ := getg().m.p.ptr()
  1234  	if !_p_.traceSweep {
  1235  		throw("missing traceGCSweepStart")
  1236  	}
  1237  	if _p_.traceSwept != 0 {
  1238  		traceEvent(traceEvGCSweepDone, -1, uint64(_p_.traceSwept), uint64(_p_.traceReclaimed))
  1239  	}
  1240  	_p_.traceSweep = false
  1241  }
  1242  
  1243  func traceGCMarkAssistStart() {
  1244  	traceEvent(traceEvGCMarkAssistStart, 1)
  1245  }
  1246  
  1247  func traceGCMarkAssistDone() {
  1248  	traceEvent(traceEvGCMarkAssistDone, -1)
  1249  }
  1250  
  1251  func traceGoCreate(newg *g, pc uintptr) {
  1252  	newg.traceseq = 0
  1253  	newg.tracelastp = getg().m.p
  1254  	// +PCQuantum because traceFrameForPC expects return PCs and subtracts PCQuantum.
  1255  	id := trace.stackTab.put([]uintptr{startPCforTrace(pc) + sys.PCQuantum})
  1256  	traceEvent(traceEvGoCreate, 2, uint64(newg.goid), uint64(id))
  1257  }
  1258  
  1259  func traceGoStart() {
  1260  	_g_ := getg().m.curg
  1261  	_p_ := _g_.m.p
  1262  	_g_.traceseq++
  1263  	if _p_.ptr().gcMarkWorkerMode != gcMarkWorkerNotWorker {
  1264  		traceEvent(traceEvGoStartLabel, -1, uint64(_g_.goid), _g_.traceseq, trace.markWorkerLabels[_p_.ptr().gcMarkWorkerMode])
  1265  	} else if _g_.tracelastp == _p_ {
  1266  		traceEvent(traceEvGoStartLocal, -1, uint64(_g_.goid))
  1267  	} else {
  1268  		_g_.tracelastp = _p_
  1269  		traceEvent(traceEvGoStart, -1, uint64(_g_.goid), _g_.traceseq)
  1270  	}
  1271  }
  1272  
  1273  func traceGoEnd() {
  1274  	traceEvent(traceEvGoEnd, -1)
  1275  }
  1276  
  1277  func traceGoSched() {
  1278  	_g_ := getg()
  1279  	_g_.tracelastp = _g_.m.p
  1280  	traceEvent(traceEvGoSched, 1)
  1281  }
  1282  
  1283  func traceGoPreempt() {
  1284  	_g_ := getg()
  1285  	_g_.tracelastp = _g_.m.p
  1286  	traceEvent(traceEvGoPreempt, 1)
  1287  }
  1288  
  1289  func traceGoPark(traceEv byte, skip int) {
  1290  	if traceEv&traceFutileWakeup != 0 {
  1291  		traceEvent(traceEvFutileWakeup, -1)
  1292  	}
  1293  	traceEvent(traceEv & ^traceFutileWakeup, skip)
  1294  }
  1295  
  1296  func traceGoUnpark(gp *g, skip int) {
  1297  	_p_ := getg().m.p
  1298  	gp.traceseq++
  1299  	if gp.tracelastp == _p_ {
  1300  		traceEvent(traceEvGoUnblockLocal, skip, uint64(gp.goid))
  1301  	} else {
  1302  		gp.tracelastp = _p_
  1303  		traceEvent(traceEvGoUnblock, skip, uint64(gp.goid), gp.traceseq)
  1304  	}
  1305  }
  1306  
  1307  func traceGoSysCall() {
  1308  	traceEvent(traceEvGoSysCall, 1)
  1309  }
  1310  
  1311  func traceGoSysExit(ts int64) {
  1312  	if ts != 0 && ts < trace.ticksStart {
  1313  		// There is a race between the code that initializes sysexitticks
  1314  		// (in exitsyscall, which runs without a P, and therefore is not
  1315  		// stopped with the rest of the world) and the code that initializes
  1316  		// a new trace. The recorded sysexitticks must therefore be treated
  1317  		// as "best effort". If they are valid for this trace, then great,
  1318  		// use them for greater accuracy. But if they're not valid for this
  1319  		// trace, assume that the trace was started after the actual syscall
  1320  		// exit (but before we actually managed to start the goroutine,
  1321  		// aka right now), and assign a fresh time stamp to keep the log consistent.
  1322  		ts = 0
  1323  	}
  1324  	_g_ := getg().m.curg
  1325  	_g_.traceseq++
  1326  	_g_.tracelastp = _g_.m.p
  1327  	traceEvent(traceEvGoSysExit, -1, uint64(_g_.goid), _g_.traceseq, uint64(ts)/traceTickDiv)
  1328  }
  1329  
  1330  func traceGoSysBlock(pp *p) {
  1331  	// Sysmon and stopTheWorld can declare syscalls running on remote Ps as blocked,
  1332  	// to handle this we temporary employ the P.
  1333  	mp := acquirem()
  1334  	oldp := mp.p
  1335  	mp.p.set(pp)
  1336  	traceEvent(traceEvGoSysBlock, -1)
  1337  	mp.p = oldp
  1338  	releasem(mp)
  1339  }
  1340  
  1341  func traceHeapAlloc() {
  1342  	traceEvent(traceEvHeapAlloc, -1, gcController.heapLive)
  1343  }
  1344  
  1345  func traceHeapGoal() {
  1346  	heapGoal := gcController.heapGoal()
  1347  	if heapGoal == ^uint64(0) {
  1348  		// Heap-based triggering is disabled.
  1349  		traceEvent(traceEvHeapGoal, -1, 0)
  1350  	} else {
  1351  		traceEvent(traceEvHeapGoal, -1, heapGoal)
  1352  	}
  1353  }
  1354  
  1355  // To access runtime functions from runtime/trace.
  1356  // See runtime/trace/annotation.go
  1357  
  1358  //go:linkname trace_userTaskCreate runtime/trace.userTaskCreate
  1359  func trace_userTaskCreate(id, parentID uint64, taskType string) {
  1360  	if !trace.enabled {
  1361  		return
  1362  	}
  1363  
  1364  	// Same as in traceEvent.
  1365  	mp, pid, bufp := traceAcquireBuffer()
  1366  	if !trace.enabled && !mp.startingtrace {
  1367  		traceReleaseBuffer(pid)
  1368  		return
  1369  	}
  1370  
  1371  	typeStringID, bufp := traceString(bufp, pid, taskType)
  1372  	traceEventLocked(0, mp, pid, bufp, traceEvUserTaskCreate, 0, 3, id, parentID, typeStringID)
  1373  	traceReleaseBuffer(pid)
  1374  }
  1375  
  1376  //go:linkname trace_userTaskEnd runtime/trace.userTaskEnd
  1377  func trace_userTaskEnd(id uint64) {
  1378  	traceEvent(traceEvUserTaskEnd, 2, id)
  1379  }
  1380  
  1381  //go:linkname trace_userRegion runtime/trace.userRegion
  1382  func trace_userRegion(id, mode uint64, name string) {
  1383  	if !trace.enabled {
  1384  		return
  1385  	}
  1386  
  1387  	mp, pid, bufp := traceAcquireBuffer()
  1388  	if !trace.enabled && !mp.startingtrace {
  1389  		traceReleaseBuffer(pid)
  1390  		return
  1391  	}
  1392  
  1393  	nameStringID, bufp := traceString(bufp, pid, name)
  1394  	traceEventLocked(0, mp, pid, bufp, traceEvUserRegion, 0, 3, id, mode, nameStringID)
  1395  	traceReleaseBuffer(pid)
  1396  }
  1397  
  1398  //go:linkname trace_userLog runtime/trace.userLog
  1399  func trace_userLog(id uint64, category, message string) {
  1400  	if !trace.enabled {
  1401  		return
  1402  	}
  1403  
  1404  	mp, pid, bufp := traceAcquireBuffer()
  1405  	if !trace.enabled && !mp.startingtrace {
  1406  		traceReleaseBuffer(pid)
  1407  		return
  1408  	}
  1409  
  1410  	categoryID, bufp := traceString(bufp, pid, category)
  1411  
  1412  	extraSpace := traceBytesPerNumber + len(message) // extraSpace for the value string
  1413  	traceEventLocked(extraSpace, mp, pid, bufp, traceEvUserLog, 0, 3, id, categoryID)
  1414  	// traceEventLocked reserved extra space for val and len(val)
  1415  	// in buf, so buf now has room for the following.
  1416  	buf := bufp.ptr()
  1417  
  1418  	// double-check the message and its length can fit.
  1419  	// Otherwise, truncate the message.
  1420  	slen := len(message)
  1421  	if room := len(buf.arr) - buf.pos; room < slen+traceBytesPerNumber {
  1422  		slen = room
  1423  	}
  1424  	buf.varint(uint64(slen))
  1425  	buf.pos += copy(buf.arr[buf.pos:], message[:slen])
  1426  
  1427  	traceReleaseBuffer(pid)
  1428  }
  1429  
  1430  // the start PC of a goroutine for tracing purposes. If pc is a wrapper,
  1431  // it returns the PC of the wrapped function. Otherwise it returns pc.
  1432  func startPCforTrace(pc uintptr) uintptr {
  1433  	f := findfunc(pc)
  1434  	if !f.valid() {
  1435  		return pc // should not happen, but don't care
  1436  	}
  1437  	w := funcdata(f, _FUNCDATA_WrapInfo)
  1438  	if w == nil {
  1439  		return pc // not a wrapper
  1440  	}
  1441  	return f.datap.textAddr(*(*uint32)(w))
  1442  }
  1443  

View as plain text