diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-10-11 09:38:45 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-10-11 09:38:45 -0300 |
commit | 1d8920dd7f508af5f2fd743678be1327f30c079b (patch) | |
tree | 31fdd2aa1c2829904c51be79aa334db020847418 | |
parent | 911f1e3e7ff01dafe3e9f77bc9c8e3a25237d7de (diff) | |
download | lua-1d8920dd7f508af5f2fd743678be1327f30c079b.tar.gz lua-1d8920dd7f508af5f2fd743678be1327f30c079b.tar.bz2 lua-1d8920dd7f508af5f2fd743678be1327f30c079b.zip |
some cleaning in GC parameters
-rw-r--r-- | lapi.c | 25 | ||||
-rw-r--r-- | lgc.c | 26 | ||||
-rw-r--r-- | lgc.h | 18 | ||||
-rw-r--r-- | lstate.c | 8 |
4 files changed, 46 insertions, 31 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.269 2017/06/01 20:22:33 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.270 2017/06/29 15:06:44 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -1114,14 +1114,14 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { | |||
1114 | } | 1114 | } |
1115 | case LUA_GCSETPAUSE: { | 1115 | case LUA_GCSETPAUSE: { |
1116 | int data = va_arg(argp, int); | 1116 | int data = va_arg(argp, int); |
1117 | res = g->gcpause + 100; | 1117 | res = getgcparam(g->gcpause); |
1118 | g->gcpause = (data >= 100) ? data - 100 : 0; | 1118 | setgcparam(g->gcpause, data); |
1119 | break; | 1119 | break; |
1120 | } | 1120 | } |
1121 | case LUA_GCSETSTEPMUL: { | 1121 | case LUA_GCSETSTEPMUL: { |
1122 | int data = va_arg(argp, int); | 1122 | int data = va_arg(argp, int); |
1123 | res = g->gcstepmul * 10; | 1123 | res = getgcparam(g->gcstepmul); |
1124 | g->gcstepmul = data / 10; | 1124 | setgcparam(g->gcstepmul, data); |
1125 | break; | 1125 | break; |
1126 | } | 1126 | } |
1127 | case LUA_GCISRUNNING: { | 1127 | case LUA_GCISRUNNING: { |
@@ -1131,8 +1131,10 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { | |||
1131 | case LUA_GCGEN: { | 1131 | case LUA_GCGEN: { |
1132 | int minormul = va_arg(argp, int); | 1132 | int minormul = va_arg(argp, int); |
1133 | int majormul = va_arg(argp, int); | 1133 | int majormul = va_arg(argp, int); |
1134 | g->genminormul = (minormul == 0) ? LUAI_GENMINORMUL : minormul; | 1134 | if (minormul != 0) |
1135 | g->genmajormul = (majormul == 0) ? LUAI_GENMAJORMUL : majormul; | 1135 | g->genminormul = minormul; |
1136 | if (majormul != 0) | ||
1137 | setgcparam(g->genmajormul, majormul); | ||
1136 | luaC_changemode(L, KGC_GEN); | 1138 | luaC_changemode(L, KGC_GEN); |
1137 | break; | 1139 | break; |
1138 | } | 1140 | } |
@@ -1140,9 +1142,12 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { | |||
1140 | int pause = va_arg(argp, int); | 1142 | int pause = va_arg(argp, int); |
1141 | int stepmul = va_arg(argp, int); | 1143 | int stepmul = va_arg(argp, int); |
1142 | int stepsize = va_arg(argp, int); | 1144 | int stepsize = va_arg(argp, int); |
1143 | g->gcpause = (pause == 0) ? LUAI_GCPAUSE : pause; | 1145 | if (pause != 0) |
1144 | g->gcstepmul = (stepmul == 0) ? LUAI_GCMUL : stepmul; | 1146 | setgcparam(g->gcpause, pause); |
1145 | g->gcstepsize = (stepsize == 0) ? LUAI_GCSTEPSIZE : stepsize; | 1147 | if (stepmul != 0) |
1148 | setgcparam(g->gcstepmul, stepmul); | ||
1149 | if (stepsize != 0) | ||
1150 | g->gcstepsize = stepsize; | ||
1146 | luaC_changemode(L, KGC_INC); | 1151 | luaC_changemode(L, KGC_INC); |
1147 | break; | 1152 | break; |
1148 | } | 1153 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lgc.c,v 2.233 2017/06/29 15:06:44 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 2.234 2017/08/31 16:06:51 roberto Exp roberto $ |
3 | ** Garbage Collector | 3 | ** Garbage Collector |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -47,9 +47,9 @@ | |||
47 | 47 | ||
48 | /* | 48 | /* |
49 | ** The equivalent, in bytes, of one unit of "work" (visiting a slot, | 49 | ** The equivalent, in bytes, of one unit of "work" (visiting a slot, |
50 | ** sweeping an object, etc.) * 10 (for scaling) | 50 | ** sweeping an object, etc.) |
51 | */ | 51 | */ |
52 | #define WORK2MEM (sizeof(TValue) * 10) | 52 | #define WORK2MEM sizeof(TValue) |
53 | 53 | ||
54 | 54 | ||
55 | /* | 55 | /* |
@@ -1256,14 +1256,14 @@ static void fullgen (lua_State *L, global_State *g) { | |||
1256 | ** than last major collection (kept in 'g->GCestimate'), does a major | 1256 | ** than last major collection (kept in 'g->GCestimate'), does a major |
1257 | ** collection. Otherwise, does a minor collection and set debt to make | 1257 | ** collection. Otherwise, does a minor collection and set debt to make |
1258 | ** another collection when memory grows 'genminormul'% larger. | 1258 | ** another collection when memory grows 'genminormul'% larger. |
1259 | ** 'GCdebt <= 0' means an explicity call to GC step with "size" zero; | 1259 | ** 'GCdebt <= 0' means an explicit call to GC step with "size" zero; |
1260 | ** in that case, always do a minor collection. | 1260 | ** in that case, always do a minor collection. |
1261 | */ | 1261 | */ |
1262 | static void genstep (lua_State *L, global_State *g) { | 1262 | static void genstep (lua_State *L, global_State *g) { |
1263 | lu_mem majorbase = g->GCestimate; | 1263 | lu_mem majorbase = g->GCestimate; |
1264 | //lua_checkmemory(L); | 1264 | int majormul = getgcparam(g->genmajormul); |
1265 | if (g->GCdebt > 0 && | 1265 | if (g->GCdebt > 0 && |
1266 | gettotalbytes(g) > (majorbase / 100) * (100 + g->genmajormul)) { | 1266 | gettotalbytes(g) > (majorbase / 100) * (100 + majormul)) { |
1267 | fullgen(L, g); | 1267 | fullgen(L, g); |
1268 | } | 1268 | } |
1269 | else { | 1269 | else { |
@@ -1273,7 +1273,6 @@ static void genstep (lua_State *L, global_State *g) { | |||
1273 | luaE_setdebt(g, -((mem / 100) * g->genminormul)); | 1273 | luaE_setdebt(g, -((mem / 100) * g->genminormul)); |
1274 | g->GCestimate = majorbase; /* preserve base value */ | 1274 | g->GCestimate = majorbase; /* preserve base value */ |
1275 | } | 1275 | } |
1276 | //lua_checkmemory(L); | ||
1277 | } | 1276 | } |
1278 | 1277 | ||
1279 | /* }====================================================== */ | 1278 | /* }====================================================== */ |
@@ -1288,13 +1287,13 @@ static void genstep (lua_State *L, global_State *g) { | |||
1288 | 1287 | ||
1289 | /* | 1288 | /* |
1290 | ** Set the "time" to wait before starting a new GC cycle; cycle will | 1289 | ** Set the "time" to wait before starting a new GC cycle; cycle will |
1291 | ** start when memory use hits the threshold of ('estimate' * gcpause / | 1290 | ** start when memory use hits the threshold of ('estimate' * pause / |
1292 | ** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero, | 1291 | ** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero, |
1293 | ** because Lua cannot even start with less than PAUSEADJ bytes). | 1292 | ** because Lua cannot even start with less than PAUSEADJ bytes). |
1294 | */ | 1293 | */ |
1295 | static void setpause (global_State *g) { | 1294 | static void setpause (global_State *g) { |
1296 | l_mem threshold, debt; | 1295 | l_mem threshold, debt; |
1297 | int pause = g->gcpause + 100; | 1296 | int pause = getgcparam(g->gcpause); |
1298 | l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ | 1297 | l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ |
1299 | lua_assert(estimate > 0); | 1298 | lua_assert(estimate > 0); |
1300 | threshold = (pause < MAX_LMEM / estimate) /* overflow? */ | 1299 | threshold = (pause < MAX_LMEM / estimate) /* overflow? */ |
@@ -1482,16 +1481,15 @@ void luaC_runtilstate (lua_State *L, int statesmask) { | |||
1482 | ** controls when next step will be performed. | 1481 | ** controls when next step will be performed. |
1483 | */ | 1482 | */ |
1484 | static void incstep (lua_State *L, global_State *g) { | 1483 | static void incstep (lua_State *L, global_State *g) { |
1485 | int stepmul = (g->gcstepmul | 1); /* avoid division by 0 */ | 1484 | int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */ |
1486 | l_mem debt = (g->GCdebt / WORK2MEM) * stepmul; | 1485 | l_mem debt = (g->GCdebt / WORK2MEM) * stepmul; |
1487 | l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem)) | 1486 | l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem)) |
1488 | ? cast(l_mem, 1) << g->gcstepsize | 1487 | ? ((cast(l_mem, 1) << g->gcstepsize) / WORK2MEM) * stepmul |
1489 | : MAX_LMEM; | 1488 | : MAX_LMEM; /* overflow; keep maximum value */ |
1490 | stepsize = -((stepsize / WORK2MEM) * stepmul); | ||
1491 | do { /* repeat until pause or enough "credit" (negative debt) */ | 1489 | do { /* repeat until pause or enough "credit" (negative debt) */ |
1492 | lu_mem work = singlestep(L); /* perform one single step */ | 1490 | lu_mem work = singlestep(L); /* perform one single step */ |
1493 | debt -= work; | 1491 | debt -= work; |
1494 | } while (debt > stepsize && g->gcstate != GCSpause); | 1492 | } while (debt > -stepsize && g->gcstate != GCSpause); |
1495 | if (g->gcstate == GCSpause) | 1493 | if (g->gcstate == GCSpause) |
1496 | setpause(g); /* pause until next cycle */ | 1494 | setpause(g); /* pause until next cycle */ |
1497 | else { | 1495 | else { |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lgc.h,v 2.97 2017/05/04 13:32:01 roberto Exp roberto $ | 2 | ** $Id: lgc.h,v 2.98 2017/05/26 19:14:29 roberto Exp roberto $ |
3 | ** Garbage Collector | 3 | ** Garbage Collector |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -119,10 +119,20 @@ | |||
119 | 119 | ||
120 | /* Default Values for GC parameters */ | 120 | /* Default Values for GC parameters */ |
121 | #define LUAI_GENMAJORMUL 100 | 121 | #define LUAI_GENMAJORMUL 100 |
122 | #define LUAI_GENMINORMUL 5 | 122 | #define LUAI_GENMINORMUL 12 |
123 | |||
124 | /* wait memory to double before starting new cycle */ | ||
125 | #define LUAI_GCPAUSE 200 /* 200% */ | ||
126 | |||
127 | /* | ||
128 | ** gc parameters are stored divided by 4 to allow a maximum value larger | ||
129 | ** than 1000 in an 'lu_byte'. | ||
130 | */ | ||
131 | #define getgcparam(p) ((p) * 4) | ||
132 | #define setgcparam(p,v) ((p) = (v) / 4) | ||
133 | |||
134 | #define LUAI_GCMUL 100 | ||
123 | 135 | ||
124 | #define LUAI_GCPAUSE 100 /* 100% */ | ||
125 | #define LUAI_GCMUL 10 | ||
126 | /* how much to allocate before next GC step (log2) */ | 136 | /* how much to allocate before next GC step (log2) */ |
127 | #define LUAI_GCSTEPSIZE 13 /* 8 KB */ | 137 | #define LUAI_GCSTEPSIZE 13 /* 8 KB */ |
128 | 138 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.c,v 2.140 2017/05/26 19:14:29 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 2.141 2017/06/29 15:06:44 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -320,9 +320,11 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
320 | g->twups = NULL; | 320 | g->twups = NULL; |
321 | g->totalbytes = sizeof(LG); | 321 | g->totalbytes = sizeof(LG); |
322 | g->GCdebt = 0; | 322 | g->GCdebt = 0; |
323 | g->gcpause = LUAI_GCPAUSE; | 323 | setgcparam(g->gcpause, LUAI_GCPAUSE); |
324 | g->gcstepmul = LUAI_GCMUL; | 324 | setgcparam(g->gcstepmul, LUAI_GCMUL); |
325 | g->gcstepsize = LUAI_GCSTEPSIZE; | 325 | g->gcstepsize = LUAI_GCSTEPSIZE; |
326 | setgcparam(g->genmajormul, LUAI_GENMAJORMUL); | ||
327 | g->genminormul = LUAI_GENMINORMUL; | ||
326 | for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; | 328 | for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; |
327 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { | 329 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { |
328 | /* memory allocation error: free partial state */ | 330 | /* memory allocation error: free partial state */ |