2016-03-01 22:57:46 +00:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
2010-10-25 17:55:50 -07:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// Export guts for testing.
|
|
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
2015-11-02 14:09:24 -05:00
|
|
|
import (
|
|
|
|
|
"runtime/internal/atomic"
|
2015-11-11 12:39:30 -05:00
|
|
|
"runtime/internal/sys"
|
2015-11-02 14:09:24 -05:00
|
|
|
"unsafe"
|
|
|
|
|
)
|
2014-08-21 21:10:45 +04:00
|
|
|
|
2010-10-25 17:55:50 -07:00
|
|
|
var Fadd64 = fadd64
|
|
|
|
|
var Fsub64 = fsub64
|
|
|
|
|
var Fmul64 = fmul64
|
|
|
|
|
var Fdiv64 = fdiv64
|
|
|
|
|
var F64to32 = f64to32
|
|
|
|
|
var F32to64 = f32to64
|
|
|
|
|
var Fcmp64 = fcmp64
|
|
|
|
|
var Fintto64 = fintto64
|
|
|
|
|
var F64toint = f64toint
|
2011-07-19 11:01:17 -04:00
|
|
|
|
|
|
|
|
var Entersyscall = entersyscall
|
|
|
|
|
var Exitsyscall = exitsyscall
|
2014-08-27 23:32:49 -04:00
|
|
|
var LockedOSThread = lockedOSThread
|
2015-11-02 14:09:24 -05:00
|
|
|
var Xadduintptr = atomic.Xadduintptr
|
2012-04-12 11:49:25 +04:00
|
|
|
|
2015-04-17 17:27:07 -07:00
|
|
|
var FuncPC = funcPC
|
|
|
|
|
|
2015-09-14 14:03:45 -07:00
|
|
|
var Fastlog2 = fastlog2
|
|
|
|
|
|
2016-10-30 01:54:19 +02:00
|
|
|
var Atoi = atoi
|
|
|
|
|
var Atoi32 = atoi32
|
|
|
|
|
|
2019-03-29 10:43:31 -07:00
|
|
|
var Nanotime = nanotime
|
2019-04-05 15:53:12 -07:00
|
|
|
var NetpollBreak = netpollBreak
|
|
|
|
|
var Usleep = usleep
|
2019-03-29 10:43:31 -07:00
|
|
|
|
2019-08-21 00:24:25 +00:00
|
|
|
var PhysPageSize = physPageSize
|
2019-04-29 21:02:18 +00:00
|
|
|
var PhysHugePageSize = physHugePageSize
|
|
|
|
|
|
2019-10-29 15:35:42 +01:00
|
|
|
var NetpollGenericInit = netpollGenericInit
|
|
|
|
|
|
2019-12-02 17:32:01 -05:00
|
|
|
var ParseRelease = parseRelease
|
|
|
|
|
|
2019-04-25 14:10:29 -04:00
|
|
|
const PreemptMSupported = preemptMSupported
|
|
|
|
|
|
2012-04-12 11:49:25 +04:00
|
|
|
type LFNode struct {
|
2014-10-27 15:57:07 -04:00
|
|
|
Next uint64
|
2012-04-12 11:49:25 +04:00
|
|
|
Pushcnt uintptr
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-21 21:10:45 +04:00
|
|
|
func LFStackPush(head *uint64, node *LFNode) {
|
2017-03-07 16:38:29 -05:00
|
|
|
(*lfstack)(head).push((*lfnode)(unsafe.Pointer(node)))
|
2014-08-21 21:10:45 +04:00
|
|
|
}
|
2012-04-12 11:49:25 +04:00
|
|
|
|
2014-08-21 21:10:45 +04:00
|
|
|
func LFStackPop(head *uint64) *LFNode {
|
2017-03-07 16:38:29 -05:00
|
|
|
return (*LFNode)(unsafe.Pointer((*lfstack)(head).pop()))
|
2014-08-21 21:10:45 +04:00
|
|
|
}
|
2012-05-11 10:50:03 +04:00
|
|
|
|
2019-10-22 00:38:08 -07:00
|
|
|
func Netpoll(delta int64) {
|
|
|
|
|
systemstack(func() {
|
|
|
|
|
netpoll(delta)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-05 14:59:31 -07:00
|
|
|
func GCMask(x interface{}) (ret []byte) {
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 14:54:31 -05:00
|
|
|
systemstack(func() {
|
2015-04-28 00:28:47 -04:00
|
|
|
ret = getgcmask(x)
|
2014-09-05 14:59:31 -07:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2014-07-29 11:01:02 +04:00
|
|
|
|
2014-09-06 10:07:23 -07:00
|
|
|
func RunSchedLocalQueueTest() {
|
2016-03-12 16:41:08 -07:00
|
|
|
_p_ := new(p)
|
|
|
|
|
gs := make([]g, len(_p_.runq))
|
|
|
|
|
for i := 0; i < len(_p_.runq); i++ {
|
|
|
|
|
if g, _ := runqget(_p_); g != nil {
|
|
|
|
|
throw("runq is not empty initially")
|
|
|
|
|
}
|
|
|
|
|
for j := 0; j < i; j++ {
|
|
|
|
|
runqput(_p_, &gs[i], false)
|
|
|
|
|
}
|
|
|
|
|
for j := 0; j < i; j++ {
|
|
|
|
|
if g, _ := runqget(_p_); g != &gs[i] {
|
|
|
|
|
print("bad element at iter ", i, "/", j, "\n")
|
|
|
|
|
throw("bad element")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if g, _ := runqget(_p_); g != nil {
|
|
|
|
|
throw("runq is not empty afterwards")
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-06 10:07:23 -07:00
|
|
|
}
|
2016-03-12 16:41:08 -07:00
|
|
|
|
2014-09-06 10:07:23 -07:00
|
|
|
func RunSchedLocalQueueStealTest() {
|
2016-03-12 16:41:08 -07:00
|
|
|
p1 := new(p)
|
|
|
|
|
p2 := new(p)
|
|
|
|
|
gs := make([]g, len(p1.runq))
|
|
|
|
|
for i := 0; i < len(p1.runq); i++ {
|
|
|
|
|
for j := 0; j < i; j++ {
|
|
|
|
|
gs[j].sig = 0
|
|
|
|
|
runqput(p1, &gs[j], false)
|
|
|
|
|
}
|
|
|
|
|
gp := runqsteal(p2, p1, true)
|
|
|
|
|
s := 0
|
|
|
|
|
if gp != nil {
|
|
|
|
|
s++
|
|
|
|
|
gp.sig++
|
|
|
|
|
}
|
|
|
|
|
for {
|
|
|
|
|
gp, _ = runqget(p2)
|
|
|
|
|
if gp == nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
s++
|
|
|
|
|
gp.sig++
|
|
|
|
|
}
|
|
|
|
|
for {
|
|
|
|
|
gp, _ = runqget(p1)
|
|
|
|
|
if gp == nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
gp.sig++
|
|
|
|
|
}
|
|
|
|
|
for j := 0; j < i; j++ {
|
|
|
|
|
if gs[j].sig != 1 {
|
|
|
|
|
print("bad element ", j, "(", gs[j].sig, ") at iter ", i, "\n")
|
|
|
|
|
throw("bad element")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if s != i/2 && s != i/2+1 {
|
|
|
|
|
print("bad steal ", s, ", want ", i/2, " or ", i/2+1, ", iter ", i, "\n")
|
|
|
|
|
throw("bad steal")
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-06 10:07:23 -07:00
|
|
|
}
|
2013-09-06 16:23:46 -07:00
|
|
|
|
2016-03-18 16:34:11 +01:00
|
|
|
func RunSchedLocalQueueEmptyTest(iters int) {
|
|
|
|
|
// Test that runq is not spuriously reported as empty.
|
|
|
|
|
// Runq emptiness affects scheduling decisions and spurious emptiness
|
|
|
|
|
// can lead to underutilization (both runnable Gs and idle Ps coexist
|
|
|
|
|
// for arbitrary long time).
|
|
|
|
|
done := make(chan bool, 1)
|
|
|
|
|
p := new(p)
|
|
|
|
|
gs := make([]g, 2)
|
|
|
|
|
ready := new(uint32)
|
|
|
|
|
for i := 0; i < iters; i++ {
|
|
|
|
|
*ready = 0
|
|
|
|
|
next0 := (i & 1) == 0
|
|
|
|
|
next1 := (i & 2) == 0
|
|
|
|
|
runqput(p, &gs[0], next0)
|
|
|
|
|
go func() {
|
|
|
|
|
for atomic.Xadd(ready, 1); atomic.Load(ready) != 2; {
|
|
|
|
|
}
|
|
|
|
|
if runqempty(p) {
|
|
|
|
|
println("next:", next0, next1)
|
|
|
|
|
throw("queue is empty")
|
|
|
|
|
}
|
|
|
|
|
done <- true
|
|
|
|
|
}()
|
|
|
|
|
for atomic.Xadd(ready, 1); atomic.Load(ready) != 2; {
|
|
|
|
|
}
|
|
|
|
|
runqput(p, &gs[1], next1)
|
|
|
|
|
runqget(p)
|
|
|
|
|
<-done
|
|
|
|
|
runqget(p)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-27 14:05:11 +02:00
|
|
|
var (
|
|
|
|
|
StringHash = stringHash
|
|
|
|
|
BytesHash = bytesHash
|
|
|
|
|
Int32Hash = int32Hash
|
|
|
|
|
Int64Hash = int64Hash
|
|
|
|
|
MemHash = memhash
|
|
|
|
|
MemHash32 = memhash32
|
|
|
|
|
MemHash64 = memhash64
|
|
|
|
|
EfaceHash = efaceHash
|
|
|
|
|
IfaceHash = ifaceHash
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var UseAeshash = &useAeshash
|
2016-10-17 18:41:56 -04:00
|
|
|
|
|
|
|
|
func MemclrBytes(b []byte) {
|
|
|
|
|
s := (*slice)(unsafe.Pointer(&b))
|
|
|
|
|
memclrNoHeapPointers(s.array, uintptr(s.len))
|
|
|
|
|
}
|
2013-09-13 14:19:23 -04:00
|
|
|
|
2013-10-04 13:54:03 -07:00
|
|
|
var HashLoad = &hashLoad
|
2014-02-06 17:43:22 -08:00
|
|
|
|
2014-09-05 15:01:09 -07:00
|
|
|
// entry point for testing
|
|
|
|
|
func GostringW(w []uint16) (s string) {
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 14:54:31 -05:00
|
|
|
systemstack(func() {
|
2014-09-05 15:01:09 -07:00
|
|
|
s = gostringw(&w[0])
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2014-09-11 16:53:34 -07:00
|
|
|
|
2015-11-11 12:39:30 -05:00
|
|
|
type Uintreg sys.Uintreg
|
2015-03-02 20:16:48 -08:00
|
|
|
|
|
|
|
|
var Open = open
|
2015-04-13 19:37:04 -04:00
|
|
|
var Close = closefd
|
2015-03-02 20:16:48 -08:00
|
|
|
var Read = read
|
|
|
|
|
var Write = write
|
2015-03-03 13:55:22 -05:00
|
|
|
|
|
|
|
|
func Envs() []string { return envs }
|
|
|
|
|
func SetEnvs(e []string) { envs = e }
|
2015-04-16 14:32:18 -07:00
|
|
|
|
2015-11-11 12:39:30 -05:00
|
|
|
var BigEndian = sys.BigEndian
|
2015-05-02 22:59:35 -04:00
|
|
|
|
|
|
|
|
// For benchmarking.
|
|
|
|
|
|
|
|
|
|
func BenchSetType(n int, x interface{}) {
|
2015-10-21 12:12:25 -07:00
|
|
|
e := *efaceOf(&x)
|
2015-05-02 22:59:35 -04:00
|
|
|
t := e._type
|
|
|
|
|
var size uintptr
|
|
|
|
|
var p unsafe.Pointer
|
|
|
|
|
switch t.kind & kindMask {
|
2015-10-12 16:01:51 -07:00
|
|
|
case kindPtr:
|
2015-05-02 22:59:35 -04:00
|
|
|
t = (*ptrtype)(unsafe.Pointer(t)).elem
|
|
|
|
|
size = t.size
|
|
|
|
|
p = e.data
|
2015-10-12 16:01:51 -07:00
|
|
|
case kindSlice:
|
2015-05-02 22:59:35 -04:00
|
|
|
slice := *(*struct {
|
|
|
|
|
ptr unsafe.Pointer
|
|
|
|
|
len, cap uintptr
|
|
|
|
|
})(e.data)
|
|
|
|
|
t = (*slicetype)(unsafe.Pointer(t)).elem
|
|
|
|
|
size = t.size * slice.len
|
|
|
|
|
p = slice.ptr
|
|
|
|
|
}
|
|
|
|
|
allocSize := roundupsize(size)
|
|
|
|
|
systemstack(func() {
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
heapBitsSetType(uintptr(p), allocSize, size, t)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2015-05-15 14:23:23 -04:00
|
|
|
|
2015-11-11 12:39:30 -05:00
|
|
|
const PtrSize = sys.PtrSize
|
2015-07-30 00:46:42 -04:00
|
|
|
|
2015-08-05 11:35:28 -04:00
|
|
|
var ForceGCPeriod = &forcegcperiod
|
2016-01-14 16:43:40 -05:00
|
|
|
|
|
|
|
|
// SetTracebackEnv is like runtime/debug.SetTraceback, but it raises
|
|
|
|
|
// the "environment" traceback level, so later calls to
|
|
|
|
|
// debug.SetTraceback (e.g., from testing timeouts) can't lower it.
|
|
|
|
|
func SetTracebackEnv(level string) {
|
|
|
|
|
setTraceback(level)
|
|
|
|
|
traceback_env = traceback_cache
|
|
|
|
|
}
|
2016-03-17 13:28:04 -07:00
|
|
|
|
|
|
|
|
var ReadUnaligned32 = readUnaligned32
|
|
|
|
|
var ReadUnaligned64 = readUnaligned64
|
runtime: fix pagesInUse accounting
When we grow the heap, we create a temporary "in use" span for the
memory acquired from the OS and then free that span to link it into
the heap. Hence, we (1) increase pagesInUse when we make the temporary
span so that (2) freeing the span will correctly decrease it.
However, currently step (1) increases pagesInUse by the number of
pages requested from the heap, while step (2) decreases it by the
number of pages requested from the OS (the size of the temporary
span). These aren't necessarily the same, since we round up the number
of pages we request from the OS, so steps 1 and 2 don't necessarily
cancel out like they're supposed to. Over time, this can add up and
cause pagesInUse to underflow and wrap around to 2^64. The garbage
collector computes the sweep ratio from this, so if this happens, the
sweep ratio becomes effectively infinite, causing the first allocation
on each P in a sweep cycle to sweep the entire heap. This makes
sweeping effectively STW.
Fix this by increasing pagesInUse in step 1 by the number of pages
requested from the OS, so that the two steps correctly cancel out. We
add a test that checks that the running total matches the actual state
of the heap.
Fixes #15022. For 1.6.x.
Change-Id: Iefd9d6abe37d0d447cbdbdf9941662e4f18eeffc
Reviewed-on: https://go-review.googlesource.com/21280
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2016-03-29 12:28:24 -04:00
|
|
|
|
|
|
|
|
func CountPagesInUse() (pagesInUse, counted uintptr) {
|
|
|
|
|
stopTheWorld("CountPagesInUse")
|
|
|
|
|
|
|
|
|
|
pagesInUse = uintptr(mheap_.pagesInUse)
|
|
|
|
|
|
2016-10-04 15:51:31 -04:00
|
|
|
for _, s := range mheap_.allspans {
|
runtime: atomically set span state and use as publication barrier
When everything is working correctly, any pointer the garbage
collector encounters can only point into a fully initialized heap
span, since the span must have been initialized before that pointer
could escape the heap allocator and become visible to the GC.
However, in various cases, we try to be defensive against bad
pointers. In findObject, this is just a sanity check: we never expect
to find a bad pointer, but programming errors can lead to them. In
spanOfHeap, we don't necessarily trust the pointer and we're trying to
check if it really does point to the heap, though it should always
point to something. Conservative scanning takes this to a new level,
since it can only guess that a word may be a pointer and verify this.
In all of these cases, we have a problem that the span lookup and
check can race with span initialization, since the span becomes
visible to lookups before it's fully initialized.
Furthermore, we're about to start initializing the span without the
heap lock held, which is going to introduce races where accesses were
previously protected by the heap lock.
To address this, this CL makes accesses to mspan.state atomic, and
ensures that the span is fully initialized before setting the state to
mSpanInUse. All loads are now atomic, and in any case where we don't
trust the pointer, it first atomically loads the span state and checks
that it's mSpanInUse, after which it will have synchronized with span
initialization and can safely check the other span fields.
For #10958, #24543, but a good fix in general.
Change-Id: I518b7c63555b02064b98aa5f802c92b758fef853
Reviewed-on: https://go-review.googlesource.com/c/go/+/203286
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-23 11:25:38 -04:00
|
|
|
if s.state.get() == mSpanInUse {
|
runtime: fix pagesInUse accounting
When we grow the heap, we create a temporary "in use" span for the
memory acquired from the OS and then free that span to link it into
the heap. Hence, we (1) increase pagesInUse when we make the temporary
span so that (2) freeing the span will correctly decrease it.
However, currently step (1) increases pagesInUse by the number of
pages requested from the heap, while step (2) decreases it by the
number of pages requested from the OS (the size of the temporary
span). These aren't necessarily the same, since we round up the number
of pages we request from the OS, so steps 1 and 2 don't necessarily
cancel out like they're supposed to. Over time, this can add up and
cause pagesInUse to underflow and wrap around to 2^64. The garbage
collector computes the sweep ratio from this, so if this happens, the
sweep ratio becomes effectively infinite, causing the first allocation
on each P in a sweep cycle to sweep the entire heap. This makes
sweeping effectively STW.
Fix this by increasing pagesInUse in step 1 by the number of pages
requested from the OS, so that the two steps correctly cancel out. We
add a test that checks that the running total matches the actual state
of the heap.
Fixes #15022. For 1.6.x.
Change-Id: Iefd9d6abe37d0d447cbdbdf9941662e4f18eeffc
Reviewed-on: https://go-review.googlesource.com/21280
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2016-03-29 12:28:24 -04:00
|
|
|
counted += s.npages
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startTheWorld()
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-01-05 09:36:27 +03:00
|
|
|
|
2017-02-13 12:46:17 -08:00
|
|
|
func Fastrand() uint32 { return fastrand() }
|
|
|
|
|
func Fastrandn(n uint32) uint32 { return fastrandn(n) }
|
2017-02-09 13:58:48 -05:00
|
|
|
|
|
|
|
|
type ProfBuf profBuf
|
|
|
|
|
|
|
|
|
|
func NewProfBuf(hdrsize, bufwords, tags int) *ProfBuf {
|
|
|
|
|
return (*ProfBuf)(newProfBuf(hdrsize, bufwords, tags))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *ProfBuf) Write(tag *unsafe.Pointer, now int64, hdr []uint64, stk []uintptr) {
|
|
|
|
|
(*profBuf)(p).write(tag, now, hdr, stk)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
ProfBufBlocking = profBufBlocking
|
|
|
|
|
ProfBufNonBlocking = profBufNonBlocking
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (p *ProfBuf) Read(mode profBufReadMode) ([]uint64, []unsafe.Pointer, bool) {
|
|
|
|
|
return (*profBuf)(p).read(profBufReadMode(mode))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *ProfBuf) Close() {
|
|
|
|
|
(*profBuf)(p).close()
|
|
|
|
|
}
|
2017-01-03 10:15:55 -07:00
|
|
|
|
|
|
|
|
// ReadMemStatsSlow returns both the runtime-computed MemStats and
|
|
|
|
|
// MemStats accumulated by scanning the heap.
|
|
|
|
|
func ReadMemStatsSlow() (base, slow MemStats) {
|
|
|
|
|
stopTheWorld("ReadMemStatsSlow")
|
|
|
|
|
|
|
|
|
|
// Run on the system stack to avoid stack growth allocation.
|
|
|
|
|
systemstack(func() {
|
|
|
|
|
// Make sure stats don't change.
|
|
|
|
|
getg().m.mallocing++
|
|
|
|
|
|
|
|
|
|
readmemstats_m(&base)
|
|
|
|
|
|
|
|
|
|
// Initialize slow from base and zero the fields we're
|
|
|
|
|
// recomputing.
|
|
|
|
|
slow = base
|
|
|
|
|
slow.Alloc = 0
|
|
|
|
|
slow.TotalAlloc = 0
|
|
|
|
|
slow.Mallocs = 0
|
|
|
|
|
slow.Frees = 0
|
2018-10-15 23:00:58 +00:00
|
|
|
slow.HeapReleased = 0
|
2017-01-03 10:15:55 -07:00
|
|
|
var bySize [_NumSizeClasses]struct {
|
|
|
|
|
Mallocs, Frees uint64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add up current allocations in spans.
|
|
|
|
|
for _, s := range mheap_.allspans {
|
runtime: atomically set span state and use as publication barrier
When everything is working correctly, any pointer the garbage
collector encounters can only point into a fully initialized heap
span, since the span must have been initialized before that pointer
could escape the heap allocator and become visible to the GC.
However, in various cases, we try to be defensive against bad
pointers. In findObject, this is just a sanity check: we never expect
to find a bad pointer, but programming errors can lead to them. In
spanOfHeap, we don't necessarily trust the pointer and we're trying to
check if it really does point to the heap, though it should always
point to something. Conservative scanning takes this to a new level,
since it can only guess that a word may be a pointer and verify this.
In all of these cases, we have a problem that the span lookup and
check can race with span initialization, since the span becomes
visible to lookups before it's fully initialized.
Furthermore, we're about to start initializing the span without the
heap lock held, which is going to introduce races where accesses were
previously protected by the heap lock.
To address this, this CL makes accesses to mspan.state atomic, and
ensures that the span is fully initialized before setting the state to
mSpanInUse. All loads are now atomic, and in any case where we don't
trust the pointer, it first atomically loads the span state and checks
that it's mSpanInUse, after which it will have synchronized with span
initialization and can safely check the other span fields.
For #10958, #24543, but a good fix in general.
Change-Id: I518b7c63555b02064b98aa5f802c92b758fef853
Reviewed-on: https://go-review.googlesource.com/c/go/+/203286
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-23 11:25:38 -04:00
|
|
|
if s.state.get() != mSpanInUse {
|
2017-01-03 10:15:55 -07:00
|
|
|
continue
|
|
|
|
|
}
|
2016-02-09 17:53:07 -05:00
|
|
|
if sizeclass := s.spanclass.sizeclass(); sizeclass == 0 {
|
2017-01-03 10:15:55 -07:00
|
|
|
slow.Mallocs++
|
|
|
|
|
slow.Alloc += uint64(s.elemsize)
|
|
|
|
|
} else {
|
|
|
|
|
slow.Mallocs += uint64(s.allocCount)
|
|
|
|
|
slow.Alloc += uint64(s.allocCount) * uint64(s.elemsize)
|
2016-02-09 17:53:07 -05:00
|
|
|
bySize[sizeclass].Mallocs += uint64(s.allocCount)
|
2017-01-03 10:15:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add in frees. readmemstats_m flushed the cached stats, so
|
|
|
|
|
// these are up-to-date.
|
|
|
|
|
var smallFree uint64
|
|
|
|
|
slow.Frees = mheap_.nlargefree
|
|
|
|
|
for i := range mheap_.nsmallfree {
|
|
|
|
|
slow.Frees += mheap_.nsmallfree[i]
|
|
|
|
|
bySize[i].Frees = mheap_.nsmallfree[i]
|
|
|
|
|
bySize[i].Mallocs += mheap_.nsmallfree[i]
|
|
|
|
|
smallFree += mheap_.nsmallfree[i] * uint64(class_to_size[i])
|
|
|
|
|
}
|
|
|
|
|
slow.Frees += memstats.tinyallocs
|
|
|
|
|
slow.Mallocs += slow.Frees
|
|
|
|
|
|
|
|
|
|
slow.TotalAlloc = slow.Alloc + mheap_.largefree + smallFree
|
|
|
|
|
|
|
|
|
|
for i := range slow.BySize {
|
|
|
|
|
slow.BySize[i].Mallocs = bySize[i].Mallocs
|
|
|
|
|
slow.BySize[i].Frees = bySize[i].Frees
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 16:12:10 +00:00
|
|
|
for i := mheap_.pages.start; i < mheap_.pages.end; i++ {
|
2019-11-14 23:58:50 +00:00
|
|
|
pg := mheap_.pages.chunkOf(i).scavenged.popcntRange(0, pallocChunkPages)
|
2019-09-04 16:12:10 +00:00
|
|
|
slow.HeapReleased += uint64(pg) * pageSize
|
2018-11-26 23:56:35 +00:00
|
|
|
}
|
2019-09-16 21:23:24 +00:00
|
|
|
for _, p := range allp {
|
2019-11-08 16:11:29 -05:00
|
|
|
pg := sys.OnesCount64(p.pcache.scav)
|
2019-09-16 21:23:24 +00:00
|
|
|
slow.HeapReleased += uint64(pg) * pageSize
|
|
|
|
|
}
|
2018-10-15 23:00:58 +00:00
|
|
|
|
runtime: grow the heap incrementally
Currently, we map and grow the heap a whole arena (64MB) at a time.
Unfortunately, in order to fix #32828, we need to switch from
scavenging inline with allocation back to scavenging on heap growth,
but heap-growth scavenging happens in large jumps because we grow the
heap in large jumps.
In order to prepare for better heap-growth scavenging, this CL
separates mapping more space for the heap from actually "growing" it
(tracking the new space with spans). Instead, growing the heap keeps
track of the "current arena" it's growing into. It track that with new
spans as needed, and only maps more arena space when the current arena
is inadequate. The effect to the user is the same, but this will let
us scavenge on much smaller increments of heap growth.
There are two slightly subtleties to this change:
1. If an allocation requires mapping a new arena and that new arena
isn't contiguous with the current arena, we don't want to lose the
unused space in the current arena, so we have to immediately track
that with a span.
2. The mapped space must be accounted as released and idle, even
though it isn't actually tracked in a span.
For #32828, since this makes heap-growth scavenging far more
effective, especially at small heap sizes. For example, this change is
necessary for TestPhysicalMemoryUtilization to pass once we remove
inline scavenging.
Change-Id: I300e74a0534062467e4ce91cdc3508e5ef9aa73a
Reviewed-on: https://go-review.googlesource.com/c/go/+/189957
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-08-12 14:54:28 -04:00
|
|
|
// Unused space in the current arena also counts as released space.
|
|
|
|
|
slow.HeapReleased += uint64(mheap_.curArena.end - mheap_.curArena.base)
|
|
|
|
|
|
2017-01-03 10:15:55 -07:00
|
|
|
getg().m.mallocing--
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
startTheWorld()
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-03-10 10:59:39 -05:00
|
|
|
|
|
|
|
|
// BlockOnSystemStack switches to the system stack, prints "x\n" to
|
|
|
|
|
// stderr, and blocks in a stack containing
|
|
|
|
|
// "runtime.blockOnSystemStackInternal".
|
|
|
|
|
func BlockOnSystemStack() {
|
|
|
|
|
systemstack(blockOnSystemStackInternal)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func blockOnSystemStackInternal() {
|
|
|
|
|
print("x\n")
|
|
|
|
|
lock(&deadlock)
|
|
|
|
|
lock(&deadlock)
|
|
|
|
|
}
|
2017-06-15 16:42:08 -07:00
|
|
|
|
|
|
|
|
type RWMutex struct {
|
|
|
|
|
rw rwmutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rw *RWMutex) RLock() {
|
|
|
|
|
rw.rw.rlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rw *RWMutex) RUnlock() {
|
|
|
|
|
rw.rw.runlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rw *RWMutex) Lock() {
|
|
|
|
|
rw.rw.lock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rw *RWMutex) Unlock() {
|
|
|
|
|
rw.rw.unlock()
|
|
|
|
|
}
|
2017-09-01 12:32:38 -07:00
|
|
|
|
2018-01-29 21:40:57 +01:00
|
|
|
const RuntimeHmapSize = unsafe.Sizeof(hmap{})
|
|
|
|
|
|
2017-09-02 18:46:59 +02:00
|
|
|
func MapBucketsCount(m map[int]int) int {
|
2017-09-01 12:32:38 -07:00
|
|
|
h := *(**hmap)(unsafe.Pointer(&m))
|
|
|
|
|
return 1 << h.B
|
|
|
|
|
}
|
2017-06-14 11:46:35 -04:00
|
|
|
|
2017-09-02 18:46:59 +02:00
|
|
|
func MapBucketsPointerIsNil(m map[int]int) bool {
|
|
|
|
|
h := *(**hmap)(unsafe.Pointer(&m))
|
|
|
|
|
return h.buckets == nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-14 11:46:35 -04:00
|
|
|
func LockOSCounts() (external, internal uint32) {
|
|
|
|
|
g := getg()
|
|
|
|
|
if g.m.lockedExt+g.m.lockedInt == 0 {
|
|
|
|
|
if g.lockedm != 0 {
|
|
|
|
|
panic("lockedm on non-locked goroutine")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if g.lockedm == 0 {
|
|
|
|
|
panic("nil lockedm on locked goroutine")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return g.m.lockedExt, g.m.lockedInt
|
|
|
|
|
}
|
2017-10-27 15:20:21 -04:00
|
|
|
|
|
|
|
|
//go:noinline
|
|
|
|
|
func TracebackSystemstack(stk []uintptr, i int) int {
|
|
|
|
|
if i == 0 {
|
2018-04-26 14:06:08 -04:00
|
|
|
pc, sp := getcallerpc(), getcallersp()
|
2017-10-27 15:20:21 -04:00
|
|
|
return gentraceback(pc, sp, 0, getg(), 0, &stk[0], len(stk), nil, nil, _TraceJumpStack)
|
|
|
|
|
}
|
|
|
|
|
n := 0
|
|
|
|
|
systemstack(func() {
|
|
|
|
|
n = TracebackSystemstack(stk, i-1)
|
|
|
|
|
})
|
|
|
|
|
return n
|
|
|
|
|
}
|
runtime: use sparse mappings for the heap
This replaces the contiguous heap arena mapping with a potentially
sparse mapping that can support heap mappings anywhere in the address
space.
This has several advantages over the current approach:
* There is no longer any limit on the size of the Go heap. (Currently
it's limited to 512GB.) Hence, this fixes #10460.
* It eliminates many failures modes of heap initialization and
growing. In particular it eliminates any possibility of panicking
with an address space conflict. This can happen for many reasons and
even causes a low but steady rate of TSAN test failures because of
conflicts with the TSAN runtime. See #16936 and #11993.
* It eliminates the notion of "non-reserved" heap, which was added
because creating huge address space reservations (particularly on
64-bit) led to huge process VSIZE. This was at best confusing and at
worst conflicted badly with ulimit -v. However, the non-reserved
heap logic is complicated, can race with other mappings in non-pure
Go binaries (e.g., #18976), and requires that the entire heap be
either reserved or non-reserved. We currently maintain the latter
property, but it's quite difficult to convince yourself of that, and
hence difficult to keep correct. This logic is still present, but
will be removed in the next CL.
* It fixes problems on 32-bit where skipping over parts of the address
space leads to mapping huge (and never-to-be-used) metadata
structures. See #19831.
This also completely rewrites and significantly simplifies
mheap.sysAlloc, which has been a source of many bugs. E.g., #21044,
#20259, #18651, and #13143 (and maybe #23222).
This change also makes it possible to allocate individual objects
larger than 512GB. As a result, a few tests that expected huge
allocations to fail needed to be changed to make even larger
allocations. However, at the moment attempting to allocate a humongous
object may cause the program to freeze for several minutes on Linux as
we fall back to probing every page with addrspace_free. That logic
(and this failure mode) will be removed in the next CL.
Fixes #10460.
Fixes #22204 (since it rewrites the code involved).
This slightly slows down compilebench and the x/benchmarks garbage
benchmark.
name old time/op new time/op delta
Template 184ms ± 1% 185ms ± 1% ~ (p=0.065 n=10+9)
Unicode 86.9ms ± 3% 86.3ms ± 1% ~ (p=0.631 n=10+10)
GoTypes 599ms ± 0% 602ms ± 0% +0.56% (p=0.000 n=10+9)
Compiler 2.87s ± 1% 2.89s ± 1% +0.51% (p=0.002 n=9+10)
SSA 7.29s ± 1% 7.25s ± 1% ~ (p=0.182 n=10+9)
Flate 118ms ± 2% 118ms ± 1% ~ (p=0.113 n=9+9)
GoParser 147ms ± 1% 148ms ± 1% +1.07% (p=0.003 n=9+10)
Reflect 401ms ± 1% 404ms ± 1% +0.71% (p=0.003 n=10+9)
Tar 175ms ± 1% 175ms ± 1% ~ (p=0.604 n=9+10)
XML 209ms ± 1% 210ms ± 1% ~ (p=0.052 n=10+10)
(https://perf.golang.org/search?q=upload:20171231.4)
name old time/op new time/op delta
Garbage/benchmem-MB=64-12 2.23ms ± 1% 2.25ms ± 1% +0.84% (p=0.000 n=19+19)
(https://perf.golang.org/search?q=upload:20171231.3)
Relative to the start of the sparse heap changes (starting at and
including "runtime: fix various contiguous bitmap assumptions"),
overall slowdown is roughly 1% on GC-intensive benchmarks:
name old time/op new time/op delta
Template 183ms ± 1% 185ms ± 1% +1.32% (p=0.000 n=9+9)
Unicode 84.9ms ± 2% 86.3ms ± 1% +1.65% (p=0.000 n=9+10)
GoTypes 595ms ± 1% 602ms ± 0% +1.19% (p=0.000 n=9+9)
Compiler 2.86s ± 0% 2.89s ± 1% +0.91% (p=0.000 n=9+10)
SSA 7.19s ± 0% 7.25s ± 1% +0.75% (p=0.000 n=8+9)
Flate 117ms ± 1% 118ms ± 1% +1.10% (p=0.000 n=10+9)
GoParser 146ms ± 2% 148ms ± 1% +1.48% (p=0.002 n=10+10)
Reflect 398ms ± 1% 404ms ± 1% +1.51% (p=0.000 n=10+9)
Tar 173ms ± 1% 175ms ± 1% +1.17% (p=0.000 n=10+10)
XML 208ms ± 1% 210ms ± 1% +0.62% (p=0.011 n=10+10)
[Geo mean] 369ms 373ms +1.17%
(https://perf.golang.org/search?q=upload:20180101.2)
name old time/op new time/op delta
Garbage/benchmem-MB=64-12 2.22ms ± 1% 2.25ms ± 1% +1.51% (p=0.000 n=20+19)
(https://perf.golang.org/search?q=upload:20180101.3)
Change-Id: I5daf4cfec24b252e5a57001f0a6c03f22479d0f0
Reviewed-on: https://go-review.googlesource.com/85887
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2017-12-19 22:05:23 -08:00
|
|
|
|
|
|
|
|
func KeepNArenaHints(n int) {
|
|
|
|
|
hint := mheap_.arenaHints
|
|
|
|
|
for i := 1; i < n; i++ {
|
|
|
|
|
hint = hint.next
|
|
|
|
|
if hint == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
hint.next = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MapNextArenaHint reserves a page at the next arena growth hint,
|
|
|
|
|
// preventing the arena from growing there, and returns the range of
|
|
|
|
|
// addresses that are no longer viable.
|
|
|
|
|
func MapNextArenaHint() (start, end uintptr) {
|
|
|
|
|
hint := mheap_.arenaHints
|
|
|
|
|
addr := hint.addr
|
|
|
|
|
if hint.down {
|
|
|
|
|
start, end = addr-heapArenaBytes, addr
|
|
|
|
|
addr -= physPageSize
|
|
|
|
|
} else {
|
|
|
|
|
start, end = addr, addr+heapArenaBytes
|
|
|
|
|
}
|
runtime: remove non-reserved heap logic
Currently large sysReserve calls on some OSes don't actually reserve
the memory, but just check that it can be reserved. This was important
when we called sysReserve to "reserve" many gigabytes for the heap up
front, but now that we map memory in small increments as we need it,
this complication is no longer necessary.
This has one curious side benefit: currently, on Linux, allocations
that are large enough to be rejected by mmap wind up freezing the
application for a long time before it panics. This happens because
sysReserve doesn't reserve the memory, so sysMap calls mmap_fixed,
which calls mmap, which fails because the mapping is too large.
However, mmap_fixed doesn't inspect *why* mmap fails, so it falls back
to probing every page in the desired region individually with mincore
before performing an (otherwise dangerous) MAP_FIXED mapping, which
will also fail. This takes a long time for a large region. Now this
logic is gone, so the mmap failure leads to an immediate panic.
Updates #10460.
Change-Id: I8efe88c611871cdb14f99fadd09db83e0161ca2e
Reviewed-on: https://go-review.googlesource.com/85888
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2017-12-30 19:35:46 -05:00
|
|
|
sysReserve(unsafe.Pointer(addr), physPageSize)
|
runtime: use sparse mappings for the heap
This replaces the contiguous heap arena mapping with a potentially
sparse mapping that can support heap mappings anywhere in the address
space.
This has several advantages over the current approach:
* There is no longer any limit on the size of the Go heap. (Currently
it's limited to 512GB.) Hence, this fixes #10460.
* It eliminates many failures modes of heap initialization and
growing. In particular it eliminates any possibility of panicking
with an address space conflict. This can happen for many reasons and
even causes a low but steady rate of TSAN test failures because of
conflicts with the TSAN runtime. See #16936 and #11993.
* It eliminates the notion of "non-reserved" heap, which was added
because creating huge address space reservations (particularly on
64-bit) led to huge process VSIZE. This was at best confusing and at
worst conflicted badly with ulimit -v. However, the non-reserved
heap logic is complicated, can race with other mappings in non-pure
Go binaries (e.g., #18976), and requires that the entire heap be
either reserved or non-reserved. We currently maintain the latter
property, but it's quite difficult to convince yourself of that, and
hence difficult to keep correct. This logic is still present, but
will be removed in the next CL.
* It fixes problems on 32-bit where skipping over parts of the address
space leads to mapping huge (and never-to-be-used) metadata
structures. See #19831.
This also completely rewrites and significantly simplifies
mheap.sysAlloc, which has been a source of many bugs. E.g., #21044,
#20259, #18651, and #13143 (and maybe #23222).
This change also makes it possible to allocate individual objects
larger than 512GB. As a result, a few tests that expected huge
allocations to fail needed to be changed to make even larger
allocations. However, at the moment attempting to allocate a humongous
object may cause the program to freeze for several minutes on Linux as
we fall back to probing every page with addrspace_free. That logic
(and this failure mode) will be removed in the next CL.
Fixes #10460.
Fixes #22204 (since it rewrites the code involved).
This slightly slows down compilebench and the x/benchmarks garbage
benchmark.
name old time/op new time/op delta
Template 184ms ± 1% 185ms ± 1% ~ (p=0.065 n=10+9)
Unicode 86.9ms ± 3% 86.3ms ± 1% ~ (p=0.631 n=10+10)
GoTypes 599ms ± 0% 602ms ± 0% +0.56% (p=0.000 n=10+9)
Compiler 2.87s ± 1% 2.89s ± 1% +0.51% (p=0.002 n=9+10)
SSA 7.29s ± 1% 7.25s ± 1% ~ (p=0.182 n=10+9)
Flate 118ms ± 2% 118ms ± 1% ~ (p=0.113 n=9+9)
GoParser 147ms ± 1% 148ms ± 1% +1.07% (p=0.003 n=9+10)
Reflect 401ms ± 1% 404ms ± 1% +0.71% (p=0.003 n=10+9)
Tar 175ms ± 1% 175ms ± 1% ~ (p=0.604 n=9+10)
XML 209ms ± 1% 210ms ± 1% ~ (p=0.052 n=10+10)
(https://perf.golang.org/search?q=upload:20171231.4)
name old time/op new time/op delta
Garbage/benchmem-MB=64-12 2.23ms ± 1% 2.25ms ± 1% +0.84% (p=0.000 n=19+19)
(https://perf.golang.org/search?q=upload:20171231.3)
Relative to the start of the sparse heap changes (starting at and
including "runtime: fix various contiguous bitmap assumptions"),
overall slowdown is roughly 1% on GC-intensive benchmarks:
name old time/op new time/op delta
Template 183ms ± 1% 185ms ± 1% +1.32% (p=0.000 n=9+9)
Unicode 84.9ms ± 2% 86.3ms ± 1% +1.65% (p=0.000 n=9+10)
GoTypes 595ms ± 1% 602ms ± 0% +1.19% (p=0.000 n=9+9)
Compiler 2.86s ± 0% 2.89s ± 1% +0.91% (p=0.000 n=9+10)
SSA 7.19s ± 0% 7.25s ± 1% +0.75% (p=0.000 n=8+9)
Flate 117ms ± 1% 118ms ± 1% +1.10% (p=0.000 n=10+9)
GoParser 146ms ± 2% 148ms ± 1% +1.48% (p=0.002 n=10+10)
Reflect 398ms ± 1% 404ms ± 1% +1.51% (p=0.000 n=10+9)
Tar 173ms ± 1% 175ms ± 1% +1.17% (p=0.000 n=10+10)
XML 208ms ± 1% 210ms ± 1% +0.62% (p=0.011 n=10+10)
[Geo mean] 369ms 373ms +1.17%
(https://perf.golang.org/search?q=upload:20180101.2)
name old time/op new time/op delta
Garbage/benchmem-MB=64-12 2.22ms ± 1% 2.25ms ± 1% +1.51% (p=0.000 n=20+19)
(https://perf.golang.org/search?q=upload:20180101.3)
Change-Id: I5daf4cfec24b252e5a57001f0a6c03f22479d0f0
Reviewed-on: https://go-review.googlesource.com/85887
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2017-12-19 22:05:23 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetNextArenaHint() uintptr {
|
|
|
|
|
return mheap_.arenaHints.addr
|
|
|
|
|
}
|
2018-03-09 08:24:10 -08:00
|
|
|
|
|
|
|
|
type G = g
|
2018-04-26 21:43:19 -04:00
|
|
|
|
|
|
|
|
func Getg() *G {
|
|
|
|
|
return getg()
|
|
|
|
|
}
|
2018-06-28 16:45:28 -07:00
|
|
|
|
|
|
|
|
//go:noinline
|
|
|
|
|
func PanicForTesting(b []byte, i int) byte {
|
|
|
|
|
return unexportedPanicForTesting(b, i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//go:noinline
|
|
|
|
|
func unexportedPanicForTesting(b []byte, i int) byte {
|
|
|
|
|
return b[i]
|
|
|
|
|
}
|
2018-06-25 18:00:43 -04:00
|
|
|
|
|
|
|
|
func G0StackOverflow() {
|
|
|
|
|
systemstack(func() {
|
|
|
|
|
stackOverflow(nil)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func stackOverflow(x *byte) {
|
|
|
|
|
var buf [256]byte
|
|
|
|
|
stackOverflow(&buf[0])
|
|
|
|
|
}
|
2018-10-15 17:24:21 -07:00
|
|
|
|
|
|
|
|
func MapTombstoneCheck(m map[int]int) {
|
|
|
|
|
// Make sure emptyOne and emptyRest are distributed correctly.
|
|
|
|
|
// We should have a series of filled and emptyOne cells, followed by
|
|
|
|
|
// a series of emptyRest cells.
|
|
|
|
|
h := *(**hmap)(unsafe.Pointer(&m))
|
|
|
|
|
i := interface{}(m)
|
|
|
|
|
t := *(**maptype)(unsafe.Pointer(&i))
|
|
|
|
|
|
|
|
|
|
for x := 0; x < 1<<h.B; x++ {
|
|
|
|
|
b0 := (*bmap)(add(h.buckets, uintptr(x)*uintptr(t.bucketsize)))
|
|
|
|
|
n := 0
|
|
|
|
|
for b := b0; b != nil; b = b.overflow(t) {
|
|
|
|
|
for i := 0; i < bucketCnt; i++ {
|
|
|
|
|
if b.tophash[i] != emptyRest {
|
|
|
|
|
n++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
k := 0
|
|
|
|
|
for b := b0; b != nil; b = b.overflow(t) {
|
|
|
|
|
for i := 0; i < bucketCnt; i++ {
|
|
|
|
|
if k < n && b.tophash[i] == emptyRest {
|
|
|
|
|
panic("early emptyRest")
|
|
|
|
|
}
|
|
|
|
|
if k >= n && b.tophash[i] != emptyRest {
|
|
|
|
|
panic("late non-emptyRest")
|
|
|
|
|
}
|
|
|
|
|
if k == n-1 && b.tophash[i] == emptyOne {
|
|
|
|
|
panic("last non-emptyRest entry is emptyOne")
|
|
|
|
|
}
|
|
|
|
|
k++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-09 00:13:37 +00:00
|
|
|
|
2019-07-16 11:33:10 -04:00
|
|
|
func RunGetgThreadSwitchTest() {
|
|
|
|
|
// Test that getg works correctly with thread switch.
|
|
|
|
|
// With gccgo, if we generate getg inlined, the backend
|
|
|
|
|
// may cache the address of the TLS variable, which
|
|
|
|
|
// will become invalid after a thread switch. This test
|
|
|
|
|
// checks that the bad caching doesn't happen.
|
|
|
|
|
|
|
|
|
|
ch := make(chan int)
|
|
|
|
|
go func(ch chan int) {
|
|
|
|
|
ch <- 5
|
|
|
|
|
LockOSThread()
|
|
|
|
|
}(ch)
|
|
|
|
|
|
|
|
|
|
g1 := getg()
|
|
|
|
|
|
|
|
|
|
// Block on a receive. This is likely to get us a thread
|
|
|
|
|
// switch. If we yield to the sender goroutine, it will
|
|
|
|
|
// lock the thread, forcing us to resume on a different
|
|
|
|
|
// thread.
|
|
|
|
|
<-ch
|
|
|
|
|
|
|
|
|
|
g2 := getg()
|
|
|
|
|
if g1 != g2 {
|
|
|
|
|
panic("g1 != g2")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Also test getg after some control flow, as the
|
|
|
|
|
// backend is sensitive to control flow.
|
|
|
|
|
g3 := getg()
|
|
|
|
|
if g1 != g3 {
|
|
|
|
|
panic("g1 != g3")
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-12 19:08:39 +00:00
|
|
|
|
|
|
|
|
const (
|
2019-08-21 00:24:25 +00:00
|
|
|
PageSize = pageSize
|
2019-08-12 19:08:39 +00:00
|
|
|
PallocChunkPages = pallocChunkPages
|
2019-11-18 19:23:39 +00:00
|
|
|
PageAlloc64Bit = pageAlloc64Bit
|
2019-08-12 19:08:39 +00:00
|
|
|
)
|
|
|
|
|
|
2019-09-25 15:55:29 +00:00
|
|
|
// Expose pallocSum for testing.
|
|
|
|
|
type PallocSum pallocSum
|
|
|
|
|
|
|
|
|
|
func PackPallocSum(start, max, end uint) PallocSum { return PallocSum(packPallocSum(start, max, end)) }
|
|
|
|
|
func (m PallocSum) Start() uint { return pallocSum(m).start() }
|
|
|
|
|
func (m PallocSum) Max() uint { return pallocSum(m).max() }
|
|
|
|
|
func (m PallocSum) End() uint { return pallocSum(m).end() }
|
|
|
|
|
|
2019-08-12 19:08:39 +00:00
|
|
|
// Expose pallocBits for testing.
|
|
|
|
|
type PallocBits pallocBits
|
|
|
|
|
|
|
|
|
|
func (b *PallocBits) Find(npages uintptr, searchIdx uint) (uint, uint) {
|
|
|
|
|
return (*pallocBits)(b).find(npages, searchIdx)
|
|
|
|
|
}
|
2019-09-10 18:53:51 +00:00
|
|
|
func (b *PallocBits) AllocRange(i, n uint) { (*pallocBits)(b).allocRange(i, n) }
|
|
|
|
|
func (b *PallocBits) Free(i, n uint) { (*pallocBits)(b).free(i, n) }
|
|
|
|
|
func (b *PallocBits) Summarize() PallocSum { return PallocSum((*pallocBits)(b).summarize()) }
|
|
|
|
|
func (b *PallocBits) PopcntRange(i, n uint) uint { return (*pageBits)(b).popcntRange(i, n) }
|
2019-09-25 15:55:29 +00:00
|
|
|
|
|
|
|
|
// SummarizeSlow is a slow but more obviously correct implementation
|
|
|
|
|
// of (*pallocBits).summarize. Used for testing.
|
|
|
|
|
func SummarizeSlow(b *PallocBits) PallocSum {
|
|
|
|
|
var start, max, end uint
|
|
|
|
|
|
|
|
|
|
const N = uint(len(b)) * 64
|
|
|
|
|
for start < N && (*pageBits)(b).get(start) == 0 {
|
|
|
|
|
start++
|
|
|
|
|
}
|
|
|
|
|
for end < N && (*pageBits)(b).get(N-end-1) == 0 {
|
|
|
|
|
end++
|
|
|
|
|
}
|
|
|
|
|
run := uint(0)
|
|
|
|
|
for i := uint(0); i < N; i++ {
|
|
|
|
|
if (*pageBits)(b).get(i) == 0 {
|
|
|
|
|
run++
|
|
|
|
|
} else {
|
|
|
|
|
run = 0
|
|
|
|
|
}
|
|
|
|
|
if run > max {
|
|
|
|
|
max = run
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return PackPallocSum(start, max, end)
|
|
|
|
|
}
|
2019-08-12 19:08:39 +00:00
|
|
|
|
|
|
|
|
// Expose non-trivial helpers for testing.
|
|
|
|
|
func FindBitRange64(c uint64, n uint) uint { return findBitRange64(c, n) }
|
|
|
|
|
|
|
|
|
|
// Given two PallocBits, returns a set of bit ranges where
|
|
|
|
|
// they differ.
|
|
|
|
|
func DiffPallocBits(a, b *PallocBits) []BitRange {
|
|
|
|
|
ba := (*pageBits)(a)
|
|
|
|
|
bb := (*pageBits)(b)
|
|
|
|
|
|
|
|
|
|
var d []BitRange
|
|
|
|
|
base, size := uint(0), uint(0)
|
|
|
|
|
for i := uint(0); i < uint(len(ba))*64; i++ {
|
|
|
|
|
if ba.get(i) != bb.get(i) {
|
|
|
|
|
if size == 0 {
|
|
|
|
|
base = i
|
|
|
|
|
}
|
|
|
|
|
size++
|
|
|
|
|
} else {
|
|
|
|
|
if size != 0 {
|
|
|
|
|
d = append(d, BitRange{base, size})
|
|
|
|
|
}
|
|
|
|
|
size = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if size != 0 {
|
|
|
|
|
d = append(d, BitRange{base, size})
|
|
|
|
|
}
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StringifyPallocBits gets the bits in the bit range r from b,
|
|
|
|
|
// and returns a string containing the bits as ASCII 0 and 1
|
|
|
|
|
// characters.
|
|
|
|
|
func StringifyPallocBits(b *PallocBits, r BitRange) string {
|
|
|
|
|
str := ""
|
|
|
|
|
for j := r.I; j < r.I+r.N; j++ {
|
|
|
|
|
if (*pageBits)(b).get(j) != 0 {
|
|
|
|
|
str += "1"
|
|
|
|
|
} else {
|
|
|
|
|
str += "0"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 00:24:25 +00:00
|
|
|
// Expose pallocData for testing.
|
|
|
|
|
type PallocData pallocData
|
|
|
|
|
|
|
|
|
|
func (d *PallocData) FindScavengeCandidate(searchIdx uint, min, max uintptr) (uint, uint) {
|
|
|
|
|
return (*pallocData)(d).findScavengeCandidate(searchIdx, min, max)
|
|
|
|
|
}
|
|
|
|
|
func (d *PallocData) AllocRange(i, n uint) { (*pallocData)(d).allocRange(i, n) }
|
|
|
|
|
func (d *PallocData) ScavengedSetRange(i, n uint) {
|
|
|
|
|
(*pallocData)(d).scavenged.setRange(i, n)
|
|
|
|
|
}
|
|
|
|
|
func (d *PallocData) PallocBits() *PallocBits {
|
|
|
|
|
return (*PallocBits)(&(*pallocData)(d).pallocBits)
|
|
|
|
|
}
|
|
|
|
|
func (d *PallocData) Scavenged() *PallocBits {
|
|
|
|
|
return (*PallocBits)(&(*pallocData)(d).scavenged)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Expose fillAligned for testing.
|
|
|
|
|
func FillAligned(x uint64, m uint) uint64 { return fillAligned(x, m) }
|
|
|
|
|
|
2019-09-18 17:51:16 +00:00
|
|
|
// Expose pageCache for testing.
|
|
|
|
|
type PageCache pageCache
|
|
|
|
|
|
|
|
|
|
const PageCachePages = pageCachePages
|
|
|
|
|
|
|
|
|
|
func NewPageCache(base uintptr, cache, scav uint64) PageCache {
|
|
|
|
|
return PageCache(pageCache{base: base, cache: cache, scav: scav})
|
|
|
|
|
}
|
|
|
|
|
func (c *PageCache) Empty() bool { return (*pageCache)(c).empty() }
|
|
|
|
|
func (c *PageCache) Base() uintptr { return (*pageCache)(c).base }
|
|
|
|
|
func (c *PageCache) Cache() uint64 { return (*pageCache)(c).cache }
|
|
|
|
|
func (c *PageCache) Scav() uint64 { return (*pageCache)(c).scav }
|
|
|
|
|
func (c *PageCache) Alloc(npages uintptr) (uintptr, uintptr) {
|
|
|
|
|
return (*pageCache)(c).alloc(npages)
|
|
|
|
|
}
|
|
|
|
|
func (c *PageCache) Flush(s *PageAlloc) {
|
|
|
|
|
(*pageCache)(c).flush((*pageAlloc)(s))
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 16:32:12 +00:00
|
|
|
// Expose chunk index type.
|
|
|
|
|
type ChunkIdx chunkIdx
|
|
|
|
|
|
|
|
|
|
// Expose pageAlloc for testing. Note that because pageAlloc is
|
|
|
|
|
// not in the heap, so is PageAlloc.
|
|
|
|
|
type PageAlloc pageAlloc
|
|
|
|
|
|
2019-09-10 18:53:51 +00:00
|
|
|
func (p *PageAlloc) Alloc(npages uintptr) (uintptr, uintptr) {
|
|
|
|
|
return (*pageAlloc)(p).alloc(npages)
|
|
|
|
|
}
|
2019-09-18 17:51:16 +00:00
|
|
|
func (p *PageAlloc) AllocToCache() PageCache {
|
|
|
|
|
return PageCache((*pageAlloc)(p).allocToCache())
|
|
|
|
|
}
|
2019-09-10 18:53:51 +00:00
|
|
|
func (p *PageAlloc) Free(base, npages uintptr) {
|
|
|
|
|
(*pageAlloc)(p).free(base, npages)
|
|
|
|
|
}
|
2019-08-14 16:32:12 +00:00
|
|
|
func (p *PageAlloc) Bounds() (ChunkIdx, ChunkIdx) {
|
|
|
|
|
return ChunkIdx((*pageAlloc)(p).start), ChunkIdx((*pageAlloc)(p).end)
|
|
|
|
|
}
|
2019-09-12 18:24:56 +00:00
|
|
|
func (p *PageAlloc) Scavenge(nbytes uintptr, locked bool) (r uintptr) {
|
2019-08-21 00:24:25 +00:00
|
|
|
systemstack(func() {
|
2019-09-12 18:24:56 +00:00
|
|
|
r = (*pageAlloc)(p).scavenge(nbytes, locked)
|
2019-08-21 00:24:25 +00:00
|
|
|
})
|
|
|
|
|
return
|
2019-08-14 16:32:12 +00:00
|
|
|
}
|
2019-11-15 23:30:30 +00:00
|
|
|
func (p *PageAlloc) InUse() []AddrRange {
|
|
|
|
|
ranges := make([]AddrRange, 0, len(p.inUse.ranges))
|
|
|
|
|
for _, r := range p.inUse.ranges {
|
|
|
|
|
ranges = append(ranges, AddrRange{
|
|
|
|
|
Base: r.base,
|
|
|
|
|
Limit: r.limit,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return ranges
|
|
|
|
|
}
|
2019-08-14 16:32:12 +00:00
|
|
|
|
2019-11-14 23:58:50 +00:00
|
|
|
// Returns nil if the PallocData's L2 is missing.
|
|
|
|
|
func (p *PageAlloc) PallocData(i ChunkIdx) *PallocData {
|
|
|
|
|
ci := chunkIdx(i)
|
|
|
|
|
l2 := (*pageAlloc)(p).chunks[ci.l1()]
|
|
|
|
|
if l2 == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return (*PallocData)(&l2[ci.l2()])
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 23:30:30 +00:00
|
|
|
// AddrRange represents a range over addresses.
|
|
|
|
|
// Specifically, it represents the range [Base, Limit).
|
|
|
|
|
type AddrRange struct {
|
|
|
|
|
Base, Limit uintptr
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 19:08:39 +00:00
|
|
|
// BitRange represents a range over a bitmap.
|
|
|
|
|
type BitRange struct {
|
|
|
|
|
I, N uint // bit index and length in bits
|
|
|
|
|
}
|
2019-08-14 16:32:12 +00:00
|
|
|
|
|
|
|
|
// NewPageAlloc creates a new page allocator for testing and
|
2019-08-21 00:24:25 +00:00
|
|
|
// initializes it with the scav and chunks maps. Each key in these maps
|
|
|
|
|
// represents a chunk index and each value is a series of bit ranges to
|
|
|
|
|
// set within each bitmap's chunk.
|
|
|
|
|
//
|
|
|
|
|
// The initialization of the pageAlloc preserves the invariant that if a
|
|
|
|
|
// scavenged bit is set the alloc bit is necessarily unset, so some
|
|
|
|
|
// of the bits described by scav may be cleared in the final bitmap if
|
|
|
|
|
// ranges in chunks overlap with them.
|
|
|
|
|
//
|
|
|
|
|
// scav is optional, and if nil, the scavenged bitmap will be cleared
|
|
|
|
|
// (as opposed to all 1s, which it usually is). Furthermore, every
|
|
|
|
|
// chunk index in scav must appear in chunks; ones that do not are
|
|
|
|
|
// ignored.
|
|
|
|
|
func NewPageAlloc(chunks, scav map[ChunkIdx][]BitRange) *PageAlloc {
|
2019-08-14 16:32:12 +00:00
|
|
|
p := new(pageAlloc)
|
|
|
|
|
|
|
|
|
|
// We've got an entry, so initialize the pageAlloc.
|
|
|
|
|
p.init(new(mutex), nil)
|
2019-08-21 00:24:25 +00:00
|
|
|
p.test = true
|
2019-08-14 16:32:12 +00:00
|
|
|
|
|
|
|
|
for i, init := range chunks {
|
|
|
|
|
addr := chunkBase(chunkIdx(i))
|
|
|
|
|
|
|
|
|
|
// Mark the chunk's existence in the pageAlloc.
|
|
|
|
|
p.grow(addr, pallocChunkBytes)
|
|
|
|
|
|
|
|
|
|
// Initialize the bitmap and update pageAlloc metadata.
|
2019-11-14 23:58:50 +00:00
|
|
|
chunk := p.chunkOf(chunkIndex(addr))
|
2019-08-21 00:24:25 +00:00
|
|
|
|
|
|
|
|
// Clear all the scavenged bits which grow set.
|
|
|
|
|
chunk.scavenged.clearRange(0, pallocChunkPages)
|
|
|
|
|
|
|
|
|
|
// Apply scavenge state if applicable.
|
|
|
|
|
if scav != nil {
|
|
|
|
|
if scvg, ok := scav[i]; ok {
|
|
|
|
|
for _, s := range scvg {
|
|
|
|
|
// Ignore the case of s.N == 0. setRange doesn't handle
|
|
|
|
|
// it and it's a no-op anyway.
|
|
|
|
|
if s.N != 0 {
|
|
|
|
|
chunk.scavenged.setRange(s.I, s.N)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
p.resetScavengeAddr()
|
|
|
|
|
|
|
|
|
|
// Apply alloc state.
|
2019-08-14 16:32:12 +00:00
|
|
|
for _, s := range init {
|
|
|
|
|
// Ignore the case of s.N == 0. allocRange doesn't handle
|
|
|
|
|
// it and it's a no-op anyway.
|
|
|
|
|
if s.N != 0 {
|
|
|
|
|
chunk.allocRange(s.I, s.N)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update heap metadata for the allocRange calls above.
|
|
|
|
|
p.update(addr, pallocChunkPages, false, false)
|
|
|
|
|
}
|
|
|
|
|
return (*PageAlloc)(p)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FreePageAlloc releases hard OS resources owned by the pageAlloc. Once this
|
|
|
|
|
// is called the pageAlloc may no longer be used. The object itself will be
|
|
|
|
|
// collected by the garbage collector once it is no longer live.
|
|
|
|
|
func FreePageAlloc(pp *PageAlloc) {
|
|
|
|
|
p := (*pageAlloc)(pp)
|
|
|
|
|
|
|
|
|
|
// Free all the mapped space for the summary levels.
|
|
|
|
|
if pageAlloc64Bit != 0 {
|
|
|
|
|
for l := 0; l < summaryLevels; l++ {
|
|
|
|
|
sysFree(unsafe.Pointer(&p.summary[l][0]), uintptr(cap(p.summary[l]))*pallocSumBytes, nil)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
resSize := uintptr(0)
|
|
|
|
|
for _, s := range p.summary {
|
|
|
|
|
resSize += uintptr(cap(s)) * pallocSumBytes
|
|
|
|
|
}
|
|
|
|
|
sysFree(unsafe.Pointer(&p.summary[0][0]), alignUp(resSize, physPageSize), nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Free the mapped space for chunks.
|
2019-11-14 23:58:50 +00:00
|
|
|
for i := range p.chunks {
|
|
|
|
|
if x := p.chunks[i]; x != nil {
|
|
|
|
|
p.chunks[i] = nil
|
|
|
|
|
// This memory comes from sysAlloc and will always be page-aligned.
|
|
|
|
|
sysFree(unsafe.Pointer(x), unsafe.Sizeof(*p.chunks[0]), nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-14 16:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BaseChunkIdx is a convenient chunkIdx value which works on both
|
|
|
|
|
// 64 bit and 32 bit platforms, allowing the tests to share code
|
|
|
|
|
// between the two.
|
2019-11-07 22:42:38 +00:00
|
|
|
//
|
2019-11-13 13:46:42 +01:00
|
|
|
// On AIX, the arenaBaseOffset is 0x0a00000000000000. However, this
|
|
|
|
|
// constant can't be used here because it is negative and will cause
|
|
|
|
|
// a constant overflow.
|
|
|
|
|
//
|
2019-11-07 22:42:38 +00:00
|
|
|
// This should not be higher than 0x100*pallocChunkBytes to support
|
|
|
|
|
// mips and mipsle, which only have 31-bit address spaces.
|
2019-11-13 13:46:42 +01:00
|
|
|
var BaseChunkIdx = ChunkIdx(chunkIndex(((0xc000*pageAlloc64Bit + 0x100*pageAlloc32Bit) * pallocChunkBytes) + 0x0a00000000000000*sys.GoosAix))
|
2019-08-14 16:32:12 +00:00
|
|
|
|
|
|
|
|
// PageBase returns an address given a chunk index and a page index
|
|
|
|
|
// relative to that chunk.
|
|
|
|
|
func PageBase(c ChunkIdx, pageIdx uint) uintptr {
|
|
|
|
|
return chunkBase(chunkIdx(c)) + uintptr(pageIdx)*pageSize
|
|
|
|
|
}
|
2019-10-17 17:42:15 +00:00
|
|
|
|
|
|
|
|
type BitsMismatch struct {
|
|
|
|
|
Base uintptr
|
|
|
|
|
Got, Want uint64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CheckScavengedBitsCleared(mismatches []BitsMismatch) (n int, ok bool) {
|
|
|
|
|
ok = true
|
|
|
|
|
|
|
|
|
|
// Run on the system stack to avoid stack growth allocation.
|
|
|
|
|
systemstack(func() {
|
|
|
|
|
getg().m.mallocing++
|
|
|
|
|
|
|
|
|
|
// Lock so that we can safely access the bitmap.
|
|
|
|
|
lock(&mheap_.lock)
|
|
|
|
|
chunkLoop:
|
|
|
|
|
for i := mheap_.pages.start; i < mheap_.pages.end; i++ {
|
2019-11-14 23:58:50 +00:00
|
|
|
chunk := mheap_.pages.chunkOf(i)
|
2019-10-17 17:42:15 +00:00
|
|
|
for j := 0; j < pallocChunkPages/64; j++ {
|
|
|
|
|
// Run over each 64-bit bitmap section and ensure
|
|
|
|
|
// scavenged is being cleared properly on allocation.
|
|
|
|
|
// If a used bit and scavenged bit are both set, that's
|
|
|
|
|
// an error, and could indicate a larger problem, or
|
|
|
|
|
// an accounting problem.
|
|
|
|
|
want := chunk.scavenged[j] &^ chunk.pallocBits[j]
|
|
|
|
|
got := chunk.scavenged[j]
|
|
|
|
|
if want != got {
|
|
|
|
|
ok = false
|
|
|
|
|
if n >= len(mismatches) {
|
|
|
|
|
break chunkLoop
|
|
|
|
|
}
|
|
|
|
|
mismatches[n] = BitsMismatch{
|
|
|
|
|
Base: chunkBase(i) + uintptr(j)*64*pageSize,
|
|
|
|
|
Got: got,
|
|
|
|
|
Want: want,
|
|
|
|
|
}
|
|
|
|
|
n++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unlock(&mheap_.lock)
|
|
|
|
|
|
|
|
|
|
getg().m.mallocing--
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-09-16 21:23:24 +00:00
|
|
|
|
|
|
|
|
func PageCachePagesLeaked() (leaked uintptr) {
|
|
|
|
|
stopTheWorld("PageCachePagesLeaked")
|
|
|
|
|
|
|
|
|
|
// Walk over destroyed Ps and look for unflushed caches.
|
|
|
|
|
deadp := allp[len(allp):cap(allp)]
|
|
|
|
|
for _, p := range deadp {
|
|
|
|
|
// Since we're going past len(allp) we may see nil Ps.
|
|
|
|
|
// Just ignore them.
|
|
|
|
|
if p != nil {
|
2019-11-08 16:11:29 -05:00
|
|
|
leaked += uintptr(sys.OnesCount64(p.pcache.cache))
|
2019-09-16 21:23:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startTheWorld()
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-11-08 10:30:24 -08:00
|
|
|
|
|
|
|
|
var Semacquire = semacquire
|
|
|
|
|
var Semrelease1 = semrelease1
|
|
|
|
|
|
|
|
|
|
func SemNwait(addr *uint32) uint32 {
|
|
|
|
|
root := semroot(addr)
|
|
|
|
|
return atomic.Load(&root.nwait)
|
|
|
|
|
}
|