@@ -174,6 +174,20 @@ type mTraceState struct {
174174 startingTrace bool // this M is in TraceStart, potentially before traceEnabled is true
175175}
176176
177+ // pTraceState is per-P state for the tracer.
178+ type pTraceState struct {
179+ buf traceBufPtr
180+
181+ // inSweep indicates the sweep events should be traced.
182+ // This is used to defer the sweep start event until a span
183+ // has actually been swept.
184+ inSweep bool
185+
186+ // swept and reclaimed track the number of bytes swept and reclaimed
187+ // by sweeping in the current sweep loop (while inSweep was true).
188+ swept , reclaimed uintptr
189+ }
190+
177191// traceLockInit initializes global trace locks.
178192func traceLockInit () {
179193 lockInit (& trace .bufLock , lockRankTraceBuf )
@@ -379,10 +393,10 @@ func StopTrace() {
379393 // Loop over all allocated Ps because dead Ps may still have
380394 // trace buffers.
381395 for _ , p := range allp [:cap (allp )] {
382- buf := p .tracebuf
396+ buf := p .trace . buf
383397 if buf != 0 {
384398 traceFullQueue (buf )
385- p .tracebuf = 0
399+ p .trace . buf = 0
386400 }
387401 }
388402 if trace .buf != 0 {
@@ -429,7 +443,7 @@ func StopTrace() {
429443 // The lock protects us from races with StartTrace/StopTrace because they do stop-the-world.
430444 lock (& trace .lock )
431445 for _ , p := range allp [:cap (allp )] {
432- if p .tracebuf != 0 {
446+ if p .trace . buf != 0 {
433447 throw ("trace: non-empty trace buffer in proc" )
434448 }
435449 }
@@ -650,8 +664,8 @@ func traceReaderAvailable() *g {
650664//
651665//go:systemstack
652666func traceProcFree (pp * p ) {
653- buf := pp .tracebuf
654- pp .tracebuf = 0
667+ buf := pp .trace . buf
668+ pp .trace . buf = 0
655669 if buf == 0 {
656670 return
657671 }
@@ -980,7 +994,7 @@ func traceAcquireBuffer() (mp *m, pid int32, bufp *traceBufPtr) {
980994
981995 mp = acquirem ()
982996 if p := mp .p .ptr (); p != nil {
983- return mp , p .id , & p .tracebuf
997+ return mp , p .id , & p .trace . buf
984998 }
985999 lock (& trace .bufLock )
9861000 return mp , traceGlobProc , & trace .buf
@@ -1480,10 +1494,10 @@ func traceGCSweepStart() {
14801494 // Delay the actual GCSweepStart event until the first span
14811495 // sweep. If we don't sweep anything, don't emit any events.
14821496 pp := getg ().m .p .ptr ()
1483- if pp .traceSweep {
1497+ if pp .trace . inSweep {
14841498 throw ("double traceGCSweepStart" )
14851499 }
1486- pp .traceSweep , pp .traceSwept , pp .traceReclaimed = true , 0 , 0
1500+ pp .trace . inSweep , pp .trace . swept , pp .trace . reclaimed = true , 0 , 0
14871501}
14881502
14891503// traceGCSweepSpan traces the sweep of a single page.
@@ -1492,23 +1506,23 @@ func traceGCSweepStart() {
14921506// pair; however, it will not emit any trace events in this case.
14931507func traceGCSweepSpan (bytesSwept uintptr ) {
14941508 pp := getg ().m .p .ptr ()
1495- if pp .traceSweep {
1496- if pp .traceSwept == 0 {
1509+ if pp .trace . inSweep {
1510+ if pp .trace . swept == 0 {
14971511 traceEvent (traceEvGCSweepStart , 1 )
14981512 }
1499- pp .traceSwept += bytesSwept
1513+ pp .trace . swept += bytesSwept
15001514 }
15011515}
15021516
15031517func traceGCSweepDone () {
15041518 pp := getg ().m .p .ptr ()
1505- if ! pp .traceSweep {
1519+ if ! pp .trace . inSweep {
15061520 throw ("missing traceGCSweepStart" )
15071521 }
1508- if pp .traceSwept != 0 {
1509- traceEvent (traceEvGCSweepDone , - 1 , uint64 (pp .traceSwept ), uint64 (pp .traceReclaimed ))
1522+ if pp .trace . swept != 0 {
1523+ traceEvent (traceEvGCSweepDone , - 1 , uint64 (pp .trace . swept ), uint64 (pp .trace . reclaimed ))
15101524 }
1511- pp .traceSweep = false
1525+ pp .trace . inSweep = false
15121526}
15131527
15141528func traceGCMarkAssistStart () {
0 commit comments