Black Lives Matter. Support the Equal Justice Initiative.

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

View as plain text