Commit 31457cef authored by Russ Cox's avatar Russ Cox

all: merge dev.garbage (d1238958d4ae) into default branch

When we start work on Gerrit, ppc64 and garbage collection
work will continue in the master branch, not the dev branches.

(We may still use dev branches for other things later, but
these are ready to be merged, and doing it now, before moving
to Git means we don't have to have dev branches working
in the Gerrit workflow on day one.)

TBR=rlh
CC=golang-codereviews
https://golang.org/cl/183140043
parents 09d92b6b db406241
...@@ -2024,13 +2024,13 @@ def submit(ui, repo, *pats, **opts): ...@@ -2024,13 +2024,13 @@ def submit(ui, repo, *pats, **opts):
# push to remote; if it fails for any reason, roll back # push to remote; if it fails for any reason, roll back
try: try:
new_heads = len(hg_heads(ui, repo).split()) new_heads = len(hg_heads(ui, repo).split())
if old_heads != new_heads and not (old_heads == 0 and new_heads == 1): if cl.desc.find("create new branch") < 0 and old_heads != new_heads and not (old_heads == 0 and new_heads == 1):
# Created new head, so we weren't up to date. # Created new head, so we weren't up to date.
need_sync() need_sync()
# Push changes to remote. If it works, we're committed. If not, roll back. # Push changes to remote. If it works, we're committed. If not, roll back.
try: try:
if hg_push(ui, repo): if hg_push(ui, repo, new_branch=cl.desc.find("create new branch")>=0):
raise hg_util.Abort("push error") raise hg_util.Abort("push error")
except hg_error.Abort, e: except hg_error.Abort, e:
if e.message.find("push creates new heads") >= 0: if e.message.find("push creates new heads") >= 0:
......
...@@ -24,6 +24,8 @@ char *runtimeimport = ...@@ -24,6 +24,8 @@ char *runtimeimport =
"func @\"\".printslice (? any)\n" "func @\"\".printslice (? any)\n"
"func @\"\".printnl ()\n" "func @\"\".printnl ()\n"
"func @\"\".printsp ()\n" "func @\"\".printsp ()\n"
"func @\"\".printlock ()\n"
"func @\"\".printunlock ()\n"
"func @\"\".concatstring2 (? string, ? string) (? string)\n" "func @\"\".concatstring2 (? string, ? string) (? string)\n"
"func @\"\".concatstring3 (? string, ? string, ? string) (? string)\n" "func @\"\".concatstring3 (? string, ? string, ? string) (? string)\n"
"func @\"\".concatstring4 (? string, ? string, ? string, ? string) (? string)\n" "func @\"\".concatstring4 (? string, ? string, ? string, ? string) (? string)\n"
...@@ -86,10 +88,33 @@ char *runtimeimport = ...@@ -86,10 +88,33 @@ char *runtimeimport =
"func @\"\".writebarrierstring (@\"\".dst·1 *any, @\"\".src·2 any)\n" "func @\"\".writebarrierstring (@\"\".dst·1 *any, @\"\".src·2 any)\n"
"func @\"\".writebarrierslice (@\"\".dst·1 *any, @\"\".src·2 any)\n" "func @\"\".writebarrierslice (@\"\".dst·1 *any, @\"\".src·2 any)\n"
"func @\"\".writebarrieriface (@\"\".dst·1 *any, @\"\".src·2 any)\n" "func @\"\".writebarrieriface (@\"\".dst·1 *any, @\"\".src·2 any)\n"
"func @\"\".writebarrierfat2 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n" "func @\"\".writebarrierfat01 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat3 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n" "func @\"\".writebarrierfat10 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat4 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n" "func @\"\".writebarrierfat11 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat001 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat010 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat011 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat100 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat101 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat110 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat111 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat0001 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat0010 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat0011 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat0100 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat0101 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat0110 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat0111 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat1000 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat1001 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat1010 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat1011 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat1100 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat1101 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat1110 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat1111 (@\"\".dst·1 *any, _ *byte, @\"\".src·3 any)\n"
"func @\"\".writebarrierfat (@\"\".typ·1 *byte, @\"\".dst·2 *any, @\"\".src·3 *any)\n" "func @\"\".writebarrierfat (@\"\".typ·1 *byte, @\"\".dst·2 *any, @\"\".src·3 *any)\n"
"func @\"\".writebarriercopy (@\"\".typ·2 *byte, @\"\".dst·3 any, @\"\".src·4 any) (? int)\n"
"func @\"\".selectnbsend (@\"\".chanType·2 *byte, @\"\".hchan·3 chan<- any, @\"\".elem·4 *any) (? bool)\n" "func @\"\".selectnbsend (@\"\".chanType·2 *byte, @\"\".hchan·3 chan<- any, @\"\".elem·4 *any) (? bool)\n"
"func @\"\".selectnbrecv (@\"\".chanType·2 *byte, @\"\".elem·3 *any, @\"\".hchan·4 <-chan any) (? bool)\n" "func @\"\".selectnbrecv (@\"\".chanType·2 *byte, @\"\".elem·3 *any, @\"\".hchan·4 <-chan any) (? bool)\n"
"func @\"\".selectnbrecv2 (@\"\".chanType·2 *byte, @\"\".elem·3 *any, @\"\".received·4 *bool, @\"\".hchan·5 <-chan any) (? bool)\n" "func @\"\".selectnbrecv2 (@\"\".chanType·2 *byte, @\"\".elem·3 *any, @\"\".received·4 *bool, @\"\".hchan·5 <-chan any) (? bool)\n"
......
...@@ -1473,6 +1473,7 @@ void walk(Node *fn); ...@@ -1473,6 +1473,7 @@ void walk(Node *fn);
void walkexpr(Node **np, NodeList **init); void walkexpr(Node **np, NodeList **init);
void walkexprlist(NodeList *l, NodeList **init); void walkexprlist(NodeList *l, NodeList **init);
void walkexprlistsafe(NodeList *l, NodeList **init); void walkexprlistsafe(NodeList *l, NodeList **init);
void walkexprlistcheap(NodeList *l, NodeList **init);
void walkstmt(Node **np); void walkstmt(Node **np);
void walkstmtlist(NodeList *l); void walkstmtlist(NodeList *l);
Node* conv(Node*, Type*); Node* conv(Node*, Type*);
......
...@@ -36,6 +36,8 @@ func printeface(any) ...@@ -36,6 +36,8 @@ func printeface(any)
func printslice(any) func printslice(any)
func printnl() func printnl()
func printsp() func printsp()
func printlock()
func printunlock()
func concatstring2(string, string) string func concatstring2(string, string) string
func concatstring3(string, string, string) string func concatstring3(string, string, string) string
...@@ -115,10 +117,35 @@ func writebarrieriface(dst *any, src any) ...@@ -115,10 +117,35 @@ func writebarrieriface(dst *any, src any)
// The unused *byte argument makes sure that src is 2-pointer-aligned, // The unused *byte argument makes sure that src is 2-pointer-aligned,
// which is the maximum alignment on NaCl amd64p32 // which is the maximum alignment on NaCl amd64p32
// (and possibly on 32-bit systems if we start 64-bit aligning uint64s). // (and possibly on 32-bit systems if we start 64-bit aligning uint64s).
func writebarrierfat2(dst *any, _ *byte, src any) // The bitmap in the name tells which words being copied are pointers.
func writebarrierfat3(dst *any, _ *byte, src any) func writebarrierfat01(dst *any, _ *byte, src any)
func writebarrierfat4(dst *any, _ *byte, src any) func writebarrierfat10(dst *any, _ *byte, src any)
func writebarrierfat11(dst *any, _ *byte, src any)
func writebarrierfat001(dst *any, _ *byte, src any)
func writebarrierfat010(dst *any, _ *byte, src any)
func writebarrierfat011(dst *any, _ *byte, src any)
func writebarrierfat100(dst *any, _ *byte, src any)
func writebarrierfat101(dst *any, _ *byte, src any)
func writebarrierfat110(dst *any, _ *byte, src any)
func writebarrierfat111(dst *any, _ *byte, src any)
func writebarrierfat0001(dst *any, _ *byte, src any)
func writebarrierfat0010(dst *any, _ *byte, src any)
func writebarrierfat0011(dst *any, _ *byte, src any)
func writebarrierfat0100(dst *any, _ *byte, src any)
func writebarrierfat0101(dst *any, _ *byte, src any)
func writebarrierfat0110(dst *any, _ *byte, src any)
func writebarrierfat0111(dst *any, _ *byte, src any)
func writebarrierfat1000(dst *any, _ *byte, src any)
func writebarrierfat1001(dst *any, _ *byte, src any)
func writebarrierfat1010(dst *any, _ *byte, src any)
func writebarrierfat1011(dst *any, _ *byte, src any)
func writebarrierfat1100(dst *any, _ *byte, src any)
func writebarrierfat1101(dst *any, _ *byte, src any)
func writebarrierfat1110(dst *any, _ *byte, src any)
func writebarrierfat1111(dst *any, _ *byte, src any)
func writebarrierfat(typ *byte, dst *any, src *any) func writebarrierfat(typ *byte, dst *any, src *any)
func writebarriercopy(typ *byte, dst any, src any) int
func selectnbsend(chanType *byte, hchan chan<- any, elem *any) bool func selectnbsend(chanType *byte, hchan chan<- any, elem *any) bool
func selectnbrecv(chanType *byte, elem *any, hchan <-chan any) bool func selectnbrecv(chanType *byte, elem *any, hchan <-chan any) bool
......
...@@ -2891,7 +2891,8 @@ typecheckas(Node *n) ...@@ -2891,7 +2891,8 @@ typecheckas(Node *n)
case OSLICE3: case OSLICE3:
case OSLICESTR: case OSLICESTR:
// For x = x[0:y], x can be updated in place, without touching pointer. // For x = x[0:y], x can be updated in place, without touching pointer.
if(samesafeexpr(n->left, n->right->left) && (n->right->right->left == N || iszero(n->right->right->left))) // TODO(rsc): Reenable once it is actually updated in place without touching the pointer.
if(0 && samesafeexpr(n->left, n->right->left) && (n->right->right->left == N || iszero(n->right->right->left)))
n->right->reslice = 1; n->right->reslice = 1;
break; break;
...@@ -2899,7 +2900,8 @@ typecheckas(Node *n) ...@@ -2899,7 +2900,8 @@ typecheckas(Node *n)
// For x = append(x, ...), x can be updated in place when there is capacity, // For x = append(x, ...), x can be updated in place when there is capacity,
// without touching the pointer; otherwise the emitted code to growslice // without touching the pointer; otherwise the emitted code to growslice
// can take care of updating the pointer, and only in that case. // can take care of updating the pointer, and only in that case.
if(n->right->list != nil && samesafeexpr(n->left, n->right->list->n)) // TODO(rsc): Reenable once the emitted code does update the pointer.
if(0 && n->right->list != nil && samesafeexpr(n->left, n->right->list->n))
n->right->reslice = 1; n->right->reslice = 1;
break; break;
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <libc.h> #include <libc.h>
#include "go.h" #include "go.h"
#include "../ld/textflag.h" #include "../ld/textflag.h"
#include "../../runtime/mgc0.h"
static Node* walkprint(Node*, NodeList**); static Node* walkprint(Node*, NodeList**);
static Node* writebarrierfn(char*, Type*, Type*); static Node* writebarrierfn(char*, Type*, Type*);
...@@ -362,6 +363,15 @@ walkexprlistsafe(NodeList *l, NodeList **init) ...@@ -362,6 +363,15 @@ walkexprlistsafe(NodeList *l, NodeList **init)
} }
} }
void
walkexprlistcheap(NodeList *l, NodeList **init)
{
for(; l; l=l->next) {
l->n = cheapexpr(l->n, init);
walkexpr(&l->n, init);
}
}
void void
walkexpr(Node **np, NodeList **init) walkexpr(Node **np, NodeList **init)
{ {
...@@ -1771,6 +1781,11 @@ walkprint(Node *nn, NodeList **init) ...@@ -1771,6 +1781,11 @@ walkprint(Node *nn, NodeList **init)
calls = nil; calls = nil;
notfirst = 0; notfirst = 0;
// Hoist all the argument evaluation up before the lock.
walkexprlistcheap(all, init);
calls = list(calls, mkcall("printlock", T, init));
for(l=all; l; l=l->next) { for(l=all; l; l=l->next) {
if(notfirst) { if(notfirst) {
calls = list(calls, mkcall("printsp", T, init)); calls = list(calls, mkcall("printsp", T, init));
...@@ -1851,6 +1866,9 @@ walkprint(Node *nn, NodeList **init) ...@@ -1851,6 +1866,9 @@ walkprint(Node *nn, NodeList **init)
if(op == OPRINTN) if(op == OPRINTN)
calls = list(calls, mkcall("printnl", T, nil)); calls = list(calls, mkcall("printnl", T, nil));
calls = list(calls, mkcall("printunlock", T, init));
typechecklist(calls, Etop); typechecklist(calls, Etop);
walkexprlist(calls, init); walkexprlist(calls, init);
...@@ -1987,6 +2005,9 @@ applywritebarrier(Node *n, NodeList **init) ...@@ -1987,6 +2005,9 @@ applywritebarrier(Node *n, NodeList **init)
{ {
Node *l, *r; Node *l, *r;
Type *t; Type *t;
vlong x;
static Bvec *bv;
char name[32];
if(n->left && n->right && needwritebarrier(n->left, n->right)) { if(n->left && n->right && needwritebarrier(n->left, n->right)) {
t = n->left->type; t = n->left->type;
...@@ -2004,14 +2025,35 @@ applywritebarrier(Node *n, NodeList **init) ...@@ -2004,14 +2025,35 @@ applywritebarrier(Node *n, NodeList **init)
} else if(isinter(t)) { } else if(isinter(t)) {
n = mkcall1(writebarrierfn("writebarrieriface", t, n->right->type), T, init, n = mkcall1(writebarrierfn("writebarrieriface", t, n->right->type), T, init,
l, n->right); l, n->right);
} else if(t->width == 2*widthptr) { } else if(t->width <= 4*widthptr) {
n = mkcall1(writebarrierfn("writebarrierfat2", t, n->right->type), T, init, x = 0;
l, nodnil(), n->right); if(bv == nil)
} else if(t->width == 3*widthptr) { bv = bvalloc(BitsPerPointer*4);
n = mkcall1(writebarrierfn("writebarrierfat3", t, n->right->type), T, init, bvresetall(bv);
l, nodnil(), n->right); twobitwalktype1(t, &x, bv);
} else if(t->width == 4*widthptr) { // The bvgets are looking for BitsPointer in successive slots.
n = mkcall1(writebarrierfn("writebarrierfat4", t, n->right->type), T, init, enum {
PtrBit = 1,
};
if(BitsPointer != (1<<PtrBit))
fatal("wrong PtrBit");
switch(t->width/widthptr) {
default:
fatal("found writebarrierfat for %d-byte object of type %T", (int)t->width, t);
case 2:
snprint(name, sizeof name, "writebarrierfat%d%d",
bvget(bv, PtrBit), bvget(bv, BitsPerPointer+PtrBit));
break;
case 3:
snprint(name, sizeof name, "writebarrierfat%d%d%d",
bvget(bv, PtrBit), bvget(bv, BitsPerPointer+PtrBit), bvget(bv, 2*BitsPerPointer+PtrBit));
break;
case 4:
snprint(name, sizeof name, "writebarrierfat%d%d%d%d",
bvget(bv, PtrBit), bvget(bv, BitsPerPointer+PtrBit), bvget(bv, 2*BitsPerPointer+PtrBit), bvget(bv, 3*BitsPerPointer+PtrBit));
break;
}
n = mkcall1(writebarrierfn(name, t, n->right->type), T, init,
l, nodnil(), n->right); l, nodnil(), n->right);
} else { } else {
r = n->right; r = n->right;
...@@ -2873,6 +2915,11 @@ copyany(Node *n, NodeList **init, int runtimecall) ...@@ -2873,6 +2915,11 @@ copyany(Node *n, NodeList **init, int runtimecall)
{ {
Node *nl, *nr, *nfrm, *nto, *nif, *nlen, *nwid, *fn; Node *nl, *nr, *nfrm, *nto, *nif, *nlen, *nwid, *fn;
NodeList *l; NodeList *l;
if(haspointers(n->left->type->type)) {
fn = writebarrierfn("writebarriercopy", n->left->type, n->right->type);
return mkcall1(fn, n->type, init, typename(n->left->type->type), n->left, n->right);
}
if(runtimecall) { if(runtimecall) {
if(n->right->type->etype == TSTRING) if(n->right->type->etype == TSTRING)
......
...@@ -2285,3 +2285,23 @@ TEXT runtime·getg(SB),NOSPLIT,$0-4 ...@@ -2285,3 +2285,23 @@ TEXT runtime·getg(SB),NOSPLIT,$0-4
MOVL AX, ret+0(FP) MOVL AX, ret+0(FP)
RET RET
TEXT runtime·prefetcht0(SB),NOSPLIT,$0-4
MOVL addr+0(FP), AX
PREFETCHT0 (AX)
RET
TEXT runtime·prefetcht1(SB),NOSPLIT,$0-4
MOVL addr+0(FP), AX
PREFETCHT1 (AX)
RET
TEXT runtime·prefetcht2(SB),NOSPLIT,$0-4
MOVL addr+0(FP), AX
PREFETCHT2 (AX)
RET
TEXT runtime·prefetchnta(SB),NOSPLIT,$0-4
MOVL addr+0(FP), AX
PREFETCHNTA (AX)
RET
...@@ -2228,3 +2228,23 @@ TEXT runtime·getg(SB),NOSPLIT,$0-8 ...@@ -2228,3 +2228,23 @@ TEXT runtime·getg(SB),NOSPLIT,$0-8
MOVQ g(CX), AX MOVQ g(CX), AX
MOVQ AX, ret+0(FP) MOVQ AX, ret+0(FP)
RET RET
TEXT runtime·prefetcht0(SB),NOSPLIT,$0-8
MOVQ addr+0(FP), AX
PREFETCHT0 (AX)
RET
TEXT runtime·prefetcht1(SB),NOSPLIT,$0-8
MOVQ addr+0(FP), AX
PREFETCHT1 (AX)
RET
TEXT runtime·prefetcht2(SB),NOSPLIT,$0-8
MOVQ addr+0(FP), AX
PREFETCHT2 (AX)
RET
TEXT runtime·prefetchnta(SB),NOSPLIT,$0-8
MOVQ addr+0(FP), AX
PREFETCHNTA (AX)
RET
...@@ -1079,3 +1079,24 @@ TEXT runtime·getg(SB),NOSPLIT,$0-4 ...@@ -1079,3 +1079,24 @@ TEXT runtime·getg(SB),NOSPLIT,$0-4
MOVL g(CX), AX MOVL g(CX), AX
MOVL AX, ret+0(FP) MOVL AX, ret+0(FP)
RET RET
TEXT runtime·prefetcht0(SB),NOSPLIT,$0-4
MOVL addr+0(FP), AX
PREFETCHT0 (AX)
RET
TEXT runtime·prefetcht1(SB),NOSPLIT,$0-4
MOVL addr+0(FP), AX
PREFETCHT1 (AX)
RET
TEXT runtime·prefetcht2(SB),NOSPLIT,$0-4
MOVL addr+0(FP), AX
PREFETCHT2 (AX)
RET
TEXT runtime·prefetchnta(SB),NOSPLIT,$0-4
MOVL addr+0(FP), AX
PREFETCHNTA (AX)
RET
...@@ -1320,3 +1320,15 @@ TEXT runtime·goexit(SB),NOSPLIT,$-4-0 ...@@ -1320,3 +1320,15 @@ TEXT runtime·goexit(SB),NOSPLIT,$-4-0
TEXT runtime·getg(SB),NOSPLIT,$-4-4 TEXT runtime·getg(SB),NOSPLIT,$-4-4
MOVW g, ret+0(FP) MOVW g, ret+0(FP)
RET RET
TEXT runtime·prefetcht0(SB),NOSPLIT,$0-4
RET
TEXT runtime·prefetcht1(SB),NOSPLIT,$0-4
RET
TEXT runtime·prefetcht2(SB),NOSPLIT,$0-4
RET
TEXT runtime·prefetchnta(SB),NOSPLIT,$0-4
RET
...@@ -977,3 +977,15 @@ TEXT runtime·goexit(SB),NOSPLIT,$-8-0 ...@@ -977,3 +977,15 @@ TEXT runtime·goexit(SB),NOSPLIT,$-8-0
TEXT runtime·getg(SB),NOSPLIT,$-8-8 TEXT runtime·getg(SB),NOSPLIT,$-8-8
MOVD g, ret+0(FP) MOVD g, ret+0(FP)
RETURN RETURN
TEXT runtime·prefetcht0(SB),NOSPLIT,$0-8
RETURN
TEXT runtime·prefetcht1(SB),NOSPLIT,$0-8
RETURN
TEXT runtime·prefetcht2(SB),NOSPLIT,$0-8
RETURN
TEXT runtime·prefetchnta(SB),NOSPLIT,$0-8
RETURN
...@@ -26,7 +26,7 @@ var Exitsyscall = exitsyscall ...@@ -26,7 +26,7 @@ var Exitsyscall = exitsyscall
var LockedOSThread = lockedOSThread var LockedOSThread = lockedOSThread
type LFNode struct { type LFNode struct {
Next *LFNode Next uint64
Pushcnt uintptr Pushcnt uintptr
} }
......
...@@ -464,8 +464,8 @@ func dumpobjs() { ...@@ -464,8 +464,8 @@ func dumpobjs() {
if n > uintptr(len(freemark)) { if n > uintptr(len(freemark)) {
gothrow("freemark array doesn't have enough entries") gothrow("freemark array doesn't have enough entries")
} }
for l := s.freelist; l != nil; l = l.next { for l := s.freelist; l.ptr() != nil; l = l.ptr().next {
freemark[(uintptr(unsafe.Pointer(l))-p)/size] = true freemark[(uintptr(l)-p)/size] = true
} }
for j := uintptr(0); j < n; j, p = j+1, p+size { for j := uintptr(0); j < n; j, p = j+1, p+size {
if freemark[j] { if freemark[j] {
......
...@@ -18,7 +18,7 @@ func lfstackpush(head *uint64, node *lfnode) { ...@@ -18,7 +18,7 @@ func lfstackpush(head *uint64, node *lfnode) {
} }
for { for {
old := atomicload64(head) old := atomicload64(head)
node.next, _ = lfstackUnpack(old) node.next = old
if cas64(head, old, new) { if cas64(head, old, new) {
break break
} }
...@@ -32,12 +32,8 @@ func lfstackpop(head *uint64) unsafe.Pointer { ...@@ -32,12 +32,8 @@ func lfstackpop(head *uint64) unsafe.Pointer {
return nil return nil
} }
node, _ := lfstackUnpack(old) node, _ := lfstackUnpack(old)
node2 := (*lfnode)(atomicloadp(unsafe.Pointer(&node.next))) next := atomicload64(&node.next)
new := uint64(0) if cas64(head, old, next) {
if node2 != nil {
new = lfstackPack(node2, node2.pushcnt)
}
if cas64(head, old, new) {
return unsafe.Pointer(node) return unsafe.Pointer(node)
} }
} }
......
...@@ -121,7 +121,7 @@ func TestLFStackStress(t *testing.T) { ...@@ -121,7 +121,7 @@ func TestLFStackStress(t *testing.T) {
} }
cnt++ cnt++
sum2 += node.data sum2 += node.data
node.Next = nil node.Next = 0
} }
} }
if cnt != K { if cnt != K {
......
...@@ -140,14 +140,14 @@ func mallocgc(size uintptr, typ *_type, flags uint32) unsafe.Pointer { ...@@ -140,14 +140,14 @@ func mallocgc(size uintptr, typ *_type, flags uint32) unsafe.Pointer {
// Allocate a new maxTinySize block. // Allocate a new maxTinySize block.
s = c.alloc[tinySizeClass] s = c.alloc[tinySizeClass]
v := s.freelist v := s.freelist
if v == nil { if v.ptr() == nil {
systemstack(func() { systemstack(func() {
mCache_Refill(c, tinySizeClass) mCache_Refill(c, tinySizeClass)
}) })
s = c.alloc[tinySizeClass] s = c.alloc[tinySizeClass]
v = s.freelist v = s.freelist
} }
s.freelist = v.next s.freelist = v.ptr().next
s.ref++ s.ref++
//TODO: prefetch v.next //TODO: prefetch v.next
x = unsafe.Pointer(v) x = unsafe.Pointer(v)
...@@ -170,19 +170,19 @@ func mallocgc(size uintptr, typ *_type, flags uint32) unsafe.Pointer { ...@@ -170,19 +170,19 @@ func mallocgc(size uintptr, typ *_type, flags uint32) unsafe.Pointer {
size = uintptr(class_to_size[sizeclass]) size = uintptr(class_to_size[sizeclass])
s = c.alloc[sizeclass] s = c.alloc[sizeclass]
v := s.freelist v := s.freelist
if v == nil { if v.ptr() == nil {
systemstack(func() { systemstack(func() {
mCache_Refill(c, int32(sizeclass)) mCache_Refill(c, int32(sizeclass))
}) })
s = c.alloc[sizeclass] s = c.alloc[sizeclass]
v = s.freelist v = s.freelist
} }
s.freelist = v.next s.freelist = v.ptr().next
s.ref++ s.ref++
//TODO: prefetch //TODO: prefetch
x = unsafe.Pointer(v) x = unsafe.Pointer(v)
if flags&flagNoZero == 0 { if flags&flagNoZero == 0 {
v.next = nil v.ptr().next = 0
if size > 2*ptrSize && ((*[2]uintptr)(x))[1] != 0 { if size > 2*ptrSize && ((*[2]uintptr)(x))[1] != 0 {
memclr(unsafe.Pointer(v), size) memclr(unsafe.Pointer(v), size)
} }
...@@ -241,6 +241,8 @@ func mallocgc(size uintptr, typ *_type, flags uint32) unsafe.Pointer { ...@@ -241,6 +241,8 @@ func mallocgc(size uintptr, typ *_type, flags uint32) unsafe.Pointer {
masksize = masksize * pointersPerByte / 8 // 4 bits per word masksize = masksize * pointersPerByte / 8 // 4 bits per word
masksize++ // unroll flag in the beginning masksize++ // unroll flag in the beginning
if masksize > maxGCMask && typ.gc[1] != 0 { if masksize > maxGCMask && typ.gc[1] != 0 {
// write barriers have not been updated to deal with this case yet.
gothrow("maxGCMask too small for now")
// If the mask is too large, unroll the program directly // If the mask is too large, unroll the program directly
// into the GC bitmap. It's 7 times slower than copying // into the GC bitmap. It's 7 times slower than copying
// from the pre-unrolled mask, but saves 1/16 of type size // from the pre-unrolled mask, but saves 1/16 of type size
...@@ -295,6 +297,17 @@ func mallocgc(size uintptr, typ *_type, flags uint32) unsafe.Pointer { ...@@ -295,6 +297,17 @@ func mallocgc(size uintptr, typ *_type, flags uint32) unsafe.Pointer {
} }
} }
marked: marked:
// GCmarkterminate allocates black
// All slots hold nil so no scanning is needed.
// This may be racing with GC so do it atomically if there can be
// a race marking the bit.
if gcphase == _GCmarktermination {
systemstack(func() {
gcmarknewobject_m(uintptr(x))
})
}
if raceenabled { if raceenabled {
racemalloc(x, size) racemalloc(x, size)
} }
...@@ -328,13 +341,43 @@ marked: ...@@ -328,13 +341,43 @@ marked:
} }
} }
if memstats.heap_alloc >= memstats.next_gc { if memstats.heap_alloc >= memstats.next_gc/2 {
gogc(0) gogc(0)
} }
return x return x
} }
func loadPtrMask(typ *_type) []uint8 {
var ptrmask *uint8
nptr := (uintptr(typ.size) + ptrSize - 1) / ptrSize
if typ.kind&kindGCProg != 0 {
masksize := nptr
if masksize%2 != 0 {
masksize *= 2 // repeated
}
masksize = masksize * pointersPerByte / 8 // 4 bits per word
masksize++ // unroll flag in the beginning
if masksize > maxGCMask && typ.gc[1] != 0 {
// write barriers have not been updated to deal with this case yet.
gothrow("maxGCMask too small for now")
}
ptrmask = (*uint8)(unsafe.Pointer(uintptr(typ.gc[0])))
// Check whether the program is already unrolled
// by checking if the unroll flag byte is set
maskword := uintptr(atomicloadp(unsafe.Pointer(ptrmask)))
if *(*uint8)(unsafe.Pointer(&maskword)) == 0 {
systemstack(func() {
unrollgcprog_m(typ)
})
}
ptrmask = (*uint8)(add(unsafe.Pointer(ptrmask), 1)) // skip the unroll flag byte
} else {
ptrmask = (*uint8)(unsafe.Pointer(typ.gc[0])) // pointer to unrolled mask
}
return (*[1 << 30]byte)(unsafe.Pointer(ptrmask))[:(nptr+1)/2]
}
// implementation of new builtin // implementation of new builtin
func newobject(typ *_type) unsafe.Pointer { func newobject(typ *_type) unsafe.Pointer {
flags := uint32(0) flags := uint32(0)
...@@ -429,7 +472,21 @@ func gogc(force int32) { ...@@ -429,7 +472,21 @@ func gogc(force int32) {
mp = acquirem() mp = acquirem()
mp.gcing = 1 mp.gcing = 1
releasem(mp) releasem(mp)
systemstack(stoptheworld) systemstack(stoptheworld)
systemstack(finishsweep_m) // finish sweep before we start concurrent scan.
if true { // To turn on concurrent scan and mark set to true...
systemstack(starttheworld)
// Do a concurrent heap scan before we stop the world.
systemstack(gcscan_m)
systemstack(stoptheworld)
systemstack(gcinstallmarkwb_m)
systemstack(starttheworld)
systemstack(gcmark_m)
systemstack(stoptheworld)
systemstack(gcinstalloffwb_m)
}
if mp != acquirem() { if mp != acquirem() {
gothrow("gogc: rescheduled") gothrow("gogc: rescheduled")
} }
...@@ -445,17 +502,21 @@ func gogc(force int32) { ...@@ -445,17 +502,21 @@ func gogc(force int32) {
if debug.gctrace > 1 { if debug.gctrace > 1 {
n = 2 n = 2
} }
eagersweep := force >= 2
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
if i > 0 { if i > 0 {
startTime = nanotime() startTime = nanotime()
} }
// switch to g0, call gc, then switch back // switch to g0, call gc, then switch back
eagersweep := force >= 2
systemstack(func() { systemstack(func() {
gc_m(startTime, eagersweep) gc_m(startTime, eagersweep)
}) })
} }
systemstack(func() {
gccheckmark_m(startTime, eagersweep)
})
// all done // all done
mp.gcing = 0 mp.gcing = 0
semrelease(&worldsema) semrelease(&worldsema)
...@@ -470,6 +531,14 @@ func gogc(force int32) { ...@@ -470,6 +531,14 @@ func gogc(force int32) {
} }
} }
func GCcheckmarkenable() {
systemstack(gccheckmarkenable_m)
}
func GCcheckmarkdisable() {
systemstack(gccheckmarkdisable_m)
}
// GC runs a garbage collection. // GC runs a garbage collection.
func GC() { func GC() {
gogc(2) gogc(2)
......
...@@ -139,10 +139,35 @@ const ( ...@@ -139,10 +139,35 @@ const (
) )
// A generic linked list of blocks. (Typically the block is bigger than sizeof(MLink).) // A generic linked list of blocks. (Typically the block is bigger than sizeof(MLink).)
// Since assignments to mlink.next will result in a write barrier being preformed
// this can not be used by some of the internal GC structures. For example when
// the sweeper is placing an unmarked object on the free list it does not want the
// write barrier to be called since that could result in the object being reachable.
type mlink struct { type mlink struct {
next *mlink next *mlink
} }
// A gclink is a node in a linked list of blocks, like mlink,
// but it is opaque to the garbage collector.
// The GC does not trace the pointers during collection,
// and the compiler does not emit write barriers for assignments
// of gclinkptr values. Code should store references to gclinks
// as gclinkptr, not as *gclink.
type gclink struct {
next gclinkptr
}
// A gclinkptr is a pointer to a gclink, but it is opaque
// to the garbage collector.
type gclinkptr uintptr
// ptr returns the *gclink form of p.
// The result should be used for accessing fields, not stored
// in other data structures.
func (p gclinkptr) ptr() *gclink {
return (*gclink)(unsafe.Pointer(p))
}
// sysAlloc obtains a large chunk of zeroed memory from the // sysAlloc obtains a large chunk of zeroed memory from the
// operating system, typically on the order of a hundred kilobytes // operating system, typically on the order of a hundred kilobytes
// or a megabyte. // or a megabyte.
...@@ -275,8 +300,8 @@ type mcachelist struct { ...@@ -275,8 +300,8 @@ type mcachelist struct {
} }
type stackfreelist struct { type stackfreelist struct {
list *mlink // linked list of free stacks list gclinkptr // linked list of free stacks
size uintptr // total size of stacks in list size uintptr // total size of stacks in list
} }
// Per-thread (in Go, per-P) cache for small objects. // Per-thread (in Go, per-P) cache for small objects.
...@@ -299,8 +324,6 @@ type mcache struct { ...@@ -299,8 +324,6 @@ type mcache struct {
sudogcache *sudog sudogcache *sudog
gcworkbuf unsafe.Pointer
// Local allocator stats, flushed during GC. // Local allocator stats, flushed during GC.
local_nlookup uintptr // number of pointer lookups local_nlookup uintptr // number of pointer lookups
local_largefree uintptr // bytes freed for large objects (>maxsmallsize) local_largefree uintptr // bytes freed for large objects (>maxsmallsize)
...@@ -348,11 +371,11 @@ const ( ...@@ -348,11 +371,11 @@ const (
) )
type mspan struct { type mspan struct {
next *mspan // in a span linked list next *mspan // in a span linked list
prev *mspan // in a span linked list prev *mspan // in a span linked list
start pageID // starting page number start pageID // starting page number
npages uintptr // number of pages in span npages uintptr // number of pages in span
freelist *mlink // list of free objects freelist gclinkptr // list of free objects
// sweep generation: // sweep generation:
// if sweepgen == h->sweepgen - 2, the span needs sweeping // if sweepgen == h->sweepgen - 2, the span needs sweeping
// if sweepgen == h->sweepgen - 1, the span is currently being swept // if sweepgen == h->sweepgen - 1, the span is currently being swept
......
...@@ -38,7 +38,12 @@ func freemcache(c *mcache) { ...@@ -38,7 +38,12 @@ func freemcache(c *mcache) {
systemstack(func() { systemstack(func() {
mCache_ReleaseAll(c) mCache_ReleaseAll(c)
stackcache_clear(c) stackcache_clear(c)
gcworkbuffree(c.gcworkbuf)
// NOTE(rsc,rlh): If gcworkbuffree comes back, we need to coordinate
// with the stealing of gcworkbufs during garbage collection to avoid
// a race where the workbuf is double-freed.
// gcworkbuffree(c.gcworkbuf)
lock(&mheap_.lock) lock(&mheap_.lock)
purgecachedstats(c) purgecachedstats(c)
fixAlloc_Free(&mheap_.cachealloc, unsafe.Pointer(c)) fixAlloc_Free(&mheap_.cachealloc, unsafe.Pointer(c))
...@@ -54,7 +59,7 @@ func mCache_Refill(c *mcache, sizeclass int32) *mspan { ...@@ -54,7 +59,7 @@ func mCache_Refill(c *mcache, sizeclass int32) *mspan {
_g_.m.locks++ _g_.m.locks++
// Return the current cached span to the central lists. // Return the current cached span to the central lists.
s := c.alloc[sizeclass] s := c.alloc[sizeclass]
if s.freelist != nil { if s.freelist.ptr() != nil {
gothrow("refill on a nonempty span") gothrow("refill on a nonempty span")
} }
if s != &emptymspan { if s != &emptymspan {
...@@ -66,7 +71,7 @@ func mCache_Refill(c *mcache, sizeclass int32) *mspan { ...@@ -66,7 +71,7 @@ func mCache_Refill(c *mcache, sizeclass int32) *mspan {
if s == nil { if s == nil {
gothrow("out of memory") gothrow("out of memory")
} }
if s.freelist == nil { if s.freelist.ptr() == nil {
println(s.ref, (s.npages<<_PageShift)/s.elemsize) println(s.ref, (s.npages<<_PageShift)/s.elemsize)
gothrow("empty span") gothrow("empty span")
} }
......
...@@ -55,7 +55,7 @@ retry: ...@@ -55,7 +55,7 @@ retry:
mSpanList_InsertBack(&c.empty, s) mSpanList_InsertBack(&c.empty, s)
unlock(&c.lock) unlock(&c.lock)
mSpan_Sweep(s, true) mSpan_Sweep(s, true)
if s.freelist != nil { if s.freelist.ptr() != nil {
goto havespan goto havespan
} }
lock(&c.lock) lock(&c.lock)
...@@ -90,7 +90,7 @@ havespan: ...@@ -90,7 +90,7 @@ havespan:
if n == 0 { if n == 0 {
gothrow("empty span") gothrow("empty span")
} }
if s.freelist == nil { if s.freelist.ptr() == nil {
gothrow("freelist empty") gothrow("freelist empty")
} }
s.incache = true s.incache = true
...@@ -122,14 +122,14 @@ func mCentral_UncacheSpan(c *mcentral, s *mspan) { ...@@ -122,14 +122,14 @@ func mCentral_UncacheSpan(c *mcentral, s *mspan) {
// the latest generation. // the latest generation.
// If preserve=true, don't return the span to heap nor relink in MCentral lists; // If preserve=true, don't return the span to heap nor relink in MCentral lists;
// caller takes care of it. // caller takes care of it.
func mCentral_FreeSpan(c *mcentral, s *mspan, n int32, start *mlink, end *mlink, preserve bool) bool { func mCentral_FreeSpan(c *mcentral, s *mspan, n int32, start gclinkptr, end gclinkptr, preserve bool) bool {
if s.incache { if s.incache {
gothrow("freespan into cached span") gothrow("freespan into cached span")
} }
// Add the objects back to s's free list. // Add the objects back to s's free list.
wasempty := s.freelist == nil wasempty := s.freelist.ptr() == nil
end.next = s.freelist end.ptr().next = s.freelist
s.freelist = start s.freelist = start
s.ref -= uint16(n) s.ref -= uint16(n)
...@@ -165,7 +165,7 @@ func mCentral_FreeSpan(c *mcentral, s *mspan, n int32, start *mlink, end *mlink, ...@@ -165,7 +165,7 @@ func mCentral_FreeSpan(c *mcentral, s *mspan, n int32, start *mlink, end *mlink,
// s is completely freed, return it to the heap. // s is completely freed, return it to the heap.
mSpanList_Remove(s) mSpanList_Remove(s)
s.needzero = 1 s.needzero = 1
s.freelist = nil s.freelist = 0
unlock(&c.lock) unlock(&c.lock)
unmarkspan(uintptr(s.start)<<_PageShift, s.npages<<_PageShift) unmarkspan(uintptr(s.start)<<_PageShift, s.npages<<_PageShift)
mHeap_Free(&mheap_, s, 0) mHeap_Free(&mheap_, s, 0)
...@@ -183,17 +183,21 @@ func mCentral_Grow(c *mcentral) *mspan { ...@@ -183,17 +183,21 @@ func mCentral_Grow(c *mcentral) *mspan {
return nil return nil
} }
// Carve span into sequence of blocks.
tailp := &s.freelist
p := uintptr(s.start << _PageShift) p := uintptr(s.start << _PageShift)
s.limit = p + size*n s.limit = p + size*n
for i := uintptr(0); i < n; i++ { head := gclinkptr(p)
v := (*mlink)(unsafe.Pointer(p)) tail := gclinkptr(p)
*tailp = v // i==0 iteration already done
tailp = &v.next for i := uintptr(1); i < n; i++ {
p += size p += size
tail.ptr().next = gclinkptr(p)
tail = gclinkptr(p)
} }
*tailp = nil if s.freelist.ptr() != nil {
gothrow("freelist not empty")
}
tail.ptr().next = 0
s.freelist = head
markspan(unsafe.Pointer(uintptr(s.start)<<_PageShift), size, n, size*n < s.npages<<_PageShift) markspan(unsafe.Pointer(uintptr(s.start)<<_PageShift), size, n, size*n < s.npages<<_PageShift)
return s return s
} }
...@@ -7,22 +7,72 @@ ...@@ -7,22 +7,72 @@
// Garbage collector (GC). // Garbage collector (GC).
// //
// GC is: // The GC runs concurrently with mutator threads, is type accurate (aka precise), allows multiple GC
// - mark&sweep // thread to run in parallel. It is a concurrent mark and sweep that uses a write barrier. It is
// - mostly precise (with the exception of some C-allocated objects, assembly frames/arguments, etc) // non-generational and non-compacting. Allocation is done using size segregated per P allocation
// - parallel (up to MaxGcproc threads) // areas to minimize fragmentation while eliminating locks in the common case.
// - partially concurrent (mark is stop-the-world, while sweep is concurrent)
// - non-moving/non-compacting
// - full (non-partial)
// //
// GC rate. // The algorithm decomposes into several steps.
// Next GC is after we've allocated an extra amount of memory proportional to // This is a high level description of the algorithm being used. For an overview of GC a good
// the amount already in use. The proportion is controlled by GOGC environment variable // place to start is Richard Jones' gchandbook.org.
// (100 by default). If GOGC=100 and we're using 4M, we'll GC again when we get to 8M //
// (this mark is tracked in next_gc variable). This keeps the GC cost in linear // The algorithm's intellectual heritage includes Dijkstra's on-the-fly algorithm, see
// proportion to the allocation cost. Adjusting GOGC just changes the linear constant // Edsger W. Dijkstra, Leslie Lamport, A. J. Martin, C. S. Scholten, and E. F. M. Steffens. 1978.
// (and also the amount of extra memory used). // On-the-fly garbage collection: an exercise in cooperation. Commun. ACM 21, 11 (November 1978), 966-975.
// For journal quality proofs that these steps are complete, correct, and terminate see
// Hudson, R., and Moss, J.E.B. Copying Garbage Collection without stopping the world.
// Concurrency and Computation: Practice and Experience 15(3-5), 2003.
// //
// 0. Set phase = GCscan from GCoff.
// 1. Wait for all P's to acknowledge phase change.
// At this point all goroutines have passed through a GC safepoint and
// know we are in the GCscan phase.
// 2. GC scans all goroutine stacks, mark and enqueues all encountered pointers
// (marking avoids most duplicate enqueuing but races may produce duplication which is benign).
// Preempted goroutines are scanned before P schedules next goroutine.
// 3. Set phase = GCmark.
// 4. Wait for all P's to acknowledge phase change.
// 5. Now write barrier marks and enqueues black, grey, or white to white pointers.
// Malloc still allocates white (non-marked) objects.
// 6. Meanwhile GC transitively walks the heap marking reachable objects.
// 7. When GC finishes marking heap, it preempts P's one-by-one and
// retakes partial wbufs (filled by write barrier or during a stack scan of the goroutine
// currently scheduled on the P).
// 8. Once the GC has exhausted all available marking work it sets phase = marktermination.
// 9. Wait for all P's to acknowledge phase change.
// 10. Malloc now allocates black objects, so number of unmarked reachable objects
// monotonically decreases.
// 11. GC preempts P's one-by-one taking partial wbufs and marks all unmarked yet reachable objects.
// 12. When GC completes a full cycle over P's and discovers no new grey
// objects, (which means all reachable objects are marked) set phase = GCsweep.
// 13. Wait for all P's to acknowledge phase change.
// 14. Now malloc allocates white (but sweeps spans before use).
// Write barrier becomes nop.
// 15. GC does background sweeping, see description below.
// 16. When sweeping is complete set phase to GCoff.
// 17. When sufficient allocation has taken place replay the sequence starting at 0 above,
// see discussion of GC rate below.
// Changing phases.
// Phases are changed by setting the gcphase to the next phase and possibly calling ackgcphase.
// All phase action must be benign in the presence of a change.
// Starting with GCoff
// GCoff to GCscan
// GSscan scans stacks and globals greying them and never marks an object black.
// Once all the P's are aware of the new phase they will scan gs on preemption.
// This means that the scanning of preempted gs can't start until all the Ps
// have acknowledged.
// GCscan to GCmark
// GCMark turns on the write barrier which also only greys objects. No scanning
// of objects (making them black) can happen until all the Ps have acknowledged
// the phase change.
// GCmark to GCmarktermination
// The only change here is that we start allocating black so the Ps must acknowledge
// the change before we begin the termination algorithm
// GCmarktermination to GSsweep
// Object currently on the freelist must be marked black for this to work.
// Are things on the free lists black or white? How does the sweep phase work?
// Concurrent sweep. // Concurrent sweep.
// The sweep phase proceeds concurrently with normal program execution. // The sweep phase proceeds concurrently with normal program execution.
// The heap is swept span-by-span both lazily (when a goroutine needs another span) // The heap is swept span-by-span both lazily (when a goroutine needs another span)
...@@ -53,6 +103,14 @@ ...@@ -53,6 +103,14 @@
// The finalizer goroutine is kicked off only when all spans are swept. // The finalizer goroutine is kicked off only when all spans are swept.
// When the next GC starts, it sweeps all not-yet-swept spans (if any). // When the next GC starts, it sweeps all not-yet-swept spans (if any).
// GC rate.
// Next GC is after we've allocated an extra amount of memory proportional to
// the amount already in use. The proportion is controlled by GOGC environment variable
// (100 by default). If GOGC=100 and we're using 4M, we'll GC again when we get to 8M
// (this mark is tracked in next_gc variable). This keeps the GC cost in linear
// proportion to the allocation cost. Adjusting GOGC just changes the linear constant
// (and also the amount of extra memory used).
package runtime package runtime
import "unsafe" import "unsafe"
...@@ -75,7 +133,7 @@ const ( ...@@ -75,7 +133,7 @@ const (
// ptrmask for an allocation containing a single pointer. // ptrmask for an allocation containing a single pointer.
var oneptr = [...]uint8{bitsPointer} var oneptr = [...]uint8{bitsPointer}
// Initialized from $GOGC. GOGC=off means no gc. // Initialized from $GOGC. GOGC=off means no GC.
var gcpercent int32 var gcpercent int32
// Holding worldsema grants an M the right to try to stop the world. // Holding worldsema grants an M the right to try to stop the world.
...@@ -93,6 +151,17 @@ var gcpercent int32 ...@@ -93,6 +151,17 @@ var gcpercent int32
// //
var worldsema uint32 = 1 var worldsema uint32 = 1
// It is a bug if bits does not have bitBoundary set but
// there are still some cases where this happens related
// to stack spans.
type markbits struct {
bitp *byte // pointer to the byte holding xbits
shift uintptr // bits xbits needs to be shifted to get bits
xbits byte // byte holding all the bits from *bitp
bits byte // mark and boundary bits relevant to corresponding slot.
tbits byte // pointer||scalar bits relevant to corresponding slot.
}
type workbuf struct { type workbuf struct {
node lfnode // must be first node lfnode // must be first
nobj uintptr nobj uintptr
...@@ -121,6 +190,7 @@ var nbadblock int32 ...@@ -121,6 +190,7 @@ var nbadblock int32
type workdata struct { type workdata struct {
full uint64 // lock-free list of full blocks full uint64 // lock-free list of full blocks
empty uint64 // lock-free list of empty blocks empty uint64 // lock-free list of empty blocks
partial uint64 // lock-free list of partially filled blocks
pad0 [_CacheLineSize]uint8 // prevents false-sharing between full/empty and nproc/nwait pad0 [_CacheLineSize]uint8 // prevents false-sharing between full/empty and nproc/nwait
nproc uint32 nproc uint32
tstart int64 tstart int64
...@@ -143,293 +213,405 @@ func have_cgo_allocate() bool { ...@@ -143,293 +213,405 @@ func have_cgo_allocate() bool {
return &weak_cgo_allocate != nil return &weak_cgo_allocate != nil
} }
// scanblock scans a block of n bytes starting at pointer b for references // To help debug the concurrent GC we remark with the world
// to other objects, scanning any it finds recursively until there are no // stopped ensuring that any object encountered has their normal
// unscanned objects left. Instead of using an explicit recursion, it keeps // mark bit set. To do this we use an orthogonal bit
// a work list in the Workbuf* structures and loops in the main function // pattern to indicate the object is marked. The following pattern
// body. Keeping an explicit work list is easier on the stack allocator and // uses the upper two bits in the object's bounday nibble.
// more efficient. // 01: scalar not marked
func scanblock(b, n uintptr, ptrmask *uint8) { // 10: pointer not marked
// Cache memory arena parameters in local vars. // 11: pointer marked
arena_start := mheap_.arena_start // 00: scalar marked
arena_used := mheap_.arena_used // Xoring with 01 will flip the pattern from marked to unmarked and vica versa.
// The higher bit is 1 for pointers and 0 for scalars, whether the object
wbuf := getempty(nil) // is marked or not.
nobj := wbuf.nobj // The first nibble no longer holds the bitsDead pattern indicating that the
wp := &wbuf.obj[nobj] // there are no more pointers in the object. This information is held
keepworking := b == 0 // in the second nibble.
// When marking an object if the bool checkmark is true one uses the above
// encoding, otherwise one uses the bitMarked bit in the lower two bits
// of the nibble.
var (
checkmark = false
gccheckmarkenable = true
)
var ptrbitp unsafe.Pointer // Is address b in the known heap. If it doesn't have a valid gcmap
// returns false. For example pointers into stacks will return false.
func inheap(b uintptr) bool {
if b == 0 || b < mheap_.arena_start || b >= mheap_.arena_used {
return false
}
// Not a beginning of a block, consult span table to find the block beginning.
k := b >> _PageShift
x := k
x -= mheap_.arena_start >> _PageShift
s := h_spans[x]
if s == nil || pageID(k) < s.start || b >= s.limit || s.state != mSpanInUse {
return false
}
return true
}
// ptrmask can have 2 possible values: // Given an address in the heap return the relevant byte from the gcmap. This routine
// 1. nil - obtain pointer mask from GC bitmap. // can be used on addresses to the start of an object or to the interior of the an object.
// 2. pointer to a compact mask (for stacks and data). func slottombits(obj uintptr, mbits *markbits) {
goto_scanobj := b != 0 off := (obj&^(ptrSize-1) - mheap_.arena_start) / ptrSize
mbits.bitp = (*byte)(unsafe.Pointer(mheap_.arena_start - off/wordsPerBitmapByte - 1))
mbits.shift = off % wordsPerBitmapByte * gcBits
mbits.xbits = *mbits.bitp
mbits.bits = (mbits.xbits >> mbits.shift) & bitMask
mbits.tbits = ((mbits.xbits >> mbits.shift) & bitPtrMask) >> 2
}
// b is a pointer into the heap.
// Find the start of the object refered to by b.
// Set mbits to the associated bits from the bit map.
// If b is not a valid heap object return nil and
// undefined values in mbits.
func objectstart(b uintptr, mbits *markbits) uintptr {
obj := b &^ (ptrSize - 1)
for { for {
if goto_scanobj { slottombits(obj, mbits)
goto_scanobj = false if mbits.bits&bitBoundary == bitBoundary {
} else { break
if nobj == 0 { }
// Out of work in workbuf.
if !keepworking {
putempty(wbuf)
return
}
// Refill workbuf from global queue. // Not a beginning of a block, consult span table to find the block beginning.
wbuf = getfull(wbuf) k := b >> _PageShift
if wbuf == nil { x := k
return x -= mheap_.arena_start >> _PageShift
} s := h_spans[x]
nobj = wbuf.nobj if s == nil || pageID(k) < s.start || b >= s.limit || s.state != mSpanInUse {
if nobj < uintptr(len(wbuf.obj)) { if s != nil && s.state == _MSpanStack {
wp = &wbuf.obj[nobj] return 0 // This is legit.
} else {
wp = nil
}
} }
// If another proc wants a pointer, give it some. // The following ensures that we are rigorous about what data
if work.nwait > 0 && nobj > 4 && work.full == 0 { // structures hold valid pointers
wbuf.nobj = nobj if false {
wbuf = handoff(wbuf) // Still happens sometimes. We don't know why.
nobj = wbuf.nobj printlock()
if nobj < uintptr(len(wbuf.obj)) { print("runtime:objectstart Span weird: obj=", hex(obj), " k=", hex(k))
wp = &wbuf.obj[nobj] if s == nil {
print(" s=nil\n")
} else { } else {
wp = nil print(" s.start=", hex(s.start<<_PageShift), " s.limit=", hex(s.limit), " s.state=", s.state, "\n")
} }
printunlock()
gothrow("objectstart: bad pointer in unexpected span")
} }
return 0
nobj--
wp = &wbuf.obj[nobj]
b = *wp
n = arena_used - uintptr(b)
ptrmask = nil // use GC bitmap for pointer info
} }
if _DebugGCPtrs { p := uintptr(s.start) << _PageShift
print("scanblock ", b, " +", hex(n), " ", ptrmask, "\n") if s.sizeclass != 0 {
size := s.elemsize
idx := (obj - p) / size
p = p + idx*size
} }
if p == obj {
// Find bits of the beginning of the object. print("runtime: failed to find block beginning for ", hex(p), " s=", hex(s.start*_PageSize), " s.limit=", hex(s.limit), "\n")
if ptrmask == nil { gothrow("failed to find block beginning")
off := (uintptr(b) - arena_start) / ptrSize
ptrbitp = unsafe.Pointer(arena_start - off/wordsPerBitmapByte - 1)
} }
obj = p
}
var i uintptr // if size(obj.firstfield) < PtrSize, the &obj.secondfield could map to the boundary bit
for i = 0; i < n; i += ptrSize { // Clear any low bits to get to the start of the object.
// Find bits for this word. // greyobject depends on this.
var bits uintptr return obj
if ptrmask == nil { }
// Check if we have reached end of span.
if (uintptr(b)+i)%_PageSize == 0 &&
h_spans[(uintptr(b)-arena_start)>>_PageShift] != h_spans[(uintptr(b)+i-arena_start)>>_PageShift] {
break
}
// Consult GC bitmap. // Slow for now as we serialize this, since this is on a debug path
bits = uintptr(*(*byte)(ptrbitp)) // speed is not critical at this point.
var andlock mutex
if wordsPerBitmapByte != 2 { func atomicand8(src *byte, val byte) {
gothrow("alg doesn't work for wordsPerBitmapByte != 2") lock(&andlock)
} *src &= val
j := (uintptr(b) + i) / ptrSize & 1 unlock(&andlock)
ptrbitp = add(ptrbitp, -j) }
bits >>= gcBits * j
if bits&bitBoundary != 0 && i != 0 { // Mark using the checkmark scheme.
break // reached beginning of the next object func docheckmark(mbits *markbits) {
} // xor 01 moves 01(scalar unmarked) to 00(scalar marked)
bits = (bits >> 2) & bitsMask // and 10(pointer unmarked) to 11(pointer marked)
if bits == bitsDead { if mbits.tbits == _BitsScalar {
break // reached no-scan part of the object atomicand8(mbits.bitp, ^byte(_BitsCheckMarkXor<<mbits.shift<<2))
} } else if mbits.tbits == _BitsPointer {
} else { atomicor8(mbits.bitp, byte(_BitsCheckMarkXor<<mbits.shift<<2))
// dense mask (stack or data) }
bits = (uintptr(*(*byte)(add(unsafe.Pointer(ptrmask), (i/ptrSize)/4))) >> (((i / ptrSize) % 4) * bitsPerPointer)) & bitsMask
}
if bits <= _BitsScalar { // BitsScalar || BitsDead // reload bits for ischeckmarked
continue mbits.xbits = *mbits.bitp
} mbits.bits = (mbits.xbits >> mbits.shift) & bitMask
mbits.tbits = ((mbits.xbits >> mbits.shift) & bitPtrMask) >> 2
}
if bits != _BitsPointer { // In the default scheme does mbits refer to a marked object.
gothrow("unexpected garbage collection bits") func ismarked(mbits *markbits) bool {
} if mbits.bits&bitBoundary != bitBoundary {
gothrow("ismarked: bits should have boundary bit set")
}
return mbits.bits&bitMarked == bitMarked
}
obj := *(*uintptr)(unsafe.Pointer(b + i)) // In the checkmark scheme does mbits refer to a marked object.
obj0 := obj func ischeckmarked(mbits *markbits) bool {
if mbits.bits&bitBoundary != bitBoundary {
gothrow("ischeckmarked: bits should have boundary bit set")
}
return mbits.tbits == _BitsScalarMarked || mbits.tbits == _BitsPointerMarked
}
markobj: // When in GCmarkterminate phase we allocate black.
var s *mspan func gcmarknewobject_m(obj uintptr) {
var off, bitp, shift, xbits uintptr if gcphase != _GCmarktermination {
gothrow("marking new object while not in mark termination phase")
}
if checkmark { // The world should be stopped so this should not happen.
gothrow("gcmarknewobject called while doing checkmark")
}
// At this point we have extracted the next potential pointer. var mbits markbits
// Check if it points into heap. slottombits(obj, &mbits)
if obj == 0 { if mbits.bits&bitMarked != 0 {
continue return
} }
if obj < arena_start || arena_used <= obj {
if uintptr(obj) < _PhysPageSize && invalidptr != 0 {
s = nil
goto badobj
}
continue
}
// Mark the object. // Each byte of GC bitmap holds info for two words.
obj &^= ptrSize - 1 // If the current object is larger than two words, or if the object is one word
off = (obj - arena_start) / ptrSize // but the object it shares the byte with is already marked,
bitp = arena_start - off/wordsPerBitmapByte - 1 // then all the possible concurrent updates are trying to set the same bit,
shift = (off % wordsPerBitmapByte) * gcBits // so we can use a non-atomic update.
xbits = uintptr(*(*byte)(unsafe.Pointer(bitp))) if mbits.xbits&(bitMask|(bitMask<<gcBits)) != bitBoundary|bitBoundary<<gcBits || work.nproc == 1 {
bits = (xbits >> shift) & bitMask *mbits.bitp = mbits.xbits | bitMarked<<mbits.shift
if (bits & bitBoundary) == 0 { } else {
// Not a beginning of a block, consult span table to find the block beginning. atomicor8(mbits.bitp, bitMarked<<mbits.shift)
k := pageID(obj >> _PageShift) }
x := k }
x -= pageID(arena_start >> _PageShift)
s = h_spans[x] // obj is the start of an object with mark mbits.
if s == nil || k < s.start || s.limit <= obj || s.state != mSpanInUse { // If it isn't already marked, mark it and enqueue into workbuf.
// Stack pointers lie within the arena bounds but are not part of the GC heap. // Return possibly new workbuf to use.
// Ignore them. func greyobject(obj uintptr, mbits *markbits, wbuf *workbuf) *workbuf {
if s != nil && s.state == _MSpanStack { // obj should be start of allocation, and so must be at least pointer-aligned.
continue if obj&(ptrSize-1) != 0 {
} gothrow("greyobject: obj not pointer-aligned")
goto badobj }
}
p := uintptr(s.start) << _PageShift if checkmark {
if s.sizeclass != 0 { if !ismarked(mbits) {
size := s.elemsize print("runtime:greyobject: checkmarks finds unexpected unmarked object obj=", hex(obj), ", mbits->bits=", hex(mbits.bits), " *mbits->bitp=", hex(*mbits.bitp), "\n")
idx := (obj - p) / size
p = p + idx*size k := obj >> _PageShift
} x := k
if p == obj { x -= mheap_.arena_start >> _PageShift
print("runtime: failed to find block beginning for ", hex(p), " s=", hex(s.start*_PageSize), " s.limit=", hex(s.limit), "\n") s := h_spans[x]
gothrow("failed to find block beginning") printlock()
print("runtime:greyobject Span: obj=", hex(obj), " k=", hex(k))
if s == nil {
print(" s=nil\n")
} else {
print(" s.start=", hex(s.start*_PageSize), " s.limit=", hex(s.limit), " s.sizeclass=", s.sizeclass, " s.elemsize=", s.elemsize, "\n")
// NOTE(rsc): This code is using s.sizeclass as an approximation of the
// number of pointer-sized words in an object. Perhaps not what was intended.
for i := 0; i < int(s.sizeclass); i++ {
print(" *(obj+", i*ptrSize, ") = ", hex(*(*uintptr)(unsafe.Pointer(obj + uintptr(i)*ptrSize))), "\n")
} }
obj = p
goto markobj
} }
gothrow("checkmark found unmarked object")
}
if ischeckmarked(mbits) {
return wbuf
}
docheckmark(mbits)
if !ischeckmarked(mbits) {
print("mbits xbits=", hex(mbits.xbits), " bits=", hex(mbits.bits), " tbits=", hex(mbits.tbits), " shift=", mbits.shift, "\n")
gothrow("docheckmark and ischeckmarked disagree")
}
} else {
// If marked we have nothing to do.
if mbits.bits&bitMarked != 0 {
return wbuf
}
if _DebugGCPtrs { // Each byte of GC bitmap holds info for two words.
print("scan *", hex(b+i), " = ", hex(obj0), " => base ", hex(obj), "\n") // If the current object is larger than two words, or if the object is one word
} // but the object it shares the byte with is already marked,
// then all the possible concurrent updates are trying to set the same bit,
// so we can use a non-atomic update.
if mbits.xbits&(bitMask|bitMask<<gcBits) != bitBoundary|bitBoundary<<gcBits || work.nproc == 1 {
*mbits.bitp = mbits.xbits | bitMarked<<mbits.shift
} else {
atomicor8(mbits.bitp, bitMarked<<mbits.shift)
}
}
if nbadblock > 0 && obj == badblock[nbadblock-1] { if !checkmark && (mbits.xbits>>(mbits.shift+2))&_BitsMask == _BitsDead {
// Running garbage collection again because return wbuf // noscan object
// we want to find the path from a root to a bad pointer. }
// Found possible next step; extend or finish path.
for j := int32(0); j < nbadblock; j++ { // Queue the obj for scanning. The PREFETCH(obj) logic has been removed but
if badblock[j] == b { // seems like a nice optimization that can be added back in.
goto AlreadyBad // There needs to be time between the PREFETCH and the use.
} // Previously we put the obj in an 8 element buffer that is drained at a rate
} // to give the PREFETCH time to do its work.
print("runtime: found *(", hex(b), "+", hex(i), ") = ", hex(obj0), "+", hex(obj-obj0), "\n") // Use of PREFETCHNTA might be more appropriate than PREFETCH
if ptrmask != nil {
gothrow("bad pointer") // If workbuf is full, obtain an empty one.
} if wbuf.nobj >= uintptr(len(wbuf.obj)) {
if nbadblock >= int32(len(badblock)) { wbuf = getempty(wbuf)
gothrow("badblock trace too long") }
}
badblock[nbadblock] = uintptr(b) wbuf.obj[wbuf.nobj] = obj
nbadblock++ wbuf.nobj++
AlreadyBad: return wbuf
}
// Scan the object b of size n, adding pointers to wbuf.
// Return possibly new wbuf to use.
// If ptrmask != nil, it specifies where pointers are in b.
// If ptrmask == nil, the GC bitmap should be consulted.
// In this case, n may be an overestimate of the size; the GC bitmap
// must also be used to make sure the scan stops at the end of b.
func scanobject(b, n uintptr, ptrmask *uint8, wbuf *workbuf) *workbuf {
arena_start := mheap_.arena_start
arena_used := mheap_.arena_used
// Find bits of the beginning of the object.
var ptrbitp unsafe.Pointer
var mbits markbits
if ptrmask == nil {
b = objectstart(b, &mbits)
if b == 0 {
return wbuf
}
ptrbitp = unsafe.Pointer(mbits.bitp)
}
for i := uintptr(0); i < n; i += ptrSize {
// Find bits for this word.
var bits uintptr
if ptrmask != nil {
// dense mask (stack or data)
bits = (uintptr(*(*byte)(add(unsafe.Pointer(ptrmask), (i/ptrSize)/4))) >> (((i / ptrSize) % 4) * bitsPerPointer)) & bitsMask
} else {
// Check if we have reached end of span.
// n is an overestimate of the size of the object.
if (b+i)%_PageSize == 0 && h_spans[(b-arena_start)>>_PageShift] != h_spans[(b+i-arena_start)>>_PageShift] {
break
} }
// Now we have bits, bitp, and shift correct for // Consult GC bitmap.
// obj pointing at the base of the object. bits = uintptr(*(*byte)(ptrbitp))
// Only care about not marked objects. if wordsPerBitmapByte != 2 {
if bits&bitMarked != 0 { gothrow("alg doesn't work for wordsPerBitmapByte != 2")
continue
} }
j := (uintptr(b) + i) / ptrSize & 1 // j indicates upper nibble or lower nibble
bits >>= gcBits * j
if i == 0 {
bits &^= bitBoundary
}
ptrbitp = add(ptrbitp, -j)
// If obj size is greater than 8, then each byte of GC bitmap if bits&bitBoundary != 0 && i != 0 {
// contains info for at most one object. In such case we use break // reached beginning of the next object
// non-atomic byte store to mark the object. This can lead
// to double enqueue of the object for scanning, but scanning
// is an idempotent operation, so it is OK. This cannot lead
// to bitmap corruption because the single marked bit is the
// only thing that can change in the byte.
// For 8-byte objects we use non-atomic store, if the other
// quadruple is already marked. Otherwise we resort to CAS
// loop for marking.
if xbits&(bitMask|bitMask<<gcBits) != bitBoundary|bitBoundary<<gcBits || work.nproc == 1 {
*(*byte)(unsafe.Pointer(bitp)) = uint8(xbits | bitMarked<<shift)
} else {
atomicor8((*byte)(unsafe.Pointer(bitp)), bitMarked<<shift)
} }
bits = (bits & bitPtrMask) >> 2 // bits refer to the type bits.
if (xbits>>(shift+2))&bitsMask == bitsDead { if i != 0 && bits == bitsDead { // BitsDead in first nibble not valid during checkmark
continue // noscan object break // reached no-scan part of the object
} }
}
// Queue the obj for scanning. if bits <= _BitsScalar { // _BitsScalar, _BitsDead, _BitsScalarMarked
// TODO: PREFETCH here. continue
}
// If workbuf is full, obtain an empty one. if bits&_BitsPointer != _BitsPointer {
if nobj >= uintptr(len(wbuf.obj)) { print("gc checkmark=", checkmark, " b=", hex(b), " ptrmask=", ptrmask, " mbits.bitp=", mbits.bitp, " mbits.xbits=", hex(mbits.xbits), " bits=", hex(bits), "\n")
wbuf.nobj = nobj gothrow("unexpected garbage collection bits")
wbuf = getempty(wbuf) }
nobj = wbuf.nobj
wp = &wbuf.obj[nobj] obj := *(*uintptr)(unsafe.Pointer(b + i))
}
*wp = obj // At this point we have extracted the next potential pointer.
nobj++ // Check if it points into heap.
if nobj < uintptr(len(wbuf.obj)) { if obj == 0 || obj < arena_start || obj >= arena_used {
wp = &wbuf.obj[nobj] continue
} else { }
wp = nil
} // Mark the object. return some important bits.
// We we combine the following two rotines we don't have to pass mbits or obj around.
var mbits markbits
obj = objectstart(obj, &mbits)
if obj == 0 {
continue continue
}
wbuf = greyobject(obj, &mbits, wbuf)
}
return wbuf
}
badobj: // scanblock starts by scanning b as scanobject would.
// If cgo_allocate is linked into the binary, it can allocate // If the gcphase is GCscan, that's all scanblock does.
// memory as []unsafe.Pointer that may not contain actual // Otherwise it traverses some fraction of the pointers it found in b, recursively.
// pointers and must be scanned conservatively. // As a special case, scanblock(nil, 0, nil) means to scan previously queued work,
// In this case alone, allow the bad pointer. // stopping only when no work is left in the system.
if have_cgo_allocate() && ptrmask == nil { func scanblock(b, n uintptr, ptrmask *uint8) {
continue wbuf := getpartialorempty()
if b != 0 {
wbuf = scanobject(b, n, ptrmask, wbuf)
if gcphase == _GCscan {
if inheap(b) && ptrmask == nil {
// b is in heap, we are in GCscan so there should be a ptrmask.
gothrow("scanblock: In GCscan phase and inheap is true.")
} }
// GCscan only goes one level deep since mark wb not turned on.
putpartial(wbuf)
return
}
}
if gcphase == _GCscan {
gothrow("scanblock: In GCscan phase but no b passed in.")
}
// Anything else indicates a bug somewhere. keepworking := b == 0
// If we're in the middle of chasing down a different bad pointer,
// don't confuse the trace by printing about this one.
if nbadblock > 0 {
continue
}
print("runtime: garbage collector found invalid heap pointer *(", hex(b), "+", hex(i), ")=", hex(obj)) // ptrmask can have 2 possible values:
if s == nil { // 1. nil - obtain pointer mask from GC bitmap.
print(" s=nil\n") // 2. pointer to a compact mask (for stacks and data).
} else { for {
print(" span=", uintptr(s.start)<<_PageShift, "-", s.limit, "-", (uintptr(s.start)+s.npages)<<_PageShift, " state=", s.state, "\n") if wbuf.nobj == 0 {
if !keepworking {
putempty(wbuf)
return
} }
if ptrmask != nil { // Refill workbuf from global queue.
gothrow("invalid heap pointer") wbuf = getfull(wbuf)
if wbuf == nil { // nil means out of work barrier reached
return
} }
// Add to badblock list, which will cause the garbage collection
// to keep repeating until it has traced the chain of pointers if wbuf.nobj <= 0 {
// leading to obj all the way back to a root. gothrow("runtime:scanblock getfull returns empty buffer")
if nbadblock == 0 {
badblock[nbadblock] = uintptr(b)
nbadblock++
} }
} }
if _DebugGCPtrs {
print("end scanblock ", hex(b), " +", hex(n), " ", ptrmask, "\n") // If another proc wants a pointer, give it some.
} if work.nwait > 0 && wbuf.nobj > 4 && work.full == 0 {
if _DebugGC > 0 && ptrmask == nil { wbuf = handoff(wbuf)
// For heap objects ensure that we did not overscan.
var p, n uintptr
if mlookup(b, &p, &n, nil) == 0 || b != p || i > n {
print("runtime: scanned (", hex(b), "+", hex(i), "), heap object (", hex(p), "+", hex(n), ")\n")
gothrow("scanblock: scanned invalid object")
}
} }
// This might be a good place to add prefetch code...
// if(wbuf->nobj > 4) {
// PREFETCH(wbuf->obj[wbuf->nobj - 3];
// }
wbuf.nobj--
b = wbuf.obj[wbuf.nobj]
wbuf = scanobject(b, mheap_.arena_used-b, nil, wbuf)
} }
} }
...@@ -455,7 +637,8 @@ func markroot(desc *parfor, i uint32) { ...@@ -455,7 +637,8 @@ func markroot(desc *parfor, i uint32) {
if s.state != mSpanInUse { if s.state != mSpanInUse {
continue continue
} }
if s.sweepgen != sg { if !checkmark && s.sweepgen != sg {
// sweepgen was updated (+2) during non-checkmark GC pass
print("sweep ", s.sweepgen, " ", sg, "\n") print("sweep ", s.sweepgen, " ", sg, "\n")
gothrow("gc: unswept span") gothrow("gc: unswept span")
} }
...@@ -468,13 +651,17 @@ func markroot(desc *parfor, i uint32) { ...@@ -468,13 +651,17 @@ func markroot(desc *parfor, i uint32) {
spf := (*specialfinalizer)(unsafe.Pointer(sp)) spf := (*specialfinalizer)(unsafe.Pointer(sp))
// A finalizer can be set for an inner byte of an object, find object beginning. // A finalizer can be set for an inner byte of an object, find object beginning.
p := uintptr(s.start<<_PageShift) + uintptr(spf.special.offset)/s.elemsize*s.elemsize p := uintptr(s.start<<_PageShift) + uintptr(spf.special.offset)/s.elemsize*s.elemsize
scanblock(p, s.elemsize, nil) if gcphase != _GCscan {
scanblock(p, s.elemsize, nil) // scanned during mark phase
}
scanblock(uintptr(unsafe.Pointer(&spf.fn)), ptrSize, &oneptr[0]) scanblock(uintptr(unsafe.Pointer(&spf.fn)), ptrSize, &oneptr[0])
} }
} }
case _RootFlushCaches: case _RootFlushCaches:
flushallmcaches() if gcphase != _GCscan { // Do not flush mcaches during GCscan phase.
flushallmcaches()
}
default: default:
// the rest is scanning goroutine stacks // the rest is scanning goroutine stacks
...@@ -482,21 +669,44 @@ func markroot(desc *parfor, i uint32) { ...@@ -482,21 +669,44 @@ func markroot(desc *parfor, i uint32) {
gothrow("markroot: bad index") gothrow("markroot: bad index")
} }
gp := allgs[i-_RootCount] gp := allgs[i-_RootCount]
// remember when we've first observed the G blocked // remember when we've first observed the G blocked
// needed only to output in traceback // needed only to output in traceback
status := readgstatus(gp) status := readgstatus(gp) // We are not in a scan state
if (status == _Gwaiting || status == _Gsyscall) && gp.waitsince == 0 { if (status == _Gwaiting || status == _Gsyscall) && gp.waitsince == 0 {
gp.waitsince = work.tstart gp.waitsince = work.tstart
} }
// Shrink a stack if not much of it is being used.
shrinkstack(gp) // Shrink a stack if not much of it is being used but not in the scan phase.
if gcphase != _GCscan { // Do not shrink during GCscan phase.
shrinkstack(gp)
}
if readgstatus(gp) == _Gdead { if readgstatus(gp) == _Gdead {
gp.gcworkdone = true gp.gcworkdone = true
} else { } else {
gp.gcworkdone = false gp.gcworkdone = false
} }
restart := stopg(gp) restart := stopg(gp)
scanstack(gp)
// goroutine will scan its own stack when it stops running.
// Wait until it has.
for readgstatus(gp) == _Grunning && !gp.gcworkdone {
}
// scanstack(gp) is done as part of gcphasework
// But to make sure we finished we need to make sure that
// the stack traps have all responded so drop into
// this while loop until they respond.
for !gp.gcworkdone {
status = readgstatus(gp)
if status == _Gdead {
gp.gcworkdone = true // scan is a noop
break
}
if status == _Gwaiting || status == _Grunnable {
restart = stopg(gp)
}
}
if restart { if restart {
restartg(gp) restartg(gp)
} }
...@@ -506,48 +716,83 @@ func markroot(desc *parfor, i uint32) { ...@@ -506,48 +716,83 @@ func markroot(desc *parfor, i uint32) {
// Get an empty work buffer off the work.empty list, // Get an empty work buffer off the work.empty list,
// allocating new buffers as needed. // allocating new buffers as needed.
func getempty(b *workbuf) *workbuf { func getempty(b *workbuf) *workbuf {
_g_ := getg()
if b != nil { if b != nil {
lfstackpush(&work.full, &b.node) putfull(b)
b = nil
} }
b = nil if work.empty != 0 {
c := _g_.m.mcache
if c.gcworkbuf != nil {
b = (*workbuf)(c.gcworkbuf)
c.gcworkbuf = nil
}
if b == nil {
b = (*workbuf)(lfstackpop(&work.empty)) b = (*workbuf)(lfstackpop(&work.empty))
} }
if b != nil && b.nobj != 0 {
_g_ := getg()
print("m", _g_.m.id, ": getempty: popped b=", b, " with non-zero b.nobj=", b.nobj, "\n")
gothrow("getempty: workbuffer not empty, b->nobj not 0")
}
if b == nil { if b == nil {
b = (*workbuf)(persistentalloc(unsafe.Sizeof(*b), _CacheLineSize, &memstats.gc_sys)) b = (*workbuf)(persistentalloc(unsafe.Sizeof(*b), _CacheLineSize, &memstats.gc_sys))
b.nobj = 0
} }
b.nobj = 0
return b return b
} }
func putempty(b *workbuf) { func putempty(b *workbuf) {
_g_ := getg() if b.nobj != 0 {
c := _g_.m.mcache gothrow("putempty: b->nobj not 0")
if c.gcworkbuf == nil {
c.gcworkbuf = (unsafe.Pointer)(b)
return
} }
lfstackpush(&work.empty, &b.node) lfstackpush(&work.empty, &b.node)
} }
func gcworkbuffree(b unsafe.Pointer) { func putfull(b *workbuf) {
if b != nil { if b.nobj <= 0 {
putempty((*workbuf)(b)) gothrow("putfull: b->nobj <= 0")
}
lfstackpush(&work.full, &b.node)
}
// Get an partially empty work buffer
// if none are available get an empty one.
func getpartialorempty() *workbuf {
b := (*workbuf)(lfstackpop(&work.partial))
if b == nil {
b = getempty(nil)
}
return b
}
func putpartial(b *workbuf) {
if b.nobj == 0 {
lfstackpush(&work.empty, &b.node)
} else if b.nobj < uintptr(len(b.obj)) {
lfstackpush(&work.partial, &b.node)
} else if b.nobj == uintptr(len(b.obj)) {
lfstackpush(&work.full, &b.node)
} else {
print("b=", b, " b.nobj=", b.nobj, " len(b.obj)=", len(b.obj), "\n")
gothrow("putpartial: bad Workbuf b.nobj")
} }
} }
// Get a full work buffer off the work.full list, or return nil. // Get a full work buffer off the work.full or a partially
// filled one off the work.partial list. If nothing is available
// wait until all the other gc helpers have finished and then
// return nil.
// getfull acts as a barrier for work.nproc helpers. As long as one
// gchelper is actively marking objects it
// may create a workbuffer that the other helpers can work on.
// The for loop either exits when a work buffer is found
// or when _all_ of the work.nproc GC helpers are in the loop
// looking for work and thus not capable of creating new work.
// This is in fact the termination condition for the STW mark
// phase.
func getfull(b *workbuf) *workbuf { func getfull(b *workbuf) *workbuf {
if b != nil { if b != nil {
lfstackpush(&work.empty, &b.node) putempty(b)
} }
b = (*workbuf)(lfstackpop(&work.full)) b = (*workbuf)(lfstackpop(&work.full))
if b == nil {
b = (*workbuf)(lfstackpop(&work.partial))
}
if b != nil || work.nproc == 1 { if b != nil || work.nproc == 1 {
return b return b
} }
...@@ -557,6 +802,9 @@ func getfull(b *workbuf) *workbuf { ...@@ -557,6 +802,9 @@ func getfull(b *workbuf) *workbuf {
if work.full != 0 { if work.full != 0 {
xadd(&work.nwait, -1) xadd(&work.nwait, -1)
b = (*workbuf)(lfstackpop(&work.full)) b = (*workbuf)(lfstackpop(&work.full))
if b == nil {
b = (*workbuf)(lfstackpop(&work.partial))
}
if b != nil { if b != nil {
return b return b
} }
...@@ -675,14 +923,11 @@ func scanframe(frame *stkframe, unused unsafe.Pointer) bool { ...@@ -675,14 +923,11 @@ func scanframe(frame *stkframe, unused unsafe.Pointer) bool {
} }
func scanstack(gp *g) { func scanstack(gp *g) {
// TODO(rsc): Due to a precedence error, this was never checked in the original C version.
// If you enable the check, the gothrow happens. if readgstatus(gp)&_Gscan == 0 {
/* print("runtime:scanstack: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", hex(readgstatus(gp)), "\n")
if readgstatus(gp)&_Gscan == 0 { gothrow("scanstack - bad status")
print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n") }
gothrow("mark - bad status")
}
*/
switch readgstatus(gp) &^ _Gscan { switch readgstatus(gp) &^ _Gscan {
default: default:
...@@ -692,7 +937,7 @@ func scanstack(gp *g) { ...@@ -692,7 +937,7 @@ func scanstack(gp *g) {
return return
case _Grunning: case _Grunning:
print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n") print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n")
gothrow("mark - world not stopped") gothrow("scanstack: goroutine not stopped")
case _Grunnable, _Gsyscall, _Gwaiting: case _Grunnable, _Gsyscall, _Gwaiting:
// ok // ok
} }
...@@ -709,18 +954,114 @@ func scanstack(gp *g) { ...@@ -709,18 +954,114 @@ func scanstack(gp *g) {
tracebackdefers(gp, scanframe, nil) tracebackdefers(gp, scanframe, nil)
} }
// The gp has been moved to a gc safepoint. If there is gcphase specific // If the slot is grey or black return true, if white return false.
// work it is done here. // If the slot is not in the known heap and thus does not have a valid GC bitmap then
// it is considered grey. Globals and stacks can hold such slots.
// The slot is grey if its mark bit is set and it is enqueued to be scanned.
// The slot is black if it has already been scanned.
// It is white if it has a valid mark bit and the bit is not set.
func shaded(slot uintptr) bool {
if !inheap(slot) { // non-heap slots considered grey
return true
}
var mbits markbits
valid := objectstart(slot, &mbits)
if valid == 0 {
return true
}
if checkmark {
return ischeckmarked(&mbits)
}
return mbits.bits&bitMarked != 0
}
// Shade the object if it isn't already.
// The object is not nil and known to be in the heap.
func shade(b uintptr) {
if !inheap(b) {
gothrow("shade: passed an address not in the heap")
}
wbuf := getpartialorempty()
// Mark the object, return some important bits.
// If we combine the following two rotines we don't have to pass mbits or obj around.
var mbits markbits
obj := objectstart(b, &mbits)
if obj != 0 {
wbuf = greyobject(obj, &mbits, wbuf) // augments the wbuf
}
putpartial(wbuf)
}
// This is the Dijkstra barrier coarsened to always shade the ptr (dst) object.
// The original Dijkstra barrier only shaded ptrs being placed in black slots.
//
// Shade indicates that it has seen a white pointer by adding the referent
// to wbuf as well as marking it.
//
// slot is the destination (dst) in go code
// ptr is the value that goes into the slot (src) in the go code
//
// Dijkstra pointed out that maintaining the no black to white
// pointers means that white to white pointers not need
// to be noted by the write barrier. Furthermore if either
// white object dies before it is reached by the
// GC then the object can be collected during this GC cycle
// instead of waiting for the next cycle. Unfortunately the cost of
// ensure that the object holding the slot doesn't concurrently
// change to black without the mutator noticing seems prohibitive.
//
// Consider the following example where the mutator writes into
// a slot and then loads the slot's mark bit while the GC thread
// writes to the slot's mark bit and then as part of scanning reads
// the slot.
//
// Initially both [slot] and [slotmark] are 0 (nil)
// Mutator thread GC thread
// st [slot], ptr st [slotmark], 1
//
// ld r1, [slotmark] ld r2, [slot]
//
// This is a classic example of independent reads of independent writes,
// aka IRIW. The question is if r1==r2==0 is allowed and for most HW the
// answer is yes without inserting a memory barriers between the st and the ld.
// These barriers are expensive so we have decided that we will
// always grey the ptr object regardless of the slot's color.
func gcmarkwb_m(slot *uintptr, ptr uintptr) {
switch gcphase {
default:
gothrow("gcphasework in bad gcphase")
case _GCoff, _GCquiesce, _GCstw, _GCsweep, _GCscan:
// ok
case _GCmark, _GCmarktermination:
if ptr != 0 && inheap(ptr) {
shade(ptr)
}
}
}
// The gp has been moved to a GC safepoint. GC phase specific
// work is done here.
func gcphasework(gp *g) { func gcphasework(gp *g) {
switch gcphase { switch gcphase {
default: default:
gothrow("gcphasework in bad gcphase") gothrow("gcphasework in bad gcphase")
case _GCoff, _GCquiesce, _GCstw, _GCsweep: case _GCoff, _GCquiesce, _GCstw, _GCsweep:
// No work for now. // No work.
case _GCscan:
// scan the stack, mark the objects, put pointers in work buffers
// hanging off the P where this is being run.
scanstack(gp)
case _GCmark: case _GCmark:
// Disabled until concurrent GC is implemented // No work.
// but indicate the scan has been done. case _GCmarktermination:
// scanstack(gp); scanstack(gp)
// All available mark work will be emptied before returning.
} }
gp.gcworkdone = true gp.gcworkdone = true
} }
...@@ -797,6 +1138,7 @@ func iterate_finq(callback func(*funcval, unsafe.Pointer, uintptr, *_type, *ptrt ...@@ -797,6 +1138,7 @@ func iterate_finq(callback func(*funcval, unsafe.Pointer, uintptr, *_type, *ptrt
} }
} }
// Returns only when span s has been swept.
func mSpan_EnsureSwept(s *mspan) { func mSpan_EnsureSwept(s *mspan) {
// Caller must disable preemption. // Caller must disable preemption.
// Otherwise when this function returns the span can become unswept again // Otherwise when this function returns the span can become unswept again
...@@ -810,6 +1152,7 @@ func mSpan_EnsureSwept(s *mspan) { ...@@ -810,6 +1152,7 @@ func mSpan_EnsureSwept(s *mspan) {
if atomicload(&s.sweepgen) == sg { if atomicload(&s.sweepgen) == sg {
return return
} }
// The caller must be sure that the span is a MSpanInUse span.
if cas(&s.sweepgen, sg-2, sg-1) { if cas(&s.sweepgen, sg-2, sg-1) {
mSpan_Sweep(s, false) mSpan_Sweep(s, false)
return return
...@@ -826,6 +1169,10 @@ func mSpan_EnsureSwept(s *mspan) { ...@@ -826,6 +1169,10 @@ func mSpan_EnsureSwept(s *mspan) {
// If preserve=true, don't return it to heap nor relink in MCentral lists; // If preserve=true, don't return it to heap nor relink in MCentral lists;
// caller takes care of it. // caller takes care of it.
func mSpan_Sweep(s *mspan, preserve bool) bool { func mSpan_Sweep(s *mspan, preserve bool) bool {
if checkmark {
gothrow("MSpan_Sweep: checkmark only runs in STW and after the sweep")
}
// It's critical that we enter this function with preemption disabled, // It's critical that we enter this function with preemption disabled,
// GC must not start while we are in the middle of this function. // GC must not start while we are in the middle of this function.
_g_ := getg() _g_ := getg()
...@@ -851,13 +1198,14 @@ func mSpan_Sweep(s *mspan, preserve bool) bool { ...@@ -851,13 +1198,14 @@ func mSpan_Sweep(s *mspan, preserve bool) bool {
} }
res := false res := false
nfree := 0 nfree := 0
var head mlink
end := &head var head, end gclinkptr
c := _g_.m.mcache c := _g_.m.mcache
sweepgenset := false sweepgenset := false
// Mark any free objects in this span so we don't collect them. // Mark any free objects in this span so we don't collect them.
for link := s.freelist; link != nil; link = link.next { for link := s.freelist; link.ptr() != nil; link = link.ptr().next {
off := (uintptr(unsafe.Pointer(link)) - arena_start) / ptrSize off := (uintptr(unsafe.Pointer(link)) - arena_start) / ptrSize
bitp := arena_start - off/wordsPerBitmapByte - 1 bitp := arena_start - off/wordsPerBitmapByte - 1
shift := (off % wordsPerBitmapByte) * gcBits shift := (off % wordsPerBitmapByte) * gcBits
...@@ -978,8 +1326,13 @@ func mSpan_Sweep(s *mspan, preserve bool) bool { ...@@ -978,8 +1326,13 @@ func mSpan_Sweep(s *mspan, preserve bool) bool {
} else if size > ptrSize { } else if size > ptrSize {
*(*uintptr)(unsafe.Pointer(p + ptrSize)) = 0 *(*uintptr)(unsafe.Pointer(p + ptrSize)) = 0
} }
end.next = (*mlink)(unsafe.Pointer(p)) if head.ptr() == nil {
end = end.next head = gclinkptr(p)
} else {
end.ptr().next = gclinkptr(p)
}
end = gclinkptr(p)
end.ptr().next = gclinkptr(0x0bade5)
nfree++ nfree++
} }
} }
...@@ -1002,7 +1355,7 @@ func mSpan_Sweep(s *mspan, preserve bool) bool { ...@@ -1002,7 +1355,7 @@ func mSpan_Sweep(s *mspan, preserve bool) bool {
c.local_nsmallfree[cl] += uintptr(nfree) c.local_nsmallfree[cl] += uintptr(nfree)
c.local_cachealloc -= intptr(uintptr(nfree) * size) c.local_cachealloc -= intptr(uintptr(nfree) * size)
xadd64(&memstats.next_gc, -int64(nfree)*int64(size)*int64(gcpercent+100)/100) xadd64(&memstats.next_gc, -int64(nfree)*int64(size)*int64(gcpercent+100)/100)
res = mCentral_FreeSpan(&mheap_.central[cl].mcentral, s, int32(nfree), head.next, end, preserve) res = mCentral_FreeSpan(&mheap_.central[cl].mcentral, s, int32(nfree), head, end, preserve)
// MCentral_FreeSpan updates sweepgen // MCentral_FreeSpan updates sweepgen
} }
return res return res
...@@ -1073,11 +1426,11 @@ func gchelper() { ...@@ -1073,11 +1426,11 @@ func gchelper() {
_g_.m.traceback = 2 _g_.m.traceback = 2
gchelperstart() gchelperstart()
// parallel mark for over gc roots // parallel mark for over GC roots
parfordo(work.markfor) parfordo(work.markfor)
if gcphase != _GCscan {
// help other threads scan secondary blocks scanblock(0, 0, nil) // blocks in getfull
scanblock(0, 0, nil) }
nproc := work.nproc // work.nproc can change right after we increment work.ndone nproc := work.nproc // work.nproc can change right after we increment work.ndone
if xadd(&work.ndone, +1) == nproc-1 { if xadd(&work.ndone, +1) == nproc-1 {
...@@ -1204,6 +1557,7 @@ func gcinit() { ...@@ -1204,6 +1557,7 @@ func gcinit() {
gcbssmask = unrollglobgcprog((*byte)(unsafe.Pointer(&gcbss)), uintptr(unsafe.Pointer(&ebss))-uintptr(unsafe.Pointer(&bss))) gcbssmask = unrollglobgcprog((*byte)(unsafe.Pointer(&gcbss)), uintptr(unsafe.Pointer(&ebss))-uintptr(unsafe.Pointer(&bss)))
} }
// Called from malloc.go using onM, stopping and starting the world handled in caller.
func gc_m(start_time int64, eagersweep bool) { func gc_m(start_time int64, eagersweep bool) {
_g_ := getg() _g_ := getg()
gp := _g_.m.curg gp := _g_.m.curg
...@@ -1211,18 +1565,266 @@ func gc_m(start_time int64, eagersweep bool) { ...@@ -1211,18 +1565,266 @@ func gc_m(start_time int64, eagersweep bool) {
gp.waitreason = "garbage collection" gp.waitreason = "garbage collection"
gc(start_time, eagersweep) gc(start_time, eagersweep)
casgstatus(gp, _Gwaiting, _Grunning)
}
// Similar to clearcheckmarkbits but works on a single span.
// It preforms two tasks.
// 1. When used before the checkmark phase it converts BitsDead (00) to bitsScalar (01)
// for nibbles with the BoundaryBit set.
// 2. When used after the checkmark phase it converts BitsPointerMark (11) to BitsPointer 10 and
// BitsScalarMark (00) to BitsScalar (01), thus clearing the checkmark mark encoding.
// For the second case it is possible to restore the BitsDead pattern but since
// clearmark is a debug tool performance has a lower priority than simplicity.
// The span is MSpanInUse and the world is stopped.
func clearcheckmarkbitsspan(s *mspan) {
if s.state != _MSpanInUse {
print("runtime:clearcheckmarkbitsspan: state=", s.state, "\n")
gothrow("clearcheckmarkbitsspan: bad span state")
}
if nbadblock > 0 { arena_start := mheap_.arena_start
// Work out path from root to bad block. cl := s.sizeclass
for { size := s.elemsize
gc(start_time, eagersweep) var n int32
if nbadblock >= int32(len(badblock)) { if cl == 0 {
gothrow("cannot find path to bad pointer") n = 1
} else {
// Chunk full of small blocks
npages := class_to_allocnpages[cl]
n = npages << _PageShift / int32(size)
}
// MSpan_Sweep has similar code but instead of overloading and
// complicating that routine we do a simpler walk here.
// Sweep through n objects of given size starting at p.
// This thread owns the span now, so it can manipulate
// the block bitmap without atomic operations.
p := uintptr(s.start) << _PageShift
// Find bits for the beginning of the span.
off := (p - arena_start) / ptrSize
bitp := (*byte)(unsafe.Pointer(arena_start - off/wordsPerBitmapByte - 1))
step := size / (ptrSize * wordsPerBitmapByte)
// The type bit values are:
// 00 - BitsDead, for us BitsScalarMarked
// 01 - BitsScalar
// 10 - BitsPointer
// 11 - unused, for us BitsPointerMarked
//
// When called to prepare for the checkmark phase (checkmark==1),
// we change BitsDead to BitsScalar, so that there are no BitsScalarMarked
// type bits anywhere.
//
// The checkmark phase marks by changing BitsScalar to BitsScalarMarked
// and BitsPointer to BitsPointerMarked.
//
// When called to clean up after the checkmark phase (checkmark==0),
// we unmark by changing BitsScalarMarked back to BitsScalar and
// BitsPointerMarked back to BitsPointer.
//
// There are two problems with the scheme as just described.
// First, the setup rewrites BitsDead to BitsScalar, but the type bits
// following a BitsDead are uninitialized and must not be used.
// Second, objects that are free are expected to have their type
// bits zeroed (BitsDead), so in the cleanup we need to restore
// any BitsDeads that were there originally.
//
// In a one-word object (8-byte allocation on 64-bit system),
// there is no difference between BitsScalar and BitsDead, because
// neither is a pointer and there are no more words in the object,
// so using BitsScalar during the checkmark is safe and mapping
// both back to BitsDead during cleanup is also safe.
//
// In a larger object, we need to be more careful. During setup,
// if the type of the first word is BitsDead, we change it to BitsScalar
// (as we must) but also initialize the type of the second
// word to BitsDead, so that a scan during the checkmark phase
// will still stop before seeing the uninitialized type bits in the
// rest of the object. The sequence 'BitsScalar BitsDead' never
// happens in real type bitmaps - BitsDead is always as early
// as possible, so immediately after the last BitsPointer.
// During cleanup, if we see a BitsScalar, we can check to see if it
// is followed by BitsDead. If so, it was originally BitsDead and
// we can change it back.
if step == 0 {
// updating top and bottom nibbles, all boundaries
for i := int32(0); i < n/2; i, bitp = i+1, addb(bitp, uintptrMask&-1) {
if *bitp&bitBoundary == 0 {
gothrow("missing bitBoundary")
}
b := (*bitp & bitPtrMask) >> 2
if !checkmark && (b == _BitsScalar || b == _BitsScalarMarked) {
*bitp &^= 0x0c // convert to _BitsDead
} else if b == _BitsScalarMarked || b == _BitsPointerMarked {
*bitp &^= _BitsCheckMarkXor << 2
}
if (*bitp>>gcBits)&bitBoundary == 0 {
gothrow("missing bitBoundary")
}
b = ((*bitp >> gcBits) & bitPtrMask) >> 2
if !checkmark && (b == _BitsScalar || b == _BitsScalarMarked) {
*bitp &^= 0xc0 // convert to _BitsDead
} else if b == _BitsScalarMarked || b == _BitsPointerMarked {
*bitp &^= _BitsCheckMarkXor << (2 + gcBits)
}
}
} else {
// updating bottom nibble for first word of each object
for i := int32(0); i < n; i, bitp = i+1, addb(bitp, -step) {
if *bitp&bitBoundary == 0 {
gothrow("missing bitBoundary")
}
b := (*bitp & bitPtrMask) >> 2
if checkmark && b == _BitsDead {
// move BitsDead into second word.
// set bits to BitsScalar in preparation for checkmark phase.
*bitp &^= 0xc0
*bitp |= _BitsScalar << 2
} else if !checkmark && (b == _BitsScalar || b == _BitsScalarMarked) && *bitp&0xc0 == 0 {
// Cleaning up after checkmark phase.
// First word is scalar or dead (we forgot)
// and second word is dead.
// First word might as well be dead too.
*bitp &^= 0x0c
} else if b == _BitsScalarMarked || b == _BitsPointerMarked {
*bitp ^= _BitsCheckMarkXor << 2
} }
} }
} }
}
casgstatus(gp, _Gwaiting, _Grunning) // clearcheckmarkbits preforms two tasks.
// 1. When used before the checkmark phase it converts BitsDead (00) to bitsScalar (01)
// for nibbles with the BoundaryBit set.
// 2. When used after the checkmark phase it converts BitsPointerMark (11) to BitsPointer 10 and
// BitsScalarMark (00) to BitsScalar (01), thus clearing the checkmark mark encoding.
// This is a bit expensive but preserves the BitsDead encoding during the normal marking.
// BitsDead remains valid for every nibble except the ones with BitsBoundary set.
func clearcheckmarkbits() {
for _, s := range work.spans {
if s.state == _MSpanInUse {
clearcheckmarkbitsspan(s)
}
}
}
// Called from malloc.go using onM.
// The world is stopped. Rerun the scan and mark phases
// using the bitMarkedCheck bit instead of the
// bitMarked bit. If the marking encounters an
// bitMarked bit that is not set then we throw.
func gccheckmark_m(startTime int64, eagersweep bool) {
if !gccheckmarkenable {
return
}
if checkmark {
gothrow("gccheckmark_m, entered with checkmark already true")
}
checkmark = true
clearcheckmarkbits() // Converts BitsDead to BitsScalar.
gc_m(startTime, eagersweep) // turns off checkmark
// Work done, fixed up the GC bitmap to remove the checkmark bits.
clearcheckmarkbits()
}
func gccheckmarkenable_m() {
gccheckmarkenable = true
}
func gccheckmarkdisable_m() {
gccheckmarkenable = false
}
func finishsweep_m() {
// The world is stopped so we should be able to complete the sweeps
// quickly.
for sweepone() != ^uintptr(0) {
sweep.npausesweep++
}
// There may be some other spans being swept concurrently that
// we need to wait for. If finishsweep_m is done with the world stopped
// this code is not required.
sg := mheap_.sweepgen
for _, s := range work.spans {
if s.sweepgen != sg && s.state == _MSpanInUse {
mSpan_EnsureSwept(s)
}
}
}
// Scan all of the stacks, greying (or graying if in America) the referents
// but not blackening them since the mark write barrier isn't installed.
func gcscan_m() {
_g_ := getg()
// Grab the g that called us and potentially allow rescheduling.
// This allows it to be scanned like other goroutines.
mastergp := _g_.m.curg
casgstatus(mastergp, _Grunning, _Gwaiting)
mastergp.waitreason = "garbage collection scan"
// Span sweeping has been done by finishsweep_m.
// Long term we will want to make this goroutine runnable
// by placing it onto a scanenqueue state and then calling
// runtime·restartg(mastergp) to make it Grunnable.
// At the bottom we will want to return this p back to the scheduler.
oldphase := gcphase
// Prepare flag indicating that the scan has not been completed.
lock(&allglock)
local_allglen := allglen
for i := uintptr(0); i < local_allglen; i++ {
gp := allgs[i]
gp.gcworkdone = false // set to true in gcphasework
}
unlock(&allglock)
work.nwait = 0
work.ndone = 0
work.nproc = 1 // For now do not do this in parallel.
gcphase = _GCscan
// ackgcphase is not needed since we are not scanning running goroutines.
parforsetup(work.markfor, work.nproc, uint32(_RootCount+local_allglen), nil, false, markroot)
parfordo(work.markfor)
lock(&allglock)
// Check that gc work is done.
for i := uintptr(0); i < local_allglen; i++ {
gp := allgs[i]
if !gp.gcworkdone {
gothrow("scan missed a g")
}
}
unlock(&allglock)
gcphase = oldphase
casgstatus(mastergp, _Gwaiting, _Grunning)
// Let the g that called us continue to run.
}
// Mark all objects that are known about.
func gcmark_m() {
scanblock(0, 0, nil)
}
// For now this must be bracketed with a stoptheworld and a starttheworld to ensure
// all go routines see the new barrier.
func gcinstallmarkwb_m() {
gcphase = _GCmark
}
// For now this must be bracketed with a stoptheworld and a starttheworld to ensure
// all go routines see the new barrier.
func gcinstalloffwb_m() {
gcphase = _GCoff
} }
func gc(start_time int64, eagersweep bool) { func gc(start_time int64, eagersweep bool) {
...@@ -1244,9 +1846,8 @@ func gc(start_time int64, eagersweep bool) { ...@@ -1244,9 +1846,8 @@ func gc(start_time int64, eagersweep bool) {
t1 = nanotime() t1 = nanotime()
} }
// Sweep what is not sweeped by bgsweep. if !checkmark {
for sweepone() != ^uintptr(0) { finishsweep_m() // skip during checkmark debug phase.
sweep.npausesweep++
} }
// Cache runtime.mheap_.allspans in work.spans to avoid conflicts with // Cache runtime.mheap_.allspans in work.spans to avoid conflicts with
...@@ -1266,10 +1867,19 @@ func gc(start_time int64, eagersweep bool) { ...@@ -1266,10 +1867,19 @@ func gc(start_time int64, eagersweep bool) {
mheap_.gcspans = mheap_.allspans mheap_.gcspans = mheap_.allspans
work.spans = h_allspans work.spans = h_allspans
unlock(&mheap_.lock) unlock(&mheap_.lock)
oldphase := gcphase
work.nwait = 0 work.nwait = 0
work.ndone = 0 work.ndone = 0
work.nproc = uint32(gcprocs()) work.nproc = uint32(gcprocs())
gcphase = _GCmarktermination
// World is stopped so allglen will not change.
for i := uintptr(0); i < allglen; i++ {
gp := allgs[i]
gp.gcworkdone = false // set to true in gcphasework
}
parforsetup(work.markfor, work.nproc, uint32(_RootCount+allglen), nil, false, markroot) parforsetup(work.markfor, work.nproc, uint32(_RootCount+allglen), nil, false, markroot)
if work.nproc > 1 { if work.nproc > 1 {
noteclear(&work.alldone) noteclear(&work.alldone)
...@@ -1285,6 +1895,14 @@ func gc(start_time int64, eagersweep bool) { ...@@ -1285,6 +1895,14 @@ func gc(start_time int64, eagersweep bool) {
parfordo(work.markfor) parfordo(work.markfor)
scanblock(0, 0, nil) scanblock(0, 0, nil)
if work.full != 0 {
gothrow("work.full != 0")
}
if work.partial != 0 {
gothrow("work.partial != 0")
}
gcphase = oldphase
var t3 int64 var t3 int64
if debug.gctrace > 0 { if debug.gctrace > 0 {
t3 = nanotime() t3 = nanotime()
...@@ -1349,6 +1967,15 @@ func gc(start_time int64, eagersweep bool) { ...@@ -1349,6 +1967,15 @@ func gc(start_time int64, eagersweep bool) {
sysFree(unsafe.Pointer(&work.spans[0]), uintptr(len(work.spans))*unsafe.Sizeof(work.spans[0]), &memstats.other_sys) sysFree(unsafe.Pointer(&work.spans[0]), uintptr(len(work.spans))*unsafe.Sizeof(work.spans[0]), &memstats.other_sys)
} }
if gccheckmarkenable {
if !checkmark {
// first half of two-pass; don't set up sweep
unlock(&mheap_.lock)
return
}
checkmark = false // done checking marks
}
// Cache the current array for sweeping. // Cache the current array for sweeping.
mheap_.gcspans = mheap_.allspans mheap_.gcspans = mheap_.allspans
mheap_.sweepgen += 2 mheap_.sweepgen += 2
......
...@@ -97,54 +97,155 @@ func bgsweep() { ...@@ -97,54 +97,155 @@ func bgsweep() {
} }
} }
const (
_PoisonGC = 0xf969696969696969 & (1<<(8*ptrSize) - 1)
_PoisonStack = 0x6868686868686868 & (1<<(8*ptrSize) - 1)
)
func needwb() bool {
return gcphase == _GCmark || gcphase == _GCmarktermination
}
// NOTE: Really dst *unsafe.Pointer, src unsafe.Pointer, // NOTE: Really dst *unsafe.Pointer, src unsafe.Pointer,
// but if we do that, Go inserts a write barrier on *dst = src. // but if we do that, Go inserts a write barrier on *dst = src.
//go:nosplit //go:nosplit
func writebarrierptr(dst *uintptr, src uintptr) { func writebarrierptr(dst *uintptr, src uintptr) {
*dst = src *dst = src
if needwb() {
writebarrierptr_nostore(dst, src)
}
}
// Like writebarrierptr, but the store has already been applied.
// Do not reapply.
//go:nosplit
func writebarrierptr_nostore(dst *uintptr, src uintptr) {
if getg() == nil || !needwb() { // very low-level startup
return
}
if src != 0 && (src < _PageSize || src == _PoisonGC || src == _PoisonStack) {
systemstack(func() { gothrow("bad pointer in write barrier") })
}
mp := acquirem()
if mp.inwb || mp.dying > 0 {
releasem(mp)
return
}
mp.inwb = true
systemstack(func() {
gcmarkwb_m(dst, src)
})
mp.inwb = false
releasem(mp)
} }
//go:nosplit //go:nosplit
func writebarrierstring(dst *[2]uintptr, src [2]uintptr) { func writebarrierstring(dst *[2]uintptr, src [2]uintptr) {
dst[0] = src[0] writebarrierptr(&dst[0], src[0])
dst[1] = src[1] dst[1] = src[1]
} }
//go:nosplit //go:nosplit
func writebarrierslice(dst *[3]uintptr, src [3]uintptr) { func writebarrierslice(dst *[3]uintptr, src [3]uintptr) {
dst[0] = src[0] writebarrierptr(&dst[0], src[0])
dst[1] = src[1] dst[1] = src[1]
dst[2] = src[2] dst[2] = src[2]
} }
//go:nosplit //go:nosplit
func writebarrieriface(dst *[2]uintptr, src [2]uintptr) { func writebarrieriface(dst *[2]uintptr, src [2]uintptr) {
dst[0] = src[0] writebarrierptr(&dst[0], src[0])
dst[1] = src[1] writebarrierptr(&dst[1], src[1])
} }
//go:nosplit //go:generate go run wbfat_gen.go -- wbfat.go
func writebarrierfat2(dst *[2]uintptr, _ *byte, src [2]uintptr) { //
dst[0] = src[0] // The above line generates multiword write barriers for
dst[1] = src[1] // all the combinations of ptr+scalar up to four words.
} // The implementations are written to wbfat.go.
//go:nosplit //go:nosplit
func writebarrierfat3(dst *[3]uintptr, _ *byte, src [3]uintptr) { func writebarrierfat(typ *_type, dst, src unsafe.Pointer) {
dst[0] = src[0] if !needwb() {
dst[1] = src[1] memmove(dst, src, typ.size)
dst[2] = src[2] return
} }
//go:nosplit systemstack(func() {
func writebarrierfat4(dst *[4]uintptr, _ *byte, src [4]uintptr) { mask := loadPtrMask(typ)
dst[0] = src[0] nptr := typ.size / ptrSize
dst[1] = src[1] for i := uintptr(0); i < nptr; i += 2 {
dst[2] = src[2] bits := mask[i/2]
dst[3] = src[3] if (bits>>2)&_BitsMask == _BitsPointer {
writebarrierptr((*uintptr)(dst), *(*uintptr)(src))
} else {
*(*uintptr)(dst) = *(*uintptr)(src)
}
dst = add(dst, ptrSize)
src = add(src, ptrSize)
if i+1 == nptr {
break
}
bits >>= 4
if (bits>>2)&_BitsMask == _BitsPointer {
writebarrierptr((*uintptr)(dst), *(*uintptr)(src))
} else {
*(*uintptr)(dst) = *(*uintptr)(src)
}
dst = add(dst, ptrSize)
src = add(src, ptrSize)
}
})
} }
//go:nosplit //go:nosplit
func writebarrierfat(typ *_type, dst, src unsafe.Pointer) { func writebarriercopy(typ *_type, dst, src slice) int {
memmove(dst, src, typ.size) n := dst.len
if n > src.len {
n = src.len
}
if n == 0 {
return 0
}
dstp := unsafe.Pointer(dst.array)
srcp := unsafe.Pointer(src.array)
if !needwb() {
memmove(dstp, srcp, uintptr(n)*typ.size)
return int(n)
}
systemstack(func() {
if uintptr(srcp) < uintptr(dstp) && uintptr(srcp)+uintptr(n)*typ.size > uintptr(dstp) {
// Overlap with src before dst.
// Copy backward, being careful not to move dstp/srcp
// out of the array they point into.
dstp = add(dstp, uintptr(n-1)*typ.size)
srcp = add(srcp, uintptr(n-1)*typ.size)
i := uint(0)
for {
writebarrierfat(typ, dstp, srcp)
if i++; i >= n {
break
}
dstp = add(dstp, -typ.size)
srcp = add(srcp, -typ.size)
}
} else {
// Copy forward, being careful not to move dstp/srcp
// out of the array they point into.
i := uint(0)
for {
writebarrierfat(typ, dstp, srcp)
if i++; i >= n {
break
}
dstp = add(dstp, typ.size)
srcp = add(srcp, typ.size)
}
}
})
return int(n)
} }
...@@ -12,9 +12,11 @@ enum { ...@@ -12,9 +12,11 @@ enum {
BitsPointer = 2, BitsPointer = 2,
BitsMask = 3, BitsMask = 3,
PointersPerByte = 8/BitsPerPointer, PointersPerByte = 8/BitsPerPointer,
MaxGCMask = 64,
insData = 1, insData = 1,
insArray, insArray,
insArrayEnd, insArrayEnd,
insEnd, insEnd,
// 64 bytes cover objects of size 1024/512 on 64/32 bits, respectively.
MaxGCMask = 65536, // TODO(rsc): change back to 64
}; };
...@@ -50,12 +50,15 @@ const ( ...@@ -50,12 +50,15 @@ const (
// If you change these, also change scanblock. // If you change these, also change scanblock.
// scanblock does "if(bits == BitsScalar || bits == BitsDead)" as "if(bits <= BitsScalar)". // scanblock does "if(bits == BitsScalar || bits == BitsDead)" as "if(bits <= BitsScalar)".
_BitsDead = 0 _BitsDead = 0
_BitsScalar = 1 _BitsScalar = 1 // 01
_BitsPointer = 2 _BitsPointer = 2 // 10
_BitsCheckMarkXor = 1 // 10
_BitsScalarMarked = _BitsScalar ^ _BitsCheckMarkXor // 00
_BitsPointerMarked = _BitsPointer ^ _BitsCheckMarkXor // 11
// 64 bytes cover objects of size 1024/512 on 64/32 bits, respectively. // 64 bytes cover objects of size 1024/512 on 64/32 bits, respectively.
_MaxGCMask = 64 _MaxGCMask = 65536 // TODO(rsc): change back to 64
) )
// Bits in per-word bitmap. // Bits in per-word bitmap.
......
...@@ -196,7 +196,7 @@ func mHeap_Alloc_m(h *mheap, npage uintptr, sizeclass int32, large bool) *mspan ...@@ -196,7 +196,7 @@ func mHeap_Alloc_m(h *mheap, npage uintptr, sizeclass int32, large bool) *mspan
// able to map interior pointer to containing span. // able to map interior pointer to containing span.
atomicstore(&s.sweepgen, h.sweepgen) atomicstore(&s.sweepgen, h.sweepgen)
s.state = _MSpanInUse s.state = _MSpanInUse
s.freelist = nil s.freelist = 0
s.ref = 0 s.ref = 0
s.sizeclass = uint8(sizeclass) s.sizeclass = uint8(sizeclass)
if sizeclass == 0 { if sizeclass == 0 {
...@@ -248,7 +248,7 @@ func mHeap_AllocStack(h *mheap, npage uintptr) *mspan { ...@@ -248,7 +248,7 @@ func mHeap_AllocStack(h *mheap, npage uintptr) *mspan {
s := mHeap_AllocSpanLocked(h, npage) s := mHeap_AllocSpanLocked(h, npage)
if s != nil { if s != nil {
s.state = _MSpanStack s.state = _MSpanStack
s.freelist = nil s.freelist = 0
s.ref = 0 s.ref = 0
memstats.stacks_inuse += uint64(s.npages << _PageShift) memstats.stacks_inuse += uint64(s.npages << _PageShift)
} }
...@@ -571,7 +571,7 @@ func mSpan_Init(span *mspan, start pageID, npages uintptr) { ...@@ -571,7 +571,7 @@ func mSpan_Init(span *mspan, start pageID, npages uintptr) {
span.prev = nil span.prev = nil
span.start = start span.start = start
span.npages = npages span.npages = npages
span.freelist = nil span.freelist = 0
span.ref = 0 span.ref = 0
span.sizeclass = 0 span.sizeclass = 0
span.incache = false span.incache = false
......
...@@ -14,8 +14,7 @@ const ( ...@@ -14,8 +14,7 @@ const (
var _vdso uint32 var _vdso uint32
//go:nosplit func sysargs(argc int32, argv **byte) {
func linux_setup_vdso(argc int32, argv **byte) {
// skip over argv, envv to get to auxv // skip over argv, envv to get to auxv
n := argc + 1 n := argc + 1
for argv_index(argv, n) != nil { for argv_index(argv, n) != nil {
......
...@@ -41,7 +41,31 @@ func snprintf(dst *byte, n int32, s *byte) { ...@@ -41,7 +41,31 @@ func snprintf(dst *byte, n int32, s *byte) {
gp.writebuf = nil gp.writebuf = nil
} }
//var debuglock mutex var debuglock mutex
// The compiler emits calls to printlock and printunlock around
// the multiple calls that implement a single Go print or println
// statement. Some of the print helpers (printsp, for example)
// call print recursively. There is also the problem of a crash
// happening during the print routines and needing to acquire
// the print lock to print information about the crash.
// For both these reasons, let a thread acquire the printlock 'recursively'.
func printlock() {
mp := getg().m
mp.printlock++
if mp.printlock == 1 {
lock(&debuglock)
}
}
func printunlock() {
mp := getg().m
mp.printlock--
if mp.printlock == 0 {
unlock(&debuglock)
}
}
// write to goroutine-local buffer if diverting output, // write to goroutine-local buffer if diverting output,
// or else standard error. // or else standard error.
...@@ -80,7 +104,7 @@ func printnl() { ...@@ -80,7 +104,7 @@ func printnl() {
// Very simple printf. Only for debugging prints. // Very simple printf. Only for debugging prints.
// Do not add to this without checking with Rob. // Do not add to this without checking with Rob.
func vprintf(str string, arg unsafe.Pointer) { func vprintf(str string, arg unsafe.Pointer) {
//lock(&debuglock); printlock()
s := bytes(str) s := bytes(str)
start := 0 start := 0
...@@ -160,7 +184,7 @@ func vprintf(str string, arg unsafe.Pointer) { ...@@ -160,7 +184,7 @@ func vprintf(str string, arg unsafe.Pointer) {
gwrite(s[start:i]) gwrite(s[start:i])
} }
//unlock(&debuglock); printunlock()
} }
func printpc(p unsafe.Pointer) { func printpc(p unsafe.Pointer) {
......
...@@ -181,6 +181,9 @@ func acquireSudog() *sudog { ...@@ -181,6 +181,9 @@ func acquireSudog() *sudog {
// which keeps the garbage collector from being invoked. // which keeps the garbage collector from being invoked.
mp := acquirem() mp := acquirem()
p := new(sudog) p := new(sudog)
if p.elem != nil {
gothrow("acquireSudog: found p.elem != nil after new")
}
releasem(mp) releasem(mp)
return p return p
} }
......
...@@ -316,6 +316,10 @@ func casfrom_Gscanstatus(gp *g, oldval, newval uint32) { ...@@ -316,6 +316,10 @@ func casfrom_Gscanstatus(gp *g, oldval, newval uint32) {
// Check that transition is valid. // Check that transition is valid.
switch oldval { switch oldval {
default:
print("runtime: casfrom_Gscanstatus bad oldval gp=", gp, ", oldval=", hex(oldval), ", newval=", hex(newval), "\n")
dumpgstatus(gp)
gothrow("casfrom_Gscanstatus:top gp->status is not in scan state")
case _Gscanrunnable, case _Gscanrunnable,
_Gscanwaiting, _Gscanwaiting,
_Gscanrunning, _Gscanrunning,
...@@ -377,12 +381,12 @@ func casgstatus(gp *g, oldval, newval uint32) { ...@@ -377,12 +381,12 @@ func casgstatus(gp *g, oldval, newval uint32) {
}) })
} }
// Help GC if needed. // Help GC if needed.
if gp.preemptscan && !gp.gcworkdone && (oldval == _Grunning || oldval == _Gsyscall) { // if gp.preemptscan && !gp.gcworkdone && (oldval == _Grunning || oldval == _Gsyscall) {
gp.preemptscan = false // gp.preemptscan = false
systemstack(func() { // systemstack(func() {
gcphasework(gp) // gcphasework(gp)
}) // })
} // }
} }
} }
...@@ -512,9 +516,10 @@ func stopscanstart(gp *g) { ...@@ -512,9 +516,10 @@ func stopscanstart(gp *g) {
// Runs on g0 and does the actual work after putting the g back on the run queue. // Runs on g0 and does the actual work after putting the g back on the run queue.
func mquiesce(gpmaster *g) { func mquiesce(gpmaster *g) {
activeglen := len(allgs)
// enqueue the calling goroutine. // enqueue the calling goroutine.
restartg(gpmaster) restartg(gpmaster)
activeglen := len(allgs)
for i := 0; i < activeglen; i++ { for i := 0; i < activeglen; i++ {
gp := allgs[i] gp := allgs[i]
if readgstatus(gp) == _Gdead { if readgstatus(gp) == _Gdead {
...@@ -1579,7 +1584,8 @@ func save(pc, sp uintptr) { ...@@ -1579,7 +1584,8 @@ func save(pc, sp uintptr) {
_g_.sched.lr = 0 _g_.sched.lr = 0
_g_.sched.ret = 0 _g_.sched.ret = 0
_g_.sched.ctxt = nil _g_.sched.ctxt = nil
_g_.sched.g = _g_ // write as uintptr to avoid write barrier, which will smash _g_.sched.
*(*uintptr)(unsafe.Pointer(&_g_.sched.g)) = uintptr(unsafe.Pointer(_g_))
} }
// The goroutine g is about to enter a system call. // The goroutine g is about to enter a system call.
...@@ -1625,7 +1631,10 @@ func reentersyscall(pc, sp uintptr) { ...@@ -1625,7 +1631,10 @@ func reentersyscall(pc, sp uintptr) {
_g_.syscallpc = pc _g_.syscallpc = pc
casgstatus(_g_, _Grunning, _Gsyscall) casgstatus(_g_, _Grunning, _Gsyscall)
if _g_.syscallsp < _g_.stack.lo || _g_.stack.hi < _g_.syscallsp { if _g_.syscallsp < _g_.stack.lo || _g_.stack.hi < _g_.syscallsp {
systemstack(entersyscall_bad) systemstack(func() {
print("entersyscall inconsistent ", hex(_g_.syscallsp), " [", hex(_g_.stack.lo), ",", hex(_g_.stack.hi), "]\n")
gothrow("entersyscall")
})
} }
if atomicload(&sched.sysmonwait) != 0 { // TODO: fast atomic if atomicload(&sched.sysmonwait) != 0 { // TODO: fast atomic
...@@ -1654,13 +1663,6 @@ func entersyscall(dummy int32) { ...@@ -1654,13 +1663,6 @@ func entersyscall(dummy int32) {
reentersyscall(getcallerpc(unsafe.Pointer(&dummy)), getcallersp(unsafe.Pointer(&dummy))) reentersyscall(getcallerpc(unsafe.Pointer(&dummy)), getcallersp(unsafe.Pointer(&dummy)))
} }
func entersyscall_bad() {
var gp *g
gp = getg().m.curg
print("entersyscall inconsistent ", hex(gp.syscallsp), " [", hex(gp.stack.lo), ",", hex(gp.stack.hi), "]\n")
gothrow("entersyscall")
}
func entersyscall_sysmon() { func entersyscall_sysmon() {
lock(&sched.lock) lock(&sched.lock)
if atomicload(&sched.sysmonwait) != 0 { if atomicload(&sched.sysmonwait) != 0 {
...@@ -1692,12 +1694,26 @@ func entersyscallblock(dummy int32) { ...@@ -1692,12 +1694,26 @@ func entersyscallblock(dummy int32) {
_g_.stackguard0 = stackPreempt // see comment in entersyscall _g_.stackguard0 = stackPreempt // see comment in entersyscall
// Leave SP around for GC and traceback. // Leave SP around for GC and traceback.
save(getcallerpc(unsafe.Pointer(&dummy)), getcallersp(unsafe.Pointer(&dummy))) pc := getcallerpc(unsafe.Pointer(&dummy))
sp := getcallersp(unsafe.Pointer(&dummy))
save(pc, sp)
_g_.syscallsp = _g_.sched.sp _g_.syscallsp = _g_.sched.sp
_g_.syscallpc = _g_.sched.pc _g_.syscallpc = _g_.sched.pc
if _g_.syscallsp < _g_.stack.lo || _g_.stack.hi < _g_.syscallsp {
sp1 := sp
sp2 := _g_.sched.sp
sp3 := _g_.syscallsp
systemstack(func() {
print("entersyscallblock inconsistent ", hex(sp1), " ", hex(sp2), " ", hex(sp3), " [", hex(_g_.stack.lo), ",", hex(_g_.stack.hi), "]\n")
gothrow("entersyscallblock")
})
}
casgstatus(_g_, _Grunning, _Gsyscall) casgstatus(_g_, _Grunning, _Gsyscall)
if _g_.syscallsp < _g_.stack.lo || _g_.stack.hi < _g_.syscallsp { if _g_.syscallsp < _g_.stack.lo || _g_.stack.hi < _g_.syscallsp {
systemstack(entersyscall_bad) systemstack(func() {
print("entersyscallblock inconsistent ", hex(sp), " ", hex(_g_.sched.sp), " ", hex(_g_.syscallsp), " [", hex(_g_.stack.lo), ",", hex(_g_.stack.hi), "]\n")
gothrow("entersyscallblock")
})
} }
systemstack(entersyscallblock_handoff) systemstack(entersyscallblock_handoff)
...@@ -1776,6 +1792,7 @@ func exitsyscallfast() bool { ...@@ -1776,6 +1792,7 @@ func exitsyscallfast() bool {
// Freezetheworld sets stopwait but does not retake P's. // Freezetheworld sets stopwait but does not retake P's.
if sched.stopwait != 0 { if sched.stopwait != 0 {
_g_.m.mcache = nil
_g_.m.p = nil _g_.m.p = nil
return false return false
} }
...@@ -1789,6 +1806,7 @@ func exitsyscallfast() bool { ...@@ -1789,6 +1806,7 @@ func exitsyscallfast() bool {
} }
// Try to get any other idle P. // Try to get any other idle P.
_g_.m.mcache = nil
_g_.m.p = nil _g_.m.p = nil
if sched.pidle != nil { if sched.pidle != nil {
var ok bool var ok bool
...@@ -2363,6 +2381,8 @@ func setcpuprofilerate_m(hz int32) { ...@@ -2363,6 +2381,8 @@ func setcpuprofilerate_m(hz int32) {
} }
// Change number of processors. The world is stopped, sched is locked. // Change number of processors. The world is stopped, sched is locked.
// gcworkbufs are not being modified by either the GC or
// the write barrier code.
func procresize(new int32) { func procresize(new int32) {
old := gomaxprocs old := gomaxprocs
if old < 0 || old > _MaxGomaxprocs || new <= 0 || new > _MaxGomaxprocs { if old < 0 || old > _MaxGomaxprocs || new <= 0 || new > _MaxGomaxprocs {
......
...@@ -9,7 +9,6 @@ TEXT _rt0_386_linux(SB),NOSPLIT,$8 ...@@ -9,7 +9,6 @@ TEXT _rt0_386_linux(SB),NOSPLIT,$8
LEAL 12(SP), BX LEAL 12(SP), BX
MOVL AX, 0(SP) MOVL AX, 0(SP)
MOVL BX, 4(SP) MOVL BX, 4(SP)
CALL runtime·linux_setup_vdso(SB)
CALL main(SB) CALL main(SB)
INT $3 INT $3
......
...@@ -97,7 +97,10 @@ func testAtomic64() { ...@@ -97,7 +97,10 @@ func testAtomic64() {
z64 = 42 z64 = 42
x64 = 0 x64 = 0
// TODO: PREFETCH((unsafe.Pointer)(&z64)) prefetcht0(uintptr(unsafe.Pointer(&z64)))
prefetcht1(uintptr(unsafe.Pointer(&z64)))
prefetcht2(uintptr(unsafe.Pointer(&z64)))
prefetchnta(uintptr(unsafe.Pointer(&z64)))
if cas64(&z64, x64, 1) { if cas64(&z64, x64, 1) {
gothrow("cas64 failed") gothrow("cas64 failed")
} }
......
...@@ -227,6 +227,8 @@ type m struct { ...@@ -227,6 +227,8 @@ type m struct {
helpgc int32 helpgc int32
spinning bool // m is out of work and is actively looking for work spinning bool // m is out of work and is actively looking for work
blocked bool // m is blocked on a note blocked bool // m is blocked on a note
inwb bool // m is executing a write barrier
printlock int8
fastrand uint32 fastrand uint32
ncgocall uint64 // number of cgo calls in total ncgocall uint64 // number of cgo calls in total
ncgo int32 // number of cgo calls currently in progress ncgo int32 // number of cgo calls currently in progress
...@@ -402,8 +404,9 @@ type itab struct { ...@@ -402,8 +404,9 @@ type itab struct {
} }
// Lock-free stack node. // Lock-free stack node.
// // Also known to export_test.go.
type lfnode struct { type lfnode struct {
next *lfnode next uint64
pushcnt uintptr pushcnt uintptr
} }
...@@ -448,11 +451,13 @@ type debugvars struct { ...@@ -448,11 +451,13 @@ type debugvars struct {
// Indicates to write barrier and sychronization task to preform. // Indicates to write barrier and sychronization task to preform.
const ( const (
_GCoff = iota // stop and start nop _GCoff = iota // GC not running, write barrier disabled
_GCquiesce // stop and start nop _GCquiesce // unused state
_GCstw // stop the ps nop _GCstw // unused state
_GCmark // scan the stacks and start no white to black _GCscan // GC collecting roots into workbufs, write barrier disabled
_GCsweep // stop and start nop _GCmark // GC marking from workbufs, write barrier ENABLED
_GCmarktermination // GC mark termination: allocate black, P's help GC, write barrier ENABLED
_GCsweep // GC mark completed; sweeping in background, write barrier disabled
) )
type forcegcstate struct { type forcegcstate struct {
......
...@@ -377,12 +377,7 @@ loop: ...@@ -377,12 +377,7 @@ loop:
// iterating through the linked list they are in reverse order. // iterating through the linked list they are in reverse order.
cas = nil cas = nil
sglist = gp.waiting sglist = gp.waiting
// Clear all selectdone and elem before unlinking from gp.waiting. // Clear all elem before unlinking from gp.waiting.
// They must be cleared before being put back into the sudog cache.
// Clear before unlinking, because if a stack copy happens after the unlink,
// they will not be updated, they will be left pointing to the old stack,
// which creates dangling pointers, which may be detected by the
// garbage collector.
for sg1 := gp.waiting; sg1 != nil; sg1 = sg1.waitlink { for sg1 := gp.waiting; sg1 != nil; sg1 = sg1.waitlink {
sg1.selectdone = nil sg1.selectdone = nil
sg1.elem = nil sg1.elem = nil
......
...@@ -17,7 +17,7 @@ enum { ...@@ -17,7 +17,7 @@ enum {
#endif // Windows #endif // Windows
StackBig = 4096, StackBig = 4096,
StackGuard = 512 + StackSystem, StackGuard = 640 + StackSystem,
StackSmall = 128, StackSmall = 128,
StackLimit = StackGuard - StackSystem - StackSmall, StackLimit = StackGuard - StackSystem - StackSmall,
}; };
......
...@@ -58,7 +58,7 @@ func stackinit() { ...@@ -58,7 +58,7 @@ func stackinit() {
// Allocates a stack from the free pool. Must be called with // Allocates a stack from the free pool. Must be called with
// stackpoolmu held. // stackpoolmu held.
func stackpoolalloc(order uint8) *mlink { func stackpoolalloc(order uint8) gclinkptr {
list := &stackpool[order] list := &stackpool[order]
s := list.next s := list.next
if s == list { if s == list {
...@@ -70,23 +70,23 @@ func stackpoolalloc(order uint8) *mlink { ...@@ -70,23 +70,23 @@ func stackpoolalloc(order uint8) *mlink {
if s.ref != 0 { if s.ref != 0 {
gothrow("bad ref") gothrow("bad ref")
} }
if s.freelist != nil { if s.freelist.ptr() != nil {
gothrow("bad freelist") gothrow("bad freelist")
} }
for i := uintptr(0); i < _StackCacheSize; i += _FixedStack << order { for i := uintptr(0); i < _StackCacheSize; i += _FixedStack << order {
x := (*mlink)(unsafe.Pointer(uintptr(s.start)<<_PageShift + i)) x := gclinkptr(uintptr(s.start)<<_PageShift + i)
x.next = s.freelist x.ptr().next = s.freelist
s.freelist = x s.freelist = x
} }
mSpanList_Insert(list, s) mSpanList_Insert(list, s)
} }
x := s.freelist x := s.freelist
if x == nil { if x.ptr() == nil {
gothrow("span has no free stacks") gothrow("span has no free stacks")
} }
s.freelist = x.next s.freelist = x.ptr().next
s.ref++ s.ref++
if s.freelist == nil { if s.freelist.ptr() == nil {
// all stacks in s are allocated. // all stacks in s are allocated.
mSpanList_Remove(s) mSpanList_Remove(s)
} }
...@@ -94,22 +94,22 @@ func stackpoolalloc(order uint8) *mlink { ...@@ -94,22 +94,22 @@ func stackpoolalloc(order uint8) *mlink {
} }
// Adds stack x to the free pool. Must be called with stackpoolmu held. // Adds stack x to the free pool. Must be called with stackpoolmu held.
func stackpoolfree(x *mlink, order uint8) { func stackpoolfree(x gclinkptr, order uint8) {
s := mHeap_Lookup(&mheap_, (unsafe.Pointer)(x)) s := mHeap_Lookup(&mheap_, (unsafe.Pointer)(x))
if s.state != _MSpanStack { if s.state != _MSpanStack {
gothrow("freeing stack not in a stack span") gothrow("freeing stack not in a stack span")
} }
if s.freelist == nil { if s.freelist.ptr() == nil {
// s will now have a free stack // s will now have a free stack
mSpanList_Insert(&stackpool[order], s) mSpanList_Insert(&stackpool[order], s)
} }
x.next = s.freelist x.ptr().next = s.freelist
s.freelist = x s.freelist = x
s.ref-- s.ref--
if s.ref == 0 { if s.ref == 0 {
// span is completely free - return to heap // span is completely free - return to heap
mSpanList_Remove(s) mSpanList_Remove(s)
s.freelist = nil s.freelist = 0
mHeap_FreeStack(&mheap_, s) mHeap_FreeStack(&mheap_, s)
} }
} }
...@@ -123,12 +123,12 @@ func stackcacherefill(c *mcache, order uint8) { ...@@ -123,12 +123,12 @@ func stackcacherefill(c *mcache, order uint8) {
// Grab some stacks from the global cache. // Grab some stacks from the global cache.
// Grab half of the allowed capacity (to prevent thrashing). // Grab half of the allowed capacity (to prevent thrashing).
var list *mlink var list gclinkptr
var size uintptr var size uintptr
lock(&stackpoolmu) lock(&stackpoolmu)
for size < _StackCacheSize/2 { for size < _StackCacheSize/2 {
x := stackpoolalloc(order) x := stackpoolalloc(order)
x.next = list x.ptr().next = list
list = x list = x
size += _FixedStack << order size += _FixedStack << order
} }
...@@ -145,7 +145,7 @@ func stackcacherelease(c *mcache, order uint8) { ...@@ -145,7 +145,7 @@ func stackcacherelease(c *mcache, order uint8) {
size := c.stackcache[order].size size := c.stackcache[order].size
lock(&stackpoolmu) lock(&stackpoolmu)
for size > _StackCacheSize/2 { for size > _StackCacheSize/2 {
y := x.next y := x.ptr().next
stackpoolfree(x, order) stackpoolfree(x, order)
x = y x = y
size -= _FixedStack << order size -= _FixedStack << order
...@@ -162,12 +162,12 @@ func stackcache_clear(c *mcache) { ...@@ -162,12 +162,12 @@ func stackcache_clear(c *mcache) {
lock(&stackpoolmu) lock(&stackpoolmu)
for order := uint8(0); order < _NumStackOrders; order++ { for order := uint8(0); order < _NumStackOrders; order++ {
x := c.stackcache[order].list x := c.stackcache[order].list
for x != nil { for x.ptr() != nil {
y := x.next y := x.ptr().next
stackpoolfree(x, order) stackpoolfree(x, order)
x = y x = y
} }
c.stackcache[order].list = nil c.stackcache[order].list = 0
c.stackcache[order].size = 0 c.stackcache[order].size = 0
} }
unlock(&stackpoolmu) unlock(&stackpoolmu)
...@@ -207,7 +207,7 @@ func stackalloc(n uint32) stack { ...@@ -207,7 +207,7 @@ func stackalloc(n uint32) stack {
order++ order++
n2 >>= 1 n2 >>= 1
} }
var x *mlink var x gclinkptr
c := thisg.m.mcache c := thisg.m.mcache
if c == nil || thisg.m.gcing != 0 || thisg.m.helpgc != 0 { if c == nil || thisg.m.gcing != 0 || thisg.m.helpgc != 0 {
// c == nil can happen in the guts of exitsyscall or // c == nil can happen in the guts of exitsyscall or
...@@ -219,11 +219,11 @@ func stackalloc(n uint32) stack { ...@@ -219,11 +219,11 @@ func stackalloc(n uint32) stack {
unlock(&stackpoolmu) unlock(&stackpoolmu)
} else { } else {
x = c.stackcache[order].list x = c.stackcache[order].list
if x == nil { if x.ptr() == nil {
stackcacherefill(c, order) stackcacherefill(c, order)
x = c.stackcache[order].list x = c.stackcache[order].list
} }
c.stackcache[order].list = x.next c.stackcache[order].list = x.ptr().next
c.stackcache[order].size -= uintptr(n) c.stackcache[order].size -= uintptr(n)
} }
v = (unsafe.Pointer)(x) v = (unsafe.Pointer)(x)
...@@ -270,7 +270,7 @@ func stackfree(stk stack) { ...@@ -270,7 +270,7 @@ func stackfree(stk stack) {
order++ order++
n2 >>= 1 n2 >>= 1
} }
x := (*mlink)(v) x := gclinkptr(v)
c := gp.m.mcache c := gp.m.mcache
if c == nil || gp.m.gcing != 0 || gp.m.helpgc != 0 { if c == nil || gp.m.gcing != 0 || gp.m.helpgc != 0 {
lock(&stackpoolmu) lock(&stackpoolmu)
...@@ -280,7 +280,7 @@ func stackfree(stk stack) { ...@@ -280,7 +280,7 @@ func stackfree(stk stack) {
if c.stackcache[order].size >= _StackCacheSize { if c.stackcache[order].size >= _StackCacheSize {
stackcacherelease(c, order) stackcacherelease(c, order)
} }
x.next = c.stackcache[order].list x.ptr().next = c.stackcache[order].list
c.stackcache[order].list = x c.stackcache[order].list = x
c.stackcache[order].size += n c.stackcache[order].size += n
} }
...@@ -526,6 +526,7 @@ func fillstack(stk stack, b byte) { ...@@ -526,6 +526,7 @@ func fillstack(stk stack, b byte) {
} }
// Copies gp's stack to a new stack of a different size. // Copies gp's stack to a new stack of a different size.
// Caller must have changed gp status to Gcopystack.
func copystack(gp *g, newsize uintptr) { func copystack(gp *g, newsize uintptr) {
if gp.syscallsp != 0 { if gp.syscallsp != 0 {
gothrow("stack growth not allowed in system call") gothrow("stack growth not allowed in system call")
...@@ -563,15 +564,11 @@ func copystack(gp *g, newsize uintptr) { ...@@ -563,15 +564,11 @@ func copystack(gp *g, newsize uintptr) {
} }
memmove(unsafe.Pointer(new.hi-used), unsafe.Pointer(old.hi-used), used) memmove(unsafe.Pointer(new.hi-used), unsafe.Pointer(old.hi-used), used)
oldstatus := casgcopystack(gp) // cas from Gwaiting or Grunnable to Gcopystack, return old status
// Swap out old stack for new one // Swap out old stack for new one
gp.stack = new gp.stack = new
gp.stackguard0 = new.lo + _StackGuard // NOTE: might clobber a preempt request gp.stackguard0 = new.lo + _StackGuard // NOTE: might clobber a preempt request
gp.sched.sp = new.hi - used gp.sched.sp = new.hi - used
casgstatus(gp, _Gcopystack, oldstatus) // oldstatus is Gwaiting or Grunnable
// free old stack // free old stack
if stackPoisonCopy != 0 { if stackPoisonCopy != 0 {
fillstack(old, 0xfc) fillstack(old, 0xfc)
...@@ -669,6 +666,14 @@ func newstack() { ...@@ -669,6 +666,14 @@ func newstack() {
gothrow("runtime: split stack overflow") gothrow("runtime: split stack overflow")
} }
if gp.sched.ctxt != nil {
// morestack wrote sched.ctxt on its way in here,
// without a write barrier. Run the write barrier now.
// It is not possible to be preempted between then
// and now, so it's okay.
writebarrierptr_nostore((*uintptr)(unsafe.Pointer(&gp.sched.ctxt)), uintptr(gp.sched.ctxt))
}
if gp.stackguard0 == stackPreempt { if gp.stackguard0 == stackPreempt {
if gp == thisg.m.g0 { if gp == thisg.m.g0 {
gothrow("runtime: preempt g0") gothrow("runtime: preempt g0")
...@@ -677,7 +682,12 @@ func newstack() { ...@@ -677,7 +682,12 @@ func newstack() {
gothrow("runtime: g is running but p is not") gothrow("runtime: g is running but p is not")
} }
if gp.preemptscan { if gp.preemptscan {
for !castogscanstatus(gp, _Gwaiting, _Gscanwaiting) {
// Likely to be racing with the GC as it sees a _Gwaiting and does the stack scan.
// If so this stack will be scanned twice which does not change correctness.
}
gcphasework(gp) gcphasework(gp)
casfrom_Gscanstatus(gp, _Gscanwaiting, _Gwaiting)
casgstatus(gp, _Gwaiting, _Grunning) casgstatus(gp, _Gwaiting, _Grunning)
gp.stackguard0 = gp.stack.lo + _StackGuard gp.stackguard0 = gp.stack.lo + _StackGuard
gp.preempt = false gp.preempt = false
...@@ -708,13 +718,15 @@ func newstack() { ...@@ -708,13 +718,15 @@ func newstack() {
gothrow("stack overflow") gothrow("stack overflow")
} }
// Note that the concurrent GC might be scanning the stack as we try to replace it. casgstatus(gp, _Gwaiting, _Gcopystack)
// copystack takes care of the appropriate coordination with the stack scanner.
// The concurrent GC will not scan the stack while we are doing the copy since
// the gp is in a Gcopystack status.
copystack(gp, uintptr(newsize)) copystack(gp, uintptr(newsize))
if stackDebug >= 1 { if stackDebug >= 1 {
print("stack grow done\n") print("stack grow done\n")
} }
casgstatus(gp, _Gwaiting, _Grunning) casgstatus(gp, _Gcopystack, _Grunning)
gogo(&gp.sched) gogo(&gp.sched)
} }
...@@ -767,17 +779,17 @@ func shrinkstack(gp *g) { ...@@ -767,17 +779,17 @@ func shrinkstack(gp *g) {
if gp.syscallsp != 0 { if gp.syscallsp != 0 {
return return
} }
if goos_windows != 0 && gp.m != nil && gp.m.libcallsp != 0 {
/* TODO
if goos_windows && gp.m != nil && gp.m.libcallsp != 0 {
return return
} }
*/
if stackDebug > 0 { if stackDebug > 0 {
print("shrinking stack ", oldsize, "->", newsize, "\n") print("shrinking stack ", oldsize, "->", newsize, "\n")
} }
oldstatus := casgcopystack(gp)
copystack(gp, newsize) copystack(gp, newsize)
casgstatus(gp, _Gcopystack, oldstatus)
} }
// Do any delayed stack freeing that was queued up during GC. // Do any delayed stack freeing that was queued up during GC.
......
...@@ -84,7 +84,7 @@ const ( ...@@ -84,7 +84,7 @@ const (
// The stack guard is a pointer this many bytes above the // The stack guard is a pointer this many bytes above the
// bottom of the stack. // bottom of the stack.
_StackGuard = 512 + _StackSystem _StackGuard = 640 + _StackSystem
// After a stack split check the SP is allowed to be this // After a stack split check the SP is allowed to be this
// many bytes below the stack guard. This saves an instruction // many bytes below the stack guard. This saves an instruction
......
...@@ -231,3 +231,8 @@ func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32) ...@@ -231,3 +231,8 @@ func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32)
func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32) func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32)
func systemstack_switch() func systemstack_switch()
func prefetcht0(addr uintptr)
func prefetcht1(addr uintptr)
func prefetcht2(addr uintptr)
func prefetchnta(addr uintptr)
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build !linux !amd64 // +build !linux !amd64
// +build !linux !386
package runtime package runtime
......
// generated by wbfat_gen.go; use go generate
package runtime
//go:nosplit
func writebarrierfat01(dst *[2]uintptr, _ *byte, src [2]uintptr) {
dst[0] = src[0]
writebarrierptr(&dst[1], src[1])
}
//go:nosplit
func writebarrierfat10(dst *[2]uintptr, _ *byte, src [2]uintptr) {
writebarrierptr(&dst[0], src[0])
dst[1] = src[1]
}
//go:nosplit
func writebarrierfat11(dst *[2]uintptr, _ *byte, src [2]uintptr) {
writebarrierptr(&dst[0], src[0])
writebarrierptr(&dst[1], src[1])
}
//go:nosplit
func writebarrierfat001(dst *[3]uintptr, _ *byte, src [3]uintptr) {
dst[0] = src[0]
dst[1] = src[1]
writebarrierptr(&dst[2], src[2])
}
//go:nosplit
func writebarrierfat010(dst *[3]uintptr, _ *byte, src [3]uintptr) {
dst[0] = src[0]
writebarrierptr(&dst[1], src[1])
dst[2] = src[2]
}
//go:nosplit
func writebarrierfat011(dst *[3]uintptr, _ *byte, src [3]uintptr) {
dst[0] = src[0]
writebarrierptr(&dst[1], src[1])
writebarrierptr(&dst[2], src[2])
}
//go:nosplit
func writebarrierfat100(dst *[3]uintptr, _ *byte, src [3]uintptr) {
writebarrierptr(&dst[0], src[0])
dst[1] = src[1]
dst[2] = src[2]
}
//go:nosplit
func writebarrierfat101(dst *[3]uintptr, _ *byte, src [3]uintptr) {
writebarrierptr(&dst[0], src[0])
dst[1] = src[1]
writebarrierptr(&dst[2], src[2])
}
//go:nosplit
func writebarrierfat110(dst *[3]uintptr, _ *byte, src [3]uintptr) {
writebarrierptr(&dst[0], src[0])
writebarrierptr(&dst[1], src[1])
dst[2] = src[2]
}
//go:nosplit
func writebarrierfat111(dst *[3]uintptr, _ *byte, src [3]uintptr) {
writebarrierptr(&dst[0], src[0])
writebarrierptr(&dst[1], src[1])
writebarrierptr(&dst[2], src[2])
}
//go:nosplit
func writebarrierfat0001(dst *[4]uintptr, _ *byte, src [4]uintptr) {
dst[0] = src[0]
dst[1] = src[1]
dst[2] = src[2]
writebarrierptr(&dst[3], src[3])
}
//go:nosplit
func writebarrierfat0010(dst *[4]uintptr, _ *byte, src [4]uintptr) {
dst[0] = src[0]
dst[1] = src[1]
writebarrierptr(&dst[2], src[2])
dst[3] = src[3]
}
//go:nosplit
func writebarrierfat0011(dst *[4]uintptr, _ *byte, src [4]uintptr) {
dst[0] = src[0]
dst[1] = src[1]
writebarrierptr(&dst[2], src[2])
writebarrierptr(&dst[3], src[3])
}
//go:nosplit
func writebarrierfat0100(dst *[4]uintptr, _ *byte, src [4]uintptr) {
dst[0] = src[0]
writebarrierptr(&dst[1], src[1])
dst[2] = src[2]
dst[3] = src[3]
}
//go:nosplit
func writebarrierfat0101(dst *[4]uintptr, _ *byte, src [4]uintptr) {
dst[0] = src[0]
writebarrierptr(&dst[1], src[1])
dst[2] = src[2]
writebarrierptr(&dst[3], src[3])
}
//go:nosplit
func writebarrierfat0110(dst *[4]uintptr, _ *byte, src [4]uintptr) {
dst[0] = src[0]
writebarrierptr(&dst[1], src[1])
writebarrierptr(&dst[2], src[2])
dst[3] = src[3]
}
//go:nosplit
func writebarrierfat0111(dst *[4]uintptr, _ *byte, src [4]uintptr) {
dst[0] = src[0]
writebarrierptr(&dst[1], src[1])
writebarrierptr(&dst[2], src[2])
writebarrierptr(&dst[3], src[3])
}
//go:nosplit
func writebarrierfat1000(dst *[4]uintptr, _ *byte, src [4]uintptr) {
writebarrierptr(&dst[0], src[0])
dst[1] = src[1]
dst[2] = src[2]
dst[3] = src[3]
}
//go:nosplit
func writebarrierfat1001(dst *[4]uintptr, _ *byte, src [4]uintptr) {
writebarrierptr(&dst[0], src[0])
dst[1] = src[1]
dst[2] = src[2]
writebarrierptr(&dst[3], src[3])
}
//go:nosplit
func writebarrierfat1010(dst *[4]uintptr, _ *byte, src [4]uintptr) {
writebarrierptr(&dst[0], src[0])
dst[1] = src[1]
writebarrierptr(&dst[2], src[2])
dst[3] = src[3]
}
//go:nosplit
func writebarrierfat1011(dst *[4]uintptr, _ *byte, src [4]uintptr) {
writebarrierptr(&dst[0], src[0])
dst[1] = src[1]
writebarrierptr(&dst[2], src[2])
writebarrierptr(&dst[3], src[3])
}
//go:nosplit
func writebarrierfat1100(dst *[4]uintptr, _ *byte, src [4]uintptr) {
writebarrierptr(&dst[0], src[0])
writebarrierptr(&dst[1], src[1])
dst[2] = src[2]
dst[3] = src[3]
}
//go:nosplit
func writebarrierfat1101(dst *[4]uintptr, _ *byte, src [4]uintptr) {
writebarrierptr(&dst[0], src[0])
writebarrierptr(&dst[1], src[1])
dst[2] = src[2]
writebarrierptr(&dst[3], src[3])
}
//go:nosplit
func writebarrierfat1110(dst *[4]uintptr, _ *byte, src [4]uintptr) {
writebarrierptr(&dst[0], src[0])
writebarrierptr(&dst[1], src[1])
writebarrierptr(&dst[2], src[2])
dst[3] = src[3]
}
//go:nosplit
func writebarrierfat1111(dst *[4]uintptr, _ *byte, src [4]uintptr) {
writebarrierptr(&dst[0], src[0])
writebarrierptr(&dst[1], src[1])
writebarrierptr(&dst[2], src[2])
writebarrierptr(&dst[3], src[3])
}
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
package main
import (
"flag"
"fmt"
"log"
"os"
)
func main() {
flag.Parse()
if flag.NArg() > 0 {
f, err := os.Create(flag.Arg(0))
if err != nil {
log.Fatal(err)
}
os.Stdout = f
}
fmt.Printf("// generated by wbfat_gen.go; use go generate\n\n")
fmt.Printf("package runtime\n")
for i := uint(2); i <= 4; i++ {
for j := 1; j < 1<<i; j++ {
fmt.Printf("\n//go:nosplit\n")
fmt.Printf("func writebarrierfat%0*b(dst *[%d]uintptr, _ *byte, src [%d]uintptr) {\n", int(i), j, i, i)
for k := uint(0); k < i; k++ {
if j&(1<<(i-1-k)) != 0 {
fmt.Printf("\twritebarrierptr(&dst[%d], src[%d])\n", k, k)
} else {
fmt.Printf("\tdst[%d] = src[%d]\n", k, k)
}
}
fmt.Printf("}\n")
}
}
}
...@@ -164,7 +164,7 @@ func TestSwapPointer(t *testing.T) { ...@@ -164,7 +164,7 @@ func TestSwapPointer(t *testing.T) {
x.before = magicptr x.before = magicptr
x.after = magicptr x.after = magicptr
var j uintptr var j uintptr
for delta := uintptr(1); delta+delta > delta; delta += delta { for delta := uintptr(1 << 16); delta+delta > delta; delta += delta {
k := SwapPointer(&x.i, unsafe.Pointer(delta)) k := SwapPointer(&x.i, unsafe.Pointer(delta))
if uintptr(x.i) != delta || uintptr(k) != j { if uintptr(x.i) != delta || uintptr(k) != j {
t.Fatalf("delta=%d i=%d j=%d k=%d", delta, x.i, j, k) t.Fatalf("delta=%d i=%d j=%d k=%d", delta, x.i, j, k)
...@@ -456,7 +456,7 @@ func TestCompareAndSwapPointer(t *testing.T) { ...@@ -456,7 +456,7 @@ func TestCompareAndSwapPointer(t *testing.T) {
magicptr := uintptr(m) magicptr := uintptr(m)
x.before = magicptr x.before = magicptr
x.after = magicptr x.after = magicptr
for val := uintptr(1); val+val > val; val += val { for val := uintptr(1 << 16); val+val > val; val += val {
x.i = unsafe.Pointer(val) x.i = unsafe.Pointer(val)
if !CompareAndSwapPointer(&x.i, unsafe.Pointer(val), unsafe.Pointer(val+1)) { if !CompareAndSwapPointer(&x.i, unsafe.Pointer(val), unsafe.Pointer(val+1)) {
t.Fatalf("should have swapped %#x %#x", val, val+1) t.Fatalf("should have swapped %#x %#x", val, val+1)
...@@ -595,7 +595,7 @@ func TestLoadPointer(t *testing.T) { ...@@ -595,7 +595,7 @@ func TestLoadPointer(t *testing.T) {
magicptr := uintptr(m) magicptr := uintptr(m)
x.before = magicptr x.before = magicptr
x.after = magicptr x.after = magicptr
for delta := uintptr(1); delta+delta > delta; delta += delta { for delta := uintptr(1 << 16); delta+delta > delta; delta += delta {
k := LoadPointer(&x.i) k := LoadPointer(&x.i)
if k != x.i { if k != x.i {
t.Fatalf("delta=%d i=%d k=%d", delta, x.i, k) t.Fatalf("delta=%d i=%d k=%d", delta, x.i, k)
...@@ -731,7 +731,7 @@ func TestStorePointer(t *testing.T) { ...@@ -731,7 +731,7 @@ func TestStorePointer(t *testing.T) {
x.before = magicptr x.before = magicptr
x.after = magicptr x.after = magicptr
v := unsafe.Pointer(uintptr(0)) v := unsafe.Pointer(uintptr(0))
for delta := uintptr(1); delta+delta > delta; delta += delta { for delta := uintptr(1 << 16); delta+delta > delta; delta += delta {
StorePointer(&x.i, unsafe.Pointer(v)) StorePointer(&x.i, unsafe.Pointer(v))
if x.i != v { if x.i != v {
t.Fatalf("delta=%d i=%d v=%d", delta, x.i, v) t.Fatalf("delta=%d i=%d v=%d", delta, x.i, v)
......
...@@ -9,20 +9,39 @@ ...@@ -9,20 +9,39 @@
package main package main
func printnl()
//go:noescape
func printpointer(**int)
//go:noescape
func printintpointer(*int)
//go:noescape
func printstringpointer(*string)
//go:noescape
func printstring(string)
//go:noescape
func printbytepointer(*byte)
func printint(int)
func f1() { func f1() {
var x *int var x *int
print(&x) // ERROR "live at call to printpointer: x$" printpointer(&x) // ERROR "live at call to printpointer: x$"
print(&x) // ERROR "live at call to printpointer: x$" printpointer(&x) // ERROR "live at call to printpointer: x$"
} }
func f2(b bool) { func f2(b bool) {
if b { if b {
print(0) // nothing live here printint(0) // nothing live here
return return
} }
var x *int var x *int
print(&x) // ERROR "live at call to printpointer: x$" printpointer(&x) // ERROR "live at call to printpointer: x$"
print(&x) // ERROR "live at call to printpointer: x$" printpointer(&x) // ERROR "live at call to printpointer: x$"
} }
func f3(b bool) { func f3(b bool) {
...@@ -30,22 +49,22 @@ func f3(b bool) { ...@@ -30,22 +49,22 @@ func f3(b bool) {
// live throughout the function, to avoid being poisoned // live throughout the function, to avoid being poisoned
// in GODEBUG=gcdead=1 mode. // in GODEBUG=gcdead=1 mode.
print(0) // ERROR "live at call to printint: x y$" printint(0) // ERROR "live at call to printint: x y$"
if b == false { if b == false {
print(0) // ERROR "live at call to printint: x y$" printint(0) // ERROR "live at call to printint: x y$"
return return
} }
if b { if b {
var x *int var x *int
print(&x) // ERROR "live at call to printpointer: x y$" printpointer(&x) // ERROR "live at call to printpointer: x y$"
print(&x) // ERROR "live at call to printpointer: x y$" printpointer(&x) // ERROR "live at call to printpointer: x y$"
} else { } else {
var y *int var y *int
print(&y) // ERROR "live at call to printpointer: x y$" printpointer(&y) // ERROR "live at call to printpointer: x y$"
print(&y) // ERROR "live at call to printpointer: x y$" printpointer(&y) // ERROR "live at call to printpointer: x y$"
} }
print(0) // ERROR "live at call to printint: x y$" "x \(type \*int\) is ambiguously live" "y \(type \*int\) is ambiguously live" printint(0) // ERROR "live at call to printint: x y$" "x \(type \*int\) is ambiguously live" "y \(type \*int\) is ambiguously live"
} }
// The old algorithm treated x as live on all code that // The old algorithm treated x as live on all code that
...@@ -56,20 +75,20 @@ func f3(b bool) { ...@@ -56,20 +75,20 @@ func f3(b bool) {
func f4(b1, b2 bool) { // x not live here func f4(b1, b2 bool) { // x not live here
if b2 { if b2 {
print(0) // x not live here printint(0) // x not live here
return return
} }
var z **int var z **int
x := new(int) x := new(int)
*x = 42 *x = 42
z = &x z = &x
print(**z) // ERROR "live at call to printint: x z$" printint(**z) // ERROR "live at call to printint: x z$"
if b2 { if b2 {
print(1) // ERROR "live at call to printint: x$" printint(1) // ERROR "live at call to printint: x$"
return return
} }
for { for {
print(**z) // ERROR "live at call to printint: x z$" printint(**z) // ERROR "live at call to printint: x z$"
} }
} }
...@@ -84,7 +103,7 @@ func f5(b1 bool) { ...@@ -84,7 +103,7 @@ func f5(b1 bool) {
*y = 54 *y = 54
z = &y z = &y
} }
print(**z) // ERROR "live at call to printint: x y$" "x \(type \*int\) is ambiguously live" "y \(type \*int\) is ambiguously live" printint(**z) // ERROR "live at call to printint: x y$" "x \(type \*int\) is ambiguously live" "y \(type \*int\) is ambiguously live"
} }
// confusion about the _ result used to cause spurious "live at entry to f6: _". // confusion about the _ result used to cause spurious "live at entry to f6: _".
...@@ -155,8 +174,8 @@ func f11b() *int { ...@@ -155,8 +174,8 @@ func f11b() *int {
// At this point p is dead: the code here cannot // At this point p is dead: the code here cannot
// get to the bottom of the function. // get to the bottom of the function.
// This used to have a spurious "live at call to printint: p". // This used to have a spurious "live at call to printint: p".
print(1) // nothing live here! printint(1) // nothing live here!
select { // ERROR "live at call to newselect: autotmp" "live at call to selectgo: autotmp" select { // ERROR "live at call to newselect: autotmp" "live at call to selectgo: autotmp"
case <-c: // ERROR "live at call to selectrecv: autotmp" case <-c: // ERROR "live at call to selectrecv: autotmp"
return nil return nil
case <-c: // ERROR "live at call to selectrecv: autotmp" case <-c: // ERROR "live at call to selectrecv: autotmp"
...@@ -172,8 +191,8 @@ func f11c() *int { ...@@ -172,8 +191,8 @@ func f11c() *int {
if b { if b {
// Unlike previous, the cases in this select fall through, // Unlike previous, the cases in this select fall through,
// so we can get to the println, so p is not dead. // so we can get to the println, so p is not dead.
print(1) // ERROR "live at call to printint: p" printint(1) // ERROR "live at call to printint: p"
select { // ERROR "live at call to newselect: autotmp.* p" "live at call to selectgo: autotmp.* p" select { // ERROR "live at call to newselect: autotmp.* p" "live at call to selectgo: autotmp.* p"
case <-c: // ERROR "live at call to selectrecv: autotmp.* p" case <-c: // ERROR "live at call to selectrecv: autotmp.* p"
case <-c: // ERROR "live at call to selectrecv: autotmp.* p" case <-c: // ERROR "live at call to selectrecv: autotmp.* p"
} }
...@@ -209,7 +228,7 @@ func h13(string, string) string ...@@ -209,7 +228,7 @@ func h13(string, string) string
func f14() { func f14() {
x := g14() x := g14()
print(&x) // ERROR "live at call to printpointer: x" printstringpointer(&x) // ERROR "live at call to printstringpointer: x"
} }
func g14() string func g14() string
...@@ -217,8 +236,8 @@ func g14() string ...@@ -217,8 +236,8 @@ func g14() string
func f15() { func f15() {
var x string var x string
_ = &x _ = &x
x = g15() // ERROR "live at call to g15: x" x = g15() // ERROR "live at call to g15: x"
print(x) // ERROR "live at call to printstring: x" printstring(x) // ERROR "live at call to printstring: x"
} }
func g15() string func g15() string
...@@ -282,7 +301,7 @@ func f18() { ...@@ -282,7 +301,7 @@ func f18() {
} }
z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$" z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$" z = m2[g18()] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
print(z) printbytepointer(z)
} }
var ch chan *byte var ch chan *byte
...@@ -296,7 +315,7 @@ func f19() { ...@@ -296,7 +315,7 @@ func f19() {
} }
z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$" z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$"
z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$" z = <-ch // ERROR "live at call to chanrecv1: autotmp_[0-9]+$"
print(z) printbytepointer(z)
} }
func f20() { func f20() {
...@@ -316,7 +335,7 @@ func f21() { ...@@ -316,7 +335,7 @@ func f21() {
} }
z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$" z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$" z = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
print(z) printbytepointer(z)
} }
func f23() { func f23() {
...@@ -328,7 +347,8 @@ func f23() { ...@@ -328,7 +347,8 @@ func f23() {
} }
z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$" z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$"
z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$" z, ok = m2[[2]string{"x", "y"}] // ERROR "live at call to mapaccess2: autotmp_[0-9]+$"
print(z, ok) printbytepointer(z)
print(ok)
} }
func f24() { func f24() {
...@@ -350,8 +370,8 @@ func f25(b bool) { ...@@ -350,8 +370,8 @@ func f25(b bool) {
} }
var x string var x string
_ = &x _ = &x
x = g15() // ERROR "live at call to g15: x" x = g15() // ERROR "live at call to g15: x"
print(x) // ERROR "live at call to printstring: x" printstring(x) // ERROR "live at call to printstring: x"
} // ERROR "live at call to deferreturn: x" } // ERROR "live at call to deferreturn: x"
func g25() func g25()
...@@ -366,7 +386,7 @@ func f26(b bool) { ...@@ -366,7 +386,7 @@ func f26(b bool) {
} }
print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$" print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$"
print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$" print26((*int)(nil), (*int)(nil), (*int)(nil)) // ERROR "live at call to print26: autotmp_[0-9]+$"
println() printnl()
} }
//go:noescape //go:noescape
...@@ -381,7 +401,7 @@ func f27(b bool) { ...@@ -381,7 +401,7 @@ func f27(b bool) {
} }
call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$" call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$"
call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$" call27(func() { x++ }) // ERROR "live at call to call27: autotmp_[0-9]+$"
println() printnl()
} }
// but defer does escape to later execution in the function // but defer does escape to later execution in the function
...@@ -392,7 +412,7 @@ func f27defer(b bool) { ...@@ -392,7 +412,7 @@ func f27defer(b bool) {
defer call27(func() { x++ }) // ERROR "live at call to deferproc: autotmp_[0-9]+$" "live at call to deferreturn: autotmp_[0-9]+$" defer call27(func() { x++ }) // ERROR "live at call to deferproc: autotmp_[0-9]+$" "live at call to deferreturn: autotmp_[0-9]+$"
} }
defer call27(func() { x++ }) // ERROR "live at call to deferproc: autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to deferreturn: autotmp_[0-9]+ autotmp_[0-9]+$" "ambiguously live" defer call27(func() { x++ }) // ERROR "live at call to deferproc: autotmp_[0-9]+ autotmp_[0-9]+$" "live at call to deferreturn: autotmp_[0-9]+ autotmp_[0-9]+$" "ambiguously live"
println() // ERROR "live at call to printnl: autotmp_[0-9]+ autotmp_[0-9]+$" printnl() // ERROR "live at call to printnl: autotmp_[0-9]+ autotmp_[0-9]+$"
} // ERROR "live at call to deferreturn: autotmp_[0-9]+ autotmp_[0-9]+$" } // ERROR "live at call to deferreturn: autotmp_[0-9]+ autotmp_[0-9]+$"
// and newproc (go) escapes to the heap // and newproc (go) escapes to the heap
...@@ -403,7 +423,7 @@ func f27go(b bool) { ...@@ -403,7 +423,7 @@ func f27go(b bool) {
go call27(func() { x++ }) // ERROR "live at call to newobject: &x" "live at call to newproc: &x$" go call27(func() { x++ }) // ERROR "live at call to newobject: &x" "live at call to newproc: &x$"
} }
go call27(func() { x++ }) // ERROR "live at call to newobject: &x" go call27(func() { x++ }) // ERROR "live at call to newobject: &x"
println() printnl()
} }
//go:noescape //go:noescape
...@@ -415,10 +435,10 @@ var s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 string ...@@ -415,10 +435,10 @@ var s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 string
func f28(b bool) { func f28(b bool) {
if b { if b {
print(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "live at call to concatstrings: autotmp_[0-9]+$" "live at call to printstring: autotmp_[0-9]+$" printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "live at call to concatstrings: autotmp_[0-9]+$" "live at call to printstring: autotmp_[0-9]+$"
} }
print(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "live at call to concatstrings: autotmp_[0-9]+$" "live at call to printstring: autotmp_[0-9]+$" printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "live at call to concatstrings: autotmp_[0-9]+$" "live at call to printstring: autotmp_[0-9]+$"
print(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "live at call to concatstrings: autotmp_[0-9]+$" "live at call to printstring: autotmp_[0-9]+$" printstring(s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10) // ERROR "live at call to concatstrings: autotmp_[0-9]+$" "live at call to printstring: autotmp_[0-9]+$"
} }
// map iterator should die on end of range loop // map iterator should die on end of range loop
...@@ -426,14 +446,14 @@ func f28(b bool) { ...@@ -426,14 +446,14 @@ func f28(b bool) {
func f29(b bool) { func f29(b bool) {
if b { if b {
for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$" for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$"
print(k) // ERROR "live at call to printstring: autotmp_[0-9]+$" printstring(k) // ERROR "live at call to printstring: autotmp_[0-9]+$"
} }
} }
for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$" for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$"
print(k) // ERROR "live at call to printstring: autotmp_[0-9]+$" printstring(k) // ERROR "live at call to printstring: autotmp_[0-9]+$"
} }
for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$" for k := range m { // ERROR "live at call to mapiterinit: autotmp_[0-9]+$" "live at call to mapiternext: autotmp_[0-9]+$"
print(k) // ERROR "live at call to printstring: autotmp_[0-9]+$" printstring(k) // ERROR "live at call to printstring: autotmp_[0-9]+$"
} }
} }
...@@ -446,14 +466,14 @@ func f30(b bool) { ...@@ -446,14 +466,14 @@ func f30(b bool) {
// the copy of ptrarr and the internal iterator pointer. // the copy of ptrarr and the internal iterator pointer.
if b { if b {
for _, p := range ptrarr { for _, p := range ptrarr {
print(p) // ERROR "live at call to printpointer: autotmp_[0-9]+ autotmp_[0-9]+$" printintpointer(p) // ERROR "live at call to printintpointer: autotmp_[0-9]+ autotmp_[0-9]+$"
} }
} }
for _, p := range ptrarr { for _, p := range ptrarr {
print(p) // ERROR "live at call to printpointer: autotmp_[0-9]+ autotmp_[0-9]+$" printintpointer(p) // ERROR "live at call to printintpointer: autotmp_[0-9]+ autotmp_[0-9]+$"
} }
for _, p := range ptrarr { for _, p := range ptrarr {
print(p) // ERROR "live at call to printpointer: autotmp_[0-9]+ autotmp_[0-9]+$" printintpointer(p) // ERROR "live at call to printintpointer: autotmp_[0-9]+ autotmp_[0-9]+$"
} }
} }
...@@ -503,44 +523,44 @@ var m33 map[interface{}]int ...@@ -503,44 +523,44 @@ var m33 map[interface{}]int
func f33() { func f33() {
if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$" if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
println() printnl()
return return
} else { } else {
println() printnl()
} }
println() printnl()
} }
func f34() { func f34() {
if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$" if m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
println() printnl()
return return
} }
println() printnl()
} }
func f35() { func f35() {
if m33[nil] == 0 && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$" if m33[nil] == 0 && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
println() printnl()
return return
} }
println() printnl()
} }
func f36() { func f36() {
if m33[nil] == 0 || m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$" if m33[nil] == 0 || m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
println() printnl()
return return
} }
println() printnl()
} }
func f37() { func f37() {
if (m33[nil] == 0 || m33[nil] == 0) && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$" if (m33[nil] == 0 || m33[nil] == 0) && m33[nil] == 0 { // ERROR "live at call to mapaccess1: autotmp_[0-9]+$"
println() printnl()
return return
} }
println() printnl()
} }
// select temps should disappear in the case bodies // select temps should disappear in the case bodies
...@@ -558,44 +578,44 @@ func f38(b bool) { ...@@ -558,44 +578,44 @@ func f38(b bool) {
if b { if b {
select { // ERROR "live at call" select { // ERROR "live at call"
case <-fc38(): // ERROR "live at call" case <-fc38(): // ERROR "live at call"
println() printnl()
case fc38() <- *fi38(1): // ERROR "live at call" case fc38() <- *fi38(1): // ERROR "live at call"
println() printnl()
case *fi38(2) = <-fc38(): // ERROR "live at call" case *fi38(2) = <-fc38(): // ERROR "live at call"
println() printnl()
case *fi38(3), *fb38() = <-fc38(): // ERROR "live at call" case *fi38(3), *fb38() = <-fc38(): // ERROR "live at call"
println() printnl()
} }
println() printnl()
} }
println() printnl()
} }
// issue 8097: mishandling of x = x during return. // issue 8097: mishandling of x = x during return.
func f39() (x []int) { func f39() (x []int) {
x = []int{1} x = []int{1}
println() // ERROR "live at call to printnl: x" printnl() // ERROR "live at call to printnl: x"
return x return x
} }
func f39a() (x []int) { func f39a() (x []int) {
x = []int{1} x = []int{1}
println() // ERROR "live at call to printnl: x" printnl() // ERROR "live at call to printnl: x"
return return
} }
func f39b() (x [10]*int) { func f39b() (x [10]*int) {
x = [10]*int{} x = [10]*int{}
x[0] = new(int) // ERROR "live at call to newobject: x" x[0] = new(int) // ERROR "live at call to newobject: x"
println() // ERROR "live at call to printnl: x" printnl() // ERROR "live at call to printnl: x"
return x return x
} }
func f39c() (x [10]*int) { func f39c() (x [10]*int) {
x = [10]*int{} x = [10]*int{}
x[0] = new(int) // ERROR "live at call to newobject: x" x[0] = new(int) // ERROR "live at call to newobject: x"
println() // ERROR "live at call to printnl: x" printnl() // ERROR "live at call to printnl: x"
return return
} }
...@@ -615,13 +635,13 @@ func newT40() *T40 { ...@@ -615,13 +635,13 @@ func newT40() *T40 {
func bad40() { func bad40() {
t := newT40() t := newT40()
_ = t _ = t
println() printnl()
} }
func good40() { func good40() {
ret := T40{} ret := T40{}
ret.m = make(map[int]int) // ERROR "live at call to makemap: ret" ret.m = make(map[int]int) // ERROR "live at call to makemap: ret"
t := &ret t := &ret
println() // ERROR "live at call to printnl: ret" printnl() // ERROR "live at call to printnl: ret"
_ = t _ = t
} }
...@@ -12,6 +12,8 @@ package main ...@@ -12,6 +12,8 @@ package main
// issue 8142: lost 'addrtaken' bit on inlined variables. // issue 8142: lost 'addrtaken' bit on inlined variables.
// no inlining in this test, so just checking that non-inlined works. // no inlining in this test, so just checking that non-inlined works.
func printnl()
type T40 struct { type T40 struct {
m map[int]int m map[int]int
} }
...@@ -24,7 +26,7 @@ func newT40() *T40 { ...@@ -24,7 +26,7 @@ func newT40() *T40 {
func bad40() { func bad40() {
t := newT40() // ERROR "live at call to makemap: ret" t := newT40() // ERROR "live at call to makemap: ret"
println() // ERROR "live at call to printnl: ret" printnl() // ERROR "live at call to printnl: ret"
_ = t _ = t
} }
...@@ -32,6 +34,6 @@ func good40() { ...@@ -32,6 +34,6 @@ func good40() {
ret := T40{} ret := T40{}
ret.m = make(map[int]int) // ERROR "live at call to makemap: ret" ret.m = make(map[int]int) // ERROR "live at call to makemap: ret"
t := &ret t := &ret
println() // ERROR "live at call to printnl: ret" printnl() // ERROR "live at call to printnl: ret"
_ = t _ = t
} }
...@@ -268,11 +268,11 @@ TestCases: ...@@ -268,11 +268,11 @@ TestCases:
name := m[1] name := m[1]
size, _ := strconv.Atoi(m[2]) size, _ := strconv.Atoi(m[2])
// The limit was originally 128 but is now 384. // The limit was originally 128 but is now 512.
// Instead of rewriting the test cases above, adjust // Instead of rewriting the test cases above, adjust
// the first stack frame to use up the extra 32 bytes. // the first stack frame to use up the extra 32 bytes.
if i == 0 { if i == 0 {
size += 384 - 128 size += 512 - 128
} }
if size%ptrSize == 4 { if size%ptrSize == 4 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment