1
0
mirror of https://github.com/lua/lua.git synced 2025-01-14 05:43:00 +08:00

New mechanism to query GC parameters

This commit is contained in:
Roberto Ierusalimschy 2024-01-16 17:02:55 -03:00
parent 17e0c29d9b
commit 4a8e480864
6 changed files with 35 additions and 27 deletions

5
lapi.c
View File

@ -1217,12 +1217,13 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
luaC_changemode(L, KGC_INC); luaC_changemode(L, KGC_INC);
break; break;
} }
case LUA_GCSETPARAM: { case LUA_GCPARAM: {
int param = va_arg(argp, int); int param = va_arg(argp, int);
int value = va_arg(argp, int); int value = va_arg(argp, int);
api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter"); api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter");
res = luaO_applyparam(g->gcparams[param], 100); res = luaO_applyparam(g->gcparams[param], 100);
g->gcparams[param] = luaO_codeparam(value); if (value >= 0)
g->gcparams[param] = luaO_codeparam(value);
break; break;
} }
default: res = -1; /* invalid option */ default: res = -1; /* invalid option */

View File

@ -199,10 +199,10 @@ static int pushmode (lua_State *L, int oldmode) {
static int luaB_collectgarbage (lua_State *L) { static int luaB_collectgarbage (lua_State *L) {
static const char *const opts[] = {"stop", "restart", "collect", static const char *const opts[] = {"stop", "restart", "collect",
"count", "step", "isrunning", "generational", "incremental", "count", "step", "isrunning", "generational", "incremental",
"setparam", NULL}; "param", NULL};
static const char optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, static const char optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC, LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC,
LUA_GCSETPARAM}; LUA_GCPARAM};
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
switch (o) { switch (o) {
case LUA_GCCOUNT: { case LUA_GCCOUNT: {
@ -231,7 +231,7 @@ static int luaB_collectgarbage (lua_State *L) {
case LUA_GCINC: { case LUA_GCINC: {
return pushmode(L, lua_gc(L, o)); return pushmode(L, lua_gc(L, o));
} }
case LUA_GCSETPARAM: { case LUA_GCPARAM: {
static const char *const params[] = { static const char *const params[] = {
"minormul", "majorminor", "minormajor", "minormul", "majorminor", "minormajor",
"pause", "stepmul", "stepsize", NULL}; "pause", "stepmul", "stepsize", NULL};
@ -239,7 +239,7 @@ static int luaB_collectgarbage (lua_State *L) {
LUA_GCPMINORMUL, LUA_GCPMAJORMINOR, LUA_GCPMINORMAJOR, LUA_GCPMINORMUL, LUA_GCPMAJORMINOR, LUA_GCPMINORMAJOR,
LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE}; LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE};
int p = pnum[luaL_checkoption(L, 2, NULL, params)]; int p = pnum[luaL_checkoption(L, 2, NULL, params)];
lua_Integer value = luaL_checkinteger(L, 3); lua_Integer value = luaL_optinteger(L, 3, -1);
lua_pushinteger(L, lua_gc(L, o, p, (int)value)); lua_pushinteger(L, lua_gc(L, o, p, (int)value));
return 1; return 1;
} }

2
lua.h
View File

@ -338,7 +338,7 @@ LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont);
#define LUA_GCISRUNNING 6 #define LUA_GCISRUNNING 6
#define LUA_GCGEN 7 #define LUA_GCGEN 7
#define LUA_GCINC 8 #define LUA_GCINC 8
#define LUA_GCSETPARAM 9 #define LUA_GCPARAM 9
/* /*

View File

@ -3345,9 +3345,9 @@ Changes the collector to generational mode.
Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}).
} }
@item{@defid{LUA_GCSETPARAM} (int param, int value)| @item{@defid{LUA_GCPARAM} (int param, int val)|
Changes the values of a parameter of the collector and returns Changes and/or returns the value of a parameter of the collector.
the previous value of that parameter. If @id{val} is negative, the call only returns the current value.
The argument @id{param} must have one of the following values: The argument @id{param} must have one of the following values:
@description{ @description{
@item{@defid{LUA_GCPMINORMUL}| The minor multiplier. } @item{@defid{LUA_GCPMINORMUL}| The minor multiplier. }
@ -6390,13 +6390,12 @@ Changes the collector mode to incremental and returns the previous mode.
Changes the collector mode to generational and returns the previous mode. Changes the collector mode to generational and returns the previous mode.
} }
@item{@St{setparam}| @item{@St{param}|
Changes the values of a parameter of the collector and returns Changes and/or retrieves the values of a parameter of the collector.
the previous value of that parameter. This option must be followed by one or two extra arguments:
This option must be followed by two extra arguments: The name of the parameter being changed or retrieved (a string)
The name of the parameter being changed (a string) and an optional new value for that parameter (an integer).
and the new value for that parameter (an integer). The first argument must have one of the following values:
The argument @id{param} must have one of the following values:
@description{ @description{
@item{@St{minormul}| The minor multiplier. } @item{@St{minormul}| The minor multiplier. }
@item{@St{majorminor}| The major-minor multiplier. } @item{@St{majorminor}| The major-minor multiplier. }
@ -6405,6 +6404,10 @@ The argument @id{param} must have one of the following values:
@item{@St{stepmul}| The step multiplier. } @item{@St{stepmul}| The step multiplier. }
@item{@St{stepsize}| The step size. } @item{@St{stepsize}| The step size. }
} }
The call always returns the previous value of the parameter.
If the call does not give a new value,
the value is left unchanged.
Lua rounds these values before storing them; Lua rounds these values before storing them;
so, the value returned as the previous value may not be so, the value returned as the previous value may not be
exactly the last value set. exactly the last value set.
@ -9298,7 +9301,7 @@ declare a local variable with the same name in the loop body.
@item{ @item{
Parameters for the garbage collection are not set Parameters for the garbage collection are not set
with the options @St{incremental} and @St{generational}; with the options @St{incremental} and @St{generational};
instead, there is a new option @St{setparam} to that end. instead, there is a new option @St{param} to that end.
Moreover, there were some changes in the parameters themselves. Moreover, there were some changes in the parameters themselves.
} }
@ -9327,7 +9330,7 @@ to signal the end of the dump.
@item{ @item{
Parameters for the garbage collection are not set Parameters for the garbage collection are not set
with the options @Lid{LUA_GCINC} and @Lid{LUA_GCGEN}; with the options @Lid{LUA_GCINC} and @Lid{LUA_GCGEN};
instead, there is a new option @Lid{LUA_GCSETPARAM} to that end. instead, there is a new option @Lid{LUA_GCPARAM} to that end.
Moreover, there were some changes in the parameters themselves. Moreover, there were some changes in the parameters themselves.
} }

View File

@ -28,19 +28,21 @@ end
-- test weird parameters to 'collectgarbage' -- test weird parameters to 'collectgarbage'
do do
collectgarbage("incremental") collectgarbage("incremental")
local opause = collectgarbage("setparam", "pause", 100) local opause = collectgarbage("param", "pause", 100)
local ostepmul = collectgarbage("setparam", "stepmul", 100) local ostepmul = collectgarbage("param", "stepmul", 100)
assert(collectgarbage("param", "pause") == 100)
assert(collectgarbage("param", "stepmul") == 100)
local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe} local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe}
for i = 1, #t do for i = 1, #t do
collectgarbage("setparam", "pause", t[i]) collectgarbage("param", "pause", t[i])
for j = 1, #t do for j = 1, #t do
collectgarbage("setparam", "stepmul", t[j]) collectgarbage("param", "stepmul", t[j])
collectgarbage("step", t[j]) collectgarbage("step", t[j])
end end
end end
-- restore original parameters -- restore original parameters
collectgarbage("setparam", "pause", opause) collectgarbage("param", "pause", opause)
collectgarbage("setparam", "stepmul", ostepmul) collectgarbage("param", "stepmul", ostepmul)
collectgarbage() collectgarbage()
end end

View File

@ -163,15 +163,17 @@ assert(collectgarbage'isrunning')
do print"testing stop-the-world collection" do print"testing stop-the-world collection"
local step = collectgarbage("setparam", "stepsize", 0); local step = collectgarbage("param", "stepsize", 0);
collectgarbage("incremental") collectgarbage("incremental")
assert(collectgarbage("param", "stepsize") == 0)
-- each step does a complete cycle -- each step does a complete cycle
assert(collectgarbage("step")) assert(collectgarbage("step"))
assert(collectgarbage("step")) assert(collectgarbage("step"))
-- back to default value -- back to default value
collectgarbage("setparam", "stepsize", step); collectgarbage("param", "stepsize", step);
assert(collectgarbage("param", "stepsize") == step)
end end
collectgarbage(oldmode) collectgarbage(oldmode)