Skip to content

Commit e508a5f

Browse files
committed
runtime: de-duplicate span scavenging
Currently, span scavenging was done nearly identically in two different locations. This change deduplicates that into one shared routine. For #14045. Change-Id: I15006b2c9af0e70b7a9eae9abb4168d3adca3860 Reviewed-on: https://go-review.googlesource.com/c/139297 Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent de31f63 commit e508a5f

File tree

1 file changed

+32
-52
lines changed

1 file changed

+32
-52
lines changed

src/runtime/mheap.go

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,34 @@ func (s *mspan) layout() (size, n, total uintptr) {
351351
return
352352
}
353353

354+
func (s *mspan) scavenge() uintptr {
355+
start := s.base()
356+
end := start + s.npages<<_PageShift
357+
if physPageSize > _PageSize {
358+
// We can only release pages in
359+
// physPageSize blocks, so round start
360+
// and end in. (Otherwise, madvise
361+
// will round them *out* and release
362+
// more memory than we want.)
363+
start = (start + physPageSize - 1) &^ (physPageSize - 1)
364+
end &^= physPageSize - 1
365+
if end <= start {
366+
// start and end don't span a
367+
// whole physical page.
368+
return 0
369+
}
370+
}
371+
len := end - start
372+
released := len - (s.npreleased << _PageShift)
373+
if physPageSize > _PageSize && released == 0 {
374+
return 0
375+
}
376+
memstats.heap_released += uint64(released)
377+
s.npreleased = len >> _PageShift
378+
sysUnused(unsafe.Pointer(start), len)
379+
return released
380+
}
381+
354382
// recordspan adds a newly allocated span to h.allspans.
355383
//
356384
// This only happens the first time a span is allocated from
@@ -1087,35 +1115,12 @@ func (h *mheap) busyList(npages uintptr) *mSpanList {
10871115

10881116
func scavengeTreapNode(t *treapNode, now, limit uint64) uintptr {
10891117
s := t.spanKey
1090-
var sumreleased uintptr
10911118
if (now-uint64(s.unusedsince)) > limit && s.npreleased != s.npages {
1092-
start := s.base()
1093-
end := start + s.npages<<_PageShift
1094-
if physPageSize > _PageSize {
1095-
// We can only release pages in
1096-
// physPageSize blocks, so round start
1097-
// and end in. (Otherwise, madvise
1098-
// will round them *out* and release
1099-
// more memory than we want.)
1100-
start = (start + physPageSize - 1) &^ (physPageSize - 1)
1101-
end &^= physPageSize - 1
1102-
if end <= start {
1103-
// start and end don't span a
1104-
// whole physical page.
1105-
return sumreleased
1106-
}
1119+
if released := s.scavenge(); released != 0 {
1120+
return released
11071121
}
1108-
len := end - start
1109-
released := len - (s.npreleased << _PageShift)
1110-
if physPageSize > _PageSize && released == 0 {
1111-
return sumreleased
1112-
}
1113-
memstats.heap_released += uint64(released)
1114-
sumreleased += released
1115-
s.npreleased = len >> _PageShift
1116-
sysUnused(unsafe.Pointer(start), len)
11171122
}
1118-
return sumreleased
1123+
return 0
11191124
}
11201125

11211126
func scavengelist(list *mSpanList, now, limit uint64) uintptr {
@@ -1128,32 +1133,7 @@ func scavengelist(list *mSpanList, now, limit uint64) uintptr {
11281133
if (now-uint64(s.unusedsince)) <= limit || s.npreleased == s.npages {
11291134
continue
11301135
}
1131-
start := s.base()
1132-
end := start + s.npages<<_PageShift
1133-
if physPageSize > _PageSize {
1134-
// We can only release pages in
1135-
// physPageSize blocks, so round start
1136-
// and end in. (Otherwise, madvise
1137-
// will round them *out* and release
1138-
// more memory than we want.)
1139-
start = (start + physPageSize - 1) &^ (physPageSize - 1)
1140-
end &^= physPageSize - 1
1141-
if end <= start {
1142-
// start and end don't span a
1143-
// whole physical page.
1144-
continue
1145-
}
1146-
}
1147-
len := end - start
1148-
1149-
released := len - (s.npreleased << _PageShift)
1150-
if physPageSize > _PageSize && released == 0 {
1151-
continue
1152-
}
1153-
memstats.heap_released += uint64(released)
1154-
sumreleased += released
1155-
s.npreleased = len >> _PageShift
1156-
sysUnused(unsafe.Pointer(start), len)
1136+
sumreleased += s.scavenge()
11571137
}
11581138
return sumreleased
11591139
}

0 commit comments

Comments
 (0)