Debian Codegraph @

src/runtime/traceruntime.go

raw
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Runtime -> tracer API.
6
7 package runtime
8
9 import (
10 "internal/runtime/atomic"
11 "internal/trace/tracev2"
12 _ "unsafe" // for go:linkname
13 )
14
15 // gTraceState is per-G state for the tracer.
16 type gTraceState struct {
17 traceSchedResourceState
18 }
19
20 // reset resets the gTraceState for a new goroutine.
21 func (s *gTraceState) reset() {
22 s.seq = [2]uint64{}
23 // N.B. s.statusTraced is managed and cleared separately.
24 }
25
26 // mTraceState is per-M state for the tracer.
27 type mTraceState struct {
28 seqlock atomic.Uintptr // seqlock indicating that this M is writing to a trace buffer.
29 buf [2][tracev2.NumExperiments]*traceBuf // Per-M traceBuf for writing. Indexed by trace.gen%2.
30 link *m // Snapshot of alllink or freelink.
31 reentered uint32 // Whether we've reentered tracing from within tracing.
32 oldthrowsplit bool // gp.throwsplit upon calling traceLocker.writer. For debugging.
33 }
34
35 // pTraceState is per-P state for the tracer.
36 type pTraceState struct {
37 traceSchedResourceState
38
39 // mSyscallID is the ID of the M this was bound to before entering a syscall.
40 mSyscallID int64
41
42 // maySweep indicates the sweep events should be traced.
43 // This is used to defer the sweep start event until a span
44 // has actually been swept.
45 maySweep bool
46
47 // inSweep indicates that at least one sweep event has been traced.
48 inSweep bool
49
50 // swept and reclaimed track the number of bytes swept and reclaimed
51 // by sweeping in the current sweep loop (while maySweep was true).
52 swept, reclaimed uintptr
53 }
54
55 // traceLockInit initializes global trace locks.
56 func traceLockInit() {
57 // Sharing a lock rank here is fine because they should never be accessed
58 // together. If they are, we want to find out immediately.
59 lockInit(&trace.stringTab[0].lock, lockRankTraceStrings)
60 lockInit(&trace.stringTab[0].tab.mem.lock, lockRankTraceStrings)
61 lockInit(&trace.stringTab[1].lock, lockRankTraceStrings)
62 lockInit(&trace.stringTab[1].tab.mem.lock, lockRankTraceStrings)
63 lockInit(&trace.stackTab[0].tab.mem.lock, lockRankTraceStackTab)
64 lockInit(&trace.stackTab[1].tab.mem.lock, lockRankTraceStackTab)
65 lockInit(&trace.typeTab[0].tab.mem.lock, lockRankTraceTypeTab)
66 lockInit(&trace.typeTab[1].tab.mem.lock, lockRankTraceTypeTab)
67 lockInit(&trace.lock, lockRankTrace)
68 }
69
70 // lockRankMayTraceFlush records the lock ranking effects of a
71 // potential call to traceFlush.
72 //
73 // nosplit because traceAcquire is nosplit.
74 //
75 //go:nosplit
76 func lockRankMayTraceFlush() {
77 lockWithRankMayAcquire(&trace.lock, getLockRank(&trace.lock))
78 }
79
80 // traceBlockReason is an enumeration of reasons a goroutine might block.
81 // This is the interface the rest of the runtime uses to tell the
82 // tracer why a goroutine blocked. The tracer then propagates this information
83 // into the trace however it sees fit.
84 //
85 // Note that traceBlockReasons should not be compared, since reasons that are
86 // distinct by name may *not* be distinct by value.
87 type traceBlockReason uint8
88
89 const (
90 traceBlockGeneric traceBlockReason = iota
91 traceBlockForever
92 traceBlockNet
93 traceBlockSelect
94 traceBlockCondWait
95 traceBlockSync
96 traceBlockChanSend
97 traceBlockChanRecv
98 traceBlockGCMarkAssist
99 traceBlockGCSweep
100 traceBlockSystemGoroutine
101 traceBlockPreempted
102 traceBlockDebugCall
103 traceBlockUntilGCEnds
104 traceBlockSleep
105 traceBlockGCWeakToStrongWait
106 traceBlockSynctest
107 )
108
109 var traceBlockReasonStrings = [...]string{
110 traceBlockGeneric: "unspecified",
111 traceBlockForever: "forever",
112 traceBlockNet: "network",
113 traceBlockSelect: "select",
114 traceBlockCondWait: "sync.(*Cond).Wait",
115 traceBlockSync: "sync",
116 traceBlockChanSend: "chan send",
117 traceBlockChanRecv: "chan receive",
118 traceBlockGCMarkAssist: "GC mark assist wait for work",
119 traceBlockGCSweep: "GC background sweeper wait",
120 traceBlockSystemGoroutine: "system goroutine wait",
121 traceBlockPreempted: "preempted",
122 traceBlockDebugCall: "wait for debug call",
123 traceBlockUntilGCEnds: "wait until GC ends",
124 traceBlockSleep: "sleep",
125 traceBlockGCWeakToStrongWait: "GC weak to strong wait",
126 traceBlockSynctest: "synctest",
127 }
128
129 // traceGoStopReason is an enumeration of reasons a goroutine might yield.
130 //
131 // Note that traceGoStopReasons should not be compared, since reasons that are
132 // distinct by name may *not* be distinct by value.
133 type traceGoStopReason uint8
134
135 const (
136 traceGoStopGeneric traceGoStopReason = iota
137 traceGoStopGoSched
138 traceGoStopPreempted
139 )
140
141 var traceGoStopReasonStrings = [...]string{
142 traceGoStopGeneric: "unspecified",
143 traceGoStopGoSched: "runtime.Gosched",
144 traceGoStopPreempted: "preempted",
145 }
146
147 // traceEnabled returns true if the trace is currently enabled.
148 //
149 //go:nosplit
150 func traceEnabled() bool {
151 return trace.enabled
152 }
153
154 // traceAllocFreeEnabled returns true if the trace is currently enabled
155 // and alloc/free events are also enabled.
156 //
157 //go:nosplit
158 func traceAllocFreeEnabled() bool {
159 return trace.enabledWithAllocFree
160 }
161
162 // traceShuttingDown returns true if the trace is currently shutting down.
163 func traceShuttingDown() bool {
164 return trace.shutdown.Load()
165 }
166
167 // traceLocker represents an M writing trace events. While a traceLocker value
168 // is valid, the tracer observes all operations on the G/M/P or trace events being
169 // written as happening atomically.
170 type traceLocker struct {
171 mp *m
172 gen uintptr
173 }
174
175 // debugTraceReentrancy checks if the trace is reentrant.
176 //
177 // This is optional because throwing in a function makes it instantly
178 // not inlineable, and we want traceAcquire to be inlineable for
179 // low overhead when the trace is disabled.
180 const debugTraceReentrancy = false
181
182 // traceAcquire prepares this M for writing one or more trace events.
183 //
184 // nosplit because it's called on the syscall path when stack movement is forbidden.
185 //
186 //go:nosplit
187 func traceAcquire() traceLocker {
188 if !traceEnabled() {
189 return traceLocker{}
190 }
191 return traceAcquireEnabled()
192 }
193
194 // traceAcquireEnabled is the traceEnabled path for traceAcquire. It's explicitly
195 // broken out to make traceAcquire inlineable to keep the overhead of the tracer
196 // when it's disabled low.
197 //
198 // nosplit because it's called by traceAcquire, which is nosplit.
199 //
200 //go:nosplit
201 func traceAcquireEnabled() traceLocker {
202 // Any time we acquire a traceLocker, we may flush a trace buffer. But
203 // buffer flushes are rare. Record the lock edge even if it doesn't happen
204 // this time.
205 lockRankMayTraceFlush()
206
207 // Prevent preemption.
208 mp := acquirem()
209
210 // Check if we're already tracing. It's safe to be reentrant in general,
211 // because this function (and the invariants of traceLocker.writer) ensure
212 // that it is.
213 if mp.trace.seqlock.Load()%2 == 1 {
214 mp.trace.reentered++
215 return traceLocker{mp, trace.gen.Load()}
216 }
217
218 // Acquire the trace seqlock. This prevents traceAdvance from moving forward
219 // until all Ms are observed to be outside of their seqlock critical section.
220 //
221 // Note: The seqlock is mutated here and also in traceCPUSample. If you update
222 // usage of the seqlock here, make sure to also look at what traceCPUSample is
223 // doing.
224 seq := mp.trace.seqlock.Add(1)
225 if debugTraceReentrancy && seq%2 != 1 {
226 throw("bad use of trace.seqlock")
227 }
228
229 // N.B. This load of gen appears redundant with the one in traceEnabled.
230 // However, it's very important that the gen we use for writing to the trace
231 // is acquired under a traceLocker so traceAdvance can make sure no stale
232 // gen values are being used.
233 //
234 // Because we're doing this load again, it also means that the trace
235 // might end up being disabled when we load it. In that case we need to undo
236 // what we did and bail.
237 gen := trace.gen.Load()
238 if gen == 0 {
239 mp.trace.seqlock.Add(1)
240 releasem(mp)
241 return traceLocker{}
242 }
243 return traceLocker{mp, gen}
244 }
245
246 // ok returns true if the traceLocker is valid (i.e. tracing is enabled).
247 //
248 // nosplit because it's called on the syscall path when stack movement is forbidden.
249 //
250 //go:nosplit
251 func (tl traceLocker) ok() bool {
252 return tl.gen != 0
253 }
254
255 // traceRelease indicates that this M is done writing trace events.
256 //
257 // nosplit because it's called on the syscall path when stack movement is forbidden.
258 //
259 //go:nosplit
260 func traceRelease(tl traceLocker) {
261 if tl.mp.trace.reentered > 0 {
262 tl.mp.trace.reentered--
263 } else {
264 seq := tl.mp.trace.seqlock.Add(1)
265 if debugTraceReentrancy && seq%2 != 0 {
266 print("runtime: seq=", seq, "\n")
267 throw("bad use of trace.seqlock")
268 }
269 }
270 releasem(tl.mp)
271 }
272
273 // traceExitingSyscall marks a goroutine as exiting the syscall slow path.
274 //
275 // Must be paired with a traceExitedSyscall call.
276 func traceExitingSyscall() {
277 trace.exitingSyscall.Add(1)
278 }
279
280 // traceExitedSyscall marks a goroutine as having exited the syscall slow path.
281 func traceExitedSyscall() {
282 trace.exitingSyscall.Add(-1)
283 }
284
285 // Gomaxprocs emits a ProcsChange event.
286 func (tl traceLocker) Gomaxprocs(procs int32) {
287 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvProcsChange, traceArg(procs), tl.stack(1))
288 }
289
290 // ProcStart traces a ProcStart event.
291 //
292 // Must be called with a valid P.
293 func (tl traceLocker) ProcStart() {
294 pp := tl.mp.p.ptr()
295 // Procs are typically started within the scheduler when there is no user goroutine. If there is a user goroutine,
296 // it must be in _Gsyscall because the only time a goroutine is allowed to have its Proc moved around from under it
297 // is during a syscall.
298 tl.eventWriter(tracev2.GoSyscall, tracev2.ProcIdle).event(tracev2.EvProcStart, traceArg(pp.id), pp.trace.nextSeq(tl.gen))
299 }
300
301 // ProcStop traces a ProcStop event.
302 func (tl traceLocker) ProcStop(pp *p) {
303 // The only time a goroutine is allowed to have its Proc moved around
304 // from under it is during a syscall.
305 tl.eventWriter(tracev2.GoSyscall, tracev2.ProcRunning).event(tracev2.EvProcStop)
306 }
307
308 // GCActive traces a GCActive event.
309 //
310 // Must be emitted by an actively running goroutine on an active P. This restriction can be changed
311 // easily and only depends on where it's currently called.
312 func (tl traceLocker) GCActive() {
313 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGCActive, traceArg(trace.seqGC))
314 // N.B. Only one GC can be running at a time, so this is naturally
315 // serialized by the caller.
316 trace.seqGC++
317 }
318
319 // GCStart traces a GCBegin event.
320 //
321 // Must be emitted by an actively running goroutine on an active P. This restriction can be changed
322 // easily and only depends on where it's currently called.
323 func (tl traceLocker) GCStart() {
324 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGCBegin, traceArg(trace.seqGC), tl.stack(3))
325 // N.B. Only one GC can be running at a time, so this is naturally
326 // serialized by the caller.
327 trace.seqGC++
328 }
329
330 // GCDone traces a GCEnd event.
331 //
332 // Must be emitted by an actively running goroutine on an active P. This restriction can be changed
333 // easily and only depends on where it's currently called.
334 func (tl traceLocker) GCDone() {
335 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGCEnd, traceArg(trace.seqGC))
336 // N.B. Only one GC can be running at a time, so this is naturally
337 // serialized by the caller.
338 trace.seqGC++
339 }
340
341 // STWStart traces a STWBegin event.
342 func (tl traceLocker) STWStart(reason stwReason) {
343 // Although the current P may be in _Pgcstop here, we model the P as running during the STW. This deviates from the
344 // runtime's state tracking, but it's more accurate and doesn't result in any loss of information.
345 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvSTWBegin, tl.string(reason.String()), tl.stack(2))
346 }
347
348 // STWDone traces a STWEnd event.
349 func (tl traceLocker) STWDone() {
350 // Although the current P may be in _Pgcstop here, we model the P as running during the STW. This deviates from the
351 // runtime's state tracking, but it's more accurate and doesn't result in any loss of information.
352 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvSTWEnd)
353 }
354
355 // GCSweepStart prepares to trace a sweep loop. This does not
356 // emit any events until traceGCSweepSpan is called.
357 //
358 // GCSweepStart must be paired with traceGCSweepDone and there
359 // must be no preemption points between these two calls.
360 //
361 // Must be called with a valid P.
362 func (tl traceLocker) GCSweepStart() {
363 // Delay the actual GCSweepBegin event until the first span
364 // sweep. If we don't sweep anything, don't emit any events.
365 pp := tl.mp.p.ptr()
366 if pp.trace.maySweep {
367 throw("double traceGCSweepStart")
368 }
369 pp.trace.maySweep, pp.trace.swept, pp.trace.reclaimed = true, 0, 0
370 }
371
372 // GCSweepSpan traces the sweep of a single span. If this is
373 // the first span swept since traceGCSweepStart was called, this
374 // will emit a GCSweepBegin event.
375 //
376 // This may be called outside a traceGCSweepStart/traceGCSweepDone
377 // pair; however, it will not emit any trace events in this case.
378 //
379 // Must be called with a valid P.
380 func (tl traceLocker) GCSweepSpan(bytesSwept uintptr) {
381 pp := tl.mp.p.ptr()
382 if pp.trace.maySweep {
383 if pp.trace.swept == 0 {
384 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGCSweepBegin, tl.stack(1))
385 pp.trace.inSweep = true
386 }
387 pp.trace.swept += bytesSwept
388 }
389 }
390
391 // GCSweepDone finishes tracing a sweep loop. If any memory was
392 // swept (i.e. traceGCSweepSpan emitted an event) then this will emit
393 // a GCSweepEnd event.
394 //
395 // Must be called with a valid P.
396 func (tl traceLocker) GCSweepDone() {
397 pp := tl.mp.p.ptr()
398 if !pp.trace.maySweep {
399 throw("missing traceGCSweepStart")
400 }
401 if pp.trace.inSweep {
402 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGCSweepEnd, traceArg(pp.trace.swept), traceArg(pp.trace.reclaimed))
403 pp.trace.inSweep = false
404 }
405 pp.trace.maySweep = false
406 }
407
408 // GCMarkAssistStart emits a MarkAssistBegin event.
409 func (tl traceLocker) GCMarkAssistStart() {
410 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGCMarkAssistBegin, tl.stack(1))
411 }
412
413 // GCMarkAssistDone emits a MarkAssistEnd event.
414 func (tl traceLocker) GCMarkAssistDone() {
415 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGCMarkAssistEnd)
416 }
417
418 // GoCreate emits a GoCreate event.
419 func (tl traceLocker) GoCreate(newg *g, pc uintptr, blocked bool) {
420 newg.trace.setStatusTraced(tl.gen)
421 ev := tracev2.EvGoCreate
422 if blocked {
423 ev = tracev2.EvGoCreateBlocked
424 }
425 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(ev, traceArg(newg.goid), tl.startPC(pc), tl.stack(2))
426 }
427
428 // GoStart emits a GoStart event.
429 //
430 // Must be called with a valid P.
431 func (tl traceLocker) GoStart() {
432 gp := getg().m.curg
433 pp := gp.m.p
434 w := tl.eventWriter(tracev2.GoRunnable, tracev2.ProcRunning)
435 w.event(tracev2.EvGoStart, traceArg(gp.goid), gp.trace.nextSeq(tl.gen))
436 if pp.ptr().gcMarkWorkerMode != gcMarkWorkerNotWorker {
437 w.event(tracev2.EvGoLabel, trace.markWorkerLabels[tl.gen%2][pp.ptr().gcMarkWorkerMode])
438 }
439 }
440
441 // GoEnd emits a GoDestroy event.
442 //
443 // TODO(mknyszek): Rename this to GoDestroy.
444 func (tl traceLocker) GoEnd() {
445 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGoDestroy)
446 }
447
448 // GoSched emits a GoStop event with a GoSched reason.
449 func (tl traceLocker) GoSched() {
450 tl.GoStop(traceGoStopGoSched)
451 }
452
453 // GoPreempt emits a GoStop event with a GoPreempted reason.
454 func (tl traceLocker) GoPreempt() {
455 tl.GoStop(traceGoStopPreempted)
456 }
457
458 // GoStop emits a GoStop event with the provided reason.
459 func (tl traceLocker) GoStop(reason traceGoStopReason) {
460 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGoStop, traceArg(trace.goStopReasons[tl.gen%2][reason]), tl.stack(0))
461 }
462
463 // GoPark emits a GoBlock event with the provided reason.
464 //
465 // TODO(mknyszek): Replace traceBlockReason with waitReason. It's silly
466 // that we have both, and waitReason is way more descriptive.
467 func (tl traceLocker) GoPark(reason traceBlockReason, skip int) {
468 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGoBlock, traceArg(trace.goBlockReasons[tl.gen%2][reason]), tl.stack(skip))
469 }
470
471 // GoUnpark emits a GoUnblock event.
472 func (tl traceLocker) GoUnpark(gp *g, skip int) {
473 // Emit a GoWaiting status if necessary for the unblocked goroutine.
474 tl.emitUnblockStatus(gp, tl.gen)
475 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGoUnblock, traceArg(gp.goid), gp.trace.nextSeq(tl.gen), tl.stack(skip))
476 }
477
478 // GoSwitch emits a GoSwitch event. If destroy is true, the calling goroutine
479 // is simultaneously being destroyed.
480 func (tl traceLocker) GoSwitch(nextg *g, destroy bool) {
481 // Emit a GoWaiting status if necessary for the unblocked goroutine.
482 tl.emitUnblockStatus(nextg, tl.gen)
483 w := tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning)
484 ev := tracev2.EvGoSwitch
485 if destroy {
486 ev = tracev2.EvGoSwitchDestroy
487 }
488 w.event(ev, traceArg(nextg.goid), nextg.trace.nextSeq(tl.gen))
489 }
490
491 // emitUnblockStatus emits a GoStatus GoWaiting event for a goroutine about to be
492 // unblocked to the trace writer.
493 func (tl traceLocker) emitUnblockStatus(gp *g, gen uintptr) {
494 if !gp.trace.statusWasTraced(gen) && gp.trace.acquireStatus(gen) {
495 // TODO(go.dev/issue/65634): Although it would be nice to add a stack trace here of gp,
496 // we cannot safely do so. gp is in _Gwaiting and so we don't have ownership of its stack.
497 // We can fix this by acquiring the goroutine's scan bit.
498 tl.writer().writeGoStatus(gp.goid, -1, tracev2.GoWaiting, gp.inMarkAssist, 0).end()
499 }
500 }
501
502 // GoSysCall emits a GoSyscallBegin event.
503 //
504 // Must be called with a valid P.
505 func (tl traceLocker) GoSysCall() {
506 // Scribble down the M that the P is currently attached to.
507 pp := tl.mp.p.ptr()
508 pp.trace.mSyscallID = int64(tl.mp.procid)
509 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGoSyscallBegin, pp.trace.nextSeq(tl.gen), tl.stack(1))
510 }
511
512 // GoSysExit emits a GoSyscallEnd event, possibly along with a GoSyscallBlocked event
513 // if lostP is true.
514 //
515 // lostP must be true in all cases that a goroutine loses its P during a syscall.
516 // This means it's not sufficient to check if it has no P. In particular, it needs to be
517 // true in the following cases:
518 // - The goroutine lost its P, it ran some other code, and then got it back. It's now running with that P.
519 // - The goroutine lost its P and was unable to reacquire it, and is now running without a P.
520 // - The goroutine lost its P and acquired a different one, and is now running with that P.
521 func (tl traceLocker) GoSysExit(lostP bool) {
522 ev := tracev2.EvGoSyscallEnd
523 procStatus := tracev2.ProcSyscall // Procs implicitly enter tracev2.ProcSyscall on GoSyscallBegin.
524 if lostP {
525 ev = tracev2.EvGoSyscallEndBlocked
526 procStatus = tracev2.ProcRunning // If a G has a P when emitting this event, it reacquired a P and is indeed running.
527 } else {
528 tl.mp.p.ptr().trace.mSyscallID = -1
529 }
530 tl.eventWriter(tracev2.GoSyscall, procStatus).event(ev)
531 }
532
533 // ProcSteal indicates that our current M stole a P from another M.
534 //
535 // inSyscall indicates that we're stealing the P from a syscall context.
536 //
537 // The caller must have ownership of pp.
538 func (tl traceLocker) ProcSteal(pp *p, inSyscall bool) {
539 // Grab the M ID we stole from.
540 mStolenFrom := pp.trace.mSyscallID
541 pp.trace.mSyscallID = -1
542
543 // Emit the status of the P we're stealing. We may be just about to do this when creating the event
544 // writer but it's not guaranteed, even if inSyscall is true. Although it might seem like from a
545 // syscall context we're always stealing a P for ourselves, we may have not wired it up yet (so
546 // it wouldn't be visible to eventWriter) or we may not even intend to wire it up to ourselves
547 // at all (e.g. entersyscall_gcwait).
548 if !pp.trace.statusWasTraced(tl.gen) && pp.trace.acquireStatus(tl.gen) {
549 // Careful: don't use the event writer. We never want status or in-progress events
550 // to trigger more in-progress events.
551 tl.writer().writeProcStatus(uint64(pp.id), tracev2.ProcSyscallAbandoned, pp.trace.inSweep).end()
552 }
553
554 // The status of the proc and goroutine, if we need to emit one here, is not evident from the
555 // context of just emitting this event alone. There are two cases. Either we're trying to steal
556 // the P just to get its attention (e.g. STW or sysmon retake) or we're trying to steal a P for
557 // ourselves specifically to keep running. The two contexts look different, but can be summarized
558 // fairly succinctly. In the former, we're a regular running goroutine and proc, if we have either.
559 // In the latter, we're a goroutine in a syscall.
560 goStatus := tracev2.GoRunning
561 procStatus := tracev2.ProcRunning
562 if inSyscall {
563 goStatus = tracev2.GoSyscall
564 procStatus = tracev2.ProcSyscallAbandoned
565 }
566 tl.eventWriter(goStatus, procStatus).event(tracev2.EvProcSteal, traceArg(pp.id), pp.trace.nextSeq(tl.gen), traceArg(mStolenFrom))
567 }
568
569 // HeapAlloc emits a HeapAlloc event.
570 func (tl traceLocker) HeapAlloc(live uint64) {
571 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvHeapAlloc, traceArg(live))
572 }
573
574 // HeapGoal reads the current heap goal and emits a HeapGoal event.
575 func (tl traceLocker) HeapGoal() {
576 heapGoal := gcController.heapGoal()
577 // The heapGoal calculations will result in strange numbers if the GC if off. See go.dev/issue/63864.
578 // Check gcPercent before using the heapGoal in the trace.
579 if heapGoal == ^uint64(0) || gcController.gcPercent.Load() < 0 {
580 // Heap-based triggering is disabled.
581 heapGoal = 0
582 }
583 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvHeapGoal, traceArg(heapGoal))
584 }
585
586 // GoCreateSyscall indicates that a goroutine has transitioned from dead to GoSyscall.
587 //
588 // Unlike GoCreate, the caller must be running on gp.
589 //
590 // This occurs when C code calls into Go. On pthread platforms it occurs only when
591 // a C thread calls into Go code for the first time.
592 func (tl traceLocker) GoCreateSyscall(gp *g) {
593 // N.B. We should never trace a status for this goroutine (which we're currently running on),
594 // since we want this to appear like goroutine creation.
595 gp.trace.setStatusTraced(tl.gen)
596 tl.eventWriter(tracev2.GoBad, tracev2.ProcBad).event(tracev2.EvGoCreateSyscall, traceArg(gp.goid))
597 }
598
599 // GoDestroySyscall indicates that a goroutine has transitioned from GoSyscall to dead.
600 //
601 // Must not have a P.
602 //
603 // This occurs when Go code returns back to C. On pthread platforms it occurs only when
604 // the C thread is destroyed.
605 func (tl traceLocker) GoDestroySyscall() {
606 // N.B. If we trace a status here, we must never have a P, and we must be on a goroutine
607 // that is in the syscall state.
608 tl.eventWriter(tracev2.GoSyscall, tracev2.ProcBad).event(tracev2.EvGoDestroySyscall)
609 }
610
611 // To access runtime functions from runtime/trace.
612 // See runtime/trace/annotation.go
613
614 // trace_userTaskCreate emits a UserTaskCreate event.
615 //
616 //go:linkname trace_userTaskCreate runtime/trace.userTaskCreate
617 func trace_userTaskCreate(id, parentID uint64, taskType string) {
618 tl := traceAcquire()
619 if !tl.ok() {
620 // Need to do this check because the caller won't have it.
621 return
622 }
623 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvUserTaskBegin, traceArg(id), traceArg(parentID), tl.string(taskType), tl.stack(3))
624 traceRelease(tl)
625 }
626
627 // trace_userTaskEnd emits a UserTaskEnd event.
628 //
629 //go:linkname trace_userTaskEnd runtime/trace.userTaskEnd
630 func trace_userTaskEnd(id uint64) {
631 tl := traceAcquire()
632 if !tl.ok() {
633 // Need to do this check because the caller won't have it.
634 return
635 }
636 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvUserTaskEnd, traceArg(id), tl.stack(2))
637 traceRelease(tl)
638 }
639
640 // trace_userRegion emits a UserRegionBegin or UserRegionEnd event,
641 // depending on mode (0 == Begin, 1 == End).
642 //
643 // TODO(mknyszek): Just make this two functions.
644 //
645 //go:linkname trace_userRegion runtime/trace.userRegion
646 func trace_userRegion(id, mode uint64, name string) {
647 tl := traceAcquire()
648 if !tl.ok() {
649 // Need to do this check because the caller won't have it.
650 return
651 }
652 var ev tracev2.EventType
653 switch mode {
654 case 0:
655 ev = tracev2.EvUserRegionBegin
656 case 1:
657 ev = tracev2.EvUserRegionEnd
658 default:
659 return
660 }
661 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(ev, traceArg(id), tl.string(name), tl.stack(3))
662 traceRelease(tl)
663 }
664
665 // trace_userLog emits a UserRegionBegin or UserRegionEnd event.
666 //
667 //go:linkname trace_userLog runtime/trace.userLog
668 func trace_userLog(id uint64, category, message string) {
669 tl := traceAcquire()
670 if !tl.ok() {
671 // Need to do this check because the caller won't have it.
672 return
673 }
674 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvUserLog, traceArg(id), tl.string(category), tl.uniqueString(message), tl.stack(3))
675 traceRelease(tl)
676 }
677
678 // traceThreadDestroy is called when a thread is removed from
679 // sched.freem.
680 //
681 // mp must not be able to emit trace events anymore.
682 //
683 // sched.lock must be held to synchronize with traceAdvance.
684 func traceThreadDestroy(mp *m) {
685 assertLockHeld(&sched.lock)
686
687 // Flush all outstanding buffers to maintain the invariant
688 // that an M only has active buffers while on sched.freem
689 // or allm.
690 //
691 // Perform a traceAcquire/traceRelease on behalf of mp to
692 // synchronize with the tracer trying to flush our buffer
693 // as well.
694 seq := mp.trace.seqlock.Add(1)
695 if debugTraceReentrancy && seq%2 != 1 {
696 throw("bad use of trace.seqlock")
697 }
698 systemstack(func() {
699 lock(&trace.lock)
700 for i := range mp.trace.buf {
701 for exp, buf := range mp.trace.buf[i] {
702 if buf != nil {
703 // N.B. traceBufFlush accepts a generation, but it
704 // really just cares about gen%2.
705 traceBufFlush(buf, uintptr(i))
706 mp.trace.buf[i][exp] = nil
707 }
708 }
709 }
710 unlock(&trace.lock)
711 })
712 seq1 := mp.trace.seqlock.Add(1)
713 if seq1 != seq+1 {
714 print("runtime: seq1=", seq1, "\n")
715 throw("bad use of trace.seqlock")
716 }
717 }