Skip to content

Commit b7e767b

Browse files
mknyszekgopherbot
authored andcommitted
runtime: capture per-p trace state in a type
More tightening up of the tracer's interface. Change-Id: I992141c7f30e5c2d5d77d1fcd6817d35bc6e5f6d Reviewed-on: https://go-review.googlesource.com/c/go/+/494191 Auto-Submit: Michael Knyszek <[email protected]> Reviewed-by: Michael Pratt <[email protected]> Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 2f35a65 commit b7e767b

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

src/runtime/mgcsweep.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ func (sl *sweepLocked) sweep(preserve bool) bool {
652652
s.freeindex = 0 // reset allocation index to start of span.
653653
s.freeIndexForScan = 0
654654
if traceEnabled() {
655-
getg().m.p.ptr().traceReclaimed += uintptr(nfreed) * s.elemsize
655+
getg().m.p.ptr().trace.reclaimed += uintptr(nfreed) * s.elemsize
656656
}
657657

658658
// gcmarkBits becomes the allocBits.

src/runtime/runtime2.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -674,15 +674,7 @@ type p struct {
674674
buf [128]*mspan
675675
}
676676

677-
tracebuf traceBufPtr
678-
679-
// traceSweep indicates the sweep events should be traced.
680-
// This is used to defer the sweep start event until a span
681-
// has actually been swept.
682-
traceSweep bool
683-
// traceSwept and traceReclaimed track the number of bytes
684-
// swept and reclaimed by sweeping in the current sweep loop.
685-
traceSwept, traceReclaimed uintptr
677+
trace pTraceState
686678

687679
palloc persistentAlloc // per-P to avoid mutex
688680

src/runtime/trace.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
178192
func 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
652666
func 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.
14931507
func 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

15031517
func 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

15141528
func traceGCMarkAssistStart() {

0 commit comments

Comments
 (0)