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);
break;
}
case LUA_GCSETPARAM: {
case LUA_GCPARAM: {
int param = va_arg(argp, int);
int value = va_arg(argp, int);
api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter");
res = luaO_applyparam(g->gcparams[param], 100);
g->gcparams[param] = luaO_codeparam(value);
if (value >= 0)
g->gcparams[param] = luaO_codeparam(value);
break;
}
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 const char *const opts[] = {"stop", "restart", "collect",
"count", "step", "isrunning", "generational", "incremental",
"setparam", NULL};
"param", NULL};
static const char optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC,
LUA_GCSETPARAM};
LUA_GCPARAM};
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
switch (o) {
case LUA_GCCOUNT: {
@ -231,7 +231,7 @@ static int luaB_collectgarbage (lua_State *L) {
case LUA_GCINC: {
return pushmode(L, lua_gc(L, o));
}
case LUA_GCSETPARAM: {
case LUA_GCPARAM: {
static const char *const params[] = {
"minormul", "majorminor", "minormajor",
"pause", "stepmul", "stepsize", NULL};
@ -239,7 +239,7 @@ static int luaB_collectgarbage (lua_State *L) {
LUA_GCPMINORMUL, LUA_GCPMAJORMINOR, LUA_GCPMINORMAJOR,
LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE};
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));
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_GCGEN 7
#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}).
}
@item{@defid{LUA_GCSETPARAM} (int param, int value)|
Changes the values of a parameter of the collector and returns
the previous value of that parameter.
@item{@defid{LUA_GCPARAM} (int param, int val)|
Changes and/or returns the value of a parameter of the collector.
If @id{val} is negative, the call only returns the current value.
The argument @id{param} must have one of the following values:
@description{
@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.
}
@item{@St{setparam}|
Changes the values of a parameter of the collector and returns
the previous value of that parameter.
This option must be followed by two extra arguments:
The name of the parameter being changed (a string)
and the new value for that parameter (an integer).
The argument @id{param} must have one of the following values:
@item{@St{param}|
Changes and/or retrieves the values of a parameter of the collector.
This option must be followed by one or two extra arguments:
The name of the parameter being changed or retrieved (a string)
and an optional new value for that parameter (an integer).
The first argument must have one of the following values:
@description{
@item{@St{minormul}| The 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{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;
so, the value returned as the previous value may not be
exactly the last value set.
@ -9298,7 +9301,7 @@ declare a local variable with the same name in the loop body.
@item{
Parameters for the garbage collection are not set
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.
}
@ -9327,7 +9330,7 @@ to signal the end of the dump.
@item{
Parameters for the garbage collection are not set
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.
}

View File

@ -28,19 +28,21 @@ end
-- test weird parameters to 'collectgarbage'
do
collectgarbage("incremental")
local opause = collectgarbage("setparam", "pause", 100)
local ostepmul = collectgarbage("setparam", "stepmul", 100)
local opause = collectgarbage("param", "pause", 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}
for i = 1, #t do
collectgarbage("setparam", "pause", t[i])
collectgarbage("param", "pause", t[i])
for j = 1, #t do
collectgarbage("setparam", "stepmul", t[j])
collectgarbage("param", "stepmul", t[j])
collectgarbage("step", t[j])
end
end
-- restore original parameters
collectgarbage("setparam", "pause", opause)
collectgarbage("setparam", "stepmul", ostepmul)
collectgarbage("param", "pause", opause)
collectgarbage("param", "stepmul", ostepmul)
collectgarbage()
end

View File

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