First version of GC counting objects for control

Still needs to review generational mode.
This commit is contained in:
Roberto Ierusalimschy 2022-11-23 17:17:20 -03:00
parent 76953316d1
commit f356d5acdd
9 changed files with 162 additions and 156 deletions

9
lapi.c
View File

@ -1154,11 +1154,11 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
} }
case LUA_GCCOUNT: { case LUA_GCCOUNT: {
/* GC values are expressed in Kbytes: #bytes/2^10 */ /* GC values are expressed in Kbytes: #bytes/2^10 */
res = cast_int(gettotalbytes(g) >> 10); res = cast_int(g->totalbytes >> 10);
break; break;
} }
case LUA_GCCOUNTB: { case LUA_GCCOUNTB: {
res = cast_int(gettotalbytes(g) & 0x3ff); res = cast_int(g->totalbytes & 0x3ff);
break; break;
} }
case LUA_GCSTEP: { case LUA_GCSTEP: {
@ -1171,7 +1171,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
luaC_step(L); luaC_step(L);
} }
else { /* add 'data' to total debt */ else { /* add 'data' to total debt */
debt = cast(l_mem, data) * 1024 + g->GCdebt; debt = data + g->GCdebt;
luaE_setdebt(g, debt); luaE_setdebt(g, debt);
luaC_checkGC(L); luaC_checkGC(L);
} }
@ -1217,7 +1217,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
if (stepmul != 0) if (stepmul != 0)
setgcparam(g->gcstepmul, stepmul); setgcparam(g->gcstepmul, stepmul);
if (stepsize != 0) if (stepsize != 0)
g->gcstepsize = stepsize; g->gcstepsize = (stepsize <= log2maxs(l_mem)) ? stepsize
: log2maxs(l_mem);
luaC_changemode(L, KGC_INC); luaC_changemode(L, KGC_INC);
break; break;
} }

251
lgc.c
View File

@ -19,6 +19,7 @@
#include "ldo.h" #include "ldo.h"
#include "lfunc.h" #include "lfunc.h"
#include "lgc.h" #include "lgc.h"
#include "llex.h"
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
#include "lstate.h" #include "lstate.h"
@ -32,7 +33,7 @@
** (Large enough to dissipate fixed overheads but small enough ** (Large enough to dissipate fixed overheads but small enough
** to allow small steps for the collector.) ** to allow small steps for the collector.)
*/ */
#define GCSWEEPMAX 100 #define GCSWEEPMAX 20
/* /*
** Maximum number of finalizers to call in each single step. ** Maximum number of finalizers to call in each single step.
@ -46,19 +47,6 @@
#define GCFINALIZECOST 50 #define GCFINALIZECOST 50
/*
** The equivalent, in bytes, of one unit of "work" (visiting a slot,
** sweeping an object, etc.)
*/
#define WORK2MEM sizeof(TValue)
/*
** macro to adjust 'pause': 'pause' is actually used like
** 'pause / PAUSEADJ' (value chosen by tests)
*/
#define PAUSEADJ 100
/* mask with all color bits */ /* mask with all color bits */
#define maskcolors (bitmask(BLACKBIT) | WHITEBITS) #define maskcolors (bitmask(BLACKBIT) | WHITEBITS)
@ -105,7 +93,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 lu_mem atomic (lua_State *L); static l_mem atomic (lua_State *L);
static void entersweep (lua_State *L); static void entersweep (lua_State *L);
@ -259,7 +247,7 @@ GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) {
global_State *g = G(L); global_State *g = G(L);
char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); char *p = cast_charp(luaM_newobject(L, novariant(tt), sz));
GCObject *o = cast(GCObject *, p + offset); GCObject *o = cast(GCObject *, p + offset);
g->totalobjs++; g->GCdebt++;
o->marked = luaC_white(g); o->marked = luaC_white(g);
o->tt = tt; o->tt = tt;
o->next = g->allgc; o->next = g->allgc;
@ -268,6 +256,9 @@ GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) {
} }
/*
** create a new collectable object with no offset.
*/
GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
return luaC_newobjdt(L, tt, sz, 0); return luaC_newobjdt(L, tt, sz, 0);
} }
@ -296,6 +287,7 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
** (only closures can), and a userdata's metatable must be a table. ** (only closures can), and a userdata's metatable must be a table.
*/ */
static void reallymarkobject (global_State *g, GCObject *o) { static void reallymarkobject (global_State *g, GCObject *o) {
g->marked++;
switch (o->tt) { switch (o->tt) {
case LUA_VSHRSTR: case LUA_VSHRSTR:
case LUA_VLNGSTR: { case LUA_VLNGSTR: {
@ -343,9 +335,9 @@ static void markmt (global_State *g) {
/* /*
** mark all objects in list of being-finalized ** mark all objects in list of being-finalized
*/ */
static lu_mem markbeingfnz (global_State *g) { static l_mem markbeingfnz (global_State *g) {
GCObject *o; GCObject *o;
lu_mem count = 0; l_mem count = 0;
for (o = g->tobefnz; o != NULL; o = o->next) { for (o = g->tobefnz; o != NULL; o = o->next) {
count++; count++;
markobject(g, o); markobject(g, o);
@ -365,12 +357,11 @@ static lu_mem markbeingfnz (global_State *g) {
** upvalues, as they have nothing to be checked. (If the thread gets an ** upvalues, as they have nothing to be checked. (If the thread gets an
** upvalue later, it will be linked in the list again.) ** upvalue later, it will be linked in the list again.)
*/ */
static int remarkupvals (global_State *g) { static l_mem remarkupvals (global_State *g) {
l_mem work = 0;
lua_State *thread; lua_State *thread;
lua_State **p = &g->twups; lua_State **p = &g->twups;
int work = 0; /* estimate of how much work was done here */
while ((thread = *p) != NULL) { while ((thread = *p) != NULL) {
work++;
if (!iswhite(thread) && thread->openupval != NULL) if (!iswhite(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 */
else { /* thread is not marked or without upvalues */ else { /* thread is not marked or without upvalues */
@ -380,13 +371,13 @@ static int remarkupvals (global_State *g) {
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) {
lua_assert(getage(uv) <= getage(thread)); lua_assert(getage(uv) <= getage(thread));
work++;
if (!iswhite(uv)) { /* upvalue already visited? */ if (!iswhite(uv)) { /* upvalue already visited? */
lua_assert(upisopen(uv) && isgray(uv)); lua_assert(upisopen(uv) && isgray(uv));
markvalue(g, uv->v.p); /* mark its value */ markvalue(g, uv->v.p); /* mark its value */
} }
} }
} }
work++;
} }
return work; return work;
} }
@ -399,10 +390,15 @@ static void cleargraylists (global_State *g) {
/* /*
** mark root set and reset all gray lists, to start a new collection ** mark root set and reset all gray lists, to start a new collection.
** 'marked' is initialized with the number of fixed objects in the state,
** to count the total number of live objects during a cycle. (That is
** the metafield names, plus the reserved words, plus "_ENV" plus the
** memory-error message.)
*/ */
static void restartcollection (global_State *g) { static void restartcollection (global_State *g) {
cleargraylists(g); cleargraylists(g);
g->marked = TM_N + NUM_RESERVED + 2;
markobject(g, g->mainthread); markobject(g, g->mainthread);
markvalue(g, &g->l_registry); markvalue(g, &g->l_registry);
markmt(g); markmt(g);
@ -540,7 +536,7 @@ static void traversestrongtable (global_State *g, Table *h) {
} }
static lu_mem traversetable (global_State *g, Table *h) { static void traversetable (global_State *g, Table *h) {
const char *weakkey, *weakvalue; const char *weakkey, *weakvalue;
const TValue *mode = gfasttm(g, h->metatable, TM_MODE); const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
markobjectN(g, h->metatable); markobjectN(g, h->metatable);
@ -557,17 +553,15 @@ static lu_mem traversetable (global_State *g, Table *h) {
} }
else /* not weak */ else /* not weak */
traversestrongtable(g, h); traversestrongtable(g, h);
return 1 + h->alimit + 2 * allocsizenode(h);
} }
static int traverseudata (global_State *g, Udata *u) { static void traverseudata (global_State *g, Udata *u) {
int i; int i;
markobjectN(g, u->metatable); /* mark its metatable */ markobjectN(g, u->metatable); /* mark its metatable */
for (i = 0; i < u->nuvalue; i++) for (i = 0; i < u->nuvalue; i++)
markvalue(g, &u->uv[i].uv); markvalue(g, &u->uv[i].uv);
genlink(g, obj2gco(u)); genlink(g, obj2gco(u));
return 1 + u->nuvalue;
} }
@ -576,7 +570,7 @@ static int traverseudata (global_State *g, Udata *u) {
** arrays can be larger than needed; the extra slots are filled with ** arrays can be larger than needed; the extra slots are filled with
** NULL, so the use of 'markobjectN') ** NULL, so the use of 'markobjectN')
*/ */
static int traverseproto (global_State *g, Proto *f) { static void traverseproto (global_State *g, Proto *f) {
int i; int i;
markobjectN(g, f->source); markobjectN(g, f->source);
for (i = 0; i < f->sizek; i++) /* mark literals */ for (i = 0; i < f->sizek; i++) /* mark literals */
@ -587,29 +581,26 @@ 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 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars;
} }
static int traverseCclosure (global_State *g, CClosure *cl) { static void 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 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 int traverseLclosure (global_State *g, LClosure *cl) { static void 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 1 + cl->nupvalues;
} }
@ -625,13 +616,13 @@ static int traverseLclosure (global_State *g, LClosure *cl) {
** (which can only happen in generational mode) or if the traverse is in ** (which can only happen in generational mode) or if the traverse is in
** the propagate phase (which can only happen in incremental mode). ** the propagate phase (which can only happen in incremental mode).
*/ */
static int traversethread (global_State *g, lua_State *th) { static void traversethread (global_State *g, lua_State *th) {
UpVal *uv; UpVal *uv;
StkId o = th->stack.p; StkId o = th->stack.p;
if (isold(th) || g->gcstate == GCSpropagate) if (isold(th) || g->gcstate == GCSpropagate)
linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
if (o == NULL) if (o == NULL)
return 1; /* stack not completely built yet */ return; /* stack not completely built yet */
lua_assert(g->gcstate == GCSatomic || lua_assert(g->gcstate == GCSatomic ||
th->openupval == NULL || isintwups(th)); th->openupval == NULL || isintwups(th));
for (; o < th->top.p; o++) /* mark live elements in the stack */ for (; o < th->top.p; o++) /* mark live elements in the stack */
@ -649,34 +640,35 @@ static int 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 1 + stacksize(th);
} }
/* /*
** traverse one gray object, turning it to black. ** traverse one gray object, turning it to black.
*/ */
static lu_mem propagatemark (global_State *g) { static void propagatemark (global_State *g) {
GCObject *o = g->gray; GCObject *o = g->gray;
nw2black(o); nw2black(o);
g->gray = *getgclist(o); /* remove from 'gray' list */ g->gray = *getgclist(o); /* remove from 'gray' list */
switch (o->tt) { switch (o->tt) {
case LUA_VTABLE: return traversetable(g, gco2t(o)); case LUA_VTABLE: traversetable(g, gco2t(o)); break;
case LUA_VUSERDATA: return traverseudata(g, gco2u(o)); case LUA_VUSERDATA: traverseudata(g, gco2u(o)); break;
case LUA_VLCL: return traverseLclosure(g, gco2lcl(o)); case LUA_VLCL: traverseLclosure(g, gco2lcl(o)); break;
case LUA_VCCL: return traverseCclosure(g, gco2ccl(o)); case LUA_VCCL: traverseCclosure(g, gco2ccl(o)); break;
case LUA_VPROTO: return traverseproto(g, gco2p(o)); case LUA_VPROTO: traverseproto(g, gco2p(o)); break;
case LUA_VTHREAD: return traversethread(g, gco2th(o)); case LUA_VTHREAD: traversethread(g, gco2th(o)); break;
default: lua_assert(0); return 0; default: lua_assert(0);
} }
} }
static lu_mem propagateall (global_State *g) { static l_mem propagateall (global_State *g) {
lu_mem tot = 0; l_mem work = 0;
while (g->gray) while (g->gray) {
tot += propagatemark(g); propagatemark(g);
return tot; work++;
}
return work;
} }
@ -685,10 +677,10 @@ static lu_mem propagateall (global_State *g) {
** Repeat until it converges, that is, nothing new is marked. 'dir' ** Repeat until it converges, that is, nothing new is marked. 'dir'
** inverts the direction of the traversals, trying to speed up ** inverts the direction of the traversals, trying to speed up
** convergence on chains in the same table. ** convergence on chains in the same table.
**
*/ */
static void convergeephemerons (global_State *g) { static l_mem convergeephemerons (global_State *g) {
int changed; int changed;
l_mem work = 0;
int dir = 0; int dir = 0;
do { do {
GCObject *w; GCObject *w;
@ -703,9 +695,11 @@ static void convergeephemerons (global_State *g) {
propagateall(g); /* propagate changes */ propagateall(g); /* propagate changes */
changed = 1; /* will have to revisit all ephemeron tables */ changed = 1; /* will have to revisit all ephemeron tables */
} }
work++;
} }
dir = !dir; /* invert direction next time */ dir = !dir; /* invert direction next time */
} while (changed); /* repeat until no more changes */ } while (changed); /* repeat until no more changes */
return work;
} }
/* }====================================================== */ /* }====================================================== */
@ -721,7 +715,8 @@ static void convergeephemerons (global_State *g) {
/* /*
** clear entries with unmarked keys from all weaktables in list 'l' ** clear entries with unmarked keys from all weaktables in list 'l'
*/ */
static void clearbykeys (global_State *g, GCObject *l) { static l_mem clearbykeys (global_State *g, GCObject *l) {
l_mem work = 0;
for (; l; l = gco2t(l)->gclist) { for (; l; l = gco2t(l)->gclist) {
Table *h = gco2t(l); Table *h = gco2t(l);
Node *limit = gnodelast(h); Node *limit = gnodelast(h);
@ -732,7 +727,9 @@ static void clearbykeys (global_State *g, GCObject *l) {
if (isempty(gval(n))) /* is entry empty? */ if (isempty(gval(n))) /* is entry empty? */
clearkey(n); /* clear its key */ clearkey(n); /* clear its key */
} }
work++;
} }
return work;
} }
@ -740,7 +737,8 @@ static void clearbykeys (global_State *g, GCObject *l) {
** clear entries with unmarked values from all weaktables in list 'l' up ** clear entries with unmarked values from all weaktables in list 'l' up
** to element 'f' ** to element 'f'
*/ */
static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { static l_mem clearbyvalues (global_State *g, GCObject *l, GCObject *f) {
l_mem work = 0;
for (; l != f; l = gco2t(l)->gclist) { for (; l != f; l = gco2t(l)->gclist) {
Table *h = gco2t(l); Table *h = gco2t(l);
Node *n, *limit = gnodelast(h); Node *n, *limit = gnodelast(h);
@ -757,7 +755,9 @@ static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) {
if (isempty(gval(n))) /* is entry empty? */ if (isempty(gval(n))) /* is entry empty? */
clearkey(n); /* clear its key */ clearkey(n); /* clear its key */
} }
work++;
} }
return work;
} }
@ -819,10 +819,9 @@ static void freeobj (lua_State *L, GCObject *o) {
** 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. ('*countout' gets the number of elements traversed.) ** list is finished.
*/ */
static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, 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 i;
@ -839,8 +838,6 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin,
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;
} }
@ -851,7 +848,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin,
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, NULL); p = sweeplist(L, p, 1);
} while (p == old); } while (p == old);
return p; return p;
} }
@ -870,11 +867,8 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) {
*/ */
static void checkSizes (lua_State *L, global_State *g) { static void checkSizes (lua_State *L, global_State *g) {
if (!g->gcemergency) { if (!g->gcemergency) {
if (g->strt.nuse < g->strt.size / 4) { /* string table too big? */ if (g->strt.nuse < g->strt.size / 4) /* string table too big? */
l_mem olddebt = g->GCdebt;
luaS_resize(L, g->strt.size / 2); luaS_resize(L, g->strt.size / 2);
g->GCestimate += g->GCdebt - olddebt; /* correct estimate */
}
} }
} }
@ -935,12 +929,11 @@ static void GCTM (lua_State *L) {
/* /*
** Call a few finalizers ** Call a few finalizers
*/ */
static int runafewfinalizers (lua_State *L, int n) { static void runafewfinalizers (lua_State *L, int n) {
global_State *g = G(L); global_State *g = G(L);
int i; int i;
for (i = 0; i < n && g->tobefnz; i++) for (i = 0; i < n && g->tobefnz; i++)
GCTM(L); /* call one finalizer */ GCTM(L); /* call one finalizer */
return i;
} }
@ -1052,19 +1045,16 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
/* /*
** Set the "time" to wait before starting a new GC cycle; cycle will ** Set the "time" to wait before starting a new GC cycle; cycle will
** start when memory use hits the threshold of ('estimate' * pause / ** start when number of objects in use hits the threshold of
** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero, ** approximately ('marked' * pause / 100). (A direct multiplication
** because Lua cannot even start with less than PAUSEADJ bytes). ** by 'pause' may overflow, and a direct division by 100 may undeflow
** to zero. So, the division is done in two steps. 8 * 12 is near 100
** and the division by 8 is cheap.)
*/ */
static void setpause (global_State *g) { static void setpause (global_State *g) {
l_mem threshold, debt; unsigned int pause = getgcparam(g->gcpause);
int pause = getgcparam(g->gcpause); lu_mem threshold = g->marked / 8 * pause / 12;
l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ l_mem debt = gettotalobjs(g) - threshold;
lua_assert(estimate > 0);
threshold = (pause < MAX_LMEM / estimate) /* overflow? */
? estimate * pause /* no overflow */
: MAX_LMEM; /* overflow; truncate to maximum */
debt = gettotalbytes(g) - threshold;
if (debt > 0) debt = 0; if (debt > 0) debt = 0;
luaE_setdebt(g, debt); luaE_setdebt(g, debt);
} }
@ -1306,17 +1296,17 @@ static void atomic2gen (lua_State *L, global_State *g) {
g->gckind = KGC_GEN; g->gckind = KGC_GEN;
g->lastatomic = 0; g->lastatomic = 0;
g->GCestimate = gettotalbytes(g); /* base for memory control */ g->GCestimate = gettotalobjs(g); /* base for memory control */
finishgencycle(L, g); finishgencycle(L, g);
} }
/* /*
** Set debt for the next minor collection, which will happen when ** Set debt for the next minor collection, which will happen when
** memory grows 'genminormul'%. ** total number of objects grows 'genminormul'%.
*/ */
static void setminordebt (global_State *g) { static void setminordebt (global_State *g) {
luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul)); luaE_setdebt(g, -(cast(l_mem, (gettotalobjs(g) / 100)) * g->genminormul));
} }
@ -1326,14 +1316,12 @@ static void setminordebt (global_State *g) {
** are cleared. Then, turn all objects into old and finishes the ** are cleared. Then, turn all objects into old and finishes the
** collection. ** collection.
*/ */
static lu_mem entergen (lua_State *L, global_State *g) { static void entergen (lua_State *L, global_State *g) {
lu_mem numobjs;
luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */
luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */
numobjs = atomic(L); /* propagates all and then do the atomic stuff */ atomic(L); /* propagates all and then do the atomic stuff */
atomic2gen(L, g); atomic2gen(L, g);
setminordebt(g); /* set debt assuming next cycle will be minor */ setminordebt(g); /* set debt assuming next cycle will be minor */
return numobjs;
} }
@ -1372,9 +1360,9 @@ void luaC_changemode (lua_State *L, int newmode) {
/* /*
** Does a full collection in generational mode. ** Does a full collection in generational mode.
*/ */
static lu_mem fullgen (lua_State *L, global_State *g) { static void fullgen (lua_State *L, global_State *g) {
enterinc(g); enterinc(g);
return entergen(L, g); entergen(L, g);
} }
@ -1400,22 +1388,22 @@ static lu_mem fullgen (lua_State *L, global_State *g) {
** ('g->lastatomic != 0' also means that the last collection was bad.) ** ('g->lastatomic != 0' also means that the last collection was bad.)
*/ */
static void stepgenfull (lua_State *L, global_State *g) { static void stepgenfull (lua_State *L, global_State *g) {
lu_mem newatomic; /* count of traversed objects */
lu_mem lastatomic = g->lastatomic; /* count from last collection */ lu_mem lastatomic = g->lastatomic; /* count from last collection */
if (g->gckind == KGC_GEN) /* still in generational mode? */ if (g->gckind == KGC_GEN) /* still in generational mode? */
enterinc(g); /* enter incremental mode */ enterinc(g); /* enter incremental mode */
luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */
newatomic = atomic(L); /* mark everybody */ g->marked = 0;
if (newatomic < lastatomic + (lastatomic >> 3)) { /* good collection? */ atomic(L); /* mark everybody */
if (g->marked < lastatomic + (lastatomic >> 3)) { /* good collection? */
atomic2gen(L, g); /* return to generational mode */ atomic2gen(L, g); /* return to generational mode */
setminordebt(g); setminordebt(g);
} }
else { /* another bad collection; stay in incremental mode */ else { /* another bad collection; stay in incremental mode */
g->GCestimate = gettotalbytes(g); /* first estimate */; g->GCestimate = gettotalobjs(g); /* first estimate */;
g->lastatomic = g->marked;
entersweep(L); entersweep(L);
luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */
setpause(g); setpause(g);
g->lastatomic = newatomic;
} }
} }
@ -1443,21 +1431,23 @@ static void genstep (lua_State *L, global_State *g) {
if (g->lastatomic != 0) /* last collection was a bad one? */ if (g->lastatomic != 0) /* last collection was a bad one? */
stepgenfull(L, g); /* do a full step */ stepgenfull(L, g); /* do a full step */
else { else {
lu_mem majorbase = g->GCestimate; /* memory after last major collection */ l_mem majorbase = g->GCestimate; /* objects after last major collection */
lu_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul); l_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul);
if (g->GCdebt > 0 && gettotalbytes(g) > majorbase + majorinc) { if (g->GCdebt > 0 && gettotalobjs(g) > majorbase + majorinc) {
lu_mem numobjs = fullgen(L, g); /* do a major collection */ g->marked = 0;
if (gettotalbytes(g) < majorbase + (majorinc / 2)) { fullgen(L, g); /* do a major collection */
/* collected at least half of memory growth since last major if (gettotalobjs(g) < majorbase + (majorinc / 2)) {
/* collected at least half of object growth since last major
collection; keep doing minor collections. */ collection; keep doing minor collections. */
lua_assert(g->lastatomic == 0); lua_assert(g->lastatomic == 0);
} }
else { /* bad collection */ else { /* bad collection */
g->lastatomic = numobjs; /* signal that last collection was bad */ g->lastatomic = g->marked; /* signal that last collection was bad */
setpause(g); /* do a long wait for next (major) collection */ setpause(g); /* do a long wait for next (major) collection */
} }
} }
else { /* regular case; do a minor collection */ else { /* regular case; do a minor collection */
g->marked = 0;
youngcollection(L, g); youngcollection(L, g);
setminordebt(g); setminordebt(g);
g->GCestimate = majorbase; /* preserve base value */ g->GCestimate = majorbase; /* preserve base value */
@ -1522,9 +1512,9 @@ void luaC_freeallobjects (lua_State *L) {
} }
static lu_mem atomic (lua_State *L) { static l_mem atomic (lua_State *L) {
l_mem work = 0;
global_State *g = G(L); global_State *g = G(L);
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;
@ -1541,50 +1531,47 @@ static lu_mem atomic (lua_State *L) {
work += propagateall(g); /* propagate changes */ work += propagateall(g); /* propagate changes */
g->gray = grayagain; g->gray = grayagain;
work += propagateall(g); /* traverse 'grayagain' list */ work += propagateall(g); /* traverse 'grayagain' list */
convergeephemerons(g); work += 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 */
clearbyvalues(g, g->weak, NULL); work += clearbyvalues(g, g->weak, NULL);
clearbyvalues(g, g->allweak, NULL); work += clearbyvalues(g, g->allweak, NULL);
origweak = g->weak; origall = g->allweak; origweak = g->weak; origall = g->allweak;
separatetobefnz(g, 0); /* separate objects to be finalized */ separatetobefnz(g, 0); /* separate objects to be finalized */
work += markbeingfnz(g); /* mark objects that will be finalized */ work += markbeingfnz(g); /* mark objects that will be finalized */
work += propagateall(g); /* remark, to propagate 'resurrection' */ work += propagateall(g); /* remark, to propagate 'resurrection' */
convergeephemerons(g); work += 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 */
clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron tables */ work += clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron */
clearbykeys(g, g->allweak); /* clear keys from all 'allweak' tables */ work += clearbykeys(g, g->allweak); /* clear keys from all 'allweak' */
/* clear values from resurrected weak tables */ /* clear values from resurrected weak tables */
clearbyvalues(g, g->weak, origweak); work += clearbyvalues(g, g->weak, origweak);
clearbyvalues(g, g->allweak, origall); work += clearbyvalues(g, g->allweak, origall);
luaS_clearcache(g); luaS_clearcache(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);
return work; /* estimate of slots marked by 'atomic' */ return work;
} }
static int sweepstep (lua_State *L, global_State *g, static void 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;
int count; g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count);
g->GCestimate += g->GCdebt - olddebt; /* update estimate */ g->GCestimate += g->GCdebt - olddebt; /* update estimate */
return count;
} }
else { /* enter next state */ else { /* enter next state */
g->gcstate = nextstate; g->gcstate = nextstate;
g->sweepgc = nextlist; g->sweepgc = nextlist;
return 0; /* no work done */
} }
} }
static lu_mem singlestep (lua_State *L) { static l_mem singlestep (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
lu_mem work; l_mem work;
lua_assert(!g->gcstopem); /* collector is not reentrant */ lua_assert(!g->gcstopem); /* collector is not reentrant */
g->gcstopem = 1; /* no emergency collections while collecting */ g->gcstopem = 1; /* no emergency collections while collecting */
switch (g->gcstate) { switch (g->gcstate) {
@ -1599,26 +1586,30 @@ static lu_mem singlestep (lua_State *L) {
g->gcstate = GCSenteratomic; /* finish propagate phase */ g->gcstate = GCSenteratomic; /* finish propagate phase */
work = 0; work = 0;
} }
else else {
work = propagatemark(g); /* traverse one gray object */ propagatemark(g); /* traverse one gray object */
work = 1;
}
break; break;
} }
case GCSenteratomic: { case GCSenteratomic: {
work = atomic(L); /* work is what was traversed by 'atomic' */ work = atomic(L);
entersweep(L); entersweep(L);
g->GCestimate = gettotalbytes(g); /* first estimate */;
break; break;
} }
case GCSswpallgc: { /* sweep "regular" objects */ case GCSswpallgc: { /* sweep "regular" objects */
work = sweepstep(L, g, GCSswpfinobj, &g->finobj); sweepstep(L, g, GCSswpfinobj, &g->finobj);
work = GCSWEEPMAX;
break; break;
} }
case GCSswpfinobj: { /* sweep objects with finalizers */ case GCSswpfinobj: { /* sweep objects with finalizers */
work = sweepstep(L, g, GCSswptobefnz, &g->tobefnz); sweepstep(L, g, GCSswptobefnz, &g->tobefnz);
work = GCSWEEPMAX;
break; break;
} }
case GCSswptobefnz: { /* sweep objects to be finalized */ case GCSswptobefnz: { /* sweep objects to be finalized */
work = sweepstep(L, g, GCSswpend, NULL); sweepstep(L, g, GCSswpend, NULL);
work = GCSWEEPMAX;
break; break;
} }
case GCSswpend: { /* finish sweeps */ case GCSswpend: { /* finish sweeps */
@ -1630,7 +1621,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) {
g->gcstopem = 0; /* ok collections during finalizers */ g->gcstopem = 0; /* ok collections during finalizers */
work = runafewfinalizers(L, GCFINMAX) * GCFINALIZECOST; runafewfinalizers(L, GCFINMAX);
work = GCFINMAX * 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 */
@ -1666,18 +1658,16 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
*/ */
static void incstep (lua_State *L, global_State *g) { static void incstep (lua_State *L, global_State *g) {
int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */ int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */
l_mem debt = (g->GCdebt / WORK2MEM) * stepmul; l_mem debt = (g->GCdebt / 100) * stepmul;
l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem)) l_mem stepsize = cast(l_mem, 1) << g->gcstepsize;
? ((cast(l_mem, 1) << g->gcstepsize) / WORK2MEM) * stepmul
: MAX_LMEM; /* overflow; keep maximum value */
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 */ l_mem work = singlestep(L); /* perform one single step */
debt -= work; debt -= work;
} while (debt > -stepsize && 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 / stepmul) * WORK2MEM; /* convert 'work units' to bytes */ debt = (debt / stepmul) * 100; /* apply step multiplier */
luaE_setdebt(g, debt); luaE_setdebt(g, debt);
} }
} }
@ -1710,9 +1700,8 @@ static void fullinc (lua_State *L, global_State *g) {
/* finish any pending sweep phase to start a new cycle */ /* finish any pending sweep phase to start a new cycle */
luaC_runtilstate(L, bitmask(GCSpause)); luaC_runtilstate(L, bitmask(GCSpause));
luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */
/* estimate must be correct after a full GC cycle */
lua_assert(g->GCestimate == gettotalbytes(g));
luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */
/* estimate must be correct after a full GC cycle */
setpause(g); setpause(g);
} }

6
lgc.h
View File

@ -135,10 +135,10 @@
#define getgcparam(p) ((p) * 4) #define getgcparam(p) ((p) * 4)
#define setgcparam(p,v) ((p) = (v) / 4) #define setgcparam(p,v) ((p) = (v) / 4)
#define LUAI_GCMUL 100 #define LUAI_GCMUL 300
/* how much to allocate before next GC step (log2) */ /* how many objects to allocate before next GC step (log2) */
#define LUAI_GCSTEPSIZE 13 /* 8 KB */ #define LUAI_GCSTEPSIZE 8 /* 256 objects */
/* /*

View File

@ -57,7 +57,7 @@ typedef signed char ls_byte;
** floor of the log2 of the maximum signed value for integral type 't'. ** floor of the log2 of the maximum signed value for integral type 't'.
** (That is, maximum 'n' such that '2^n' fits in the given signed type.) ** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
*/ */
#define log2maxs(t) (sizeof(t) * 8 - 2) #define log2maxs(t) cast_int(sizeof(t) * 8 - 2)
/* /*

8
lmem.c
View File

@ -133,7 +133,7 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) {
global_State *g = G(L); global_State *g = G(L);
lua_assert((osize == 0) == (block == NULL)); lua_assert((osize == 0) == (block == NULL));
(*g->frealloc)(g->ud, block, osize, 0); (*g->frealloc)(g->ud, block, osize, 0);
g->GCdebt -= osize; g->totalbytes -= osize;
} }
@ -167,10 +167,10 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
if (l_unlikely(newblock == NULL && nsize > 0)) { if (l_unlikely(newblock == NULL && nsize > 0)) {
newblock = tryagain(L, block, osize, nsize); newblock = tryagain(L, block, osize, nsize);
if (newblock == NULL) /* still no memory? */ if (newblock == NULL) /* still no memory? */
return NULL; /* do not update 'GCdebt' */ return NULL; /* do not update 'totalbytes' */
} }
lua_assert((nsize == 0) == (newblock == NULL)); lua_assert((nsize == 0) == (newblock == NULL));
g->GCdebt = (g->GCdebt + nsize) - osize; g->totalbytes += nsize - osize;
return newblock; return newblock;
} }
@ -195,7 +195,7 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
if (newblock == NULL) if (newblock == NULL)
luaM_error(L); luaM_error(L);
} }
g->GCdebt += size; g->totalbytes += size;
return newblock; return newblock;
} }
} }

View File

@ -83,15 +83,15 @@ static unsigned int luai_makeseed (lua_State *L) {
/* /*
** set GCdebt to a new value keeping the value (totalbytes + GCdebt) ** set GCdebt to a new value keeping the value (totalobjs + GCdebt)
** invariant (and avoiding underflows in 'totalbytes') ** invariant (and avoiding underflows in 'totalobjs')
*/ */
void luaE_setdebt (global_State *g, l_mem debt) { void luaE_setdebt (global_State *g, l_mem debt) {
l_mem tb = gettotalbytes(g); l_mem tb = gettotalobjs(g);
lua_assert(tb > 0); lua_assert(tb > 0);
if (debt < tb - MAX_LMEM) if (debt < tb - MAX_LMEM)
debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */ debt = tb - MAX_LMEM; /* will make 'totalobjs == MAX_LMEM' */
g->totalbytes = tb - debt; g->totalobjs = tb - debt;
g->GCdebt = debt; g->GCdebt = debt;
} }
@ -278,8 +278,8 @@ static void close_state (lua_State *L) {
} }
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
freestack(L); freestack(L);
lua_assert(gettotalbytes(g) == sizeof(LG)); lua_assert(g->totalbytes == sizeof(LG));
lua_assert(g->totalobjs == 1); lua_assert(gettotalobjs(g) == 1);
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
} }
@ -389,6 +389,7 @@ 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->totalobjs = 1; g->totalobjs = 1;
g->marked = 0;
g->GCdebt = 0; g->GCdebt = 0;
g->lastatomic = 0; g->lastatomic = 0;
setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */

View File

@ -249,9 +249,10 @@ typedef struct CallInfo {
typedef struct global_State { typedef struct global_State {
lua_Alloc frealloc; /* function to reallocate memory */ lua_Alloc frealloc; /* function to reallocate memory */
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 */
l_mem totalobjs; /* total number of objects allocated */ l_mem totalobjs; /* total number of objects 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 marked; /* number of objects marked in a GC cycle */
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */ lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */
stringtable strt; /* hash table for strings */ stringtable strt; /* hash table for strings */
@ -386,8 +387,9 @@ union GCUnion {
#define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc)) #define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc))
/* actual number of total bytes allocated */ /* actual number of total objects allocated */
#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) #define gettotalobjs(g) ((g)->totalobjs + (g)->GCdebt)
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);

View File

@ -1027,6 +1027,16 @@ static int table_query (lua_State *L) {
} }
static int query_inc (lua_State *L) {
global_State *g = G(L);
lua_pushinteger(L, gettotalobjs(g));
lua_pushinteger(L, g->GCdebt);
lua_pushinteger(L, getgcparam(g->gcpause));
lua_pushinteger(L, getgcparam(g->gcstepmul));
lua_pushinteger(L, cast(l_mem, 1) << g->gcstepsize);
return 5;
}
static int string_query (lua_State *L) { static int string_query (lua_State *L) {
stringtable *tb = &G(L)->strt; stringtable *tb = &G(L)->strt;
int s = cast_int(luaL_optinteger(L, 1, 0)) - 1; int s = cast_int(luaL_optinteger(L, 1, 0)) - 1;
@ -1933,6 +1943,7 @@ static const struct luaL_Reg tests_funcs[] = {
{"pushuserdata", pushuserdata}, {"pushuserdata", pushuserdata},
{"querystr", string_query}, {"querystr", string_query},
{"querytab", table_query}, {"querytab", table_query},
{"queryinc", query_inc},
{"ref", tref}, {"ref", tref},
{"resume", coresume}, {"resume", coresume},
{"s2d", s2d}, {"s2d", s2d},

4
lua.c
View File

@ -633,7 +633,8 @@ static int pmain (lua_State *L) {
} }
luaL_openlibs(L); /* open standard libraries */ luaL_openlibs(L); /* open standard libraries */
createargtable(L, argv, argc, script); /* create table 'arg' */ createargtable(L, argv, argc, script); /* create table 'arg' */
lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */ lua_gc(L, LUA_GCRESTART); /* start GC... */
lua_gc(L, LUA_GCGEN, 0, 0); /* ...in generational mode */
if (!(args & has_E)) { /* no option '-E'? */ if (!(args & has_E)) { /* no option '-E'? */
if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
return 0; /* error running LUA_INIT */ return 0; /* error running LUA_INIT */
@ -665,6 +666,7 @@ int main (int argc, char **argv) {
l_message(argv[0], "cannot create state: not enough memory"); l_message(argv[0], "cannot create state: not enough memory");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
lua_gc(L, LUA_GCSTOP); /* stop GC while buidling state */
lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */
lua_pushinteger(L, argc); /* 1st argument */ lua_pushinteger(L, argc); /* 1st argument */
lua_pushlightuserdata(L, argv); /* 2nd argument */ lua_pushlightuserdata(L, argv); /* 2nd argument */