revamping the incremental collector

Some simplifications (not counting bytes, couting only slots visited;
no more 'gcfinnum'); more GC parameters; using vararg in 'lua_gc' to
set parameters in different GC modes
This commit is contained in:
Roberto Ierusalimschy
2017-05-26 16:14:29 -03:00
parent 4804bbd9bb
commit 72d82a296c
7 changed files with 209 additions and 199 deletions

44
lapi.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 2.266 2017/05/11 18:57:46 roberto Exp roberto $ ** $Id: lapi.c,v 2.267 2017/05/18 12:34:58 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -1046,17 +1046,12 @@ LUA_API int lua_status (lua_State *L) {
/* /*
** Garbage-collection function ** Garbage-collection function
*/ */
LUA_API int lua_gc (lua_State *L, int what, ...) {
#if !defined(LUA_GENMAJORMUL) va_list argp;
#define LUA_GENMAJORMUL 100
#define LUA_GENMINORMUL 5
#endif
LUA_API int lua_gc (lua_State *L, int what, int data) {
int res = 0; int res = 0;
global_State *g; global_State *g = G(L);
lua_lock(L); lua_lock(L);
g = G(L); va_start(argp, what);
switch (what) { switch (what) {
case LUA_GCSTOP: { case LUA_GCSTOP: {
g->gcrunning = 0; g->gcrunning = 0;
@@ -1081,11 +1076,12 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
break; break;
} }
case LUA_GCSTEP: { case LUA_GCSTEP: {
int data = va_arg(argp, int);
l_mem debt = 1; /* =1 to signal that it did an actual step */ l_mem debt = 1; /* =1 to signal that it did an actual step */
lu_byte oldrunning = g->gcrunning; lu_byte oldrunning = g->gcrunning;
g->gcrunning = 1; /* allow GC to run */ g->gcrunning = 1; /* allow GC to run */
if (data == 0) { if (data == 0) {
luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ luaE_setdebt(g, 0); /* do a basic step */
luaC_step(L); luaC_step(L);
} }
else { /* add 'data' to total debt */ else { /* add 'data' to total debt */
@@ -1099,14 +1095,15 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
break; break;
} }
case LUA_GCSETPAUSE: { case LUA_GCSETPAUSE: {
res = g->gcpause; int data = va_arg(argp, int);
g->gcpause = data; res = g->gcpause + 100;
g->gcpause = (data >= 100) ? data - 100 : 0;
break; break;
} }
case LUA_GCSETSTEPMUL: { case LUA_GCSETSTEPMUL: {
res = g->gcstepmul; int data = va_arg(argp, int);
if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */ res = g->gcstepmul * 10;
g->gcstepmul = data; g->gcstepmul = data / 10;
break; break;
} }
case LUA_GCISRUNNING: { case LUA_GCISRUNNING: {
@@ -1114,19 +1111,26 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
break; break;
} }
case LUA_GCGEN: { case LUA_GCGEN: {
lu_byte aux = data & 0xff; int minormul = va_arg(argp, int);
g->genminormul = (aux == 0) ? LUA_GENMINORMUL : aux; int majormul = va_arg(argp, int);
aux = (data >> 8) & 0xff; g->genminormul = (minormul == 0) ? LUAI_GENMINORMUL : minormul;
g->genmajormul = (aux == 0) ? LUA_GENMAJORMUL : aux; g->genmajormul = (majormul == 0) ? LUAI_GENMAJORMUL : majormul;
luaC_changemode(L, KGC_GEN); luaC_changemode(L, KGC_GEN);
break; break;
} }
case LUA_GCINC: { case LUA_GCINC: {
int pause = va_arg(argp, int);
int stepmul = va_arg(argp, int);
int stepsize = va_arg(argp, int);
g->gcpause = (pause == 0) ? LUAI_GCPAUSE : pause;
g->gcstepmul = (stepmul == 0) ? LUAI_GCMUL : stepmul;
g->gcstepsize = (stepsize == 0) ? LUAI_GCSTEPSIZE : stepsize;
luaC_changemode(L, KGC_INC); luaC_changemode(L, KGC_INC);
break; break;
} }
default: res = -1; /* invalid option */ default: res = -1; /* invalid option */
} }
va_end(argp);
lua_unlock(L); lua_unlock(L);
return res; return res;
} }

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.314 2016/09/05 19:06:34 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.315 2017/02/23 21:07:34 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -178,19 +178,46 @@ static int luaB_collectgarbage (lua_State *L) {
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
int ex = (int)luaL_optinteger(L, 2, 0);
int res = lua_gc(L, o, ex);
switch (o) { switch (o) {
case LUA_GCCOUNT: { case LUA_GCCOUNT: {
int b = lua_gc(L, LUA_GCCOUNTB, 0); int k = lua_gc(L, o);
lua_pushnumber(L, (lua_Number)res + ((lua_Number)b/1024)); int b = lua_gc(L, LUA_GCCOUNTB);
lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
return 1; return 1;
} }
case LUA_GCSTEP: case LUA_GCISRUNNING: { case LUA_GCSTEP: {
int step = (int)luaL_optinteger(L, 2, 0);
int res = lua_gc(L, o, step);
lua_pushboolean(L, res); lua_pushboolean(L, res);
return 1; return 1;
} }
case LUA_GCSETPAUSE:
case LUA_GCSETSTEPMUL: {
int p = (int)luaL_optinteger(L, 2, 0);
int previous = lua_gc(L, o, p);
lua_pushinteger(L, previous);
return 1;
}
case LUA_GCISRUNNING: {
int res = lua_gc(L, o);
lua_pushboolean(L, res);
return 1;
}
case LUA_GCGEN: {
int minormul = (int)luaL_optinteger(L, 2, 0);
int majormul = (int)luaL_optinteger(L, 3, 0);
lua_gc(L, o, minormul, majormul);
return 0;
}
case LUA_GCINC: {
int pause = (int)luaL_optinteger(L, 2, 0);
int stepmul = (int)luaL_optinteger(L, 3, 0);
int stepsize = (int)luaL_optinteger(L, 4, 0);
lua_gc(L, o, pause, stepmul, stepsize);
return 0;
}
default: { default: {
int res = lua_gc(L, o);
lua_pushinteger(L, res); lua_pushinteger(L, res);
return 1; return 1;
} }

280
lgc.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 2.227 2017/04/30 20:43:26 roberto Exp roberto $ ** $Id: lgc.c,v 2.228 2017/05/04 13:32:01 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -27,23 +27,29 @@
/* /*
** cost of sweeping one element (the size of a small object divided ** Maximum number of elements to sweep in each single step.
** by some adjust for the sweep speed) ** (Large enough to dissipate fixed overheads but small enough
** to allow small steps for the collector.)
*/ */
#define GCSWEEPCOST ((sizeof(TString) + 4) / 4) #define GCSWEEPMAX 100
/* maximum number of elements to sweep in each single step */ /*
#define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4)) ** Maximum number of finalizers to call in each single step.
*/
/* cost of calling one finalizer */ #define GCFINMAX 10
#define GCFINALIZECOST GCSWEEPCOST
/* /*
** macro to adjust 'stepmul': 'stepmul' is actually used like ** Cost of calling one finalizer.
** 'stepmul / STEPMULADJ' (value chosen by tests)
*/ */
#define STEPMULADJ 200 #define GCFINALIZECOST 50
/*
** The equivalent, in bytes, of one unit of "work" (visiting a slot,
** sweeping an object, etc.) * 10 (for scaling)
*/
#define WORK2MEM (sizeof(TValue) * 10)
/* /*
@@ -86,7 +92,7 @@
#define markobjectN(g,t) { if (t) markobject(g,t); } #define markobjectN(g,t) { if (t) markobject(g,t); }
static void reallymarkobject (global_State *g, GCObject *o); static void reallymarkobject (global_State *g, GCObject *o);
static l_mem atomic (lua_State *L); static lu_mem atomic (lua_State *L);
/* /*
@@ -246,21 +252,15 @@ static void reallymarkobject (global_State *g, GCObject *o) {
reentry: reentry:
white2gray(o); white2gray(o);
switch (o->tt) { switch (o->tt) {
case LUA_TSHRSTR: { case LUA_TSHRSTR:
gray2black(o);
g->GCmemtrav += sizelstring(gco2ts(o)->shrlen);
break;
}
case LUA_TLNGSTR: { case LUA_TLNGSTR: {
gray2black(o); gray2black(o);
g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen);
break; break;
} }
case LUA_TUSERDATA: { case LUA_TUSERDATA: {
TValue uvalue; TValue uvalue;
markobjectN(g, gco2u(o)->metatable); /* mark its metatable */ markobjectN(g, gco2u(o)->metatable); /* mark its metatable */
gray2black(o); gray2black(o);
g->GCmemtrav += sizeudata(gco2u(o));
getuservalue(g->mainthread, gco2u(o), &uvalue); getuservalue(g->mainthread, gco2u(o), &uvalue);
if (valiswhite(&uvalue)) { /* markvalue(g, &uvalue); */ if (valiswhite(&uvalue)) { /* markvalue(g, &uvalue); */
o = gcvalue(&uvalue); o = gcvalue(&uvalue);
@@ -270,7 +270,6 @@ static void reallymarkobject (global_State *g, GCObject *o) {
} }
case LUA_TUPVAL: { case LUA_TUPVAL: {
UpVal *uv = gco2upv(o); UpVal *uv = gco2upv(o);
g->GCmemtrav += sizeof(UpVal);
if (!upisopen(uv)) /* open upvalues are kept gray */ if (!upisopen(uv)) /* open upvalues are kept gray */
gray2black(o); gray2black(o);
markvalue(g, uv->v); /* mark its content */ markvalue(g, uv->v); /* mark its content */
@@ -314,10 +313,14 @@ static void markmt (global_State *g) {
/* /*
** mark all objects in list of being-finalized ** mark all objects in list of being-finalized
*/ */
static void markbeingfnz (global_State *g) { static lu_mem markbeingfnz (global_State *g) {
GCObject *o; GCObject *o;
for (o = g->tobefnz; o != NULL; o = o->next) lu_mem count = 0;
for (o = g->tobefnz; o != NULL; o = o->next) {
count++;
markobject(g, o); markobject(g, o);
}
return count;
} }
@@ -327,10 +330,12 @@ static void markbeingfnz (global_State *g) {
** thread.) Remove from the list threads that no longer have upvalues and ** thread.) Remove from the list threads that no longer have upvalues and
** not-marked threads. ** not-marked threads.
*/ */
static void remarkupvals (global_State *g) { static int remarkupvals (global_State *g) {
lua_State *thread; lua_State *thread;
lua_State **p = &g->twups; lua_State **p = &g->twups;
int work = 0;
while ((thread = *p) != NULL) { while ((thread = *p) != NULL) {
work++;
lua_assert(!isblack(thread)); /* threads are never black */ lua_assert(!isblack(thread)); /* threads are never black */
if (isgray(thread) && thread->openupval != NULL) if (isgray(thread) && thread->openupval != NULL)
p = &thread->twups; /* keep marked thread with upvalues in the list */ p = &thread->twups; /* keep marked thread with upvalues in the list */
@@ -339,11 +344,13 @@ static void remarkupvals (global_State *g) {
*p = thread->twups; /* remove thread from the list */ *p = thread->twups; /* remove thread from the list */
thread->twups = thread; /* mark that it is out of list */ thread->twups = thread; /* mark that it is out of list */
for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) {
work++;
if (!iswhite(uv)) /* upvalue already visited? */ if (!iswhite(uv)) /* upvalue already visited? */
markvalue(g, uv->v); /* mark its value */ markvalue(g, uv->v); /* mark its value */
} }
} }
} }
return work;
} }
@@ -491,8 +498,7 @@ static lu_mem traversetable (global_State *g, Table *h) {
} }
else /* not weak */ else /* not weak */
traversestrongtable(g, h); traversestrongtable(g, h);
return sizeof(Table) + sizeof(TValue) * h->sizearray + return 1 + h->sizearray + 2 * allocsizenode(h);
sizeof(Node) * cast(size_t, allocsizenode(h));
} }
@@ -539,38 +545,33 @@ static int traverseproto (global_State *g, Proto *f) {
markobjectN(g, f->p[i]); markobjectN(g, f->p[i]);
for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
markobjectN(g, f->locvars[i].varname); markobjectN(g, f->locvars[i].varname);
return sizeof(Proto) + sizeof(Instruction) * f->sizecode + return 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars;
sizeof(Proto *) * f->sizep +
sizeof(TValue) * f->sizek +
sizeof(int) * f->sizelineinfo +
sizeof(LocVar) * f->sizelocvars +
sizeof(Upvaldesc) * f->sizeupvalues;
} }
static lu_mem traverseCclosure (global_State *g, CClosure *cl) { static int traverseCclosure (global_State *g, CClosure *cl) {
int i; int i;
for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
markvalue(g, &cl->upvalue[i]); markvalue(g, &cl->upvalue[i]);
return sizeCclosure(cl->nupvalues); return 1 + cl->nupvalues;
} }
/* /*
** Traverse a Lua closure, marking its prototype and its upvalues. ** Traverse a Lua closure, marking its prototype and its upvalues.
** (Both can be NULL while closure is being created.) ** (Both can be NULL while closure is being created.)
*/ */
static lu_mem traverseLclosure (global_State *g, LClosure *cl) { static int traverseLclosure (global_State *g, LClosure *cl) {
int i; int i;
markobjectN(g, cl->p); /* mark its prototype */ markobjectN(g, cl->p); /* mark its prototype */
for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */ for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */
UpVal *uv = cl->upvals[i]; UpVal *uv = cl->upvals[i];
markobjectN(g, uv); /* mark upvalue */ markobjectN(g, uv); /* mark upvalue */
} }
return sizeLclosure(cl->nupvalues); return 1 + cl->nupvalues;
} }
static lu_mem traversethread (global_State *g, lua_State *th) { static int traversethread (global_State *g, lua_State *th) {
StkId o = th->stack; StkId o = th->stack;
if (o == NULL) if (o == NULL)
return 1; /* stack not completely built yet */ return 1; /* stack not completely built yet */
@@ -590,8 +591,7 @@ static lu_mem traversethread (global_State *g, lua_State *th) {
} }
else if (!g->gcemergency) else if (!g->gcemergency)
luaD_shrinkstack(th); /* do not change stack in emergency cycle */ luaD_shrinkstack(th); /* do not change stack in emergency cycle */
return (sizeof(lua_State) + sizeof(TValue) * th->stacksize + return 1 + th->stacksize;
sizeof(CallInfo) * th->nci);
} }
@@ -599,51 +599,47 @@ static lu_mem traversethread (global_State *g, lua_State *th) {
** traverse one gray object, turning it to black (except for threads, ** traverse one gray object, turning it to black (except for threads,
** which are always gray). ** which are always gray).
*/ */
static void propagatemark (global_State *g) { static lu_mem propagatemark (global_State *g) {
lu_mem size;
GCObject *o = g->gray; GCObject *o = g->gray;
gray2black(o); gray2black(o);
switch (o->tt) { switch (o->tt) {
case LUA_TTABLE: { case LUA_TTABLE: {
Table *h = gco2t(o); Table *h = gco2t(o);
g->gray = h->gclist; /* remove from 'gray' list */ g->gray = h->gclist; /* remove from 'gray' list */
size = traversetable(g, h); return traversetable(g, h);
break;
} }
case LUA_TLCL: { case LUA_TLCL: {
LClosure *cl = gco2lcl(o); LClosure *cl = gco2lcl(o);
g->gray = cl->gclist; /* remove from 'gray' list */ g->gray = cl->gclist; /* remove from 'gray' list */
size = traverseLclosure(g, cl); return traverseLclosure(g, cl);
break;
} }
case LUA_TCCL: { case LUA_TCCL: {
CClosure *cl = gco2ccl(o); CClosure *cl = gco2ccl(o);
g->gray = cl->gclist; /* remove from 'gray' list */ g->gray = cl->gclist; /* remove from 'gray' list */
size = traverseCclosure(g, cl); return traverseCclosure(g, cl);
break;
} }
case LUA_TTHREAD: { case LUA_TTHREAD: {
lua_State *th = gco2th(o); lua_State *th = gco2th(o);
g->gray = th->gclist; /* remove from 'gray' list */ g->gray = th->gclist; /* remove from 'gray' list */
linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
black2gray(o); black2gray(o);
size = traversethread(g, th); return traversethread(g, th);
break;
} }
case LUA_TPROTO: { case LUA_TPROTO: {
Proto *p = gco2p(o); Proto *p = gco2p(o);
g->gray = p->gclist; /* remove from 'gray' list */ g->gray = p->gclist; /* remove from 'gray' list */
size = traverseproto(g, p); return traverseproto(g, p);
break;
} }
default: lua_assert(0); return; default: lua_assert(0); return 0;
} }
g->GCmemtrav += size;
} }
static void propagateall (global_State *g) { static lu_mem propagateall (global_State *g) {
while (g->gray) propagatemark(g); lu_mem tot = 0;
while (g->gray)
tot += propagatemark(g);
return tot;
} }
@@ -719,7 +715,7 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) {
setnilvalue(o); /* remove value */ setnilvalue(o); /* remove value */
} }
for (n = gnode(h, 0); n < limit; n++) { for (n = gnode(h, 0); n < limit; n++) {
if (!ttisnil(gval(n)) && iscleared(g, gval(n))) { if (iscleared(g, gval(n))) {
setnilvalue(gval(n)); /* remove value ... */ setnilvalue(gval(n)); /* remove value ... */
removeentry(n); /* and remove entry from table */ removeentry(n); /* and remove entry from table */
} }
@@ -770,22 +766,20 @@ static void freeobj (lua_State *L, GCObject *o) {
} }
#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM)
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count);
/* /*
** sweep at most 'count' elements from a list of GCObjects erasing dead ** sweep at most 'countin' elements from a list of GCObjects erasing dead
** objects, where a dead object is one marked with the old (non current) ** objects, where a dead object is one marked with the old (non current)
** white; change all non-dead objects back to white, preparing for next ** white; change all non-dead objects back to white, preparing for next
** collection cycle. Return where to continue the traversal or NULL if ** collection cycle. Return where to continue the traversal or NULL if
** list is finished. ** list is finished. ('*countout' gets the number of elements traversed.)
*/ */
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { static GCObject **sweeplist (lua_State *L, GCObject **p, int countin,
int *countout) {
global_State *g = G(L); global_State *g = G(L);
int ow = otherwhite(g); int ow = otherwhite(g);
int i;
int white = luaC_white(g); /* current white */ int white = luaC_white(g); /* current white */
while (*p != NULL && count-- > 0) { for (i = 0; *p != NULL && i < countin; i++) {
GCObject *curr = *p; GCObject *curr = *p;
int marked = curr->marked; int marked = curr->marked;
if (isdeadm(ow, marked)) { /* is 'curr' dead? */ if (isdeadm(ow, marked)) { /* is 'curr' dead? */
@@ -797,6 +791,8 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
p = &curr->next; /* go to next element */ p = &curr->next; /* go to next element */
} }
} }
if (countout)
*countout = i; /* number of elements traversed */
return (*p == NULL) ? NULL : p; return (*p == NULL) ? NULL : p;
} }
@@ -807,7 +803,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
static GCObject **sweeptolive (lua_State *L, GCObject **p) { static GCObject **sweeptolive (lua_State *L, GCObject **p) {
GCObject **old = p; GCObject **old = p;
do { do {
p = sweeplist(L, p, 1); p = sweeplist(L, p, 1, NULL);
} while (p == old); } while (p == old);
return p; return p;
} }
@@ -892,16 +888,13 @@ static void GCTM (lua_State *L, int propagateerrors) {
/* /*
** call a few (up to 'g->gcfinnum') finalizers ** Call a few finalizers
*/ */
static int runafewfinalizers (lua_State *L) { static int runafewfinalizers (lua_State *L, int n) {
global_State *g = G(L); global_State *g = G(L);
unsigned int i; int i;
lua_assert(!g->tobefnz || g->gcfinnum > 0); for (i = 0; i < n && g->tobefnz; i++)
for (i = 0; g->tobefnz && i < g->gcfinnum; i++)
GCTM(L, 1); /* call one finalizer */ GCTM(L, 1); /* call one finalizer */
g->gcfinnum = (!g->tobefnz) ? 0 /* nothing more to finalize? */
: g->gcfinnum * 2; /* else call a few more next time */
return i; return i;
} }
@@ -1285,9 +1278,6 @@ static void genstep (lua_State *L, global_State *g) {
//lua_checkmemory(L); //lua_checkmemory(L);
} }
/* }====================================================== */ /* }====================================================== */
@@ -1299,26 +1289,28 @@ static void genstep (lua_State *L, global_State *g) {
/* /*
** Set a reasonable "time" to wait before starting a new GC cycle; cycle ** Set the "time" to wait before starting a new GC cycle; cycle will
** will start when memory use hits threshold. (Division by 'estimate' ** start when memory use hits the threshold of ('estimate' * gcpause /
** should be OK: it cannot be zero (because Lua cannot even start with ** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero,
** less than PAUSEADJ bytes). ** because Lua cannot even start with less than PAUSEADJ bytes).
*/ */
static void setpause (global_State *g) { static void setpause (global_State *g) {
l_mem threshold, debt; l_mem threshold, debt;
int pause = g->gcpause + 100;
l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
lua_assert(estimate > 0); lua_assert(estimate > 0);
threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */ threshold = (pause < MAX_LMEM / estimate) /* overflow? */
? estimate * g->gcpause /* no overflow */ ? estimate * pause /* no overflow */
: MAX_LMEM; /* overflow; truncate to maximum */ : MAX_LMEM; /* overflow; truncate to maximum */
debt = gettotalbytes(g) - threshold; debt = gettotalbytes(g) - threshold;
if (debt > 0) debt = 0;
luaE_setdebt(g, debt); luaE_setdebt(g, debt);
} }
/* /*
** Enter first sweep phase. ** Enter first sweep phase.
** The call to 'sweeplist' tries to make pointer point to an object ** The call to 'sweeptolive' makes the pointer point to an object
** inside the list (instead of to the header), so that the real sweep do ** inside the list (instead of to the header), so that the real sweep do
** not need to skip objects created between "now" and the start of the ** not need to skip objects created between "now" and the start of the
** real sweep. ** real sweep.
@@ -1327,11 +1319,15 @@ static void entersweep (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
g->gcstate = GCSswpallgc; g->gcstate = GCSswpallgc;
lua_assert(g->sweepgc == NULL); lua_assert(g->sweepgc == NULL);
g->sweepgc = sweeplist(L, &g->allgc, 1); g->sweepgc = sweeptolive(L, &g->allgc);
} }
static void deletealllist (lua_State *L, GCObject *p, GCObject *limit) { /*
** Delete all objects in list 'p' until (but not including) object
** 'limit'.
*/
static void deletelist (lua_State *L, GCObject *p, GCObject *limit) {
while (p != limit) { while (p != limit) {
GCObject *next = p->next; GCObject *next = p->next;
freeobj(L, p); freeobj(L, p);
@@ -1340,52 +1336,50 @@ static void deletealllist (lua_State *L, GCObject *p, GCObject *limit) {
} }
/*
** Call all finalizers of the objects in the given Lua state, and
** then free all objects, except for the main thread.
*/
void luaC_freeallobjects (lua_State *L) { void luaC_freeallobjects (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
luaC_changemode(L, KGC_INC); luaC_changemode(L, KGC_INC);
separatetobefnz(g, 1); /* separate all objects with finalizers */ separatetobefnz(g, 1); /* separate all objects with finalizers */
lua_assert(g->finobj == NULL); lua_assert(g->finobj == NULL);
callallpendingfinalizers(L); callallpendingfinalizers(L);
deletealllist(L, g->allgc, obj2gco(g->mainthread)); deletelist(L, g->allgc, obj2gco(g->mainthread));
deletealllist(L, g->finobj, NULL); deletelist(L, g->finobj, NULL);
deletealllist(L, g->fixedgc, NULL); /* collect fixed objects */ deletelist(L, g->fixedgc, NULL); /* collect fixed objects */
lua_assert(g->strt.nuse == 0); lua_assert(g->strt.nuse == 0);
} }
static l_mem atomic (lua_State *L) { static lu_mem atomic (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
l_mem work; lu_mem work = 0;
GCObject *origweak, *origall; GCObject *origweak, *origall;
GCObject *grayagain = g->grayagain; /* save original list */ GCObject *grayagain = g->grayagain; /* save original list */
g->grayagain = NULL; g->grayagain = NULL;
lua_assert(g->ephemeron == NULL && g->weak == NULL); lua_assert(g->ephemeron == NULL && g->weak == NULL);
lua_assert(!iswhite(g->mainthread)); lua_assert(!iswhite(g->mainthread));
g->gcstate = GCSatomic; g->gcstate = GCSatomic;
g->GCmemtrav = 0; /* start counting work */
markobject(g, L); /* mark running thread */ markobject(g, L); /* mark running thread */
/* registry and global metatables may be changed by API */ /* registry and global metatables may be changed by API */
markvalue(g, &g->l_registry); markvalue(g, &g->l_registry);
markmt(g); /* mark global metatables */ markmt(g); /* mark global metatables */
/* remark occasional upvalues of (maybe) dead threads */ /* remark occasional upvalues of (maybe) dead threads */
remarkupvals(g); work += remarkupvals(g);
propagateall(g); /* propagate changes */ work += propagateall(g); /* propagate changes */
work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */
g->gray = grayagain; g->gray = grayagain;
propagateall(g); /* traverse 'grayagain' list */ work += propagateall(g); /* traverse 'grayagain' list */
g->GCmemtrav = 0; /* restart counting */
convergeephemerons(g); convergeephemerons(g);
/* at this point, all strongly accessible objects are marked. */ /* at this point, all strongly accessible objects are marked. */
/* Clear values from weak tables, before checking finalizers */ /* Clear values from weak tables, before checking finalizers */
clearvalues(g, g->weak, NULL); clearvalues(g, g->weak, NULL);
clearvalues(g, g->allweak, NULL); clearvalues(g, g->allweak, NULL);
origweak = g->weak; origall = g->allweak; origweak = g->weak; origall = g->allweak;
work += g->GCmemtrav; /* stop counting (objects being finalized) */
separatetobefnz(g, 0); /* separate objects to be finalized */ separatetobefnz(g, 0); /* separate objects to be finalized */
g->gcfinnum = 1; /* there may be objects to be finalized */ work += markbeingfnz(g); /* mark objects that will be finalized */
markbeingfnz(g); /* mark objects that will be finalized */ work += propagateall(g); /* remark, to propagate 'resurrection' */
propagateall(g); /* remark, to propagate 'resurrection' */
g->GCmemtrav = 0; /* restart counting */
convergeephemerons(g); convergeephemerons(g);
/* at this point, all resurrected objects are marked. */ /* at this point, all resurrected objects are marked. */
/* remove dead objects from weak tables */ /* remove dead objects from weak tables */
@@ -1398,24 +1392,24 @@ static l_mem atomic (lua_State *L) {
clearprotolist(g); clearprotolist(g);
g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
lua_assert(g->gray == NULL); lua_assert(g->gray == NULL);
work += g->GCmemtrav; /* complete counting */ return work; /* estimate of slots marked by 'atomic' */
return work; /* estimate of memory marked by 'atomic' */
} }
static lu_mem sweepstep (lua_State *L, global_State *g, static int sweepstep (lua_State *L, global_State *g,
int nextstate, GCObject **nextlist) { int nextstate, GCObject **nextlist) {
if (g->sweepgc) { if (g->sweepgc) {
l_mem olddebt = g->GCdebt; l_mem olddebt = g->GCdebt;
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); int count;
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count);
g->GCestimate += g->GCdebt - olddebt; /* update estimate */ g->GCestimate += g->GCdebt - olddebt; /* update estimate */
if (g->sweepgc) /* is there still something to sweep? */ return count;
return (GCSWEEPMAX * GCSWEEPCOST); }
else { /* enter next state */
g->gcstate = nextstate;
g->sweepgc = nextlist;
return 0; /* no work done */
} }
/* else enter next state */
g->gcstate = nextstate;
g->sweepgc = nextlist;
return 0;
} }
@@ -1423,23 +1417,21 @@ static lu_mem singlestep (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
switch (g->gcstate) { switch (g->gcstate) {
case GCSpause: { case GCSpause: {
g->GCmemtrav = g->strt.size * sizeof(GCObject*);
restartcollection(g); restartcollection(g);
g->gcstate = GCSpropagate; g->gcstate = GCSpropagate;
return g->GCmemtrav; return 1;
} }
case GCSpropagate: { case GCSpropagate: {
g->GCmemtrav = 0; if (g->gray == NULL) { /* no more gray objects? */
if (g->gray == NULL) /* no more gray objects? */
g->gcstate = GCSenteratomic; /* finish propagate phase */ g->gcstate = GCSenteratomic; /* finish propagate phase */
return 0;
}
else else
propagatemark(g); /* traverse one gray object */ return propagatemark(g); /* traverse one gray object */
return g->GCmemtrav; /* memory traversed in this step */
} }
case GCSenteratomic: { case GCSenteratomic: {
lu_mem work; lu_mem work = propagateall(g); /* make sure gray list is empty */
propagateall(g); /* make sure gray list is empty */ work += atomic(L); /* work is what was traversed by 'atomic' */
work = atomic(L); /* work is what was traversed by 'atomic' */
entersweep(L); entersweep(L);
g->GCestimate = gettotalbytes(g); /* first estimate */; g->GCestimate = gettotalbytes(g); /* first estimate */;
return work; return work;
@@ -1460,8 +1452,8 @@ static lu_mem singlestep (lua_State *L) {
} }
case GCScallfin: { /* call remaining finalizers */ case GCScallfin: { /* call remaining finalizers */
if (g->tobefnz && !g->gcemergency) { if (g->tobefnz && !g->gcemergency) {
int n = runafewfinalizers(L); int n = runafewfinalizers(L, GCFINMAX);
return (n * GCFINALIZECOST); return n * GCFINALIZECOST;
} }
else { /* emergency mode or no more finalizers */ else { /* emergency mode or no more finalizers */
g->gcstate = GCSpause; /* finish collection */ g->gcstate = GCSpause; /* finish collection */
@@ -1485,45 +1477,36 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
/* /*
** get GC debt and convert it from Kb to 'work units' (avoid zero debt ** Performs a basic incremental step. The debt and step size are
** and overflows) ** converted from bytes to "units of work"; then the function loops
*/ ** running single steps until adding that many units of work or
static l_mem getdebt (global_State *g) { ** finishing a cycle (pause state). Finally, it sets the debt that
l_mem debt = g->GCdebt; ** controls when next step will be performed.
int stepmul = g->gcstepmul;
if (debt <= 0) return 0; /* minimal debt */
else {
debt = (debt / STEPMULADJ) + 1;
debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
return debt;
}
}
/*
** performs a basic incremental step
*/ */
static void incstep (lua_State *L, global_State *g) { static void incstep (lua_State *L, global_State *g) {
l_mem debt = getdebt(g); /* GC deficit (to be paid now) */ int stepmul = (g->gcstepmul | 1); /* avoid division by 0 */
l_mem debt = (g->GCdebt / WORK2MEM) * stepmul;
l_mem stepsize = cast(l_mem, 1) << g->gcstepsize;
stepsize = -((stepsize / WORK2MEM) * stepmul);
do { /* repeat until pause or enough "credit" (negative debt) */ do { /* repeat until pause or enough "credit" (negative debt) */
lu_mem work = singlestep(L); /* perform one single step */ lu_mem work = singlestep(L); /* perform one single step */
debt -= work; debt -= work;
} while (debt > -GCSTEPSIZE && g->gcstate != GCSpause); } while (debt > stepsize && g->gcstate != GCSpause);
if (g->gcstate == GCSpause) if (g->gcstate == GCSpause)
setpause(g); /* pause until next cycle */ setpause(g); /* pause until next cycle */
else { else {
debt = (debt / g->gcstepmul) * STEPMULADJ; /* convert 'work units' to Kb */ debt = (debt / stepmul) * WORK2MEM; /* convert 'work units' to bytes */
luaE_setdebt(g, debt); luaE_setdebt(g, debt);
runafewfinalizers(L);
} }
} }
/* /*
** performs a basic GC step when collector is running ** performs a basic GC step if collector is running
*/ */
void luaC_step (lua_State *L) { void luaC_step (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
if (!g->gcrunning) /* not running? */ if (!g->gcrunning) /* not running? */
luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */ luaE_setdebt(g, -MAX_LMEM); /* avoid being called without need */
else if (g->gckind == KGC_INC) else if (g->gckind == KGC_INC)
incstep(L, g); incstep(L, g);
else else
@@ -1532,9 +1515,7 @@ void luaC_step (lua_State *L) {
/* /*
** Performs a full GC cycle; if 'isemergency', set a flag to avoid ** Perform a full collection in incremental mode.
** some operations which could change the interpreter state in some
** unexpected ways (running finalizers and shrinking some structures).
** Before running the collection, check 'keepinvariant'; if it is true, ** Before running the collection, check 'keepinvariant'; if it is true,
** there may be some objects marked as black, so the collector has ** there may be some objects marked as black, so the collector has
** to sweep all objects to turn them back to white (as white has not ** to sweep all objects to turn them back to white (as white has not
@@ -1553,6 +1534,11 @@ static void fullinc (lua_State *L, global_State *g) {
} }
/*
** Performs a full GC cycle; if 'isemergency', set a flag to avoid
** some operations which could change the interpreter state in some
** unexpected ways (running finalizers and shrinking some structures).
*/
void luaC_fullgc (lua_State *L, int isemergency) { void luaC_fullgc (lua_State *L, int isemergency) {
global_State *g = G(L); global_State *g = G(L);
lua_assert(!g->gcemergency); lua_assert(!g->gcemergency);

20
lgc.h
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lgc.h,v 2.96 2017/04/11 18:41:09 roberto Exp roberto $ ** $Id: lgc.h,v 2.97 2017/05/04 13:32:01 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -25,14 +25,6 @@
*/ */
/* how much to allocate before next GC step */
#if !defined(GCSTEPSIZE)
/* ~100 small strings */
#define GCSTEPSIZE (cast_int(100 * sizeof(TString)))
#endif
/* /*
** Possible states of the Garbage Collector ** Possible states of the Garbage Collector
*/ */
@@ -125,6 +117,16 @@
check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
/* Default Values for GC parameters */
#define LUAI_GENMAJORMUL 100
#define LUAI_GENMINORMUL 5
#define LUAI_GCPAUSE 100 /* 100% */
#define LUAI_GCMUL 10
/* how much to allocate before next GC step (log2) */
#define LUAI_GCSTEPSIZE 13 /* 8 KB */
/* /*
** Does one step of collection when debt becomes positive. 'pre'/'pos' ** Does one step of collection when debt becomes positive. 'pre'/'pos'
** allows some adjustments to be done only when needed. macro ** allows some adjustments to be done only when needed. macro

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lstate.c,v 2.138 2017/04/24 16:59:26 roberto Exp roberto $ ** $Id: lstate.c,v 2.139 2017/05/04 13:32:01 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -28,14 +28,6 @@
#include "ltm.h" #include "ltm.h"
#if !defined(LUAI_GCPAUSE)
#define LUAI_GCPAUSE 200 /* 200% */
#endif
#if !defined(LUAI_GCMUL)
#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
#endif
/* /*
** a macro to help the creation of a unique random seed when a state is ** a macro to help the creation of a unique random seed when a state is
@@ -328,9 +320,9 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
g->twups = NULL; g->twups = NULL;
g->totalbytes = sizeof(LG); g->totalbytes = sizeof(LG);
g->GCdebt = 0; g->GCdebt = 0;
g->gcfinnum = 0;
g->gcpause = LUAI_GCPAUSE; g->gcpause = LUAI_GCPAUSE;
g->gcstepmul = LUAI_GCMUL; g->gcstepmul = LUAI_GCMUL;
g->gcstepsize = LUAI_GCSTEPSIZE;
for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
/* memory allocation error: free partial state */ /* memory allocation error: free partial state */

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 2.140 2017/05/04 13:32:01 roberto Exp roberto $ ** $Id: lstate.h,v 2.141 2017/05/13 13:54:47 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -139,7 +139,6 @@ typedef struct global_State {
void *ud; /* auxiliary data to 'frealloc' */ void *ud; /* auxiliary data to 'frealloc' */
l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
lu_mem GCmemtrav; /* memory traversed by the GC */
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
stringtable strt; /* hash table for strings */ stringtable strt; /* hash table for strings */
TValue l_registry; TValue l_registry;
@@ -151,6 +150,9 @@ typedef struct global_State {
lu_byte genmajormul; /* control for major generational collections */ lu_byte genmajormul; /* control for major generational collections */
lu_byte gcrunning; /* true if GC is running */ lu_byte gcrunning; /* true if GC is running */
lu_byte gcemergency; /* true if this is an emergency collection */ lu_byte gcemergency; /* true if this is an emergency collection */
lu_byte gcpause; /* size of pause between successive GCs */
lu_byte gcstepmul; /* GC "speed" */
lu_byte gcstepsize; /* (log2 of) GC granularity */
GCObject *allgc; /* list of all collectable objects */ GCObject *allgc; /* list of all collectable objects */
GCObject **sweepgc; /* current position of sweep in list */ GCObject **sweepgc; /* current position of sweep in list */
GCObject *finobj; /* list of collectable objects with finalizers */ GCObject *finobj; /* list of collectable objects with finalizers */
@@ -170,9 +172,6 @@ typedef struct global_State {
GCObject *finobjold; /* list of old objects with finalizers */ GCObject *finobjold; /* list of old objects with finalizers */
GCObject *finobjrold; /* list of really old objects with finalizers */ GCObject *finobjrold; /* list of really old objects with finalizers */
struct lua_State *twups; /* list of threads with open upvalues */ struct lua_State *twups; /* list of threads with open upvalues */
unsigned int gcfinnum; /* number of finalizers to call in each GC step */
int gcpause; /* size of pause between successive GCs */
int gcstepmul; /* GC 'granularity' */
lua_CFunction panic; /* to be called in unprotected errors */ lua_CFunction panic; /* to be called in unprotected errors */
struct lua_State *mainthread; struct lua_State *mainthread;
const lua_Number *version; /* pointer to version number */ const lua_Number *version; /* pointer to version number */

4
lua.h
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.333 2017/02/23 21:07:34 roberto Exp roberto $ ** $Id: lua.h,v 1.334 2017/05/18 12:34:58 roberto Exp roberto $
** Lua - A Scripting Language ** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file ** See Copyright Notice at the end of this file
@@ -311,7 +311,7 @@ LUA_API int (lua_isyieldable) (lua_State *L);
#define LUA_GCGEN 10 #define LUA_GCGEN 10
#define LUA_GCINC 11 #define LUA_GCINC 11
LUA_API int (lua_gc) (lua_State *L, int what, int data); LUA_API int (lua_gc) (lua_State *L, int what, ...);
/* /*