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
} }
This diff is collapsed.
...@@ -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)
......
This diff is collapsed.
...@@ -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